forked from PolymerLabs/iron-overlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iron-overlay-container.html
176 lines (153 loc) · 4.98 KB
/
iron-overlay-container.html
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
<!--
@license
Copyright (c) 2016 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
-->
<link rel="import" href="../polymer/polymer-element.html">
<!--
`iron-overlay-container` hosts the overlay renderers by litening to the
`iron-overlay-attach` and `iron-overlay-detach` events.
This element should be placed in a stacking-context safe node (e.g. `document.body`).
<div style="transform: translateZ(0);">
This div creates a new stacking context
<iron-overlay opened>
<template>
<h2>Hello world!</h2>
</template>
</iron-overlay>
</div>
<iron-overlay-container></iron-overlay-container>
Use `overlayType` to limit which types of overlays this container should host:
<iron-overlay-container overlay-type="paper-toast"></iron-overlay-container>
@demo demo/index.html
-->
<script>
(() => {
'use strict';
class IronOverlayContainer extends Polymer.Element {
static get is() {
return 'iron-overlay-container';
}
static get properties() {
return {
/**
* Which overlay type should this container host.
*/
overlayType: String
};
}
constructor() {
super();
// List of overlays handled by this container.
this._overlays = [];
this._host = null;
// Bind these methods to `this` as they are used for event listeners.
this._onAttach = this._onAttach.bind(this);
this._onDetach = this._onDetach.bind(this);
}
connectedCallback() {
super.connectedCallback();
// Listeners on the host.
const host = this._getHostRoot();
host.addEventListener('iron-overlay-attach', this._onAttach);
host.addEventListener('iron-overlay-detach', this._onDetach);
this._host = host;
}
disconnectedCallback() {
super.disconnectedCallback();
const host = this._host;
host.removeEventListener('iron-overlay-attach', this._onAttach);
host.removeEventListener('iron-overlay-detach', this._onDetach);
this._host = null;
}
/**
* Returns the current overlay.
* @type {Node|undefined}
*/
get currentOverlay() {
return this._overlays[this._overlays.length - 1];
}
/**
* Returns true if the overlay can be hosted (if it matches the overlayType).
* @param {!Polymer.IronOverlay} overlay
* @returns {boolean}
*/
canAttach(overlay) {
return (!this.overlayType || overlay.localName === this.overlayType);
}
/**
* Hosts the overlay renderer if if can be hosted (see `canHost`).
* Returns true if the overlay was hosted.
* @param {!Polymer.IronOverlay} overlay
* @returns {boolean}
*/
attachOverlay(overlay) {
const canAttach = this.canAttach(overlay);
if (canAttach) {
this._overlays.push(overlay);
this.appendChild(overlay.renderer);
}
return canAttach;
}
/**
* Stops hosting the overlay renderer, moves focus to the new `currentOverlay`.
* Returns true if the overlay was removed.
* @param {Polymer.IronOverlay} overlay
* @returns {boolean}
*/
detachOverlay(overlay) {
const i = this._overlays.indexOf(overlay);
if (i >= 0) {
this.removeChild(overlay.renderer);
this._overlays.splice(i, 1);
}
return i >= 0;
}
/**
* Adds the `root` and prevents/stops the event propagation.
* @param {!Event} event
*/
_onAttach(event) {
if (event.defaultPrevented) {
return;
}
if (this.attachOverlay(event.detail.overlay)) {
event.preventDefault();
event.stopPropagation();
}
}
/**
* Removes the `root` and prevents/stops the event propagation.
* @param {!Event} event
*/
_onDetach(event) {
if (event.defaultPrevented) {
return;
}
if (this.detachOverlay(event.detail.overlay)) {
event.preventDefault();
event.stopPropagation();
}
}
/**
* Returns the root element hosting the container.
* @returns {Node}
*/
_getHostRoot() {
let n = this;
while (n) {
if (n.nodeType === Node.DOCUMENT_FRAGMENT_NODE && n.host) {
return n;
}
n = n.parentNode;
}
return document;
}
}
customElements.define(IronOverlayContainer.is, IronOverlayContainer);
})();
</script>