bottomSheet.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*!
  2. * AngularJS Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v1.1.8-master-aba7b2b
  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>' +
  66. * 'Hello! <md-button ng-click="closeBottomSheet()">Close</md-button>' +
  67. * '</md-bottom-sheet>'
  68. * })
  69. *
  70. * // Fires when the hide() method is used
  71. * .then(function() {
  72. * console.log('You clicked the button to close the bottom sheet!');
  73. * })
  74. *
  75. * // Fires when the cancel() method is used
  76. * .catch(function() {
  77. * console.log('You hit escape or clicked the backdrop to close.');
  78. * });
  79. * };
  80. *
  81. * $scope.closeBottomSheet = function($scope, $mdBottomSheet) {
  82. * $mdBottomSheet.hide();
  83. * }
  84. *
  85. * });
  86. * </hljs>
  87. *
  88. * ### Custom Presets
  89. * Developers are also able to create their own preset, which can be easily used without repeating
  90. * their options each time.
  91. *
  92. * <hljs lang="js">
  93. * $mdBottomSheetProvider.addPreset('testPreset', {
  94. * options: function() {
  95. * return {
  96. * template:
  97. * '<md-bottom-sheet>' +
  98. * 'This is a custom preset' +
  99. * '</md-bottom-sheet>',
  100. * controllerAs: 'bottomSheet',
  101. * bindToController: true,
  102. * clickOutsideToClose: true,
  103. * escapeToClose: true
  104. * };
  105. * }
  106. * });
  107. * </hljs>
  108. *
  109. * After you create your preset during the config phase, you can easily access it.
  110. *
  111. * <hljs lang="js">
  112. * $mdBottomSheet.show(
  113. * $mdBottomSheet.testPreset()
  114. * );
  115. * </hljs>
  116. */
  117. /**
  118. * @ngdoc method
  119. * @name $mdBottomSheet#show
  120. *
  121. * @description
  122. * Show a bottom sheet with the specified options.
  123. *
  124. * <em><b>Note:</b> You should <b>always</b> provide a `.catch()` method in case the user hits the
  125. * `esc` key or clicks the background to close. In this case, the `cancel()` method will
  126. * automatically be called on the bottom sheet which will `reject()` the promise. See the @usage
  127. * section above for an example.
  128. *
  129. * Newer versions of Angular will throw a `Possibly unhandled rejection` exception if you forget
  130. * this.</em>
  131. *
  132. * @param {object} optionsOrPreset Either provide an `$mdBottomSheetPreset` defined during the config phase or
  133. * an options object, with the following properties:
  134. *
  135. * - `templateUrl` - `{string=}`: The url of an html template file that will
  136. * be used as the content of the bottom sheet. Restrictions: the template must
  137. * have an outer `md-bottom-sheet` element.
  138. * - `template` - `{string=}`: Same as templateUrl, except this is an actual
  139. * template string.
  140. * - `scope` - `{object=}`: the scope to link the template / controller to. If none is specified, it will create a new child scope.
  141. * This scope will be destroyed when the bottom sheet is removed unless `preserveScope` is set to true.
  142. * - `preserveScope` - `{boolean=}`: whether to preserve the scope when the element is removed. Default is false
  143. * - `controller` - `{string=}`: The controller to associate with this bottom sheet.
  144. * - `locals` - `{string=}`: An object containing key/value pairs. The keys will
  145. * be used as names of values to inject into the controller. For example,
  146. * `locals: {three: 3}` would inject `three` into the controller with the value
  147. * of 3.
  148. * - `clickOutsideToClose` - `{boolean=}`: Whether the user can click outside the bottom sheet to
  149. * close it. Default true.
  150. * - `bindToController` - `{boolean=}`: When set to true, the locals will be bound to the controller instance.
  151. * - `disableBackdrop` - `{boolean=}`: When set to true, the bottomsheet will not show a backdrop.
  152. * - `escapeToClose` - `{boolean=}`: Whether the user can press escape to close the bottom sheet.
  153. * Default true.
  154. * - `isLockedOpen` - `{boolean=}`: Disables all default ways of closing the bottom sheet. **Note:** this will override
  155. * the `clickOutsideToClose` and `escapeToClose` options, leaving only the `hide` and `cancel`
  156. * methods as ways of closing the bottom sheet. Defaults to false.
  157. * - `resolve` - `{object=}`: Similar to locals, except it takes promises as values
  158. * and the bottom sheet will not open until the promises resolve.
  159. * - `controllerAs` - `{string=}`: An alias to assign the controller to on the scope.
  160. * - `parent` - `{element=}`: The element to append the bottom sheet to. The `parent` may be a `function`, `string`,
  161. * `object`, or null. Defaults to appending to the body of the root element (or the root element) of the application.
  162. * e.g. angular.element(document.getElementById('content')) or "#content"
  163. * - `disableParentScroll` - `{boolean=}`: Whether to disable scrolling while the bottom sheet is open.
  164. * Default true.
  165. *
  166. * @returns {promise} A promise that can be resolved with `$mdBottomSheet.hide()` or
  167. * rejected with `$mdBottomSheet.cancel()`.
  168. */
  169. /**
  170. * @ngdoc method
  171. * @name $mdBottomSheet#hide
  172. *
  173. * @description
  174. * Hide the existing bottom sheet and resolve the promise returned from
  175. * `$mdBottomSheet.show()`. This call will close the most recently opened/current bottomsheet (if
  176. * any).
  177. *
  178. * <em><b>Note:</b> Use a `.then()` on your `.show()` to handle this callback.</em>
  179. *
  180. * @param {*=} response An argument for the resolved promise.
  181. *
  182. */
  183. /**
  184. * @ngdoc method
  185. * @name $mdBottomSheet#cancel
  186. *
  187. * @description
  188. * Hide the existing bottom sheet and reject the promise returned from
  189. * `$mdBottomSheet.show()`.
  190. *
  191. * <em><b>Note:</b> Use a `.catch()` on your `.show()` to handle this callback.</em>
  192. *
  193. * @param {*=} response An argument for the rejected promise.
  194. *
  195. */
  196. function MdBottomSheetProvider($$interimElementProvider) {
  197. // how fast we need to flick down to close the sheet, pixels/ms
  198. bottomSheetDefaults['$inject'] = ["$animate", "$mdConstant", "$mdUtil", "$mdTheming", "$mdBottomSheet", "$rootElement", "$mdGesture", "$log"];
  199. var CLOSING_VELOCITY = 0.5;
  200. var PADDING = 80; // same as css
  201. return $$interimElementProvider('$mdBottomSheet')
  202. .setDefaults({
  203. methods: ['disableParentScroll', 'escapeToClose', 'clickOutsideToClose'],
  204. options: bottomSheetDefaults
  205. });
  206. /* ngInject */
  207. function bottomSheetDefaults($animate, $mdConstant, $mdUtil, $mdTheming, $mdBottomSheet, $rootElement,
  208. $mdGesture, $log) {
  209. var backdrop;
  210. return {
  211. themable: true,
  212. onShow: onShow,
  213. onRemove: onRemove,
  214. disableBackdrop: false,
  215. escapeToClose: true,
  216. clickOutsideToClose: true,
  217. disableParentScroll: true,
  218. isLockedOpen: false
  219. };
  220. function onShow(scope, element, options, controller) {
  221. element = $mdUtil.extractElementByName(element, 'md-bottom-sheet');
  222. // prevent tab focus or click focus on the bottom-sheet container
  223. element.attr('tabindex', '-1');
  224. // Once the md-bottom-sheet has `ng-cloak` applied on his template the opening animation will not work properly.
  225. // This is a very common problem, so we have to notify the developer about this.
  226. if (element.hasClass('ng-cloak')) {
  227. var message = '$mdBottomSheet: using `<md-bottom-sheet ng-cloak>` will affect the bottom-sheet opening animations.';
  228. $log.warn( message, element[0] );
  229. }
  230. if (options.isLockedOpen) {
  231. options.clickOutsideToClose = false;
  232. options.escapeToClose = false;
  233. } else {
  234. options.cleanupGestures = registerGestures(element, options.parent);
  235. }
  236. if (!options.disableBackdrop) {
  237. // Add a backdrop that will close on click
  238. backdrop = $mdUtil.createBackdrop(scope, "md-bottom-sheet-backdrop md-opaque");
  239. // Prevent mouse focus on backdrop; ONLY programatic focus allowed.
  240. // This allows clicks on backdrop to propogate to the $rootElement and
  241. // ESC key events to be detected properly.
  242. backdrop[0].tabIndex = -1;
  243. if (options.clickOutsideToClose) {
  244. backdrop.on('click', function() {
  245. $mdUtil.nextTick($mdBottomSheet.cancel,true);
  246. });
  247. }
  248. $mdTheming.inherit(backdrop, options.parent);
  249. $animate.enter(backdrop, options.parent, null);
  250. }
  251. $mdTheming.inherit(element, options.parent);
  252. if (options.disableParentScroll) {
  253. options.restoreScroll = $mdUtil.disableScrollAround(element, options.parent);
  254. }
  255. return $animate.enter(element, options.parent, backdrop)
  256. .then(function() {
  257. var focusable = $mdUtil.findFocusTarget(element) || angular.element(
  258. element[0].querySelector('button') ||
  259. element[0].querySelector('a') ||
  260. element[0].querySelector($mdUtil.prefixer('ng-click', true))
  261. ) || backdrop;
  262. if (options.escapeToClose) {
  263. options.rootElementKeyupCallback = function(e) {
  264. if (e.keyCode === $mdConstant.KEY_CODE.ESCAPE) {
  265. $mdUtil.nextTick($mdBottomSheet.cancel,true);
  266. }
  267. };
  268. $rootElement.on('keyup', options.rootElementKeyupCallback);
  269. focusable && focusable.focus();
  270. }
  271. });
  272. }
  273. function onRemove(scope, element, options) {
  274. if (!options.disableBackdrop) $animate.leave(backdrop);
  275. return $animate.leave(element).then(function() {
  276. if (options.disableParentScroll) {
  277. options.restoreScroll();
  278. delete options.restoreScroll;
  279. }
  280. options.cleanupGestures && options.cleanupGestures();
  281. });
  282. }
  283. /**
  284. * Adds the drag gestures to the bottom sheet.
  285. */
  286. function registerGestures(element, parent) {
  287. var deregister = $mdGesture.register(parent, 'drag', { horizontal: false });
  288. parent.on('$md.dragstart', onDragStart)
  289. .on('$md.drag', onDrag)
  290. .on('$md.dragend', onDragEnd);
  291. return function cleanupGestures() {
  292. deregister();
  293. parent.off('$md.dragstart', onDragStart);
  294. parent.off('$md.drag', onDrag);
  295. parent.off('$md.dragend', onDragEnd);
  296. };
  297. function onDragStart() {
  298. // Disable transitions on transform so that it feels fast
  299. element.css($mdConstant.CSS.TRANSITION_DURATION, '0ms');
  300. }
  301. function onDrag(ev) {
  302. var transform = ev.pointer.distanceY;
  303. if (transform < 5) {
  304. // Slow down drag when trying to drag up, and stop after PADDING
  305. transform = Math.max(-PADDING, transform / 2);
  306. }
  307. element.css($mdConstant.CSS.TRANSFORM, 'translate3d(0,' + (PADDING + transform) + 'px,0)');
  308. }
  309. function onDragEnd(ev) {
  310. if (ev.pointer.distanceY > 0 &&
  311. (ev.pointer.distanceY > 20 || Math.abs(ev.pointer.velocityY) > CLOSING_VELOCITY)) {
  312. var distanceRemaining = element.prop('offsetHeight') - ev.pointer.distanceY;
  313. var transitionDuration = Math.min(distanceRemaining / ev.pointer.velocityY * 0.75, 500);
  314. element.css($mdConstant.CSS.TRANSITION_DURATION, transitionDuration + 'ms');
  315. $mdUtil.nextTick($mdBottomSheet.cancel,true);
  316. } else {
  317. element.css($mdConstant.CSS.TRANSITION_DURATION, '');
  318. element.css($mdConstant.CSS.TRANSFORM, '');
  319. }
  320. }
  321. }
  322. }
  323. }
  324. })(window, window.angular);