/
base.html
181 lines (158 loc) · 5.98 KB
/
base.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
<!--
@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
-->
<script>
Polymer.Base = {
// Used for `isInstance` type checking; cannot use `instanceof` because
// there is no common Polymer.Base in the prototype chain between type
// extensions and normal custom elements
__isPolymerInstance__: true,
// pluggable features
// `this` context is a prototype, not an instance
_addFeature: function(feature) {
this.extend(this, feature);
},
// `this` context is a prototype, not an instance
registerCallback: function() {
// TODO(sjmiles): perhaps this method should be called from polymer-bootstrap?
this._desugarBehaviors(); // abstract
this._doBehavior('beforeRegister'); // abstract
this._registerFeatures(); // abstract
if (this.forceRegister) {
this.ensureRegistered();
}
},
createdCallback: function() {
this._ensureRegistered(this.__proto__);
Polymer.telemetry.instanceCount++;
this.root = this;
this._doBehavior('created'); // abstract
this._initFeatures(); // abstract
},
/**
* When called from the element's prototype, ensures that the element has
* fully registered. By default registration tasks are defered until the
* first instance of an element is created.
*/
ensureRegistered: function() {
this._ensureRegistered(this);
},
_ensureRegistered: function(proto) {
if (proto.__hasRegistered !== proto.is) {
proto.__hasRegistered = proto.is;
if (proto._registerLazyFeatures) {
proto._registerLazyFeatures();
}
// registration extension point
proto._doBehavior('registered');
}
},
// reserved for canonical behavior
attachedCallback: function() {
// NOTE: workaround for:
// https://code.google.com/p/chromium/issues/detail?id=516550
// To allow querying style/layout data in attached, we defer it
// until we are sure rendering is ready.
var self = this;
Polymer.RenderStatus.whenReady(function() {
self.isAttached = true;
self._doBehavior('attached'); // abstract
});
},
// reserved for canonical behavior
detachedCallback: function() {
this.isAttached = false;
this._doBehavior('detached'); // abstract
},
// reserved for canonical behavior
attributeChangedCallback: function(name, oldValue, newValue) {
// TODO(sorvell): consider filtering out changes to host attributes
// note: this was barely measurable with 3 host attributes.
this._attributeChangedImpl(name); // abstract
this._doBehavior('attributeChanged', [name, oldValue, newValue]); // abstract
},
_attributeChangedImpl: function(name) {
this._setAttributeToProperty(this, name);
},
/**
* Copies own properties (including accessor descriptors) from a source
* object to a target object.
*
* @method extend
* @param {Object} prototype Target object to copy properties to.
* @param {Object} api Source object to copy properties from.
* @return {Object} prototype object that was passed as first argument.
*/
extend: function(prototype, api) {
if (prototype && api) {
var n$ = Object.getOwnPropertyNames(api);
for (var i=0, n; (i<n$.length) && (n=n$[i]); i++) {
this.copyOwnProperty(n, api, prototype);
}
}
return prototype || api;
},
/**
* Copies props from a source object to a target object.
*
* Note, this method uses a simple `for...in` strategy for enumerating
* properties. To ensure only `ownProperties` are copied from source
* to target and that accessor implementations are copied, use `extend`.
*
* @method mixin
* @param {Object} target Target object to copy properties to.
* @param {Object} source Source object to copy properties from.
* @return {Object} Target object that was passed as first argument.
*/
mixin: function(target, source) {
for (var i in source) {
target[i] = source[i];
}
return target;
},
copyOwnProperty: function(name, source, target) {
var pd = Object.getOwnPropertyDescriptor(source, name);
if (pd) {
Object.defineProperty(target, name, pd);
}
},
_log: console.log.apply.bind(console.log, console),
_warn: console.warn.apply.bind(console.warn, console),
_error: console.error.apply.bind(console.error, console),
_logf: function(/* args*/) {
return this._logPrefix.concat([this.is]).concat(Array.prototype.slice.call(arguments, 0));
}
};
Polymer.Base._logPrefix = (function(){
var color = window.chrome || (/firefox/i.test(navigator.userAgent));
return color ? ['%c[%s::%s]:', 'font-weight: bold; background-color:#EEEE00;'] : ['[%s::%s]:'];
})();
Polymer.Base.chainObject = function(object, inherited) {
if (object && inherited && object !== inherited) {
if (!Object.__proto__) {
object = Polymer.Base.extend(Object.create(inherited), object);
}
object.__proto__ = inherited;
}
return object;
};
Polymer.Base = Polymer.Base.chainObject(Polymer.Base, HTMLElement.prototype);
if (window.CustomElements) {
Polymer.instanceof = CustomElements.instanceof;
} else {
Polymer.instanceof = function(obj, ctor) {
return obj instanceof ctor;
};
}
Polymer.isInstance = function(obj) {
return Boolean(obj && obj.__isPolymerInstance__);
};
// TODO(sjmiles): ad hoc telemetry
Polymer.telemetry.instanceCount = 0;
</script>