2 * Angular Material Design
3 * https://github.com/angular/material
7 goog.provide('ng.material.components.bottomSheet');
8 goog.require('ng.material.components.backdrop');
9 goog.require('ng.material.core');
12 * @name material.components.bottomSheet
16 angular.module('material.components.bottomSheet', [
18 'material.components.backdrop'
20 .directive('mdBottomSheet', MdBottomSheetDirective)
21 .provider('$mdBottomSheet', MdBottomSheetProvider);
23 function MdBottomSheetDirective() {
31 * @name $mdBottomSheet
32 * @module material.components.bottomSheet
35 * `$mdBottomSheet` opens a bottom sheet over the app and provides a simple promise API.
39 * - The bottom sheet's template must have an outer `<md-bottom-sheet>` element.
40 * - Add the `md-grid` class to the bottom sheet for a grid layout.
41 * - Add the `md-list` class to the bottom sheet for a list layout.
45 * <div ng-controller="MyController">
46 * <md-button ng-click="openBottomSheet()">
47 * Open a Bottom Sheet!
52 * var app = angular.module('app', ['ngMaterial']);
53 * app.controller('MyController', function($scope, $mdBottomSheet) {
54 * $scope.openBottomSheet = function() {
55 * $mdBottomSheet.show({
56 * template: '<md-bottom-sheet>Hello!</md-bottom-sheet>'
65 * @name $mdBottomSheet#show
68 * Show a bottom sheet with the specified options.
70 * @param {object} options An options object, with the following properties:
72 * - `templateUrl` - `{string=}`: The url of an html template file that will
73 * be used as the content of the bottom sheet. Restrictions: the template must
74 * have an outer `md-bottom-sheet` element.
75 * - `template` - `{string=}`: Same as templateUrl, except this is an actual
77 * - `scope` - `{object=}`: the scope to link the template / controller to. If none is specified, it will create a new child scope.
78 * This scope will be destroyed when the bottom sheet is removed unless `preserveScope` is set to true.
79 * - `preserveScope` - `{boolean=}`: whether to preserve the scope when the element is removed. Default is false
80 * - `controller` - `{string=}`: The controller to associate with this bottom sheet.
81 * - `locals` - `{string=}`: An object containing key/value pairs. The keys will
82 * be used as names of values to inject into the controller. For example,
83 * `locals: {three: 3}` would inject `three` into the controller with the value
85 * - `targetEvent` - `{DOMClickEvent=}`: A click's event object. When passed in as an option,
86 * the location of the click will be used as the starting point for the opening animation
88 * - `resolve` - `{object=}`: Similar to locals, except it takes promises as values
89 * and the bottom sheet will not open until the promises resolve.
90 * - `controllerAs` - `{string=}`: An alias to assign the controller to on the scope.
91 * - `parent` - `{element=}`: The element to append the bottom sheet to. The `parent` may be a `function`, `string`,
92 * `object`, or null. Defaults to appending to the body of the root element (or the root element) of the application.
93 * e.g. angular.element(document.getElementById('content')) or "#content"
94 * - `disableParentScroll` - `{boolean=}`: Whether to disable scrolling while the bottom sheet is open.
97 * @returns {promise} A promise that can be resolved with `$mdBottomSheet.hide()` or
98 * rejected with `$mdBottomSheet.cancel()`.
103 * @name $mdBottomSheet#hide
106 * Hide the existing bottom sheet and resolve the promise returned from
107 * `$mdBottomSheet.show()`. This call will close the most recently opened/current bottomsheet (if any).
109 * @param {*=} response An argument for the resolved promise.
115 * @name $mdBottomSheet#cancel
118 * Hide the existing bottom sheet and reject the promise returned from
119 * `$mdBottomSheet.show()`.
121 * @param {*=} response An argument for the rejected promise.
125 function MdBottomSheetProvider($$interimElementProvider) {
126 // how fast we need to flick down to close the sheet, pixels/ms
127 var CLOSING_VELOCITY = 0.5;
128 var PADDING = 80; // same as css
130 bottomSheetDefaults.$inject = ["$animate", "$mdConstant", "$mdUtil", "$timeout", "$compile", "$mdTheming", "$mdBottomSheet", "$rootElement", "$mdGesture"];
131 return $$interimElementProvider('$mdBottomSheet')
133 methods: ['disableParentScroll', 'escapeToClose', 'targetEvent'],
134 options: bottomSheetDefaults
138 function bottomSheetDefaults($animate, $mdConstant, $mdUtil, $timeout, $compile, $mdTheming, $mdBottomSheet, $rootElement, $mdGesture) {
147 disableParentScroll: true
151 function onShow(scope, element, options) {
153 element = $mdUtil.extractElementByName(element, 'md-bottom-sheet');
155 // Add a backdrop that will close on click
156 backdrop = $compile('<md-backdrop class="md-opaque md-bottom-sheet-backdrop">')(scope);
157 backdrop.on('click', function() {
158 $timeout($mdBottomSheet.cancel);
160 $mdTheming.inherit(backdrop, options.parent);
162 $animate.enter(backdrop, options.parent, null);
164 var bottomSheet = new BottomSheet(element, options.parent);
165 options.bottomSheet = bottomSheet;
167 // Give up focus on calling item
168 options.targetEvent && angular.element(options.targetEvent.target).blur();
169 $mdTheming.inherit(bottomSheet.element, options.parent);
171 if (options.disableParentScroll) {
172 options.lastOverflow = options.parent.css('overflow');
173 options.parent.css('overflow', 'hidden');
176 return $animate.enter(bottomSheet.element, options.parent)
178 var focusable = angular.element(
179 element[0].querySelector('button') ||
180 element[0].querySelector('a') ||
181 element[0].querySelector('[ng-click]')
185 if (options.escapeToClose) {
186 options.rootElementKeyupCallback = function(e) {
187 if (e.keyCode === $mdConstant.KEY_CODE.ESCAPE) {
188 $timeout($mdBottomSheet.cancel);
191 $rootElement.on('keyup', options.rootElementKeyupCallback);
197 function onRemove(scope, element, options) {
199 var bottomSheet = options.bottomSheet;
201 $animate.leave(backdrop);
202 return $animate.leave(bottomSheet.element).then(function() {
203 if (options.disableParentScroll) {
204 options.parent.css('overflow', options.lastOverflow);
205 delete options.lastOverflow;
208 bottomSheet.cleanup();
211 options.targetEvent && angular.element(options.targetEvent.target).focus();
216 * BottomSheet class to apply bottom-sheet behavior to an element
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);
226 cleanup: function cleanup() {
228 parent.off('$md.dragstart', onDragStart)
229 .off('$md.drag', onDrag)
230 .off('$md.dragend', onDragEnd);
234 function onDragStart(ev) {
235 // Disable transitions on transform so that it feels fast
236 element.css($mdConstant.CSS.TRANSITION_DURATION, '0ms');
239 function onDrag(ev) {
240 var transform = ev.pointer.distanceY;
242 // Slow down drag when trying to drag up, and stop after PADDING
243 transform = Math.max(-PADDING, transform / 2);
245 element.css($mdConstant.CSS.TRANSFORM, 'translate3d(0,' + (PADDING + transform) + 'px,0)');
248 function onDragEnd(ev) {
249 if (ev.pointer.distanceY > 0 &&
250 (ev.pointer.distanceY > 20 || Math.abs(ev.pointer.velocityY) > CLOSING_VELOCITY)) {
251 var distanceRemaining = element.prop('offsetHeight') - ev.pointer.distanceY;
252 var transitionDuration = Math.min(distanceRemaining / ev.pointer.velocityY * 0.75, 500);
253 element.css($mdConstant.CSS.TRANSITION_DURATION, transitionDuration + 'ms');
254 $timeout($mdBottomSheet.cancel);
256 element.css($mdConstant.CSS.TRANSITION_DURATION, '');
257 element.css($mdConstant.CSS.TRANSFORM, '');
265 MdBottomSheetProvider.$inject = ["$$interimElementProvider"];
267 ng.material.components.bottomSheet = angular.module("material.components.bottomSheet");