dialog.js 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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.dialog
  12. */
  13. MdDialogDirective['$inject'] = ["$$rAF", "$mdTheming", "$mdDialog"];
  14. MdDialogProvider['$inject'] = ["$$interimElementProvider"];
  15. angular
  16. .module('material.components.dialog', [
  17. 'material.core',
  18. 'material.components.backdrop'
  19. ])
  20. .directive('mdDialog', MdDialogDirective)
  21. .provider('$mdDialog', MdDialogProvider);
  22. /**
  23. * @ngdoc directive
  24. * @name mdDialog
  25. * @module material.components.dialog
  26. *
  27. * @restrict E
  28. *
  29. * @description
  30. * `<md-dialog>` - The dialog's template must be inside this element.
  31. *
  32. * Inside, use an `<md-dialog-content>` element for the dialog's content, and use
  33. * an `<md-dialog-actions>` element for the dialog's actions.
  34. *
  35. * ## CSS
  36. * - `.md-dialog-content` - class that sets the padding on the content as the spec file
  37. *
  38. * ## Notes
  39. * - If you specify an `id` for the `<md-dialog>`, the `<md-dialog-content>` will have the same `id`
  40. * prefixed with `dialogContent_`.
  41. *
  42. * @usage
  43. * ### Dialog template
  44. * <hljs lang="html">
  45. * <md-dialog aria-label="List dialog">
  46. * <md-dialog-content>
  47. * <md-list>
  48. * <md-list-item ng-repeat="item in items">
  49. * <p>Number {{item}}</p>
  50. * </md-list-item>
  51. * </md-list>
  52. * </md-dialog-content>
  53. * <md-dialog-actions>
  54. * <md-button ng-click="closeDialog()" class="md-primary">Close Dialog</md-button>
  55. * </md-dialog-actions>
  56. * </md-dialog>
  57. * </hljs>
  58. */
  59. function MdDialogDirective($$rAF, $mdTheming, $mdDialog) {
  60. return {
  61. restrict: 'E',
  62. link: function(scope, element) {
  63. element.addClass('_md'); // private md component indicator for styling
  64. $mdTheming(element);
  65. $$rAF(function() {
  66. var images;
  67. var content = element[0].querySelector('md-dialog-content');
  68. if (content) {
  69. images = content.getElementsByTagName('img');
  70. addOverflowClass();
  71. //-- delayed image loading may impact scroll height, check after images are loaded
  72. angular.element(images).on('load', addOverflowClass);
  73. }
  74. scope.$on('$destroy', function() {
  75. $mdDialog.destroy(element);
  76. });
  77. /**
  78. *
  79. */
  80. function addOverflowClass() {
  81. element.toggleClass('md-content-overflow', content.scrollHeight > content.clientHeight);
  82. }
  83. });
  84. }
  85. };
  86. }
  87. /**
  88. * @ngdoc service
  89. * @name $mdDialog
  90. * @module material.components.dialog
  91. *
  92. * @description
  93. * `$mdDialog` opens a dialog over the app to inform users about critical information or require
  94. * them to make decisions. There are two approaches for setup: a simple promise API
  95. * and regular object syntax.
  96. *
  97. * ## Restrictions
  98. *
  99. * - The dialog is always given an isolate scope.
  100. * - The dialog's template must have an outer `<md-dialog>` element.
  101. * Inside, use an `<md-dialog-content>` element for the dialog's content, and use
  102. * an `<md-dialog-actions>` element for the dialog's actions.
  103. * - Dialogs must cover the entire application to keep interactions inside of them.
  104. * Use the `parent` option to change where dialogs are appended.
  105. *
  106. * ## Sizing
  107. * - Complex dialogs can be sized with `flex="percentage"`, i.e. `flex="66"`.
  108. * - Default max-width is 80% of the `rootElement` or `parent`.
  109. *
  110. * ## CSS
  111. * - `.md-dialog-content` - class that sets the padding on the content as the spec file
  112. *
  113. * @usage
  114. * <hljs lang="html">
  115. * <div ng-app="demoApp" ng-controller="EmployeeController">
  116. * <div>
  117. * <md-button ng-click="showAlert()" class="md-raised md-warn">
  118. * Employee Alert!
  119. * </md-button>
  120. * </div>
  121. * <div>
  122. * <md-button ng-click="showDialog($event)" class="md-raised">
  123. * Custom Dialog
  124. * </md-button>
  125. * </div>
  126. * <div>
  127. * <md-button ng-click="closeAlert()" ng-disabled="!hasAlert()" class="md-raised">
  128. * Close Alert
  129. * </md-button>
  130. * </div>
  131. * <div>
  132. * <md-button ng-click="showGreeting($event)" class="md-raised md-primary" >
  133. * Greet Employee
  134. * </md-button>
  135. * </div>
  136. * </div>
  137. * </hljs>
  138. *
  139. * ### JavaScript: object syntax
  140. * <hljs lang="js">
  141. * (function(angular, undefined){
  142. * "use strict";
  143. *
  144. * angular
  145. * .module('demoApp', ['ngMaterial'])
  146. * .controller('AppCtrl', AppController);
  147. *
  148. * function AppController($scope, $mdDialog) {
  149. * var alert;
  150. * $scope.showAlert = showAlert;
  151. * $scope.showDialog = showDialog;
  152. * $scope.items = [1, 2, 3];
  153. *
  154. * // Internal method
  155. * function showAlert() {
  156. * alert = $mdDialog.alert({
  157. * title: 'Attention',
  158. * textContent: 'This is an example of how easy dialogs can be!',
  159. * ok: 'Close'
  160. * });
  161. *
  162. * $mdDialog
  163. * .show( alert )
  164. * .finally(function() {
  165. * alert = undefined;
  166. * });
  167. * }
  168. *
  169. * function showDialog($event) {
  170. * var parentEl = angular.element(document.body);
  171. * $mdDialog.show({
  172. * parent: parentEl,
  173. * targetEvent: $event,
  174. * template:
  175. * '<md-dialog aria-label="List dialog">' +
  176. * ' <md-dialog-content>'+
  177. * ' <md-list>'+
  178. * ' <md-list-item ng-repeat="item in items">'+
  179. * ' <p>Number {{item}}</p>' +
  180. * ' </md-item>'+
  181. * ' </md-list>'+
  182. * ' </md-dialog-content>' +
  183. * ' <md-dialog-actions>' +
  184. * ' <md-button ng-click="closeDialog()" class="md-primary">' +
  185. * ' Close Dialog' +
  186. * ' </md-button>' +
  187. * ' </md-dialog-actions>' +
  188. * '</md-dialog>',
  189. * locals: {
  190. * items: $scope.items
  191. * },
  192. * controller: DialogController
  193. * });
  194. * function DialogController($scope, $mdDialog, items) {
  195. * $scope.items = items;
  196. * $scope.closeDialog = function() {
  197. * $mdDialog.hide();
  198. * }
  199. * }
  200. * }
  201. * }
  202. * })(angular);
  203. * </hljs>
  204. *
  205. * ### Multiple Dialogs
  206. * Using the `multiple` option for the `$mdDialog` service allows developers to show multiple dialogs
  207. * at the same time.
  208. *
  209. * <hljs lang="js">
  210. * // From plain options
  211. * $mdDialog.show({
  212. * multiple: true
  213. * });
  214. *
  215. * // From a dialog preset
  216. * $mdDialog.show(
  217. * $mdDialog
  218. * .alert()
  219. * .multiple(true)
  220. * );
  221. *
  222. * </hljs>
  223. *
  224. * ### Pre-Rendered Dialogs
  225. * By using the `contentElement` option, it is possible to use an already existing element in the DOM.
  226. *
  227. * > Pre-rendered dialogs will be not linked to any scope and will not instantiate any new controller.<br/>
  228. * > You can manually link the elements to a scope or instantiate a controller from the template (`ng-controller`)
  229. *
  230. * <hljs lang="js">
  231. * $scope.showPrerenderedDialog = function() {
  232. * $mdDialog.show({
  233. * contentElement: '#myStaticDialog',
  234. * parent: angular.element(document.body)
  235. * });
  236. * };
  237. * </hljs>
  238. *
  239. * When using a string as value, `$mdDialog` will automatically query the DOM for the specified CSS selector.
  240. *
  241. * <hljs lang="html">
  242. * <div style="visibility: hidden">
  243. * <div class="md-dialog-container" id="myStaticDialog">
  244. * <md-dialog>
  245. * This is a pre-rendered dialog.
  246. * </md-dialog>
  247. * </div>
  248. * </div>
  249. * </hljs>
  250. *
  251. * **Notice**: It is important, to use the `.md-dialog-container` as the content element, otherwise the dialog
  252. * will not show up.
  253. *
  254. * It also possible to use a DOM element for the `contentElement` option.
  255. * - `contentElement: document.querySelector('#myStaticDialog')`
  256. * - `contentElement: angular.element(TEMPLATE)`
  257. *
  258. * When using a `template` as content element, it will be not compiled upon open.
  259. * This allows you to compile the element yourself and use it each time the dialog opens.
  260. *
  261. * ### Custom Presets
  262. * Developers are also able to create their own preset, which can be easily used without repeating
  263. * their options each time.
  264. *
  265. * <hljs lang="js">
  266. * $mdDialogProvider.addPreset('testPreset', {
  267. * options: function() {
  268. * return {
  269. * template:
  270. * '<md-dialog>' +
  271. * 'This is a custom preset' +
  272. * '</md-dialog>',
  273. * controllerAs: 'dialog',
  274. * bindToController: true,
  275. * clickOutsideToClose: true,
  276. * escapeToClose: true
  277. * };
  278. * }
  279. * });
  280. * </hljs>
  281. *
  282. * After you created your preset at config phase, you can easily access it.
  283. *
  284. * <hljs lang="js">
  285. * $mdDialog.show(
  286. * $mdDialog.testPreset()
  287. * );
  288. * </hljs>
  289. *
  290. * ### JavaScript: promise API syntax, custom dialog template
  291. * <hljs lang="js">
  292. * (function(angular, undefined){
  293. * "use strict";
  294. *
  295. * angular
  296. * .module('demoApp', ['ngMaterial'])
  297. * .controller('EmployeeController', EmployeeEditor)
  298. * .controller('GreetingController', GreetingController);
  299. *
  300. * // Fictitious Employee Editor to show how to use simple and complex dialogs.
  301. *
  302. * function EmployeeEditor($scope, $mdDialog) {
  303. * var alert;
  304. *
  305. * $scope.showAlert = showAlert;
  306. * $scope.closeAlert = closeAlert;
  307. * $scope.showGreeting = showCustomGreeting;
  308. *
  309. * $scope.hasAlert = function() { return !!alert };
  310. * $scope.userName = $scope.userName || 'Bobby';
  311. *
  312. * // Dialog #1 - Show simple alert dialog and cache
  313. * // reference to dialog instance
  314. *
  315. * function showAlert() {
  316. * alert = $mdDialog.alert()
  317. * .title('Attention, ' + $scope.userName)
  318. * .textContent('This is an example of how easy dialogs can be!')
  319. * .ok('Close');
  320. *
  321. * $mdDialog
  322. * .show( alert )
  323. * .finally(function() {
  324. * alert = undefined;
  325. * });
  326. * }
  327. *
  328. * // Close the specified dialog instance and resolve with 'finished' flag
  329. * // Normally this is not needed, just use '$mdDialog.hide()' to close
  330. * // the most recent dialog popup.
  331. *
  332. * function closeAlert() {
  333. * $mdDialog.hide( alert, "finished" );
  334. * alert = undefined;
  335. * }
  336. *
  337. * // Dialog #2 - Demonstrate more complex dialogs construction and popup.
  338. *
  339. * function showCustomGreeting($event) {
  340. * $mdDialog.show({
  341. * targetEvent: $event,
  342. * template:
  343. * '<md-dialog>' +
  344. *
  345. * ' <md-dialog-content>Hello {{ employee }}!</md-dialog-content>' +
  346. *
  347. * ' <md-dialog-actions>' +
  348. * ' <md-button ng-click="closeDialog()" class="md-primary">' +
  349. * ' Close Greeting' +
  350. * ' </md-button>' +
  351. * ' </md-dialog-actions>' +
  352. * '</md-dialog>',
  353. * controller: 'GreetingController',
  354. * onComplete: afterShowAnimation,
  355. * locals: { employee: $scope.userName }
  356. * });
  357. *
  358. * // When the 'enter' animation finishes...
  359. *
  360. * function afterShowAnimation(scope, element, options) {
  361. * // post-show code here: DOM element focus, etc.
  362. * }
  363. * }
  364. *
  365. * // Dialog #3 - Demonstrate use of ControllerAs and passing $scope to dialog
  366. * // Here we used ng-controller="GreetingController as vm" and
  367. * // $scope.vm === <controller instance>
  368. *
  369. * function showCustomGreeting() {
  370. *
  371. * $mdDialog.show({
  372. * clickOutsideToClose: true,
  373. *
  374. * scope: $scope, // use parent scope in template
  375. * preserveScope: true, // do not forget this if use parent scope
  376. * // Since GreetingController is instantiated with ControllerAs syntax
  377. * // AND we are passing the parent '$scope' to the dialog, we MUST
  378. * // use 'vm.<xxx>' in the template markup
  379. *
  380. * template: '<md-dialog>' +
  381. * ' <md-dialog-content>' +
  382. * ' Hi There {{vm.employee}}' +
  383. * ' </md-dialog-content>' +
  384. * '</md-dialog>',
  385. *
  386. * controller: function DialogController($scope, $mdDialog) {
  387. * $scope.closeDialog = function() {
  388. * $mdDialog.hide();
  389. * }
  390. * }
  391. * });
  392. * }
  393. *
  394. * }
  395. *
  396. * // Greeting controller used with the more complex 'showCustomGreeting()' custom dialog
  397. *
  398. * function GreetingController($scope, $mdDialog, employee) {
  399. * // Assigned from construction <code>locals</code> options...
  400. * $scope.employee = employee;
  401. *
  402. * $scope.closeDialog = function() {
  403. * // Easily hides most recent dialog shown...
  404. * // no specific instance reference is needed.
  405. * $mdDialog.hide();
  406. * };
  407. * }
  408. *
  409. * })(angular);
  410. * </hljs>
  411. */
  412. /**
  413. * @ngdoc method
  414. * @name $mdDialog#alert
  415. *
  416. * @description
  417. * Builds a preconfigured dialog with the specified message.
  418. *
  419. * @returns {obj} an `$mdDialogPreset` with the chainable configuration methods:
  420. *
  421. * - $mdDialogPreset#title(string) - Sets the alert title.
  422. * - $mdDialogPreset#textContent(string) - Sets the alert message.
  423. * - $mdDialogPreset#htmlContent(string) - Sets the alert message as HTML. Requires ngSanitize
  424. * module to be loaded. HTML is not run through Angular's compiler.
  425. * - $mdDialogPreset#ok(string) - Sets the alert "Okay" button text.
  426. * - $mdDialogPreset#theme(string) - Sets the theme of the alert dialog.
  427. * - $mdDialogPreset#targetEvent(DOMClickEvent=) - A click's event object. When passed in as an option,
  428. * the location of the click will be used as the starting point for the opening animation
  429. * of the the dialog.
  430. *
  431. */
  432. /**
  433. * @ngdoc method
  434. * @name $mdDialog#confirm
  435. *
  436. * @description
  437. * Builds a preconfigured dialog with the specified message. You can call show and the promise returned
  438. * will be resolved only if the user clicks the confirm action on the dialog.
  439. *
  440. * @returns {obj} an `$mdDialogPreset` with the chainable configuration methods:
  441. *
  442. * Additionally, it supports the following methods:
  443. *
  444. * - $mdDialogPreset#title(string) - Sets the confirm title.
  445. * - $mdDialogPreset#textContent(string) - Sets the confirm message.
  446. * - $mdDialogPreset#htmlContent(string) - Sets the confirm message as HTML. Requires ngSanitize
  447. * module to be loaded. HTML is not run through Angular's compiler.
  448. * - $mdDialogPreset#ok(string) - Sets the confirm "Okay" button text.
  449. * - $mdDialogPreset#cancel(string) - Sets the confirm "Cancel" button text.
  450. * - $mdDialogPreset#theme(string) - Sets the theme of the confirm dialog.
  451. * - $mdDialogPreset#targetEvent(DOMClickEvent=) - A click's event object. When passed in as an option,
  452. * the location of the click will be used as the starting point for the opening animation
  453. * of the the dialog.
  454. *
  455. */
  456. /**
  457. * @ngdoc method
  458. * @name $mdDialog#prompt
  459. *
  460. * @description
  461. * Builds a preconfigured dialog with the specified message and input box. You can call show and the promise returned
  462. * will be resolved only if the user clicks the prompt action on the dialog, passing the input value as the first argument.
  463. *
  464. * @returns {obj} an `$mdDialogPreset` with the chainable configuration methods:
  465. *
  466. * Additionally, it supports the following methods:
  467. *
  468. * - $mdDialogPreset#title(string) - Sets the prompt title.
  469. * - $mdDialogPreset#textContent(string) - Sets the prompt message.
  470. * - $mdDialogPreset#htmlContent(string) - Sets the prompt message as HTML. Requires ngSanitize
  471. * module to be loaded. HTML is not run through Angular's compiler.
  472. * - $mdDialogPreset#placeholder(string) - Sets the placeholder text for the input.
  473. * - $mdDialogPreset#required(boolean) - Sets the input required value.
  474. * - $mdDialogPreset#initialValue(string) - Sets the initial value for the prompt input.
  475. * - $mdDialogPreset#ok(string) - Sets the prompt "Okay" button text.
  476. * - $mdDialogPreset#cancel(string) - Sets the prompt "Cancel" button text.
  477. * - $mdDialogPreset#theme(string) - Sets the theme of the prompt dialog.
  478. * - $mdDialogPreset#targetEvent(DOMClickEvent=) - A click's event object. When passed in as an option,
  479. * the location of the click will be used as the starting point for the opening animation
  480. * of the the dialog.
  481. *
  482. */
  483. /**
  484. * @ngdoc method
  485. * @name $mdDialog#show
  486. *
  487. * @description
  488. * Show a dialog with the specified options.
  489. *
  490. * @param {object} optionsOrPreset Either provide an `$mdDialogPreset` returned from `alert()`, and
  491. * `confirm()`, or an options object with the following properties:
  492. * - `templateUrl` - `{string=}`: The url of a template that will be used as the content
  493. * of the dialog.
  494. * - `template` - `{string=}`: HTML template to show in the dialog. This **must** be trusted HTML
  495. * with respect to Angular's [$sce service](https://docs.angularjs.org/api/ng/service/$sce).
  496. * This template should **never** be constructed with any kind of user input or user data.
  497. * - `contentElement` - `{string|Element}`: Instead of using a template, which will be compiled each time a
  498. * dialog opens, you can also use a DOM element.<br/>
  499. * * When specifying an element, which is present on the DOM, `$mdDialog` will temporary fetch the element into
  500. * the dialog and restores it at the old DOM position upon close.
  501. * * When specifying a string, the string be used as a CSS selector, to lookup for the element in the DOM.
  502. * - `autoWrap` - `{boolean=}`: Whether or not to automatically wrap the template with a
  503. * `<md-dialog>` tag if one is not provided. Defaults to true. Can be disabled if you provide a
  504. * custom dialog directive.
  505. * - `targetEvent` - `{DOMClickEvent=}`: A click's event object. When passed in as an option,
  506. * the location of the click will be used as the starting point for the opening animation
  507. * of the the dialog.
  508. * - `openFrom` - `{string|Element|object}`: The query selector, DOM element or the Rect object
  509. * that is used to determine the bounds (top, left, height, width) from which the Dialog will
  510. * originate.
  511. * - `closeTo` - `{string|Element|object}`: The query selector, DOM element or the Rect object
  512. * that is used to determine the bounds (top, left, height, width) to which the Dialog will
  513. * target.
  514. * - `scope` - `{object=}`: the scope to link the template / controller to. If none is specified,
  515. * it will create a new isolate scope.
  516. * This scope will be destroyed when the dialog is removed unless `preserveScope` is set to true.
  517. * - `preserveScope` - `{boolean=}`: whether to preserve the scope when the element is removed. Default is false
  518. * - `disableParentScroll` - `{boolean=}`: Whether to disable scrolling while the dialog is open.
  519. * Default true.
  520. * - `hasBackdrop` - `{boolean=}`: Whether there should be an opaque backdrop behind the dialog.
  521. * Default true.
  522. * - `clickOutsideToClose` - `{boolean=}`: Whether the user can click outside the dialog to
  523. * close it. Default false.
  524. * - `escapeToClose` - `{boolean=}`: Whether the user can press escape to close the dialog.
  525. * Default true.
  526. * - `focusOnOpen` - `{boolean=}`: An option to override focus behavior on open. Only disable if
  527. * focusing some other way, as focus management is required for dialogs to be accessible.
  528. * Defaults to true.
  529. * - `controller` - `{function|string=}`: The controller to associate with the dialog. The controller
  530. * will be injected with the local `$mdDialog`, which passes along a scope for the dialog.
  531. * - `locals` - `{object=}`: An object containing key/value pairs. The keys will be used as names
  532. * of values to inject into the controller. For example, `locals: {three: 3}` would inject
  533. * `three` into the controller, with the value 3. If `bindToController` is true, they will be
  534. * copied to the controller instead.
  535. * - `bindToController` - `bool`: bind the locals to the controller, instead of passing them in.
  536. * - `resolve` - `{function=}`: Similar to locals, except it takes as values functions that return promises, and the
  537. * dialog will not open until all of the promises resolve.
  538. * - `controllerAs` - `{string=}`: An alias to assign the controller to on the scope.
  539. * - `parent` - `{element=}`: The element to append the dialog to. Defaults to appending
  540. * to the root element of the application.
  541. * - `onShowing` - `function(scope, element)`: Callback function used to announce the show() action is
  542. * starting.
  543. * - `onComplete` - `function(scope, element)`: Callback function used to announce when the show() action is
  544. * finished.
  545. * - `onRemoving` - `function(element, removePromise)`: Callback function used to announce the
  546. * close/hide() action is starting. This allows developers to run custom animations
  547. * in parallel with the close animations.
  548. * - `fullscreen` `{boolean=}`: An option to toggle whether the dialog should show in fullscreen
  549. * or not. Defaults to `false`.
  550. * - `multiple` `{boolean=}`: An option to allow this dialog to display over one that's currently open.
  551. * @returns {promise} A promise that can be resolved with `$mdDialog.hide()` or
  552. * rejected with `$mdDialog.cancel()`.
  553. */
  554. /**
  555. * @ngdoc method
  556. * @name $mdDialog#hide
  557. *
  558. * @description
  559. * Hide an existing dialog and resolve the promise returned from `$mdDialog.show()`.
  560. *
  561. * @param {*=} response An argument for the resolved promise.
  562. *
  563. * @returns {promise} A promise that is resolved when the dialog has been closed.
  564. */
  565. /**
  566. * @ngdoc method
  567. * @name $mdDialog#cancel
  568. *
  569. * @description
  570. * Hide an existing dialog and reject the promise returned from `$mdDialog.show()`.
  571. *
  572. * @param {*=} response An argument for the rejected promise.
  573. *
  574. * @returns {promise} A promise that is resolved when the dialog has been closed.
  575. */
  576. function MdDialogProvider($$interimElementProvider) {
  577. // Elements to capture and redirect focus when the user presses tab at the dialog boundary.
  578. MdDialogController['$inject'] = ["$mdDialog", "$mdConstant"];
  579. dialogDefaultOptions['$inject'] = ["$mdDialog", "$mdAria", "$mdUtil", "$mdConstant", "$animate", "$document", "$window", "$rootElement", "$log", "$injector", "$mdTheming", "$interpolate", "$mdInteraction"];
  580. var topFocusTrap, bottomFocusTrap;
  581. return $$interimElementProvider('$mdDialog')
  582. .setDefaults({
  583. methods: ['disableParentScroll', 'hasBackdrop', 'clickOutsideToClose', 'escapeToClose',
  584. 'targetEvent', 'closeTo', 'openFrom', 'parent', 'fullscreen', 'multiple'],
  585. options: dialogDefaultOptions
  586. })
  587. .addPreset('alert', {
  588. methods: ['title', 'htmlContent', 'textContent', 'content', 'ariaLabel', 'ok', 'theme',
  589. 'css'],
  590. options: advancedDialogOptions
  591. })
  592. .addPreset('confirm', {
  593. methods: ['title', 'htmlContent', 'textContent', 'content', 'ariaLabel', 'ok', 'cancel',
  594. 'theme', 'css'],
  595. options: advancedDialogOptions
  596. })
  597. .addPreset('prompt', {
  598. methods: ['title', 'htmlContent', 'textContent', 'initialValue', 'content', 'placeholder', 'ariaLabel',
  599. 'ok', 'cancel', 'theme', 'css', 'required'],
  600. options: advancedDialogOptions
  601. });
  602. /* ngInject */
  603. function advancedDialogOptions() {
  604. return {
  605. template: [
  606. '<md-dialog md-theme="{{ dialog.theme || dialog.defaultTheme }}" aria-label="{{ dialog.ariaLabel }}" ng-class="dialog.css">',
  607. ' <md-dialog-content class="md-dialog-content" role="document" tabIndex="-1">',
  608. ' <h2 class="md-title">{{ dialog.title }}</h2>',
  609. ' <div ng-if="::dialog.mdHtmlContent" class="md-dialog-content-body" ',
  610. ' ng-bind-html="::dialog.mdHtmlContent"></div>',
  611. ' <div ng-if="::!dialog.mdHtmlContent" class="md-dialog-content-body">',
  612. ' <p>{{::dialog.mdTextContent}}</p>',
  613. ' </div>',
  614. ' <md-input-container md-no-float ng-if="::dialog.$type == \'prompt\'" class="md-prompt-input-container">',
  615. ' <input ng-keypress="dialog.keypress($event)" md-autofocus ng-model="dialog.result" ' +
  616. ' placeholder="{{::dialog.placeholder}}" ng-required="dialog.required">',
  617. ' </md-input-container>',
  618. ' </md-dialog-content>',
  619. ' <md-dialog-actions>',
  620. ' <md-button ng-if="dialog.$type === \'confirm\' || dialog.$type === \'prompt\'"' +
  621. ' ng-click="dialog.abort()" class="md-primary md-cancel-button">',
  622. ' {{ dialog.cancel }}',
  623. ' </md-button>',
  624. ' <md-button ng-click="dialog.hide()" class="md-primary md-confirm-button" md-autofocus="dialog.$type===\'alert\'"' +
  625. ' ng-disabled="dialog.required && !dialog.result">',
  626. ' {{ dialog.ok }}',
  627. ' </md-button>',
  628. ' </md-dialog-actions>',
  629. '</md-dialog>'
  630. ].join('').replace(/\s\s+/g, ''),
  631. controller: MdDialogController,
  632. controllerAs: 'dialog',
  633. bindToController: true,
  634. };
  635. }
  636. /**
  637. * Controller for the md-dialog interim elements
  638. * ngInject
  639. */
  640. function MdDialogController($mdDialog, $mdConstant) {
  641. // For compatibility with AngularJS 1.6+, we should always use the $onInit hook in
  642. // interimElements. The $mdCompiler simulates the $onInit hook for all versions.
  643. this.$onInit = function() {
  644. var isPrompt = this.$type == 'prompt';
  645. if (isPrompt && this.initialValue) {
  646. this.result = this.initialValue;
  647. }
  648. this.hide = function() {
  649. $mdDialog.hide(isPrompt ? this.result : true);
  650. };
  651. this.abort = function() {
  652. $mdDialog.cancel();
  653. };
  654. this.keypress = function($event) {
  655. var invalidPrompt = isPrompt && this.required && !angular.isDefined(this.result);
  656. if ($event.keyCode === $mdConstant.KEY_CODE.ENTER && !invalidPrompt) {
  657. $mdDialog.hide(this.result);
  658. }
  659. };
  660. };
  661. }
  662. /* ngInject */
  663. function dialogDefaultOptions($mdDialog, $mdAria, $mdUtil, $mdConstant, $animate, $document, $window, $rootElement,
  664. $log, $injector, $mdTheming, $interpolate, $mdInteraction) {
  665. return {
  666. hasBackdrop: true,
  667. isolateScope: true,
  668. onCompiling: beforeCompile,
  669. onShow: onShow,
  670. onShowing: beforeShow,
  671. onRemove: onRemove,
  672. clickOutsideToClose: false,
  673. escapeToClose: true,
  674. targetEvent: null,
  675. closeTo: null,
  676. openFrom: null,
  677. focusOnOpen: true,
  678. disableParentScroll: true,
  679. autoWrap: true,
  680. fullscreen: false,
  681. transformTemplate: function(template, options) {
  682. // Make the dialog container focusable, because otherwise the focus will be always redirected to
  683. // an element outside of the container, and the focus trap won't work probably..
  684. // Also the tabindex is needed for the `escapeToClose` functionality, because
  685. // the keyDown event can't be triggered when the focus is outside of the container.
  686. var startSymbol = $interpolate.startSymbol();
  687. var endSymbol = $interpolate.endSymbol();
  688. var theme = startSymbol + (options.themeWatch ? '' : '::') + 'theme' + endSymbol;
  689. var themeAttr = (options.hasTheme) ? 'md-theme="'+theme+'"': '';
  690. return '<div class="md-dialog-container" tabindex="-1" ' + themeAttr + '>' + validatedTemplate(template) + '</div>';
  691. /**
  692. * The specified template should contain a <md-dialog> wrapper element....
  693. */
  694. function validatedTemplate(template) {
  695. if (options.autoWrap && !/<\/md-dialog>/g.test(template)) {
  696. return '<md-dialog>' + (template || '') + '</md-dialog>';
  697. } else {
  698. return template || '';
  699. }
  700. }
  701. }
  702. };
  703. function beforeCompile(options) {
  704. // Automatically apply the theme, if the user didn't specify a theme explicitly.
  705. // Those option changes need to be done, before the compilation has started, because otherwise
  706. // the option changes will be not available in the $mdCompilers locales.
  707. options.defaultTheme = $mdTheming.defaultTheme();
  708. detectTheming(options);
  709. }
  710. function beforeShow(scope, element, options, controller) {
  711. if (controller) {
  712. var mdHtmlContent = controller.htmlContent || options.htmlContent || '';
  713. var mdTextContent = controller.textContent || options.textContent ||
  714. controller.content || options.content || '';
  715. if (mdHtmlContent && !$injector.has('$sanitize')) {
  716. throw Error('The ngSanitize module must be loaded in order to use htmlContent.');
  717. }
  718. if (mdHtmlContent && mdTextContent) {
  719. throw Error('md-dialog cannot have both `htmlContent` and `textContent`');
  720. }
  721. // Only assign the content if nothing throws, otherwise it'll still be compiled.
  722. controller.mdHtmlContent = mdHtmlContent;
  723. controller.mdTextContent = mdTextContent;
  724. }
  725. }
  726. /** Show method for dialogs */
  727. function onShow(scope, element, options, controller) {
  728. angular.element($document[0].body).addClass('md-dialog-is-showing');
  729. var dialogElement = element.find('md-dialog');
  730. // Once a dialog has `ng-cloak` applied on his template the dialog animation will not work properly.
  731. // This is a very common problem, so we have to notify the developer about this.
  732. if (dialogElement.hasClass('ng-cloak')) {
  733. var message = '$mdDialog: using `<md-dialog ng-cloak>` will affect the dialog opening animations.';
  734. $log.warn( message, element[0] );
  735. }
  736. captureParentAndFromToElements(options);
  737. configureAria(dialogElement, options);
  738. showBackdrop(scope, element, options);
  739. activateListeners(element, options);
  740. return dialogPopIn(element, options)
  741. .then(function() {
  742. lockScreenReader(element, options);
  743. warnDeprecatedActions();
  744. focusOnOpen();
  745. });
  746. /**
  747. * Check to see if they used the deprecated .md-actions class and log a warning
  748. */
  749. function warnDeprecatedActions() {
  750. if (element[0].querySelector('.md-actions')) {
  751. $log.warn('Using a class of md-actions is deprecated, please use <md-dialog-actions>.');
  752. }
  753. }
  754. /**
  755. * For alerts, focus on content... otherwise focus on
  756. * the close button (or equivalent)
  757. */
  758. function focusOnOpen() {
  759. if (options.focusOnOpen) {
  760. var target = $mdUtil.findFocusTarget(element) || findCloseButton() || dialogElement;
  761. target.focus();
  762. }
  763. /**
  764. * If no element with class dialog-close, try to find the last
  765. * button child in md-actions and assume it is a close button.
  766. *
  767. * If we find no actions at all, log a warning to the console.
  768. */
  769. function findCloseButton() {
  770. return element[0].querySelector('.dialog-close, md-dialog-actions button:last-child');
  771. }
  772. }
  773. }
  774. /**
  775. * Remove function for all dialogs
  776. */
  777. function onRemove(scope, element, options) {
  778. options.deactivateListeners();
  779. options.unlockScreenReader();
  780. options.hideBackdrop(options.$destroy);
  781. // Remove the focus traps that we added earlier for keeping focus within the dialog.
  782. if (topFocusTrap && topFocusTrap.parentNode) {
  783. topFocusTrap.parentNode.removeChild(topFocusTrap);
  784. }
  785. if (bottomFocusTrap && bottomFocusTrap.parentNode) {
  786. bottomFocusTrap.parentNode.removeChild(bottomFocusTrap);
  787. }
  788. // For navigation $destroy events, do a quick, non-animated removal,
  789. // but for normal closes (from clicks, etc) animate the removal
  790. return options.$destroy ? detachAndClean() : animateRemoval().then( detachAndClean );
  791. /**
  792. * For normal closes, animate the removal.
  793. * For forced closes (like $destroy events), skip the animations
  794. */
  795. function animateRemoval() {
  796. return dialogPopOut(element, options);
  797. }
  798. /**
  799. * Detach the element
  800. */
  801. function detachAndClean() {
  802. angular.element($document[0].body).removeClass('md-dialog-is-showing');
  803. // Reverse the container stretch if using a content element.
  804. if (options.contentElement) {
  805. options.reverseContainerStretch();
  806. }
  807. // Exposed cleanup function from the $mdCompiler.
  808. options.cleanupElement();
  809. // Restores the focus to the origin element if the last interaction upon opening was a keyboard.
  810. if (!options.$destroy && options.originInteraction === 'keyboard') {
  811. options.origin.focus();
  812. }
  813. }
  814. }
  815. function detectTheming(options) {
  816. // Once the user specifies a targetEvent, we will automatically try to find the correct
  817. // nested theme.
  818. var targetEl;
  819. if (options.targetEvent && options.targetEvent.target) {
  820. targetEl = angular.element(options.targetEvent.target);
  821. }
  822. var themeCtrl = targetEl && targetEl.controller('mdTheme');
  823. options.hasTheme = (!!themeCtrl);
  824. if (!options.hasTheme) {
  825. return;
  826. }
  827. options.themeWatch = themeCtrl.$shouldWatch;
  828. var theme = options.theme || themeCtrl.$mdTheme;
  829. if (theme) {
  830. options.scope.theme = theme;
  831. }
  832. var unwatch = themeCtrl.registerChanges(function (newTheme) {
  833. options.scope.theme = newTheme;
  834. if (!options.themeWatch) {
  835. unwatch();
  836. }
  837. });
  838. }
  839. /**
  840. * Capture originator/trigger/from/to element information (if available)
  841. * and the parent container for the dialog; defaults to the $rootElement
  842. * unless overridden in the options.parent
  843. */
  844. function captureParentAndFromToElements(options) {
  845. options.origin = angular.extend({
  846. element: null,
  847. bounds: null,
  848. focus: angular.noop
  849. }, options.origin || {});
  850. options.parent = getDomElement(options.parent, $rootElement);
  851. options.closeTo = getBoundingClientRect(getDomElement(options.closeTo));
  852. options.openFrom = getBoundingClientRect(getDomElement(options.openFrom));
  853. if ( options.targetEvent ) {
  854. options.origin = getBoundingClientRect(options.targetEvent.target, options.origin);
  855. options.originInteraction = $mdInteraction.getLastInteractionType();
  856. }
  857. /**
  858. * Identify the bounding RECT for the target element
  859. *
  860. */
  861. function getBoundingClientRect (element, orig) {
  862. var source = angular.element((element || {}));
  863. if (source && source.length) {
  864. // Compute and save the target element's bounding rect, so that if the
  865. // element is hidden when the dialog closes, we can shrink the dialog
  866. // back to the same position it expanded from.
  867. //
  868. // Checking if the source is a rect object or a DOM element
  869. var bounds = {top:0,left:0,height:0,width:0};
  870. var hasFn = angular.isFunction(source[0].getBoundingClientRect);
  871. return angular.extend(orig || {}, {
  872. element : hasFn ? source : undefined,
  873. bounds : hasFn ? source[0].getBoundingClientRect() : angular.extend({}, bounds, source[0]),
  874. focus : angular.bind(source, source.focus),
  875. });
  876. }
  877. }
  878. /**
  879. * If the specifier is a simple string selector, then query for
  880. * the DOM element.
  881. */
  882. function getDomElement(element, defaultElement) {
  883. if (angular.isString(element)) {
  884. element = $document[0].querySelector(element);
  885. }
  886. // If we have a reference to a raw dom element, always wrap it in jqLite
  887. return angular.element(element || defaultElement);
  888. }
  889. }
  890. /**
  891. * Listen for escape keys and outside clicks to auto close
  892. */
  893. function activateListeners(element, options) {
  894. var window = angular.element($window);
  895. var onWindowResize = $mdUtil.debounce(function() {
  896. stretchDialogContainerToViewport(element, options);
  897. }, 60);
  898. var removeListeners = [];
  899. var smartClose = function() {
  900. // Only 'confirm' dialogs have a cancel button... escape/clickOutside will
  901. // cancel or fallback to hide.
  902. var closeFn = ( options.$type == 'alert' ) ? $mdDialog.hide : $mdDialog.cancel;
  903. $mdUtil.nextTick(closeFn, true);
  904. };
  905. if (options.escapeToClose) {
  906. var parentTarget = options.parent;
  907. var keyHandlerFn = function(ev) {
  908. if (ev.keyCode === $mdConstant.KEY_CODE.ESCAPE) {
  909. ev.stopPropagation();
  910. ev.preventDefault();
  911. smartClose();
  912. }
  913. };
  914. // Add keydown listeners
  915. element.on('keydown', keyHandlerFn);
  916. parentTarget.on('keydown', keyHandlerFn);
  917. // Queue remove listeners function
  918. removeListeners.push(function() {
  919. element.off('keydown', keyHandlerFn);
  920. parentTarget.off('keydown', keyHandlerFn);
  921. });
  922. }
  923. // Register listener to update dialog on window resize
  924. window.on('resize', onWindowResize);
  925. removeListeners.push(function() {
  926. window.off('resize', onWindowResize);
  927. });
  928. if (options.clickOutsideToClose) {
  929. var target = element;
  930. var sourceElem;
  931. // Keep track of the element on which the mouse originally went down
  932. // so that we can only close the backdrop when the 'click' started on it.
  933. // A simple 'click' handler does not work,
  934. // it sets the target object as the element the mouse went down on.
  935. var mousedownHandler = function(ev) {
  936. sourceElem = ev.target;
  937. };
  938. // We check if our original element and the target is the backdrop
  939. // because if the original was the backdrop and the target was inside the dialog
  940. // we don't want to dialog to close.
  941. var mouseupHandler = function(ev) {
  942. if (sourceElem === target[0] && ev.target === target[0]) {
  943. ev.stopPropagation();
  944. ev.preventDefault();
  945. smartClose();
  946. }
  947. };
  948. // Add listeners
  949. target.on('mousedown', mousedownHandler);
  950. target.on('mouseup', mouseupHandler);
  951. // Queue remove listeners function
  952. removeListeners.push(function() {
  953. target.off('mousedown', mousedownHandler);
  954. target.off('mouseup', mouseupHandler);
  955. });
  956. }
  957. // Attach specific `remove` listener handler
  958. options.deactivateListeners = function() {
  959. removeListeners.forEach(function(removeFn) {
  960. removeFn();
  961. });
  962. options.deactivateListeners = null;
  963. };
  964. }
  965. /**
  966. * Show modal backdrop element...
  967. */
  968. function showBackdrop(scope, element, options) {
  969. if (options.disableParentScroll) {
  970. // !! DO this before creating the backdrop; since disableScrollAround()
  971. // configures the scroll offset; which is used by mdBackDrop postLink()
  972. options.restoreScroll = $mdUtil.disableScrollAround(element, options.parent);
  973. }
  974. if (options.hasBackdrop) {
  975. options.backdrop = $mdUtil.createBackdrop(scope, "md-dialog-backdrop md-opaque");
  976. $animate.enter(options.backdrop, options.parent);
  977. }
  978. /**
  979. * Hide modal backdrop element...
  980. */
  981. options.hideBackdrop = function hideBackdrop($destroy) {
  982. if (options.backdrop) {
  983. if ( $destroy ) options.backdrop.remove();
  984. else $animate.leave(options.backdrop);
  985. }
  986. if (options.disableParentScroll) {
  987. options.restoreScroll && options.restoreScroll();
  988. delete options.restoreScroll;
  989. }
  990. options.hideBackdrop = null;
  991. };
  992. }
  993. /**
  994. * Inject ARIA-specific attributes appropriate for Dialogs
  995. */
  996. function configureAria(element, options) {
  997. var role = (options.$type === 'alert') ? 'alertdialog' : 'dialog';
  998. var dialogContent = element.find('md-dialog-content');
  999. var existingDialogId = element.attr('id');
  1000. var dialogContentId = 'dialogContent_' + (existingDialogId || $mdUtil.nextUid());
  1001. element.attr({
  1002. 'role': role,
  1003. 'tabIndex': '-1'
  1004. });
  1005. if (dialogContent.length === 0) {
  1006. dialogContent = element;
  1007. // If the dialog element already had an ID, don't clobber it.
  1008. if (existingDialogId) {
  1009. dialogContentId = existingDialogId;
  1010. }
  1011. }
  1012. dialogContent.attr('id', dialogContentId);
  1013. element.attr('aria-describedby', dialogContentId);
  1014. if (options.ariaLabel) {
  1015. $mdAria.expect(element, 'aria-label', options.ariaLabel);
  1016. }
  1017. else {
  1018. $mdAria.expectAsync(element, 'aria-label', function() {
  1019. // If dialog title is specified, set aria-label with it
  1020. // See https://github.com/angular/material/issues/10582
  1021. if (options.title) {
  1022. return options.title;
  1023. } else {
  1024. var words = dialogContent.text().split(/\s+/);
  1025. if (words.length > 3) words = words.slice(0, 3).concat('...');
  1026. return words.join(' ');
  1027. }
  1028. });
  1029. }
  1030. // Set up elements before and after the dialog content to capture focus and
  1031. // redirect back into the dialog.
  1032. topFocusTrap = document.createElement('div');
  1033. topFocusTrap.classList.add('md-dialog-focus-trap');
  1034. topFocusTrap.tabIndex = 0;
  1035. bottomFocusTrap = topFocusTrap.cloneNode(false);
  1036. // When focus is about to move out of the dialog, we want to intercept it and redirect it
  1037. // back to the dialog element.
  1038. var focusHandler = function() {
  1039. element.focus();
  1040. };
  1041. topFocusTrap.addEventListener('focus', focusHandler);
  1042. bottomFocusTrap.addEventListener('focus', focusHandler);
  1043. // The top focus trap inserted immeidately before the md-dialog element (as a sibling).
  1044. // The bottom focus trap is inserted at the very end of the md-dialog element (as a child).
  1045. element[0].parentNode.insertBefore(topFocusTrap, element[0]);
  1046. element.after(bottomFocusTrap);
  1047. }
  1048. /**
  1049. * Prevents screen reader interaction behind modal window
  1050. * on swipe interfaces
  1051. */
  1052. function lockScreenReader(element, options) {
  1053. var isHidden = true;
  1054. // get raw DOM node
  1055. walkDOM(element[0]);
  1056. options.unlockScreenReader = function () {
  1057. isHidden = false;
  1058. walkDOM(element[0]);
  1059. options.unlockScreenReader = null;
  1060. };
  1061. /**
  1062. * Get all of an element's parent elements up the DOM tree
  1063. * @return {Array} The parent elements
  1064. */
  1065. function getParents(element) {
  1066. var parents = [];
  1067. while (element.parentNode) {
  1068. if (element === document.body) {
  1069. return parents;
  1070. }
  1071. var children = element.parentNode.children;
  1072. for (var i = 0; i < children.length; i++) {
  1073. // skip over child if it is an ascendant of the dialog
  1074. // a script or style tag, or a live region.
  1075. if (element !== children[i] &&
  1076. !isNodeOneOf(children[i], ['SCRIPT', 'STYLE']) &&
  1077. !children[i].hasAttribute('aria-live')) {
  1078. parents.push(children[i]);
  1079. }
  1080. }
  1081. element = element.parentNode;
  1082. }
  1083. return parents;
  1084. }
  1085. /**
  1086. * Walk DOM to apply or remove aria-hidden on sibling nodes
  1087. * and parent sibling nodes
  1088. */
  1089. function walkDOM(element) {
  1090. var elements = getParents(element);
  1091. for (var i = 0; i < elements.length; i++) {
  1092. elements[i].setAttribute('aria-hidden', isHidden);
  1093. }
  1094. }
  1095. }
  1096. /**
  1097. * Ensure the dialog container fill-stretches to the viewport
  1098. */
  1099. function stretchDialogContainerToViewport(container, options) {
  1100. var isFixed = $window.getComputedStyle($document[0].body).position == 'fixed';
  1101. var backdrop = options.backdrop ? $window.getComputedStyle(options.backdrop[0]) : null;
  1102. var height = backdrop ? Math.min($document[0].body.clientHeight, Math.ceil(Math.abs(parseInt(backdrop.height, 10)))) : 0;
  1103. var previousStyles = {
  1104. top: container.css('top'),
  1105. height: container.css('height')
  1106. };
  1107. // If the body is fixed, determine the distance to the viewport in relative from the parent.
  1108. var parentTop = Math.abs(options.parent[0].getBoundingClientRect().top);
  1109. container.css({
  1110. top: (isFixed ? parentTop : 0) + 'px',
  1111. height: height ? height + 'px' : '100%'
  1112. });
  1113. return function() {
  1114. // Reverts the modified styles back to the previous values.
  1115. // This is needed for contentElements, which should have the same styles after close
  1116. // as before.
  1117. container.css(previousStyles);
  1118. };
  1119. }
  1120. /**
  1121. * Dialog open and pop-in animation
  1122. */
  1123. function dialogPopIn(container, options) {
  1124. // Add the `md-dialog-container` to the DOM
  1125. options.parent.append(container);
  1126. options.reverseContainerStretch = stretchDialogContainerToViewport(container, options);
  1127. var dialogEl = container.find('md-dialog');
  1128. var animator = $mdUtil.dom.animator;
  1129. var buildTranslateToOrigin = animator.calculateZoomToOrigin;
  1130. var translateOptions = {transitionInClass: 'md-transition-in', transitionOutClass: 'md-transition-out'};
  1131. var from = animator.toTransformCss(buildTranslateToOrigin(dialogEl, options.openFrom || options.origin));
  1132. var to = animator.toTransformCss(""); // defaults to center display (or parent or $rootElement)
  1133. dialogEl.toggleClass('md-dialog-fullscreen', !!options.fullscreen);
  1134. return animator
  1135. .translate3d(dialogEl, from, to, translateOptions)
  1136. .then(function(animateReversal) {
  1137. // Build a reversal translate function synced to this translation...
  1138. options.reverseAnimate = function() {
  1139. delete options.reverseAnimate;
  1140. if (options.closeTo) {
  1141. // Using the opposite classes to create a close animation to the closeTo element
  1142. translateOptions = {transitionInClass: 'md-transition-out', transitionOutClass: 'md-transition-in'};
  1143. from = to;
  1144. to = animator.toTransformCss(buildTranslateToOrigin(dialogEl, options.closeTo));
  1145. return animator
  1146. .translate3d(dialogEl, from, to,translateOptions);
  1147. }
  1148. return animateReversal(
  1149. to = animator.toTransformCss(
  1150. // in case the origin element has moved or is hidden,
  1151. // let's recalculate the translateCSS
  1152. buildTranslateToOrigin(dialogEl, options.origin)
  1153. )
  1154. );
  1155. };
  1156. // Function to revert the generated animation styles on the dialog element.
  1157. // Useful when using a contentElement instead of a template.
  1158. options.clearAnimate = function() {
  1159. delete options.clearAnimate;
  1160. // Remove the transition classes, added from $animateCSS, since those can't be removed
  1161. // by reversely running the animator.
  1162. dialogEl.removeClass([
  1163. translateOptions.transitionOutClass,
  1164. translateOptions.transitionInClass
  1165. ].join(' '));
  1166. // Run the animation reversely to remove the previous added animation styles.
  1167. return animator.translate3d(dialogEl, to, animator.toTransformCss(''), {});
  1168. };
  1169. return true;
  1170. });
  1171. }
  1172. /**
  1173. * Dialog close and pop-out animation
  1174. */
  1175. function dialogPopOut(container, options) {
  1176. return options.reverseAnimate().then(function() {
  1177. if (options.contentElement) {
  1178. // When we use a contentElement, we want the element to be the same as before.
  1179. // That means, that we have to clear all the animation properties, like transform.
  1180. options.clearAnimate();
  1181. }
  1182. });
  1183. }
  1184. /**
  1185. * Utility function to filter out raw DOM nodes
  1186. */
  1187. function isNodeOneOf(elem, nodeTypeArray) {
  1188. if (nodeTypeArray.indexOf(elem.nodeName) !== -1) {
  1189. return true;
  1190. }
  1191. }
  1192. }
  1193. }
  1194. })(window, window.angular);