-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.js
294 lines (226 loc) · 8.48 KB
/
runtime.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
;(function (root, factory) {
if (typeof exports === 'object') {
// CommonJS
factory(require, exports, module);
} else if (typeof define === 'function' && define.amd) {
// AMD
define(['require', 'exports', 'module', './utils', 'cla55'], factory);
} else {
console && console.error('Unsupported module environment.'); // jshint ignore:line
}
}(this, function (require, exports, module) {
'use strict';
var _ = require('./utils'),
Cla55 = require('cla55'),
VanDyke,
// Define default objects (don't create always new objects)
emptyObject = {},
emptyTemplate = function () {
return null;
};
// Create the vandyke class
VanDyke = Cla55.extend({
constructor: function constructor(template) {
if (this instanceof VanDyke) {
// Initialize instance of VanDyke
// Set the template
this.template = template || this.template || emptyTemplate;
} else {
// Static initialize of VanDyke
// Initial define of React
this.prototype.React = template;
}
return this;
},
helpers: {
'if': function (args, body, alternate) {
if (args.length && args[0]) {
return body && body();
} else {
return alternate && alternate();
}
},
'unless': function (args, body, alternate) {
if (args.length && !args[0]) {
return body && body();
} else {
return alternate && alternate();
}
},
'each': function (args, body) {
var list = args[0],
l = list && list.length,
i = 0,
result;
if (_.isArray(list)) {
result = [];
for (; i < l; i++) {
result[i] = body(list[i], {
index: i,
position: i + 1,
first: i === 0,
last: i + 1 === l
});
}
}
return result;
},
'with': function (args, body) {
if (_.isObject(args[0])) {
return body(args[0]);
}
}
},
components: {},
// Template related methods
data: function data(ctx, path, fromScope) {
var last = ctx.scope.length - 1,
store = fromScope ? ctx.scope[last] : ctx.store[last],
key,
i,
l;
try {
for (i = 0, l = path.length; i < l; i++) {
key = path[i];
if (key === '..') {
store = fromScope ? ctx.scope[--last] : ctx.store[--last];
} else {
store = store[key];
}
}
} catch (error) {
store = void 0;
}
return store;
},
listener: function listener (ctx, name) {
return ctx.listeners[name];
},
helper: function helper(ctx, name, args, body, alternate) {
var helperArgs = [],
helperMethod = ctx.helpers[name] || this.helpers[name];
// Iterate over array / use object context
if (!helperMethod && !args.length) {
args = [this.data(ctx, [name])];
name = _.isArray(args[0]) ? 'each' : 'with';
helperMethod = ctx.helpers[name] || this.helpers[name];
}
if (!helperMethod) {
return null;
}
helperArgs.push(args);
if (body) {
helperArgs.push(function (data, scope) {
var result;
// Create new data scope
if (data) {
ctx.scope.push(scope || emptyObject);
ctx.store.push(data);
}
// Render helper body
result = body();
// Restore previous data scope
if (data) {
ctx.scope.pop();
ctx.store.pop();
}
return result;
});
if (alternate) {
helperArgs.push(function (data, scope) {
var result;
// Create new data scope
if (data) {
ctx.scope.push(scope || emptyObject);
ctx.store.push(data);
}
// Render alternate helper body
result = alternate();
// Restore previous data scope
if (data) {
ctx.scope.pop();
ctx.store.pop();
}
return result;
});
}
}
return helperMethod.apply(this, helperArgs) || null;
},
component: function element(ctx, name) {
var args = _.slice(arguments, 1);
// Get non-native component
if (!this.React.DOM[name]) {
args[0] = ctx.components[name] || this.components[name];
}
return this.React.createElement.apply(this.React, args);
},
concat: function concat(/*ctx*/) {
var list = _.slice(arguments, 1);
return list
.map(function (item) {
var type = typeof item,
skip = (item === null || item === undefined || type === 'boolean'),
str = !skip && item.toString && item.toString();
if (skip || (str && str.match(/^\[object .*?\]$/))) {
return '';
}
return str;
})
.join('');
},
list: function list(/*ctx*/) {
return _.slice(arguments, 1);
},
proxy: function proxy(method) {
var args = [this[method], this].concat(_.slice(arguments, 1));
return args[0] && _.bind.apply(_, args);
},
// General public methods
render: function render(data, options) {
if (!options) {
options = emptyObject;
}
// Create render context
var ctx = {
scope: [{}],
store: [data],
helpers: _.result(options, 'listeners', emptyObject),
components: _.result(options, 'components', emptyObject),
listeners: _.result(options, 'listeners', emptyObject)
};
// Render virtual dom
return this.template(this, ctx);
},
registerHelper: function registerHelper(name, helper) {
this.helpers[name] = helper;
return this;
},
registerComponent: function registerComponent(name, component) {
this.helpers[name] = component;
return this;
}
}, {
registerHelper: function registerHelper(name, helper) {
this.prototype.registerHelper(name, helper);
return this;
},
registerComponent: function registerComponent(name, component) {
this.prototype.registerComponent(name, component);
return this;
},
template: function template(templateMethod) {
var runtime = new this(templateMethod);
return runtime.render.bind(runtime);
},
extend: function extend() {
// For overwriting extend the static extend of the base class in required (not the extend shortcut)
var Child = Cla55.Cla55.extend.apply(this, arguments);
// Prevent changes on child class affects parent class
Child.prototype.helpers = _.create(Child.prototype.helpers);
Child.prototype.components = _.create(Child.prototype.components);
return Child;
}
});
module.exports = VanDyke;
}));