-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassTemplate.js
209 lines (142 loc) · 4.98 KB
/
ClassTemplate.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
window.a5s = typeof window.a5s == "undefined"? {}: window.a5s;
/**
*
* @singleton
* @class
* @requires HTTPRequest.js
* @requires CustomEvents.js
*/
var ClassTemplate = ( function( domain ) {
var uidI = 0,
templates = {},
re = /\${([^}]+)}/gi,
tempTemplate = "<article id=\"${id}\">Loading...</article>";
var loaders = {};
/**
* Get the template path for a 'type' based on an equally named
* script resource.
*
* @argument {String} type
* @return {String} url returns null of script is not found
*/
function getTemplatePath( type ) {
var scripts = document.getElementsByTagName("script"),
l = scripts.length,
i = l,
re = new RegExp("^(.*/)" + type + ".js\\b.*$", "i");
for ( i; i--; ) {
var s = scripts[i];
var url = s.src;
var m = url.match( re );
if ( m ) {
var templateUrl = m[1] + type + ".html";
return templateUrl;
}
}
return null;
};
helper = {
/**
* Attempt to load an html-template into memory based on the
* type of a 'component'. This method expects the file name
* to match the type and the template to reside in the same
* directory as well as having the same filename (with an
* html extension instead of js...)
*/
loadTemplate : function( type ) {
var tmpl = this.getTemplate( type ),
loader = loaders[ type ];
if ( tmpl === null && typeof loader === "undefined" ) {
loader = new RSVP.Promise();
var url = getTemplatePath( type );
var that = this;
var request = new HTTPRequest( false );
that.trigger( "template.queued", {type: type, url: url} );
request.doGet( url ).then( function( e ) {
//console.log( "HTTPRequest is finished :)", that, loader );
var wrapper = e.request,
txt = wrapper.httpRequest.responseText;
that.addTemplate( type, txt );
that.trigger("template.finished", {type: type, template: txt});
loader.trigger("template.finished", {type: type, template: txt});
loader.resolve( {type: type, template: txt} );
delete loaders[ type ];
} );
loaders[ type ] = loader;
}
return loader;
},
/**
* Render a template with 'values' into 'node'.
*
*/
renderTemplate : function( templateName, values, node, promise ) {
var t = ClassTemplate.getTemplate( templateName ),
loader = loaders[ templateName ],
promise = promise || new RSVP.Promise();
if ( t === null ) {
if ( !loader ) {
loader = this.loadTemplate( templateName );
}
//Setup delayed rendering...
// Setup handler to wait for template...
loader.on( "template.finished", function( e ) {
ClassTemplate.renderTemplate( templateName, values, node, promise );
loader.resolve();
} );
//Set temporary template
t = tempTemplate;
} else {
promise.resolve( { node: node } );
}
if ( node ) {
node.innerHTML = ClassTemplate.fillTemplate( t, values );
} else
throw "No target-node specified!!!";
return promise;
},
addTemplate : function( type, template ) {
templates[ type ] = template;
},
getTemplate : function( type ) {
return templates[ type ]?templates[ type ]:null;
},
/**
* Fill a template with the provided values. Values van be encoded into
* template as ${value} markers.
*
* @argument {String} template The type of the template as string
* @argument {JSON} values A JSON object of the values to be used
*/
fillTemplate : function( template, values ) {
var o = "",
m = null,
preIndex = 0;
while (m = re.exec(template)) {
var v = values[m[1]];
v = typeof v!=="undefined"?v:m[1];
o += RegExp.leftContext.substr(preIndex) + v;
preIndex = re.lastIndex;
}
if ( o.length <= 0 ) {
return template;
}
o += RegExp.rightContext;
return o;
}
};
if ( window.unittesting ) {
var cachedtemplates = null;
helper.reset = function( ) {
if ( cachedtemplates === null ) {
cachedtemplates = templates;
}
templates = {};
for ( var k in cachedtemplates ) {
templates[ k ] = cachedtemplate[ k ];
}
}
}
RSVP.EventTarget.mixin( helper );
return helper;
})( a5s );