forked from nippur72/RiotTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
riot-ts.globals.js
123 lines (120 loc) · 4.74 KB
/
riot-ts.globals.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
(function (exports,riot) {
'use strict';
var Observable = (function () {
function Observable() {
riot.observable(this);
}
Observable.prototype.on = function (events, callback) { };
Observable.prototype.one = function (events, callback) { };
Observable.prototype.off = function (events) { };
Observable.prototype.trigger = function (eventName) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
};
return Observable;
}());
var Element = (function () {
function Element() {
}
Element.prototype.update = function (data) { };
Element.prototype.unmount = function (keepTheParent) { };
Element.prototype.on = function (eventName, fun) { };
Element.prototype.one = function (eventName, fun) { };
Element.prototype.off = function (events) { };
Element.prototype.trigger = function (eventName) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
};
Element.prototype.mixin = function (mixinObject, instance) { };
Element.createElement = function (options) {
var tagName = this.prototype.tagName;
var el = document.createElement(tagName);
riot.mount(el, tagName, options);
return el;
};
return Element;
}());
// new extend, works with getters and setters
function extend(d, element) {
var map = Object.keys(element.prototype).reduce(function (descriptors, key) {
descriptors[key] = Object.getOwnPropertyDescriptor(element.prototype, key);
return descriptors;
}, {});
Object.defineProperties(d, map);
}
/* old extend, without getters and setters
function extend(d, element) {
Object.keys(element.prototype).forEach((key) => d[key] = element.prototype[key]);
}
*/
var precompiledTags = {};
function registerClass(element) {
function registerTag(compiledTag) {
var transformFunction = function (opts) {
extend(this, element); // copies prototype into "this"
element.apply(this, [opts]); // calls class constructor applying it on "this"
if (element.prototype.mounted !== undefined)
this.on("mount", this.mounted);
if (element.prototype.unmounted !== undefined)
this.on("unmount", this.unmounted);
if (element.prototype.updating !== undefined)
this.on("update", this.updating);
if (element.prototype.updated !== undefined)
this.on("updated", this.updated);
// TODO support for init(opts) ?
};
riot.tag2(compiledTag.tagName, compiledTag.html, compiledTag.css, compiledTag.attribs, transformFunction, riot.settings.brackets);
return compiledTag.tagName;
}
function loadTemplateFromHTTP(template) {
var req = new XMLHttpRequest();
req.open("GET", template, false);
req.send();
if (req.status == 200)
return req.responseText;
else
throw req.responseText;
}
;
var compiled;
// gets string template: inlined, via http request or via precompiled cache
if (element.prototype.template !== undefined) {
var tagTemplate = element.prototype.template;
if (tagTemplate.indexOf("<") < 0) {
// tag is a file
if (precompiledTags[tagTemplate] !== undefined) {
// loads it from precompiled cache
compiled = precompiledTags[tagTemplate];
}
else {
// loads from HTTP and compile on the fly
tagTemplate = loadTemplateFromHTTP(tagTemplate);
compiled = riot.compile(tagTemplate, true, { entities: true })[0];
}
}
else {
// tag is inlined, compile on the fly
compiled = riot.compile(tagTemplate, true, { entities: true })[0];
}
element.prototype.tagName = registerTag(compiled);
}
else
throw "template property not specified";
}
// @template decorator
function template(template) {
return function (target) {
target.prototype["template"] = template;
registerClass(target);
};
}
exports.Observable = Observable;
exports.Element = Element;
exports.precompiledTags = precompiledTags;
exports.registerClass = registerClass;
exports.template = template;
}((this.Riot = this.Riot || {}),riot));