-
Notifications
You must be signed in to change notification settings - Fork 2k
/
element-mixin.html
856 lines (817 loc) · 33.2 KB
/
element-mixin.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
<!--
@license
Copyright (c) 2017 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="../utils/boot.html">
<link rel="import" href="../utils/settings.html">
<link rel="import" href="../utils/mixin.html">
<link rel="import" href="../utils/case-map.html">
<link rel="import" href="../utils/style-gather.html">
<link rel="import" href="../utils/resolve-url.html">
<link rel="import" href="../elements/dom-module.html">
<link rel="import" href="property-effects.html">
<script>
(function() {
'use strict';
/**
* Element class mixin that provides the core API for Polymer's meta-programming
* features including template stamping, data-binding, attribute deserialization,
* and property change observation.
*
* Subclassers may provide the following static getters to return metadata
* used to configure Polymer's features for the class:
*
* - `static get is()`: When the template is provided via a `dom-module`,
* users should return the `dom-module` id from a static `is` getter. If
* no template is needed or the template is provided directly via the
* `template` getter, there is no need to define `is` for the element.
*
* - `static get template()`: Users may provide the template directly (as
* opposed to via `dom-module`) by implementing a static `template` getter.
* The getter may return an `HTMLTemplateElement` or a string, which will
* automatically be parsed into a template.
*
* - `static get properties()`: Should return an object describing
* property-related metadata used by Polymer features (key: property name
* value: object containing property metadata). Valid keys in per-property
* metadata include:
* - `type` (String|Number|Object|Array|...): Used by
* `attributeChangedCallback` to determine how string-based attributes
* are deserialized to JavaScript property values.
* - `notify` (boolean): Causes a change in the property to fire a
* non-bubbling event called `<property>-changed`. Elements that have
* enabled two-way binding to the property use this event to observe changes.
* - `readOnly` (boolean): Creates a getter for the property, but no setter.
* To set a read-only property, use the private setter method
* `_setProperty(property, value)`.
* - `observer` (string): Observer method name that will be called when
* the property changes. The arguments of the method are
* `(value, previousValue)`.
* - `computed` (string): String describing method and dependent properties
* for computing the value of this property (e.g. `'computeFoo(bar, zot)'`).
* Computed properties are read-only by default and can only be changed
* via the return value of the computing method.
*
* - `static get observers()`: Array of strings describing multi-property
* observer methods and their dependent properties (e.g.
* `'observeABC(a, b, c)'`).
*
* The base class provides default implementations for the following standard
* custom element lifecycle callbacks; users may override these, but should
* call the super method to ensure
* - `constructor`: Run when the element is created or upgraded
* - `connectedCallback`: Run each time the element is connected to the
* document
* - `disconnectedCallback`: Run each time the element is disconnected from
* the document
* - `attributeChangedCallback`: Run each time an attribute in
* `observedAttributes` is set or removed (note: this element's default
* `observedAttributes` implementation will automatically return an array
* of dash-cased attributes based on `properties`)
*
* @mixinFunction
* @polymer
* @appliesMixin Polymer.PropertyEffects
* @memberof Polymer
* @property rootPath {string} Set to the value of `Polymer.rootPath`,
* which defaults to the main document path
* @property importPath {string} Set to the value of the class's static
* `importPath` property, which defaults to the path of this element's
* `dom-module` (when `is` is used), but can be overridden for other
* import strategies.
* @summary Element class mixin that provides the core API for Polymer's
* meta-programming features.
*/
Polymer.ElementMixin = Polymer.dedupingMixin(base => {
/**
* @constructor
* @extends {base}
* @implements {Polymer_PropertyEffects}
*/
const polymerElementBase = Polymer.PropertyEffects(base);
let caseMap = Polymer.CaseMap;
/**
* Returns the `properties` object specifically on `klass`. Use for:
* (1) super chain mixes togther to make `propertiesForClass` which is
* then used to make `observedAttributes`.
* (2) properties effects and observers are created from it at `finalize` time.
*
* @param {HTMLElement} klass Element class
* @return {Object} Object containing own properties for this class
* @private
*/
function ownPropertiesForClass(klass) {
if (!klass.hasOwnProperty(
JSCompiler_renameProperty('__ownProperties', klass))) {
klass.__ownProperties =
klass.hasOwnProperty(JSCompiler_renameProperty('properties', klass)) ?
/** @type PolymerElementConstructor */ (klass).properties : {};
}
return klass.__ownProperties;
}
/**
* Returns the `observers` array specifically on `klass`. Use for
* setting up observers.
*
* @param {HTMLElement} klass Element class
* @return {Array} Array containing own observers for this class
* @private
*/
function ownObserversForClass(klass) {
if (!klass.hasOwnProperty(
JSCompiler_renameProperty('__ownObservers', klass))) {
klass.__ownObservers =
klass.hasOwnProperty(JSCompiler_renameProperty('observers', klass)) ?
/** @type PolymerElementConstructor */ (klass).observers : [];
}
return klass.__ownObservers;
}
/**
* Mixes `props` into `flattenedProps` but upgrades shorthand type
* syntax to { type: Type}.
*
* @param {Object} flattenedProps Bag to collect flattened properties into
* @param {Object} props Bag of properties to add to `flattenedProps`
* @return {Object} The input `flattenedProps` bag
* @private
*/
function flattenProperties(flattenedProps, props) {
for (let p in props) {
let o = props[p];
if (typeof o == 'function') {
o = { type: o };
}
flattenedProps[p] = o;
}
return flattenedProps;
}
/**
* Returns a flattened list of properties mixed together from the chain of all
* constructor's `config.properties`. This list is used to create
* (1) observedAttributes,
* (2) class property default values
*
* @param {PolymerElementConstructor} klass Element class
* @return {PolymerElementProperties} Flattened properties for this class
* @suppress {missingProperties} class.prototype is not a property for some reason?
* @private
*/
function propertiesForClass(klass) {
if (!klass.hasOwnProperty(
JSCompiler_renameProperty('__classProperties', klass))) {
klass.__classProperties =
flattenProperties({}, ownPropertiesForClass(klass));
let superCtor = Object.getPrototypeOf(klass.prototype).constructor;
if (superCtor.prototype instanceof PolymerElement) {
klass.__classProperties = Object.assign(
Object.create(propertiesForClass(/** @type PolymerElementConstructor */(superCtor))),
klass.__classProperties);
}
}
return klass.__classProperties;
}
/**
* Returns a list of properties with default values.
* This list is created as an optimization since it is a subset of
* the list returned from `propertiesForClass`.
* This list is used in `_initializeProperties` to set property defaults.
*
* @param {PolymerElementConstructor} klass Element class
* @return {PolymerElementProperties} Flattened properties for this class
* that have default values
* @private
*/
function propertyDefaultsForClass(klass) {
if (!klass.hasOwnProperty(
JSCompiler_renameProperty('__classPropertyDefaults', klass))) {
klass.__classPropertyDefaults = null;
let props = propertiesForClass(klass);
for (let p in props) {
let info = props[p];
if ('value' in info) {
klass.__classPropertyDefaults = klass.__classPropertyDefaults || {};
klass.__classPropertyDefaults[p] = info;
}
}
}
return klass.__classPropertyDefaults;
}
/**
* Returns true if a `klass` has finalized. Called in `ElementClass.finalize()`
* @param {PolymerElementConstructor} klass Element class
* @return {boolean} True if all metaprogramming for this class has been
* completed
* @private
*/
function hasClassFinalized(klass) {
return klass.hasOwnProperty(JSCompiler_renameProperty('__finalized', klass));
}
/**
* Called by `ElementClass.finalize()`. Ensures this `klass` and
* *all superclasses* are finalized by traversing the prototype chain
* and calling `klass.finalize()`.
*
* @param {PolymerElementConstructor} klass Element class
* @private
*/
function finalizeClassAndSuper(klass) {
let proto = /** @type PolymerElementConstructor */ (klass).prototype;
let superCtor = Object.getPrototypeOf(proto).constructor;
if (superCtor.prototype instanceof PolymerElement) {
superCtor.finalize();
}
finalizeClass(klass);
}
/**
* Configures a `klass` based on a staic `klass.config` object and
* a `template`. This includes creating accessors and effects
* for properties in `config` and the `template` as well as preparing the
* `template` for stamping.
*
* @param {PolymerElementConstructor} klass Element class
* @private
*/
function finalizeClass(klass) {
klass.__finalized = true;
let proto = /** @type PolymerElementConstructor */ (klass).prototype;
if (klass.hasOwnProperty(
JSCompiler_renameProperty('is', klass)) && klass.is) {
Polymer.telemetry.register(proto);
}
let props = ownPropertiesForClass(klass);
if (props) {
finalizeProperties(proto, props);
}
let observers = ownObserversForClass(klass);
if (observers) {
finalizeObservers(proto, observers, props);
}
// note: create "working" template that is finalized at instance time
let template = /** @type PolymerElementConstructor */ (klass).template;
if (template) {
if (typeof template === 'string') {
let t = document.createElement('template');
t.innerHTML = template;
template = t;
} else {
template = template.cloneNode(true);
}
proto._template = template;
}
}
/**
* Configures a `proto` based on a `properties` object.
* Leverages `PropertyEffects` to create property accessors and effects
* supporting, observers, reflecting to attributes, change notification,
* computed properties, and read only properties.
* @param {PolymerElement} proto Element class prototype to add accessors
* and effects to
* @param {Object} properties Flattened bag of property descriptors for
* this class
* @private
*/
function finalizeProperties(proto, properties) {
for (let p in properties) {
createPropertyFromConfig(proto, p, properties[p], properties);
}
}
/**
* Configures a `proto` based on a `observers` array.
* Leverages `PropertyEffects` to create observers.
* @param {PolymerElement} proto Element class prototype to add accessors
* and effects to
* @param {Object} observers Flattened array of observer descriptors for
* this class
* @param {Object} dynamicFns Object containing keys for any properties
* that are functions and should trigger the effect when the function
* reference is changed
* @private
*/
function finalizeObservers(proto, observers, dynamicFns) {
for (let i=0; i < observers.length; i++) {
proto._createMethodObserver(observers[i], dynamicFns);
}
}
/**
* Creates effects for a property.
*
* Note, once a property has been set to
* `readOnly`, `computed`, `reflectToAttribute`, or `notify`
* these values may not be changed. For example, a subclass cannot
* alter these settings. However, additional `observers` may be added
* by subclasses.
*
* The info object should may contain property metadata as follows:
*
* * `type`: {function} type to which an attribute matching the property
* is deserialized. Note the property is camel-cased from a dash-cased
* attribute. For example, 'foo-bar' attribute is dersialized to a
* property named 'fooBar'.
*
* * `readOnly`: {boolean} creates a readOnly property and
* makes a private setter for the private of the form '_setFoo' for a
* property 'foo',
*
* * `computed`: {string} creates a computed property. A computed property
* also automatically is set to `readOnly: true`. The value is calculated
* by running a method and arguments parsed from the given string. For
* example 'compute(foo)' will compute a given property when the
* 'foo' property changes by executing the 'compute' method. This method
* must return the computed value.
*
* * `reflectToAttriute`: {boolean} If true, the property value is reflected
* to an attribute of the same name. Note, the attribute is dash-cased
* so a property named 'fooBar' is reflected as 'foo-bar'.
*
* * `notify`: {boolean} sends a non-bubbling notification event when
* the property changes. For example, a property named 'foo' sends an
* event named 'foo-changed' with `event.detail` set to the value of
* the property.
*
* * observer: {string} name of a method that runs when the property
* changes. The arguments of the method are (value, previousValue).
*
* Note: Users may want control over modifying property
* effects via subclassing. For example, a user might want to make a
* reflectToAttribute property not do so in a subclass. We've chosen to
* disable this because it leads to additional complication.
* For example, a readOnly effect generates a special setter. If a subclass
* disables the effect, the setter would fail unexpectedly.
* Based on feedback, we may want to try to make effects more malleable
* and/or provide an advanced api for manipulating them.
* Also consider adding warnings when an effect cannot be changed.
*
* @param {PolymerElement} proto Element class prototype to add accessors
* and effects to
* @param {string} name Name of the property.
* @param {Object} info Info object from which to create property effects.
* Supported keys:
* @param {Object} allProps Flattened map of all properties defined in this
* element (including inherited properties)
* @private
*/
function createPropertyFromConfig(proto, name, info, allProps) {
// computed forces readOnly...
if (info.computed) {
info.readOnly = true;
}
// Note, since all computed properties are readOnly, this prevents
// adding additional computed property effects (which leads to a confusing
// setup where multiple triggers for setting a property)
// While we do have `hasComputedEffect` this is set on the property's
// dependencies rather than itself.
if (info.computed && !proto._hasReadOnlyEffect(name)) {
proto._createComputedProperty(name, info.computed, allProps);
}
if (info.readOnly && !proto._hasReadOnlyEffect(name)) {
proto._createReadOnlyProperty(name, !info.computed);
}
if (info.reflectToAttribute && !proto._hasReflectEffect(name)) {
proto._createReflectedProperty(name);
}
if (info.notify && !proto._hasNotifyEffect(name)) {
proto._createNotifyingProperty(name);
}
// always add observer
if (info.observer) {
proto._createPropertyObserver(name, info.observer, allProps[info.observer]);
}
}
/**
* Configures an element `proto` to function with a given `template`.
* The element name `is` and extends `ext` must be specified for ShadyCSS
* style scoping.
*
* @param {PolymerElement} proto Element class prototype to add accessors
* and effects to
* @param {!HTMLTemplateElement} template Template to process and bind
* @param {string} baseURI URL against which to resolve urls in
* style element cssText
* @param {string} is Tag name (or type extension name) for this element
* @param {string=} ext For type extensions, the tag name that was extended
* @private
*/
function finalizeTemplate(proto, template, baseURI, is, ext) {
// support `include="module-name"`
let cssText =
Polymer.StyleGather.cssFromModuleImports(is) +
Polymer.StyleGather.cssFromTemplate(template, baseURI);
if (cssText) {
let style = document.createElement('style');
style.textContent = cssText;
template.content.insertBefore(style, template.content.firstChild);
}
if (window.ShadyCSS) {
window.ShadyCSS.prepareTemplate(template, is, ext);
}
proto._bindTemplate(template);
}
/**
* @polymer
* @mixinClass
* @unrestricted
* @implements {Polymer_ElementMixin}
*/
class PolymerElement extends polymerElementBase {
/**
* Standard Custom Elements V1 API. The default implementation returns
* a list of dash-cased attributes based on a flattening of all properties
* declared in `static get properties()` for this element and any
* superclasses.
*
* @return {Array} Observed attribute list
*/
static get observedAttributes() {
if (!this.hasOwnProperty(JSCompiler_renameProperty('__observedAttributes', this))) {
let list = [];
let properties = propertiesForClass(this);
for (let prop in properties) {
list.push(Polymer.CaseMap.camelToDashCase(prop));
}
this.__observedAttributes = list;
}
return this.__observedAttributes;
}
/**
* Called automatically when the first element instance is created to
* ensure that class finalization work has been completed.
* May be called by users to eagerly perform class finalization work
* prior to the creation of the first element instance.
*
* Class finalization work generally includes meta-programming such as
* creating property accessors and any property effect metadata needed for
* the features used.
*
* @public
*/
static finalize() {
if (!hasClassFinalized(this)) {
finalizeClassAndSuper(this);
}
}
/**
* Returns the template that will be stamped into this element's shadow root.
*
* If a `static get is()` getter is defined, the default implementation
* will return the first `<template>` in a `dom-module` whose `id`
* matches this element's `is`.
*
* Users may override this getter to return an arbitrary template
* (in which case the `is` getter is unnecessary). The template returned
* may be either an `HTMLTemplateElement` or a string that will be
* automatically parsed into a template.
*
* Note that when subclassing, if the super class overrode the default
* implementation and the subclass would like to provide an alternate
* template via a `dom-module`, it should override this getter and
* return `Polymer.DomModule.import(this.is, 'template')`.
*
* If a subclass would like to modify the super class template, it should
* clone it rather than modify it in place. If the getter does expensive
* work such as cloning/modifying a template, it should memoize the
* template for maximum performance:
*
* let memoizedTemplate;
* class MySubClass extends MySuperClass {
* static get template() {
* if (!memoizedTemplate) {
* memoizedTemplate = super.template.cloneNode(true);
* let subContent = document.createElement('div');
* subContent.textContent = 'This came from MySubClass';
* memoizedTemplate.content.appendChild(subContent);
* }
* return memoizedTemplate;
* }
* }
*
* @return {HTMLTemplateElement|string} Template to be stamped
*/
static get template() {
if (!this.hasOwnProperty(JSCompiler_renameProperty('_template', this))) {
this._template = Polymer.DomModule && Polymer.DomModule.import(
/** @type PolymerElementConstructor*/ (this).is, 'template') ||
// note: implemented so a subclass can retrieve the super
// template; call the super impl this way so that `this` points
// to the superclass.
Object.getPrototypeOf(/** @type PolymerElementConstructor*/ (this).prototype).constructor.template;
}
return this._template;
}
/**
* Path matching the url from which the element was imported.
* This path is used to resolve url's in template style cssText.
* The `importPath` property is also set on element instances and can be
* used to create bindings relative to the import path.
* Defaults to the path matching the url containing a `dom-module` element
* matching this element's static `is` property.
* Note, this path should contain a trailing `/`.
*
* @return {string} The import path for this element class
*/
static get importPath() {
if (!this.hasOwnProperty(JSCompiler_renameProperty('_importPath', this))) {
const module = Polymer.DomModule && Polymer.DomModule.import(/** @type PolymerElementConstructor */ (this).is);
this._importPath = module ? module.assetpath : '' ||
Object.getPrototypeOf(/** @type PolymerElementConstructor*/ (this).prototype).constructor.importPath;
}
return this._importPath;
}
constructor() {
super();
/** @type {HTMLTemplateElement} */
this._template;
/** @type {string} */
this._importPath;
/** @type {string} */
this.rootPath;
/** @type {string} */
this.importPath;
/** @type {StampedTemplate | HTMLElement | ShadowRoot} */
this.root;
/** @type {!Object<string, !Node>} */
this.$;
}
/**
* Overrides the default `Polymer.PropertyAccessors` to ensure class
* metaprogramming related to property accessors and effects has
* completed (calls `finalize`).
*
* It also initializes any property defaults provided via `value` in
* `properties` metadata.
*
* @override
* @suppress {invalidCasts}
*/
_initializeProperties() {
Polymer.telemetry.instanceCount++;
this.constructor.finalize();
const importPath = this.constructor.importPath;
// note: finalize template when we have access to `localName` to
// avoid dependence on `is` for polyfilling styling.
if (this._template && !this._template.__polymerFinalized) {
this._template.__polymerFinalized = true;
const baseURI =
importPath ? Polymer.ResolveUrl.resolveUrl(importPath) : '';
finalizeTemplate(/** @type {!PolymerElement} */(this.__proto__), this._template, baseURI,
/**@type {!HTMLElement}*/(this).localName);
}
super._initializeProperties();
// set path defaults
this.rootPath = Polymer.rootPath;
this.importPath = importPath;
// apply property defaults...
let p$ = propertyDefaultsForClass(this.constructor);
if (!p$) {
return;
}
for (let p in p$) {
let info = p$[p];
// Don't set default value if there is already an own property, which
// happens when a `properties` property with default but no effects had
// a property set (e.g. bound) by its host before upgrade
if (!this.hasOwnProperty(p)) {
let value = typeof info.value == 'function' ?
info.value.call(this) :
info.value;
// Set via `_setProperty` if there is an accessor, to enable
// initializing readOnly property defaults
if (this._hasAccessor(p)) {
this._setPendingProperty(p, value, true);
} else {
this[p] = value;
}
}
}
}
/**
* Provides a default implementation of the standard Custom Elements
* `connectedCallback`.
*
* The default implementation enables the property effects system and
* flushes any pending properties, and updates shimmed CSS properties
* when using the ShadyCSS scoping/custom properties polyfill.
*
* @suppress {invalidCasts}
*/
connectedCallback() {
if (window.ShadyCSS && this._template) {
window.ShadyCSS.styleElement(/** @type {!HTMLElement} */(this));
}
this._enableProperties();
}
/**
* Provides a default implementation of the standard Custom Elements
* `disconnectedCallback`.
*/
disconnectedCallback() {}
/**
* Stamps the element template.
*
* @override
*/
ready() {
if (this._template) {
this.root = this._stampTemplate(this._template);
this.$ = this.root.$;
}
super.ready();
}
/**
* Implements `PropertyEffects`'s `_readyClients` call. Attaches
* element dom by calling `_attachDom` with the dom stamped from the
* element's template via `_stampTemplate`. Note that this allows
* client dom to be attached to the element prior to any observers
* running.
*
* @override
*/
_readyClients() {
if (this._template) {
this.root = this._attachDom(/** @type {StampedTemplate} */(this.root));
}
// The super._readyClients here sets the clients initialized flag.
// We must wait to do this until after client dom is created/attached
// so that this flag can be checked to prevent notifications fired
// during this process from being handled before clients are ready.
super._readyClients();
}
/**
* Attaches an element's stamped dom to itself. By default,
* this method creates a `shadowRoot` and adds the dom to it.
* However, this method may be overridden to allow an element
* to put its dom in another location.
*
* @throws {Error}
* @suppress {missingReturn}
* @param {StampedTemplate} dom to attach to the element.
* @return {ShadowRoot} node to which the dom has been attached.
*/
_attachDom(dom) {
if (this.attachShadow) {
if (dom) {
if (!this.shadowRoot) {
this.attachShadow({mode: 'open'});
}
this.shadowRoot.appendChild(dom);
return this.shadowRoot;
}
return null;
} else {
throw new Error('ShadowDOM not available. ' +
// TODO(sorvell): move to compile-time conditional when supported
'Polymer.Element can create dom as children instead of in ' +
'ShadowDOM by setting `this.root = this;\` before \`ready\`.');
}
}
/**
* Provides a default implementation of the standard Custom Elements
* `attributeChangedCallback`.
*
* By default, attributes declared in `properties` metadata are
* deserialized using their `type` information to properties of the
* same name. "Dash-cased" attributes are deserialzed to "camelCase"
* properties.
*
* @param {string} name Name of attribute.
* @param {?string} old Old value of attribute.
* @param {?string} value Current value of attribute.
* @override
*/
attributeChangedCallback(name, old, value) {
if (old !== value) {
let property = caseMap.dashToCamelCase(name);
let type = propertiesForClass(this.constructor)[property].type;
if (!this._hasReadOnlyEffect(property)) {
this._attributeToProperty(name, value, type);
}
}
}
/**
* When using the ShadyCSS scoping and custom property shim, causes all
* shimmed styles in this element (and its subtree) to be updated
* based on current custom property values.
*
* The optional parameter overrides inline custom property styles with an
* object of properties where the keys are CSS properties, and the values
* are strings.
*
* Example: `this.updateStyles({'--color': 'blue'})`
*
* These properties are retained unless a value of `null` is set.
*
* @param {Object=} properties Bag of custom property key/values to
* apply to this element.
* @suppress {invalidCasts}
*/
updateStyles(properties) {
if (window.ShadyCSS) {
window.ShadyCSS.styleSubtree(/** @type {!HTMLElement} */(this), properties);
}
}
/**
* Rewrites a given URL relative to a base URL. The base URL defaults to
* the original location of the document containing the `dom-module` for
* this element. This method will return the same URL before and after
* bundling.
*
* @param {string} url URL to resolve.
* @param {string=} base Optional base URL to resolve against, defaults
* to the element's `importPath`
* @return {string} Rewritten URL relative to base
*/
resolveUrl(url, base) {
if (!base && this.importPath) {
base = Polymer.ResolveUrl.resolveUrl(this.importPath);
}
return Polymer.ResolveUrl.resolveUrl(url, base);
}
/**
* Overrides `PropertyAccessors` to add map of dynamic functions on
* template info, for consumption by `PropertyEffects` template binding
* code. This map determines which method templates should have accessors
* created for them.
*
* @override
* @suppress {missingProperties} Interfaces in closure do not inherit statics, but classes do
*/
static _parseTemplateContent(template, templateInfo, nodeInfo) {
templateInfo.dynamicFns = templateInfo.dynamicFns || propertiesForClass(this);
return super._parseTemplateContent(template, templateInfo, nodeInfo);
}
}
return PolymerElement;
});
/**
* Provides basic tracking of element definitions (registrations) and
* instance counts.
*
* @namespace
* @summary Provides basic tracking of element definitions (registrations) and
* instance counts.
*/
Polymer.telemetry = {
/**
* Total number of Polymer element instances created.
* @type {number}
*/
instanceCount: 0,
/**
* Array of Polymer element classes that have been finalized.
* @type {Array<Polymer.Element>}
*/
registrations: [],
/**
* @param {!PolymerElementConstructor} prototype Element prototype to log
* @this {this}
* @private
*/
_regLog: function(prototype) {
console.log('[' + prototype.is + ']: registered')
},
/**
* Registers a class prototype for telemetry purposes.
* @param {HTMLElement} prototype Element prototype to register
* @this {this}
* @protected
*/
register: function(prototype) {
this.registrations.push(prototype);
Polymer.log && this._regLog(prototype);
},
/**
* Logs all elements registered with an `is` to the console.
* @public
* @this {this}
*/
dumpRegistrations: function() {
this.registrations.forEach(this._regLog);
}
};
/**
* When using the ShadyCSS scoping and custom property shim, causes all
* shimmed `styles` (via `custom-style`) in the document (and its subtree)
* to be updated based on current custom property values.
*
* The optional parameter overrides inline custom property styles with an
* object of properties where the keys are CSS properties, and the values
* are strings.
*
* Example: `Polymer.updateStyles({'--color': 'blue'})`
*
* These properties are retained unless a value of `null` is set.
*
* @param {Object=} props Bag of custom property key/values to
* apply to the document.
*/
Polymer.updateStyles = function(props) {
if (window.ShadyCSS) {
window.ShadyCSS.styleDocument(props);
}
};
})();
</script>