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.

300 lines
10 KiB

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