This repository was archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathpaper-dialog-behavior.js
More file actions
140 lines (116 loc) · 4.7 KB
/
Copy pathpaper-dialog-behavior.js
File metadata and controls
140 lines (116 loc) · 4.7 KB
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
/**
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at
http://polymer.github.io/LICENSE.txt The complete set of authors may be found at
http://polymer.github.io/AUTHORS.txt The complete set of contributors may be
found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as
part of the polymer project is also subject to an additional IP rights grant
found at http://polymer.github.io/PATENTS.txt
*/
import '@polymer/polymer/polymer-legacy.js';
import {IronOverlayBehavior} from '@polymer/iron-overlay-behavior/iron-overlay-behavior.js';
import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js';
/**
Use `Polymer.PaperDialogBehavior` and `paper-dialog-shared-styles.html` to
implement a Material Design dialog.
For example, if `<paper-dialog-impl>` implements this behavior:
<paper-dialog-impl>
<h2>Header</h2>
<div>Dialog body</div>
<div class="buttons">
<paper-button dialog-dismiss>Cancel</paper-button>
<paper-button dialog-confirm>Accept</paper-button>
</div>
</paper-dialog-impl>
`paper-dialog-shared-styles.html` provide styles for a header, content area,
and an action area for buttons. Use the `<h2>` tag for the header and the
`buttons` class for the action area. You can use the `paper-dialog-scrollable`
element (in its own repository) if you need a scrolling content area.
Use the `dialog-dismiss` and `dialog-confirm` attributes on interactive
controls to close the dialog. If the user dismisses the dialog with
`dialog-confirm`, the `closingReason` will update to include `confirmed:
true`.
### Accessibility
This element has `role="dialog"` by default. Depending on the context, it may
be more appropriate to override this attribute with `role="alertdialog"`.
If `modal` is set, the element will prevent the focus from exiting the
element. It will also ensure that focus remains in the dialog.
@hero hero.svg
@demo demo/index.html
@polymerBehavior PaperDialogBehavior
*/
export const PaperDialogBehaviorImpl = {
hostAttributes: {'role': 'dialog', 'tabindex': '-1'},
properties: {
/**
* If `modal` is true, this implies `no-cancel-on-outside-click`,
* `no-cancel-on-esc-key` and `with-backdrop`.
*/
modal: {type: Boolean, value: false},
__readied: {type: Boolean, value: false}
},
observers: ['_modalChanged(modal, __readied)'],
listeners: {'tap': '_onDialogClick'},
/**
* @return {void}
*/
ready: function() {
// Only now these properties can be read.
this.__prevNoCancelOnOutsideClick = this.noCancelOnOutsideClick;
this.__prevNoCancelOnEscKey = this.noCancelOnEscKey;
this.__prevWithBackdrop = this.withBackdrop;
this.__readied = true;
},
_modalChanged: function(modal, readied) {
// modal implies noCancelOnOutsideClick, noCancelOnEscKey and withBackdrop.
// We need to wait for the element to be ready before we can read the
// properties values.
if (!readied) {
return;
}
if (modal) {
this.__prevNoCancelOnOutsideClick = this.noCancelOnOutsideClick;
this.__prevNoCancelOnEscKey = this.noCancelOnEscKey;
this.__prevWithBackdrop = this.withBackdrop;
this.noCancelOnOutsideClick = true;
this.noCancelOnEscKey = true;
this.withBackdrop = true;
} else {
// If the value was changed to false, let it false.
this.noCancelOnOutsideClick =
this.noCancelOnOutsideClick && this.__prevNoCancelOnOutsideClick;
this.noCancelOnEscKey =
this.noCancelOnEscKey && this.__prevNoCancelOnEscKey;
this.withBackdrop = this.withBackdrop && this.__prevWithBackdrop;
}
},
_updateClosingReasonConfirmed: function(confirmed) {
this.closingReason = this.closingReason || {};
this.closingReason.confirmed = confirmed;
},
/**
* Will dismiss the dialog if user clicked on an element with dialog-dismiss
* or dialog-confirm attribute.
*/
_onDialogClick: function(event) {
// Search for the element with dialog-confirm or dialog-dismiss,
// from the root target until this (excluded).
var path = dom(event).path;
for (var i = 0, l = path.indexOf(this); i < l; i++) {
var target = path[i];
if (target.hasAttribute &&
(target.hasAttribute('dialog-dismiss') ||
target.hasAttribute('dialog-confirm'))) {
this._updateClosingReasonConfirmed(
target.hasAttribute('dialog-confirm'));
this.close();
event.stopPropagation();
break;
}
}
}
};
/** @polymerBehavior */
export const PaperDialogBehavior =
[IronOverlayBehavior, PaperDialogBehaviorImpl];