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. 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. * - `disableBackdrop` - `{boolean=}`: When set to true, the bottomsheet will not show a backdrop.
  97. * - `escapeToClose` - `{boolean=}`: Whether the user can press escape to close the bottom sheet.
  98. * Default true.
  99. * - `resolve` - `{object=}`: Similar to locals, except it takes promises as values
  100. * and the bottom sheet will not open until the promises resolve.
  101. * - `controllerAs` - `{string=}`: An alias to assign the controller to on the scope.
  102. * - `parent` - `{element=}`: The element to append the bottom sheet to. The `parent` may be a `function`, `string`,
  103. * `object`, or null. Defaults to appending to the body of the root element (or the root element) of the application.
  104. * e.g. angular.element(document.getElementById('content')) or "#content"
  105. * - `disableParentScroll` - `{boolean=}`: Whether to disable scrolling while the bottom sheet is open.
  106. * Default true.
  107. *
  108. * @returns {promise} A promise that can be resolved with `$mdBottomSheet.hide()` or
  109. * rejected with `$mdBottomSheet.cancel()`.
  110. */
  111. /**
  112. * @ngdoc method
  113. * @name $mdBottomSheet#hide
  114. *
  115. * @description
  116. * Hide the existing bottom sheet and resolve the promise returned from
  117. * `$mdBottomSheet.show()`. This call will close the most recently opened/current bottomsheet (if any).
  118. *
  119. * @param {*=} response An argument for the resolved promise.
  120. *
  121. */
  122. /**
  123. * @ngdoc method
  124. * @name $mdBottomSheet#cancel
  125. *
  126. * @description
  127. * Hide the existing bottom sheet and reject the promise returned from
  128. * `$mdBottomSheet.show()`.
  129. *
  130. * @param {*=} response An argument for the rejected promise.
  131. *
  132. */
  133. function MdBottomSheetProvider($$interimElementProvider) {
  134. // how fast we need to flick down to close the sheet, pixels/ms
  135. bottomSheetDefaults.$inject = ["$animate", "$mdConstant", "$mdUtil", "$mdTheming", "$mdBottomSheet", "$rootElement", "$mdGesture", "$log"];
  136. var CLOSING_VELOCITY = 0.5;
  137. var PADDING = 80; // same as css
  138. return $$interimElementProvider('$mdBottomSheet')
  139. .setDefaults({
  140. methods: ['disableParentScroll', 'escapeToClose', 'clickOutsideToClose'],
  141. options: bottomSheetDefaults
  142. });
  143. /* ngInject */
  144. function bottomSheetDefaults($animate, $mdConstant, $mdUtil, $mdTheming, $mdBottomSheet, $rootElement,
  145. $mdGesture, $log) {
  146. var backdrop;
  147. return {
  148. themable: true,
  149. onShow: onShow,
  150. onRemove: onRemove,
  151. disableBackdrop: false,
  152. escapeToClose: true,
  153. clickOutsideToClose: true,
  154. disableParentScroll: true
  155. };
  156. function onShow(scope, element, options, controller) {
  157. element = $mdUtil.extractElementByName(element, 'md-bottom-sheet');
  158. // prevent tab focus or click focus on the bottom-sheet container
  159. element.attr('tabindex',"-1");
  160. // Once the md-bottom-sheet has `ng-cloak` applied on his template the opening animation will not work properly.
  161. // This is a very common problem, so we have to notify the developer about this.
  162. if (element.hasClass('ng-cloak')) {
  163. var message = '$mdBottomSheet: using `<md-bottom-sheet ng-cloak >` will affect the bottom-sheet opening animations.';
  164. $log.warn( message, element[0] );
  165. }
  166. if (!options.disableBackdrop) {
  167. // Add a backdrop that will close on click
  168. backdrop = $mdUtil.createBackdrop(scope, "md-bottom-sheet-backdrop md-opaque");
  169. // Prevent mouse focus on backdrop; ONLY programatic focus allowed.
  170. // This allows clicks on backdrop to propogate to the $rootElement and
  171. // ESC key events to be detected properly.
  172. backdrop[0].tabIndex = -1;
  173. if (options.clickOutsideToClose) {
  174. backdrop.on('click', function() {
  175. $mdUtil.nextTick($mdBottomSheet.cancel,true);
  176. });
  177. }
  178. $mdTheming.inherit(backdrop, options.parent);
  179. $animate.enter(backdrop, options.parent, null);
  180. }
  181. var bottomSheet = new BottomSheet(element, options.parent);
  182. options.bottomSheet = bottomSheet;
  183. $mdTheming.inherit(bottomSheet.element, options.parent);
  184. if (options.disableParentScroll) {
  185. options.restoreScroll = $mdUtil.disableScrollAround(bottomSheet.element, options.parent);
  186. }
  187. return $animate.enter(bottomSheet.element, options.parent, backdrop)
  188. .then(function() {
  189. var focusable = $mdUtil.findFocusTarget(element) || angular.element(
  190. element[0].querySelector('button') ||
  191. element[0].querySelector('a') ||
  192. element[0].querySelector($mdUtil.prefixer('ng-click', true))
  193. ) || backdrop;
  194. if (options.escapeToClose) {
  195. options.rootElementKeyupCallback = function(e) {
  196. if (e.keyCode === $mdConstant.KEY_CODE.ESCAPE) {
  197. $mdUtil.nextTick($mdBottomSheet.cancel,true);
  198. }
  199. };
  200. $rootElement.on('keyup', options.rootElementKeyupCallback);
  201. focusable && focusable.focus();
  202. }
  203. });
  204. }
  205. function onRemove(scope, element, options) {
  206. var bottomSheet = options.bottomSheet;
  207. if (!options.disableBackdrop) $animate.leave(backdrop);
  208. return $animate.leave(bottomSheet.element).then(function() {
  209. if (options.disableParentScroll) {
  210. options.restoreScroll();
  211. delete options.restoreScroll;
  212. }
  213. bottomSheet.cleanup();
  214. });
  215. }
  216. /**
  217. * BottomSheet class to apply bottom-sheet behavior to an element
  218. */
  219. function BottomSheet(element, parent) {
  220. var deregister = $mdGesture.register(parent, 'drag', { horizontal: false });
  221. parent.on('$md.dragstart', onDragStart)
  222. .on('$md.drag', onDrag)
  223. .on('$md.dragend', onDragEnd);
  224. return {
  225. element: element,
  226. cleanup: function cleanup() {
  227. deregister();
  228. parent.off('$md.dragstart', onDragStart);
  229. parent.off('$md.drag', onDrag);
  230. parent.off('$md.dragend', onDragEnd);
  231. }
  232. };
  233. function onDragStart(ev) {
  234. // Disable transitions on transform so that it feels fast
  235. element.css($mdConstant.CSS.TRANSITION_DURATION, '0ms');
  236. }
  237. function onDrag(ev) {
  238. var transform = ev.pointer.distanceY;
  239. if (transform < 5) {
  240. // Slow down drag when trying to drag up, and stop after PADDING
  241. transform = Math.max(-PADDING, transform / 2);
  242. }
  243. element.css($mdConstant.CSS.TRANSFORM, 'translate3d(0,' + (PADDING + transform) + 'px,0)');
  244. }
  245. function onDragEnd(ev) {
  246. if (ev.pointer.distanceY > 0 &&
  247. (ev.pointer.distanceY > 20 || Math.abs(ev.pointer.velocityY) > CLOSING_VELOCITY)) {
  248. var distanceRemaining = element.prop('offsetHeight') - ev.pointer.distanceY;
  249. var transitionDuration = Math.min(distanceRemaining / ev.pointer.velocityY * 0.75, 500);
  250. element.css($mdConstant.CSS.TRANSITION_DURATION, transitionDuration + 'ms');
  251. $mdUtil.nextTick($mdBottomSheet.cancel,true);
  252. } else {
  253. element.css($mdConstant.CSS.TRANSITION_DURATION, '');
  254. element.css($mdConstant.CSS.TRANSFORM, '');
  255. }
  256. }
  257. }
  258. }
  259. }
  260. ngmaterial.components.bottomSheet = angular.module("material.components.bottomSheet");