-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathiron-resizable-behavior.js
276 lines (241 loc) · 8.33 KB
/
iron-resizable-behavior.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
/**
@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 {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js';
import {useShadow} from '@polymer/polymer/lib/utils/settings.js';
// Contains all connected resizables that do not have a parent.
var ORPHANS = new Set();
/**
* `IronResizableBehavior` is a behavior that can be used in Polymer elements to
* coordinate the flow of resize events between "resizers" (elements that
*control the size or hidden state of their children) and "resizables" (elements
*that need to be notified when they are resized or un-hidden by their parents
*in order to take action on their new measurements).
*
* Elements that perform measurement should add the `IronResizableBehavior`
*behavior to their element definition and listen for the `iron-resize` event on
*themselves. This event will be fired when they become showing after having
*been hidden, when they are resized explicitly by another resizable, or when
*the window has been resized.
*
* Note, the `iron-resize` event is non-bubbling.
*
* @polymerBehavior
* @demo demo/index.html
**/
export const IronResizableBehavior = {
properties: {
/**
* The closest ancestor element that implements `IronResizableBehavior`.
*/
_parentResizable: {
type: Object,
observer: '_parentResizableChanged',
},
/**
* True if this element is currently notifying its descendant elements of
* resize.
*/
_notifyingDescendant: {
type: Boolean,
value: false,
}
},
listeners: {
'iron-request-resize-notifications': '_onIronRequestResizeNotifications'
},
/** @override */
created: function() {
// We don't really need property effects on these, and also we want them
// to be created before the `_parentResizable` observer fires:
this._interestedResizables = [];
this._boundNotifyResize = this.notifyResize.bind(this);
this._boundOnDescendantIronResize = this._onDescendantIronResize.bind(this);
},
/** @override */
attached: function() {
this._requestResizeNotifications();
},
/** @override */
detached: function() {
if (this._parentResizable) {
this._parentResizable.stopResizeNotificationsFor(this);
} else {
ORPHANS.delete(this);
window.removeEventListener('resize', this._boundNotifyResize);
}
this._parentResizable = null;
},
/**
* Can be called to manually notify a resizable and its descendant
* resizables of a resize change.
*/
notifyResize: function() {
if (!this.isAttached) {
return;
}
this._interestedResizables.forEach(function(resizable) {
if (this.resizerShouldNotify(resizable)) {
this._notifyDescendant(resizable);
}
}, this);
this._fireResize();
},
/**
* Used to assign the closest resizable ancestor to this resizable
* if the ancestor detects a request for notifications.
*/
assignParentResizable: function(parentResizable) {
if (this._parentResizable) {
this._parentResizable.stopResizeNotificationsFor(this);
}
this._parentResizable = parentResizable;
if (parentResizable &&
parentResizable._interestedResizables.indexOf(this) === -1) {
parentResizable._interestedResizables.push(this);
parentResizable._subscribeIronResize(this);
}
},
/**
* Used to remove a resizable descendant from the list of descendants
* that should be notified of a resize change.
*/
stopResizeNotificationsFor: function(target) {
var index = this._interestedResizables.indexOf(target);
if (index > -1) {
this._interestedResizables.splice(index, 1);
this._unsubscribeIronResize(target);
}
},
/**
* Subscribe this element to listen to iron-resize events on the given target.
*
* Preferred over target.listen because the property renamer does not
* understand to rename when the target is not specifically "this"
*
* @param {!HTMLElement} target Element to listen to for iron-resize events.
*/
_subscribeIronResize: function(target) {
target.addEventListener('iron-resize', this._boundOnDescendantIronResize);
},
/**
* Unsubscribe this element from listening to to iron-resize events on the
* given target.
*
* Preferred over target.unlisten because the property renamer does not
* understand to rename when the target is not specifically "this"
*
* @param {!HTMLElement} target Element to listen to for iron-resize events.
*/
_unsubscribeIronResize: function(target) {
target.removeEventListener(
'iron-resize', this._boundOnDescendantIronResize);
},
/**
* This method can be overridden to filter nested elements that should or
* should not be notified by the current element. Return true if an element
* should be notified, or false if it should not be notified.
*
* @param {HTMLElement} element A candidate descendant element that
* implements `IronResizableBehavior`.
* @return {boolean} True if the `element` should be notified of resize.
*/
resizerShouldNotify: function(element) {
return true;
},
_onDescendantIronResize: function(event) {
if (this._notifyingDescendant) {
event.stopPropagation();
return;
}
// no need to use this during shadow dom because of event retargeting
if (!useShadow) {
this._fireResize();
}
},
_fireResize: function() {
this.fire('iron-resize', null, {node: this, bubbles: false});
},
_onIronRequestResizeNotifications: function(event) {
var target = /** @type {!EventTarget} */ (dom(event).rootTarget);
if (target === this) {
return;
}
target.assignParentResizable(this);
this._notifyDescendant(target);
event.stopPropagation();
},
_parentResizableChanged: function(parentResizable) {
if (parentResizable) {
window.removeEventListener('resize', this._boundNotifyResize);
}
},
_notifyDescendant: function(descendant) {
// NOTE(cdata): In IE10, attached is fired on children first, so it's
// important not to notify them if the parent is not attached yet (or
// else they will get redundantly notified when the parent attaches).
if (!this.isAttached) {
return;
}
this._notifyingDescendant = true;
descendant.notifyResize();
this._notifyingDescendant = false;
},
_requestResizeNotifications: function() {
if (!this.isAttached) {
return;
}
if (document.readyState === 'loading') {
var _requestResizeNotifications =
this._requestResizeNotifications.bind(this);
document.addEventListener(
'readystatechange', function readystatechanged() {
document.removeEventListener('readystatechange', readystatechanged);
_requestResizeNotifications();
});
} else {
this._findParent();
if (!this._parentResizable) {
// If this resizable is an orphan, tell other orphans to try to find
// their parent again, in case it's this resizable.
ORPHANS.forEach(function(orphan) {
if (orphan !== this) {
orphan._findParent();
}
}, this);
window.addEventListener('resize', this._boundNotifyResize);
this.notifyResize();
} else {
// If this resizable has a parent, tell other child resizables of
// that parent to try finding their parent again, in case it's this
// resizable.
this._parentResizable._interestedResizables
.forEach(function(resizable) {
if (resizable !== this) {
resizable._findParent();
}
}, this);
}
}
},
_findParent: function() {
this.assignParentResizable(null);
this.fire(
'iron-request-resize-notifications',
null,
{node: this, bubbles: true, cancelable: true});
if (!this._parentResizable) {
ORPHANS.add(this);
} else {
ORPHANS.delete(this);
}
}
};