-
Notifications
You must be signed in to change notification settings - Fork 2k
/
utils.html
336 lines (314 loc) · 11.1 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
<!--
@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 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 object.
*
* @method fire
* @param {String} type Name of event type.
* @param {*=} detail Detail object 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) ? Polymer.nob : detail;
var bubbles = options.bubbles === undefined ? true : options.bubbles;
var cancelable = Boolean(options.cancelable);
var event = new CustomEvent(type, {
bubbles: Boolean(bubbles),
cancelable: cancelable,
detail: detail
});
node.dispatchEvent(event);
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) {
return Polymer.Async.run(callback.bind(this), 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;
if (onload) {
l.onload = onload.bind(this);
}
if (onerror) {
l.onerror = onerror.bind(this);
}
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.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>