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