You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
11 KiB

7 years ago
  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v1.1.3
  6. */
  7. goog.provide('ngmaterial.components.bottomSheet');
  8. goog.require('ngmaterial.components.backdrop');
  9. goog.require('ngmaterial.core');
  10. /**
  11. * @ngdoc module
  12. * @name material.components.bottomSheet
  13. * @description
  14. * BottomSheet
  15. */
  16. MdBottomSheetDirective['$inject'] = ["$mdBottomSheet"];
  17. MdBottomSheetProvider['$inject'] = ["$$interimElementProvider"];
  18. angular
  19. .module('material.components.bottomSheet', [
  20. 'material.core',
  21. 'material.components.backdrop'
  22. ])
  23. .directive('mdBottomSheet', MdBottomSheetDirective)
  24. .provider('$mdBottomSheet', MdBottomSheetProvider);
  25. /* ngInject */
  26. function MdBottomSheetDirective($mdBottomSheet) {
  27. return {
  28. restrict: 'E',
  29. link : function postLink(scope, element) {
  30. element.addClass('_md'); // private md component indicator for styling
  31. // When navigation force destroys an interimElement, then
  32. // listen and $destroy() that interim instance...
  33. scope.$on('$destroy', function() {
  34. $mdBottomSheet.destroy();
  35. });
  36. }
  37. };
  38. }
  39. /**
  40. * @ngdoc service
  41. * @name $mdBottomSheet
  42. * @module material.components.bottomSheet
  43. *
  44. * @description
  45. * `$mdBottomSheet` opens a bottom sheet over the app and provides a simple promise API.
  46. *
  47. * ## Restrictions
  48. *
  49. * - The bottom sheet's template must have an outer `<md-bottom-sheet>` element.
  50. * - Add the `md-grid` class to the bottom sheet for a grid layout.
  51. * - Add the `md-list` class to the bottom sheet for a list layout.
  52. *
  53. * @usage
  54. * <hljs lang="html">
  55. * <div ng-controller="MyController">
  56. * <md-button ng-click="openBottomSheet()">
  57. * Open a Bottom Sheet!
  58. * </md-button>
  59. * </div>
  60. * </hljs>
  61. * <hljs lang="js">
  62. * var app = angular.module('app', ['ngMaterial']);
  63. * app.controller('MyController', function($scope, $mdBottomSheet) {
  64. * $scope.openBottomSheet = function() {
  65. * $mdBottomSheet.show({
  66. * template: '<md-bottom-sheet>Hello!</md-bottom-sheet>'
  67. * });
  68. * };
  69. * });
  70. * </hljs>
  71. */
  72. /**
  73. * @ngdoc method
  74. * @name $mdBottomSheet#show
  75. *
  76. * @description
  77. * Show a bottom sheet with the specified options.
  78. *
  79. * @param {object} options An options object, with the following properties:
  80. *
  81. * - `templateUrl` - `{string=}`: The url of an html template file that will
  82. * be used as the content of the bottom sheet. Restrictions: the template must
  83. * have an outer `md-bottom-sheet` element.
  84. * - `template` - `{string=}`: Same as templateUrl, except this is an actual
  85. * template string.
  86. * - `scope` - `{object=}`: the scope to link the template / controller to. If none is specified, it will create a new child scope.
  87. * This scope will be destroyed when the bottom sheet is removed unless `preserveScope` is set to true.
  88. * - `preserveScope` - `{boolean=}`: whether to preserve the scope when the element is removed. Default is false
  89. * - `controller` - `{string=}`: The controller to associate with this bottom sheet.
  90. * - `locals` - `{string=}`: An object containing key/value pairs. The keys will
  91. * be used as names of values to inject into the controller. For example,
  92. * `locals: {three: 3}` would inject `three` into the controller with the value
  93. * of 3.
  94. * - `clickOutsideToClose` - `{boolean=}`: Whether the user can click outside the bottom sheet to
  95. * close it. Default true.
  96. * - `bindToController` - `{boolean=}`: When set to true, the locals will be bound to the controller instance.
  97. * - `disableBackdrop` - `{boolean=}`: When set to true, the bottomsheet will not show a backdrop.
  98. * - `escapeToClose` - `{boolean=}`: Whether the user can press escape to close the bottom sheet.
  99. * Default true.
  100. * - `resolve` - `{object=}`: Similar to locals, except it takes promises as values
  101. * and the bottom sheet will not open until the promises resolve.
  102. * - `controllerAs` - `{string=}`: An alias to assign the controller to on the scope.
  103. * - `parent` - `{element=}`: The element to append the bottom sheet to. The `parent` may be a `function`, `string`,
  104. * `object`, or null. Defaults to appending to the body of the root element (or the root element) of the application.
  105. * e.g. angular.element(document.getElementById('content')) or "#content"
  106. * - `disableParentScroll` - `{boolean=}`: Whether to disable scrolling while the bottom sheet is open.
  107. * Default true.
  108. *
  109. * @returns {promise} A promise that can be resolved with `$mdBottomSheet.hide()` or
  110. * rejected with `$mdBottomSheet.cancel()`.
  111. */
  112. /**
  113. * @ngdoc method
  114. * @name $mdBottomSheet#hide
  115. *
  116. * @description
  117. * Hide the existing bottom sheet and resolve the promise returned from
  118. * `$mdBottomSheet.show()`. This call will close the most recently opened/current bottomsheet (if any).
  119. *
  120. * @param {*=} response An argument for the resolved promise.
  121. *
  122. */
  123. /**
  124. * @ngdoc method
  125. * @name $mdBottomSheet#cancel
  126. *
  127. * @description
  128. * Hide the existing bottom sheet and reject the promise returned from
  129. * `$mdBottomSheet.show()`.
  130. *
  131. * @param {*=} response An argument for the rejected promise.
  132. *
  133. */
  134. function MdBottomSheetProvider($$interimElementProvider) {
  135. // how fast we need to flick down to close the sheet, pixels/ms
  136. bottomSheetDefaults['$inject'] = ["$animate", "$mdConstant", "$mdUtil", "$mdTheming", "$mdBottomSheet", "$rootElement", "$mdGesture", "$log"];
  137. var CLOSING_VELOCITY = 0.5;
  138. var PADDING = 80; // same as css
  139. return $$interimElementProvider('$mdBottomSheet')
  140. .setDefaults({
  141. methods: ['disableParentScroll', 'escapeToClose', 'clickOutsideToClose'],
  142. options: bottomSheetDefaults
  143. });
  144. /* ngInject */
  145. function bottomSheetDefaults($animate, $mdConstant, $mdUtil, $mdTheming, $mdBottomSheet, $rootElement,
  146. $mdGesture, $log) {
  147. var backdrop;
  148. return {
  149. themable: true,
  150. onShow: onShow,
  151. onRemove: onRemove,
  152. disableBackdrop: false,
  153. escapeToClose: true,
  154. clickOutsideToClose: true,
  155. disableParentScroll: true
  156. };
  157. function onShow(scope, element, options, controller) {
  158. element = $mdUtil.extractElementByName(element, 'md-bottom-sheet');
  159. // prevent tab focus or click focus on the bottom-sheet container
  160. element.attr('tabindex',"-1");
  161. // Once the md-bottom-sheet has `ng-cloak` applied on his template the opening animation will not work properly.
  162. // This is a very common problem, so we have to notify the developer about this.
  163. if (element.hasClass('ng-cloak')) {
  164. var message = '$mdBottomSheet: using `<md-bottom-sheet ng-cloak >` will affect the bottom-sheet opening animations.';
  165. $log.warn( message, element[0] );
  166. }
  167. if (!options.disableBackdrop) {
  168. // Add a backdrop that will close on click
  169. backdrop = $mdUtil.createBackdrop(scope, "md-bottom-sheet-backdrop md-opaque");
  170. // Prevent mouse focus on backdrop; ONLY programatic focus allowed.
  171. // This allows clicks on backdrop to propogate to the $rootElement and
  172. // ESC key events to be detected properly.
  173. backdrop[0].tabIndex = -1;
  174. if (options.clickOutsideToClose) {
  175. backdrop.on('click', function() {
  176. $mdUtil.nextTick($mdBottomSheet.cancel,true);
  177. });
  178. }
  179. $mdTheming.inherit(backdrop, options.parent);
  180. $animate.enter(backdrop, options.parent, null);
  181. }
  182. var bottomSheet = new BottomSheet(element, options.parent);
  183. options.bottomSheet = bottomSheet;
  184. $mdTheming.inherit(bottomSheet.element, options.parent);
  185. if (options.disableParentScroll) {
  186. options.restoreScroll = $mdUtil.disableScrollAround(bottomSheet.element, options.parent);
  187. }
  188. return $animate.enter(bottomSheet.element, options.parent, backdrop)
  189. .then(function() {
  190. var focusable = $mdUtil.findFocusTarget(element) || angular.element(
  191. element[0].querySelector('button') ||
  192. element[0].querySelector('a') ||
  193. element[0].querySelector($mdUtil.prefixer('ng-click', true))
  194. ) || backdrop;
  195. if (options.escapeToClose) {
  196. options.rootElementKeyupCallback = function(e) {
  197. if (e.keyCode === $mdConstant.KEY_CODE.ESCAPE) {
  198. $mdUtil.nextTick($mdBottomSheet.cancel,true);
  199. }
  200. };
  201. $rootElement.on('keyup', options.rootElementKeyupCallback);
  202. focusable && focusable.focus();
  203. }
  204. });
  205. }
  206. function onRemove(scope, element, options) {
  207. var bottomSheet = options.bottomSheet;
  208. if (!options.disableBackdrop) $animate.leave(backdrop);
  209. return $animate.leave(bottomSheet.element).then(function() {
  210. if (options.disableParentScroll) {
  211. options.restoreScroll();
  212. delete options.restoreScroll;
  213. }
  214. bottomSheet.cleanup();
  215. });
  216. }
  217. /**
  218. * BottomSheet class to apply bottom-sheet behavior to an element
  219. */
  220. function BottomSheet(element, parent) {
  221. var deregister = $mdGesture.register(parent, 'drag', { horizontal: false });
  222. parent.on('$md.dragstart', onDragStart)
  223. .on('$md.drag', onDrag)
  224. .on('$md.dragend', onDragEnd);
  225. return {
  226. element: element,
  227. cleanup: function cleanup() {
  228. deregister();
  229. parent.off('$md.dragstart', onDragStart);
  230. parent.off('$md.drag', onDrag);
  231. parent.off('$md.dragend', onDragEnd);
  232. }
  233. };
  234. function onDragStart(ev) {
  235. // Disable transitions on transform so that it feels fast
  236. element.css($mdConstant.CSS.TRANSITION_DURATION, '0ms');
  237. }
  238. function onDrag(ev) {
  239. var transform = ev.pointer.distanceY;
  240. if (transform < 5) {
  241. // Slow down drag when trying to drag up, and stop after PADDING
  242. transform = Math.max(-PADDING, transform / 2);
  243. }
  244. element.css($mdConstant.CSS.TRANSFORM, 'translate3d(0,' + (PADDING + transform) + 'px,0)');
  245. }
  246. function onDragEnd(ev) {
  247. if (ev.pointer.distanceY > 0 &&
  248. (ev.pointer.distanceY > 20 || Math.abs(ev.pointer.velocityY) > CLOSING_VELOCITY)) {
  249. var distanceRemaining = element.prop('offsetHeight') - ev.pointer.distanceY;
  250. var transitionDuration = Math.min(distanceRemaining / ev.pointer.velocityY * 0.75, 500);
  251. element.css($mdConstant.CSS.TRANSITION_DURATION, transitionDuration + 'ms');
  252. $mdUtil.nextTick($mdBottomSheet.cancel,true);
  253. } else {
  254. element.css($mdConstant.CSS.TRANSITION_DURATION, '');
  255. element.css($mdConstant.CSS.TRANSFORM, '');
  256. }
  257. }
  258. }
  259. }
  260. }
  261. ngmaterial.components.bottomSheet = angular.module("material.components.bottomSheet");