bottomSheet.js 9.3 KB

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