-
Notifications
You must be signed in to change notification settings - Fork 1
/
modest-preview.js
346 lines (292 loc) · 9.62 KB
/
modest-preview.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
this.modest = this.modest || {
modules : {}, // compiled modules as strings
//#REMOVE-POST-COMPILE
$uncompiled : {}, // uncompiled modules as jquery objects
compiled : {}, // set {X:X} -- names of modules that have been compiled
saveAsJs : {}, // set {X:X} -- names of modules needed for js
dependencies : {},
setWindow : function(w){
this.window = w;
this.document = w.document;
}.bind(this),
setBasePath : function(basePath){
this.modest.basePath = basePath;
}.bind(this),
loadModules : function(){
var modest = this.modest;
var includes = this.document.getElementsByTagName('include');
var path, pathAttr, moduleName, moduleContent, i, extension, lastDot;
for (i = 0; i < includes.length; ++i) {
// Assume the include tag has a single text node with the name of the module
moduleName = includes[i].childNodes[0].nodeValue;
// Remove leading and trailing whitespace from the module name
moduleName = moduleName.replace(/^\s+/, '').replace(/\s+$/, '');
// Find and separate the extension. If no extension is given, assume '.xml'
lastDot = moduleName.lastIndexOf('.');
if(lastDot == -1){
extension = '.xml';
} else {
extension = moduleName.substring(lastDot);
moduleName = moduleName.substring(0, lastDot);
}
if (!modest.compiled[moduleName]) {
path = '';
pathAttr = includes[i].getAttribute('path');
if (pathAttr) {
// Remove trailing slashes and whitespace from the "path" attribute; Add one slash
path = pathAttr.replace(/[\/\\\s]+$/, '') + '/';
}
path += moduleName + extension;
moduleContent = modest.localFile(path);
modest.$uncompiled[moduleName] = $(moduleContent);
}
if (includes[i].hasAttribute('client')) {
modest.saveAsJs[moduleName] = moduleName;
}
includes[i].parentNode.removeChild(includes[i]);
}
}.bind(this),
compileModule : function(module){
var modest = this.modest;
var $module = modest.$uncompiled[module];
modest.compileNode($module, modest.dependencies[module]);
if ($module[0].outerHTML) {
modest.modules[module] = $module[0].outerHTML;
}
else {
if (this.XMLSerializer) {
modest.modules[module] = new XMLSerializer().serializeToString($module[0]);
}
}
modest.$uncompiled[module] = false;
modest.compiled[module] = module;
}.bind(this),
loopError : function(){
var modest = this.modest;
var badModules = '';
var module;
for (module in modest.$uncompiled) {
if (!modest.compiled[module]) {
badModules += module + ' ';
}
}
throw ('Infinite loop detected in modules: ' + badModules);
}.bind(this),
compileModules : function(){
var modest = this.modest;
var compiledCount = 0;
var numModules = 0;
var waitToCompile, module, otherModule, lastCompiledCount, d;
for (module in modest.$uncompiled) {
++numModules;
if (!modest.compiled[module]) {
modest.dependencies[module] = [];
for (otherModule in modest.$uncompiled) {
if (modest.$uncompiled[module].find(otherModule).length) {
modest.dependencies[module].push(otherModule);
}
}
}
}
while (compiledCount < numModules) {
lastCompiledCount = compiledCount;
for (module in modest.$uncompiled) {
if (modest.compiled[module]) {
++compiledCount;
continue;
}
waitToCompile = false;
for (d in modest.dependencies[module]) {
if (!modest.compiled[modest.dependencies[module][d]]) {
waitToCompile = true;
break;
}
}
if (!waitToCompile) {
modest.compileModule(module);
++compiledCount;
}
}
if (lastCompiledCount === compiledCount) {
modest.loopError();
}
}
}.bind(this),
compileNode : function($node, modules){
var modest = this.modest;
if (!modules) {
modules = modest.compiled;
}
// find and compile module views within the node
$.each(modules, function(i, module){
$node.find(module).each(function(){
modest.compileView($(this), module);
});
});
}.bind(this),
//#!REMOVE-POST-COMPILE
getAttributes : function(el){
var attrs = {};
$.each(el.attributes, function(i, attr){
attrs[attr.name] = attr.value;
});
return attrs;
},
localFile : function(path){
var modest = this.modest;
if (modest.basePath) {
return fs.readFileSync(modest.basePath + path, 'utf8');
}
else {
return modest.remoteFile(path);
}
}.bind(this),
remoteFile : function(path){
$.support.cors = true;
return $.ajax({
url : path,
async : false
}).responseText;
},
data : function(path){
// Supported data formats: JSON
return $.parseJSON(this.modest.localFile(path));
}.bind(this),
remoteData : function(path){
// Supported data formats: JSON
return $.parseJSON(this.modest.remoteFile(path));
}.bind(this),
compileView : function($view, module, parameters){
var modest = this.modest;
var params = {};
var paramAttrs = {};
var viewAttrs, $targets;
// Get parameters in the following order:
// (in case of duplicates, later overwrites earlier)
// 1. remote data
// 2. local data
// 3. js parameters
// 4. html parameters
if ($view.attr('remotedata')) {
$.each($view.attr('remotedata').toLowerCase().split(' '), function(){
$.extend(params, modest.remoteData(this + ''));
});
$view.removeAttr('remotedata');
}
if ($view.attr('data')) {
$.each($view.attr('data').toLowerCase().split(' '), function(){
$.extend(params, modest.data(this + ''));
});
$view.removeAttr('data');
}
$.extend(params, parameters);
// html parameters
$view.children().each(function(){
var param = this;
var tag = param.tagName.toLowerCase();
paramAttrs[tag] = modest.getAttributes(param);
if (param.innerText && !param.children.length) {
params[tag] = param.innerText;
}
else {
params[tag] = param.innerHTML;
}
});
// save the views attributes
viewAttrs = modest.getAttributes($view[0]);
// replace the view with the module
$view.html(modest.modules[module]);
$view = $view.children(':first').unwrap();
$view.attr(viewAttrs);
$view.addClass(module);
// find targets for the parameters
$targets = $view.find('[uses]').not('[uses=""]');
// handle parameter targets in the root element
if ($view.attr('uses')) {
$targets = $view.add($targets);
}
// inject the parameters
$targets.each(function(){
var $target = $(this);
var eq, param, u, attr, strTemplates;
var usesString = $target.attr('uses');
// string template substitution
usesString = usesString.replace(/{{([^{}]+)}}/g, function(match, param){
strTemplates = true;
return params[param.toLowerCase()] || '';
}
);
// temporarily replace spaces in string template parameters so they don't get split
if(strTemplates){
usesString = usesString.replace(/\s(?=(?:[^{]|{(?=[^{]))*}})/g, 'mSoPdAeCsEt');
}
var uses = usesString.split(' ');
for (u = 0; u < uses.length; ++u) {
switch (uses[u][0]) {
case '+':
if (params[uses[u].slice(1).toLowerCase()] === undefined) {
$target.remove();
break;
}
break;
case '-':
if (params[uses[u].slice(1).toLowerCase()] !== undefined) {
$target.remove();
break;
}
break;
case '{':
$target.html(modest.stripTemplate(uses[u]));
break;
default:
eq = uses[u].indexOf('=');
if (eq !== -1) {
attr = uses[u].slice(0, eq);
param = uses[u].slice(eq + 1);
var pVal = params[param.toLowerCase()];
if (pVal) {
$target.attr(attr, pVal);
}
else if(param[0] == '{'){
$target.attr(attr, modest.stripTemplate(param));
}
}
else {
param = uses[u].toLowerCase();
if (params[param] === undefined) {
$target.remove();
break;
}
else {
$target.attr(paramAttrs[param] || {});
$target.html(params[param]);
$target.addClass(param);
}
}
}
}
});
return $view;
}.bind(this),
stripTemplate : function(s){
// remove {{}}
s = s.slice(2, s.length - 2);
// put back the spaces we replaced earlier
return s.replace('mSoPdAeCsEt', ' ');
},
render : function(module, params){
var $view = $('<div>');
$view = this.modest.compileView($view, module, params);
$view.find('[uses]').not('[uses=""]').removeAttr('uses');
return $view[0].outerHTML;
}.bind(this)
};
//#REMOVE-POST-COMPILE
if (this.window) {
$(function(){
modest.loadModules();
modest.compileModules();
modest.compileNode($(document.body));
});
}
//#!REMOVE-POST-COMPILE