-
Notifications
You must be signed in to change notification settings - Fork 25
/
polymer-ts.js
354 lines (343 loc) · 13.3 KB
/
polymer-ts.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
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
// PolymerTS - Polymer for TypeScript
//
// https://github.com/nippur72/PolymerTS
//
// Antonino Porcino, [email protected]
var polymer;
(function (polymer) {
// create an ES6 inheritable Polymer.Base object, referenced as "polymer.Base"
function createEs6PolymerBase() {
// create a placeholder class
var pb = function () {
};
// make it available as polymer.Base
window["polymer"]["Base"] = pb;
// add a default create method()
pb["create"] = function () {
throw "element not yet registered in Polymer";
};
// add a default create method()
pb["register"] = function (dontRegister) {
if (dontRegister === true)
polymer.createClass(this);
else
polymer.createElement(this);
};
}
polymer.createEs6PolymerBase = createEs6PolymerBase;
function prepareForRegistration(elementClass) {
// copies members from inheritance chain to Polymer object
function copyMembers(dest, source) {
if (source === undefined || source === null)
return;
Object.keys(source).map(function (member) {
// copy only if has not been defined
if (!dest.hasOwnProperty(member))
dest[member] = source[member];
});
copyMembers(dest, source.__proto__);
}
// backward compatibility with TypeScript 1.4 (no decorators)
if (elementClass.prototype.is === undefined) {
var proto = elementClass.prototype;
var instance = new elementClass();
if (!instance.is) {
throw new Error("no name for " + elementClass);
}
proto.is = instance.is;
if (instance.extends) {
proto.extends = instance.extends;
}
if (instance.properties) {
proto.properties = instance.properties;
}
if (instance.listeners) {
proto.listeners = instance.listeners;
}
if (instance.observers) {
proto.observers = instance.observers;
}
if (instance.behaviors) {
proto.behaviors = instance.behaviors;
}
if (instance.hostAttributes) {
proto.hostAttributes = instance.hostAttributes;
}
if (instance.style) {
proto.style = instance.style;
}
if (instance.template) {
proto.template = instance.template;
}
}
var preparedElement = elementClass.prototype;
// artificial constructor: call constructor() and copies members
preparedElement["$custom_cons"] = function () {
// reads arguments coming from factoryImpl
var args = this.$custom_cons_args;
// applies class constructor on the polymer element (this)
elementClass.apply(this, args);
};
// arguments for artifical constructor
preparedElement["$custom_cons_args"] = [];
// modify "factoryImpl"
if (preparedElement["factoryImpl"] !== undefined) {
throw "do not use factoryImpl() use constructor() instead";
}
else {
preparedElement["factoryImpl"] = function () {
this.$custom_cons_args = arguments;
};
}
// modify "attached" event function
var attachToFunction = "attached";
var oldFunction = preparedElement[attachToFunction];
preparedElement[attachToFunction] = function () {
this.$custom_cons();
if (oldFunction !== undefined)
oldFunction.apply(this);
};
// copy inherited class members
copyMembers(preparedElement, elementClass.prototype.__proto__);
//putting string in prototype.style in decorator style makes it impossible to access and modify css styles of created elements
//so it needs to be deleted in order to access and modify styles
delete preparedElement["style"];
return preparedElement;
}
polymer.prepareForRegistration = prepareForRegistration;
/*
// see https://github.com/Polymer/polymer/issues/2114
export function createDomModule(definition: polymer.Element) {
var domModule: any = document.createElement('dom-module');
var proto = <any> definition.prototype;
domModule.id = proto.is;
// attaches style
if (proto.style !== undefined) {
var elemStyle = (<any> document).createElement('style', 'custom-style');
domModule.appendChild(elemStyle);
elemStyle.textContent = proto.style;
}
// attaches template
if (proto.template !== undefined) {
var elemTemplate = document.createElement('template');
domModule.appendChild(elemTemplate);
elemTemplate.innerHTML = proto.template;
}
// tells polymer the element has been created
domModule.createdCallback();
}
*/
// a version that works in IE11 too
function createDomModule(definition) {
var domModule = document.createElement('dom-module');
var proto = definition.prototype;
domModule.id = proto.is;
var html = "";
var style = "";
if (proto.style !== undefined)
style = "<style>" + proto.style + "</style>";
if (proto.template !== undefined)
html = "<template>" + style + proto.template + "</template>";
domModule.innerHTML = html;
domModule.createdCallback();
}
polymer.createDomModule = createDomModule;
/*
// temporary version until https://github.com/Polymer/polymer/issues/2114 is fixed
export function createDomModule(definition: polymer.Element) {
var contentDoc = document.implementation.createHTMLDocument('template');
var domModule: any = document.createElement('dom-module');
var proto = <any> definition.prototype;
domModule.id = proto.is;
// attaches style
if (proto.style !== undefined) {
var elemStyle = (<any> document).createElement('style', 'custom-style');
domModule.appendChild(elemStyle);
elemStyle.textContent = proto.style;
}
// attaches template
if (proto.template !== undefined) {
var elemTemplate = document.createElement('template');
domModule.appendChild(elemTemplate);
contentDoc.body.innerHTML = proto.template;
while (contentDoc.body.firstChild) {
(<any>elemTemplate).content.appendChild(contentDoc.body.firstChild);
}
}
// tells polymer the element has been created
domModule.createdCallback();
}
*/
/**
* @deprecated
*/
function createElement(element) {
if (polymer.isRegistered(element)) {
throw "element already registered in Polymer";
}
if ((element.prototype).template !== undefined || (element.prototype).style !== undefined) {
polymer.createDomModule(element);
}
// register element and make available its constructor as "create()"
var maker = Polymer(polymer.prepareForRegistration(element));
element["create"] = function () {
var newOb = Object.create(maker.prototype);
return maker.apply(newOb, arguments);
};
return maker;
}
polymer.createElement = createElement;
/**
* @deprecated
*/
function createClass(element) {
if (polymer.isRegistered(element)) {
throw "element already registered in Polymer";
}
if ((element.prototype).template !== undefined || (element.prototype).style !== undefined) {
polymer.createDomModule(element);
}
// register element and make available its constructor as "create()"
var maker = Polymer.Class(polymer.prepareForRegistration(element));
element["create"] = function () {
var newOb = Object.create(maker.prototype);
return maker.apply(newOb, arguments);
};
return maker;
}
polymer.createClass = createClass;
function isRegistered(element) {
return (element.prototype).$custom_cons !== undefined;
}
polymer.isRegistered = isRegistered;
})(polymer || (polymer = {})); // end module
// modifies Polymer.Base and makes it available as an ES6 class named polymer.Base
polymer.createEs6PolymerBase();
// @component decorator
function component(tagname, extendsTag) {
return function (target) {
target.prototype["is"] = tagname;
if (extendsTag !== undefined) {
target.prototype["extends"] = extendsTag;
}
};
}
// @extend decorator
function extend(tagname) {
return function (target) {
target.prototype["extends"] = tagname;
};
}
// @template decorator
function template(templateString) {
return function (target) {
target.prototype["template"] = templateString;
};
}
// @style decorator
function style(styleString) {
return function (target) {
target.prototype["style"] = styleString;
};
}
// @hostAttributes decorator
function hostAttributes(attributes) {
return function (target) {
target.prototype["hostAttributes"] = attributes;
};
}
// @property decorator with automatic name for computed props
function property(ob) {
return function (target, propertyKey) {
target.properties = target.properties || {};
if (typeof (target[propertyKey]) === "function") {
// property is function, treat it as a computed property
var params = ob["computed"];
var getterName = "get_computed_" + propertyKey;
ob["computed"] = getterName + "(" + params + ")";
target.properties[propertyKey] = ob;
target[getterName] = target[propertyKey];
}
else {
// normal property
var previousProperty = target.properties[propertyKey];
target.properties[propertyKey] = ob || {};
// make sure we grab the observer, just in case the observe decorator was called first
if (previousProperty && previousProperty.observer) {
target.properties[propertyKey].observer = previousProperty.observer;
}
}
};
}
// @computed decorator
function computed(ob) {
return function (target, computedFuncName) {
target.properties = target.properties || {};
var propOb = ob || {};
var getterName = "get_computed_" + computedFuncName;
var funcText = target[computedFuncName].toString();
var start = funcText.indexOf("(");
var end = funcText.indexOf(")");
var propertiesList = funcText.substring(start + 1, end);
propOb["computed"] = getterName + "(" + propertiesList + ")";
target.properties[computedFuncName] = propOb;
target[getterName] = target[computedFuncName];
};
}
// @listen decorator
function listen(eventName) {
return function (target, propertyKey) {
target.listeners = target.listeners || {};
target.listeners[eventName] = propertyKey;
};
}
// @behavior decorator
function behavior(behaviorObject) {
if (!behaviorObject) {
throw new Error('@behavior not found; value is ' + behaviorObject);
}
else {
return function (target) {
if (typeof (target) === "function") {
// decorator applied externally, target is the class object
target.prototype["behaviors"] = target.prototype["behaviors"] || [];
var beObject = behaviorObject.prototype === undefined ? behaviorObject : behaviorObject.prototype;
target.prototype["behaviors"].push(beObject);
}
else {
// decorator applied internally, target is class.prototype
target.behaviors = target.behaviors || [];
var beObject = behaviorObject.prototype === undefined ? behaviorObject : behaviorObject.prototype;
target.behaviors.push(beObject);
}
};
}
}
// @observe decorator
function observe(observedProps) {
if (!observedProps) {
throw new Error('@observe properties not found; value is ' + observedProps);
}
else {
if (observedProps.indexOf(",") > 0 || observedProps.indexOf(".") > 0) {
// observing multiple properties or path
return function (target, observerFuncName) {
target.observers = target.observers || [];
target.observers.push(observerFuncName + "(" + observedProps + ")");
};
}
else {
// observing single property
return function (target, observerName) {
target.properties = target.properties || {};
var propertyDef = target.properties[observedProps];
if (propertyDef && propertyDef.observer) {
console.warn("PolymerTS: simple observer '" + propertyDef.observer + "' already registered for property '" + observedProps + "'; overwriting with new observer '" + observerName + "'");
}
target.properties[observedProps] = target.properties[observedProps] || {};
target.properties[observedProps].observer = observerName;
};
}
}
}
//# sourceMappingURL=polymer-ts.js.map