-
Notifications
You must be signed in to change notification settings - Fork 2k
/
utils.html
421 lines (390 loc) · 13.8 KB
/
utils.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
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
<!--
@license
Copyright (c) 2014 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="../lib/async.html">
<link rel="import" href="../lib/debounce.html">
<script>
Polymer.Base._addFeature({
/**
* Convenience method to run `querySelector` on this local DOM scope.
*
* This function calls `Polymer.dom(this.root).querySelector(slctr)`.
*
* @method $$
* @param {string} slctr Selector to run on this local DOM scope
* @return {Element} Element found by the selector, or null if not found.
*/
$$: function(slctr) {
return Polymer.dom(this.root).querySelector(slctr);
},
/**
* Toggles a CSS class on or off.
*
* @method toggleClass
* @param {String} name CSS class name
* @param {boolean=} bool Boolean to force the class on or off.
* When unspecified, the state of the class will be reversed.
* @param {HTMLElement=} node Node to target. Defaults to `this`.
*/
toggleClass: function(name, bool, node) {
node = node || this;
if (arguments.length == 1) {
bool = !node.classList.contains(name);
}
if (bool) {
Polymer.dom(node).classList.add(name);
} else {
Polymer.dom(node).classList.remove(name);
}
},
/**
* Toggles an HTML attribute on or off.
*
* @method toggleAttribute
* @param {String} name HTML attribute name
* @param {boolean=} bool Boolean to force the attribute on or off.
* When unspecified, the state of the attribute will be reversed.
* @param {HTMLElement=} node Node to target. Defaults to `this`.
*/
toggleAttribute: function(name, bool, node) {
node = node || this;
if (arguments.length == 1) {
bool = !node.hasAttribute(name);
}
if (bool) {
Polymer.dom(node).setAttribute(name, '');
} else {
Polymer.dom(node).removeAttribute(name);
}
},
/**
* Removes a class from one node, and adds it to another.
*
* @method classFollows
* @param {String} name CSS class name
* @param {HTMLElement} toElement New element to add the class to.
* @param {HTMLElement} fromElement Old element to remove the class from.
*/
classFollows: function(name, toElement, fromElement) {
if (fromElement) {
Polymer.dom(fromElement).classList.remove(name);
}
if (toElement) {
Polymer.dom(toElement).classList.add(name);
}
},
/**
* Removes an HTML attribute from one node, and adds it to another.
*
* @method attributeFollows
* @param {String} name HTML attribute name
* @param {HTMLElement} toElement New element to add the attribute to.
* @param {HTMLElement} fromElement Old element to remove the attribute from.
*/
attributeFollows: function(name, toElement, fromElement) {
if (fromElement) {
Polymer.dom(fromElement).removeAttribute(name);
}
if (toElement) {
Polymer.dom(toElement).setAttribute(name, '');
}
},
/**
* Returns a list of nodes that are the effective childNodes. The effective
* childNodes list is the same as the element's childNodes except that
* any `<content>` elements are replaced with the list of nodes distributed
* to the `<content>`, the result of its `getDistributedNodes` method.
*
* @method getEffectiveChildNodes
* @return {Array<Node>} List of effctive child nodes.
*/
getEffectiveChildNodes: function() {
return Polymer.dom(this).getEffectiveChildNodes();
},
/**
* Returns a list of elements that are the effective children. The effective
* children list is the same as the element's children except that
* any `<content>` elements are replaced with the list of elements
* distributed to the `<content>`.
*
* @method getEffectiveChildren
* @return {Array<Node>} List of effctive children.
*/
getEffectiveChildren: function() {
var list = Polymer.dom(this).getEffectiveChildNodes();
return list.filter(function(n) {
return (n.nodeType === Node.ELEMENT_NODE);
});
},
/**
* Returns a string of text content that is the concatenation of the
* text content's of the element's effective childNodes (the elements
* returned by <a href="#getEffectiveChildNodes>getEffectiveChildNodes</a>.
*
* @method getEffectiveTextContent
* @return {Array<Node>} List of effctive children.
*/
getEffectiveTextContent: function() {
var cn = this.getEffectiveChildNodes();
var tc = [];
for (var i=0, c; c = cn[i]; i++) {
if (c.nodeType !== Node.COMMENT_NODE) {
tc.push(Polymer.dom(c).textContent);
}
}
return tc.join('');
},
queryEffectiveChildren: function(slctr) {
var e$ = Polymer.dom(this).queryDistributedElements(slctr);
return e$ && e$[0];
},
queryAllEffectiveChildren: function(slctr) {
return Polymer.dom(this).queryAllDistributedElements(slctr);
},
/**
* Returns a list of nodes distributed to this element's `<content>`.
*
* If this element contans more than one `<content>` in its local DOM,
* an optional selector may be passed to choose the desired content.
*
* @method getContentChildNodes
* @param {String=} slctr CSS selector to choose the desired
* `<content>`. Defaults to `content`.
* @return {Array<Node>} List of distributed nodes for the `<content>`.
*/
getContentChildNodes: function(slctr) {
var content = Polymer.dom(this.root).querySelector(slctr || 'content');
return content ? Polymer.dom(content).getDistributedNodes() : [];
},
/**
* Returns a list of element children distributed to this element's
* `<content>`.
*
* If this element contans more than one `<content>` in its
* local DOM, an optional selector may be passed to choose the desired
* content. This method differs from `getContentChildNodes` in that only
* elements are returned.
*
* @method getContentChildNodes
* @param {String=} slctr CSS selector to choose the desired
* `<content>`. Defaults to `content`.
* @return {Array<HTMLElement>} List of distributed nodes for the
* `<content>`.
*/
getContentChildren: function(slctr) {
return this.getContentChildNodes(slctr).filter(function(n) {
return (n.nodeType === Node.ELEMENT_NODE);
});
},
/**
* Dispatches a custom event with an optional detail value.
*
* @method fire
* @param {String} type Name of event type.
* @param {*=} detail Detail value containing event-specific
* payload.
* @param {Object=} options Object specifying options. These may include:
* `bubbles` (boolean, defaults to `true`),
* `cancelable` (boolean, defaults to false), and
* `node` on which to fire the event (HTMLElement, defaults to `this`).
* @return {CustomEvent} The new event that was fired.
*/
fire: function(type, detail, options) {
options = options || Polymer.nob;
var node = options.node || this;
var detail = (detail === null || detail === undefined) ? {} : detail;
var bubbles = options.bubbles === undefined ? true : options.bubbles;
var cancelable = Boolean(options.cancelable);
var useCache = options._useCache;
var event = this._getEvent(type, bubbles, cancelable, useCache);
event.detail = detail;
if (useCache) {
this.__eventCache[type] = null;
}
node.dispatchEvent(event);
if (useCache) {
this.__eventCache[type] = event;
}
return event;
},
__eventCache: {},
_getEvent: function(type, bubbles, cancelable, useCache) {
var event = useCache && this.__eventCache[type];
if (!event || ((event.bubbles != bubbles) ||
(event.cancelable != cancelable))) {
event = new Event(type, {
bubbles: Boolean(bubbles),
cancelable: cancelable
});
}
return event;
},
/**
* Runs a callback function asyncronously.
*
* By default (if no waitTime is specified), async callbacks are run at
* microtask timing, which will occur before paint.
*
* @method async
* @param {Function} callback The callback function to run, bound to `this`.
* @param {number=} waitTime Time to wait before calling the
* `callback`. If unspecified or 0, the callback will be run at microtask
* timing (before paint).
* @return {number} Handle that may be used to cancel the async job.
*/
async: function(callback, waitTime) {
var self = this;
return Polymer.Async.run(function() {
callback.call(self);
}, waitTime);
},
/**
* Cancels an async operation started with `async`.
*
* @method cancelAsync
* @param {number} handle Handle returned from original `async` call to
* cancel.
*/
cancelAsync: function(handle) {
Polymer.Async.cancel(handle);
},
/**
* Removes an item from an array, if it exists.
*
* If the array is specified by path, a change notification is
* generated, so that observers, data bindings and computed
* properties watching that path can update.
*
* If the array is passed directly, **no change
* notification is generated**.
*
* @method arrayDelete
* @param {String|Array} path Path to array from which to remove the item
* (or the array itself).
* @param {any} item Item to remove.
* @return {Array} Array containing item removed.
*/
arrayDelete: function(path, item) {
var index;
if (Array.isArray(path)) {
index = path.indexOf(item);
if (index >= 0) {
return path.splice(index, 1);
}
} else {
var arr = this._get(path);
index = arr.indexOf(item);
if (index >= 0) {
return this.splice(path, index, 1);
}
}
},
/**
* Cross-platform helper for setting an element's CSS `transform` property.
*
* @method transform
* @param {String} transform Transform setting.
* @param {HTMLElement=} node Element to apply the transform to.
* Defaults to `this`
*/
transform: function(transform, node) {
node = node || this;
node.style.webkitTransform = transform;
node.style.transform = transform;
},
/**
* Cross-platform helper for setting an element's CSS `translate3d`
* property.
*
* @method translate3d
* @param {number} x X offset.
* @param {number} y Y offset.
* @param {number} z Z offset.
* @param {HTMLElement=} node Element to apply the transform to.
* Defaults to `this`.
*/
translate3d: function(x, y, z, node) {
node = node || this;
this.transform('translate3d(' + x + ',' + y + ',' + z + ')', node);
},
/**
* Convenience method for importing an HTML document imperatively.
*
* This method creates a new `<link rel="import">` element with
* the provided URL and appends it to the document to start loading.
* In the `onload` callback, the `import` property of the `link`
* element will contain the imported document contents.
*
* @method importHref
* @param {string} href URL to document to load.
* @param {Function} onload Callback to notify when an import successfully
* loaded.
* @param {Function} onerror Callback to notify when an import
* unsuccessfully loaded.
* @return {HTMLLinkElement} The link element for the URL to be loaded.
*/
importHref: function(href, onload, onerror) {
var l = document.createElement('link');
l.rel = 'import';
l.href = href;
var self = this;
if (onload) {
l.onload = function(e) {
return onload.call(self, e);
}
}
if (onerror) {
l.onerror = function(e) {
return onerror.call(self, e);
}
}
document.head.appendChild(l);
return l;
},
/**
* Convenience method for creating an element and configuring it.
*
* @method create
* @param {string} tag HTML element tag to create.
* @param {Object} props Object of properties to configure on the
* instance.
* @return {Element} Newly created and configured element.
*/
create: function(tag, props) {
var elt = document.createElement(tag);
if (props) {
for (var n in props) {
elt[n] = props[n];
}
}
return elt;
},
/**
* Checks whether an element is in this element's light DOM tree.
*
* @method isLightDescendant
* @param {HTMLElement=} node The element to be checked.
* @return {Boolean} true if node is in this element's light DOM tree.
*/
isLightDescendant: function(node) {
return this !== node && this.contains(node) &&
Polymer.dom(this).getOwnerRoot() === Polymer.dom(node).getOwnerRoot();
},
/**
* Checks whether an element is in this element's local DOM tree.
*
* @method isLocalDescendant
* @param {HTMLElement=} node The element to be checked.
* @return {Boolean} true if node is in this element's local DOM tree.
*/
isLocalDescendant: function(node) {
return this.root === Polymer.dom(node).getOwnerRoot();
}
});
</script>