-
Notifications
You must be signed in to change notification settings - Fork 0
/
bwf.full.js
367 lines (330 loc) · 13.3 KB
/
bwf.full.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
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* Class responsible to create and valuate another classes
* @param [optional] element: a string to parse to a class
* @param [optional] container: the container object to put classes in (default is window)
* TODO @param [optional] options: parsing and valuating options
*/
function Bwf(elem, container, options) {
var bwf = this;
var elem = elem;
var container = container || window;
var result = {};
/**
* A template to create classes
* @param className: the name of the class (as string)
* @param options: the properties to insert into class
*/
var classTemplate = function(className, options) {
var keys = options ?
Object.keys(options).filter(function(k) {
return k !== '__types';
}) : '';
var code = 'this.f = function ' + className + '(options) {\
var c = this;\
var k = [];\
"' + keys + '".split(/,/).forEach(function(key) {\
k.push(key);\
});\
var inList = function(val, lst) {\
return lst.indexOf(val) > -1;\
};\
var keys = Object.keys(options);\
keys.forEach(function(key) {\
if (inList(key, k)) {\
c[key] = options[key];\
}\
});\
c[\'instanceof\'] = function(klass) {\
return (klass.prototype.constructor.name === this.constructor.name);\
};\
return c;\
};';
var klass = eval(code);
// class dynamic data
keys.forEach(function(key) {
klass.prototype[key] = options[key];
});
// Class static data
klass.keys = keys;
klass.__types = options.__types;
klass.toString = function() {
return 'function ' + className + '() { [native code] }';
};
return klass;
};
/**
* Used to split a string in a given pattern
* @param str: the string
* @param pattern: the pattern to use
*/
var splitOn = function(str, pattern) {
var trimParts = function(lst, pattern) {
var parts = [];
lst.forEach(function(part) {
parts.push(part.trim());
});
return parts.filter(function(p) {
return !p.match(pattern);
});
};
var t = trimParts(str.toString().trim().split(pattern), pattern);
var result = [];
t.forEach(function(r) {
if (r !== '') {
result.push(r);
}
});
return result;
};
/**
* Setup specific types used in Bwf
*/
var setupInnerTypes = function() {
if (!container['Password']) {
container['Password'] = class Password {
constructor(obj) {
this.value = btoa(obj.value);
}
getPass() {
return atob(this.value);
}
instanceof() {
return (Password.prototype.constructor.name === this.constructor.name);
}
static toString() {
return 'function Password() { [native code] }';
}
};
}
Password.__types = { value: String };
}
// the Bwf itself
bwf.prototype = {
init: function() {
setupInnerTypes();
return this;
},
/**
* Function to validate instance of Beowulf
*/
instanceof: function(klass) {
return (klass.prototype.constructor.name === 'Bwf');
},
/**
* Creates a class for the given string
* @param el: the Beowulf class notation, as string
*/
create: function(el) {
elem = el || elem;
var parts = splitOn(elem, /:/);
// can split?
if ((parts !== undefined) && (parts[0] !== '')) {
// Verify if is a class
if (parts[0] !== parts[0].toLowerCase()) {
var className = parts[0];
result[className] = new Object();
var klass = result[className];
// remove first element
parts.shift();
// Parse as Bwf JSON notation
klass = JSON.parse(parts.join(':').trim().replace(/[a-zA-Z0-9_]+[a-zA-Z0-9\-_ ]*/g,
function(val) {
return '"' + val.trim() + '"';
}
).trim());
klass.__types = new Object();
var kk = JSON.parse(parts.join(':').trim().replace(/[a-zA-Z0-9_]+[a-zA-Z0-9\-_ ]*/g,
function(val) {
return '"' + val.trim() + '"';
}
).trim());
var deep = function(kk, klass) {
// Set variable types
Object.keys(kk).forEach(function(arg) {
if (klass[arg] instanceof Array) {
switch (klass[arg][0].toString().toLowerCase()) {
case 'string':
klass[arg] = [new String()];
klass.__types[arg] = [String];
break;
case 'number':
klass[arg] = [new Number()];
klass.__types[arg] = [Number];
break;
case 'boolean':
klass[arg] = [new Boolean()];
klass.__types[arg] = [Boolean];
break;
case 'list':
klass[arg] = [new Array()];
klass.__types[arg] = [Array];
break;
case 'object':
klass[arg] = [new Object()];
klass.__types[arg] = [Object];
break;
case 'function':
klass[arg] = [new Function()];
klass.__types[arg] = [Function];
break;
default:
// Have to match PERFECTLY and CASE SENSITIVE
if (container[klass[arg]]) {
klass[arg] = [new container[klass[arg][0]]({})];
klass.__types[arg] = [container[klass[arg][0].constructor.name]];
} else {
klass[arg] = [new Object()];
klass.__types[arg] = [Object];
}
break;
}
} else if (klass[arg] instanceof Object) {
// Set __types for deep subclass
klass[arg].__types = new Object();
// run deep mapping
deep(kk[arg], klass[arg]);
// get deep mappings back to superclass
klass.__types[arg] = klass[arg].__types;
delete klass[arg].__types;
} else {
switch (klass[arg].toString().toLowerCase()) {
case 'string':
klass[arg] = new String();
klass.__types[arg] = String;
break;
case 'number':
klass[arg] = new Number();
klass.__types[arg] = Number;
break;
case 'boolean':
klass[arg] = new Boolean();
klass.__types[arg] = Boolean;
break;
case 'list':
klass[arg] = new Array();
klass.__types[arg] = Array;
break;
case 'object':
klass[arg] = new Object();
klass.__types[arg] = Object;
break;
case 'function':
klass[arg] = new Function();
klass.__types[arg] = Function;
break;
default:
// Have to match PERFECTLY and CASE SENSITIVE
if (container[klass[arg]]) {
klass[arg] = new container[klass[arg]]({});
klass.__types[arg] = container[klass[arg].constructor.name];
} else {
klass[arg] = new Object();
klass.__types[arg] = Object;
}
break;
}
}
});
}
// Deeply map class
deep(kk, klass);
// return it
container[className] = classTemplate(className, klass);
return container[className];
}
}
return result;
},
/**
* Used to give values to a created class
* @param val: the values in Beowulf notation
*/
valuate: function(val) {
var parts = splitOn(val, / /);
// can split?
if ((parts !== undefined) && (parts[0] !== '')) {
// Verify if is a class
if (parts[0] !== parts[0].toLowerCase()) {
var className = parts[0].split(/:/)[0];
// class exists ?
if (container[className]) {
// remove first element
parts.shift();
var p = [];
var pp = parts.join(' ').trim().split('');
pp.forEach(
function(k, i) {
if (i !== 0 && i !== pp.length - 1) {
p.push(k);
}
}
);
var json = JSON.parse('{' + p.join('').trim().replace(/[a-zA-Z0-9_]+[a-zA-Z0-9\-_ ]*/g,
function(val) {
return '"' + val.trim() + '"';
}
).trim() + '}');
// parse rest of the string
return new container[className](json);
}
}
}
return result;
}
};
Bwf.toString = function() {
return 'function Bwf() { [native code] }';
};
return bwf.prototype.init();
}
// Load all automatically
(function() {
/**
* Load file from an url and returns a string with its content
* @param url: the url to fetch data.
*/
load = function(url) {
var xhr;
if(window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if(window.ActiveXObject) {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
} else {
return false;
}
xhr.open('GET', url, false);
if(xhr.overrideMimeType) {
xhr.overrideMimeType('text/plain');
}
xhr.send(null);
if(xhr.status == 200) {
return xhr.responseText;
}
return false;
}
/**
* Compiles the text/bwf scripts and add to page
*/
compile = function() {
var script = document.getElementsByTagName('script');
var i, src = [], elem;
for(i = 0; i < script.length; i++) {
if(script[i].type == 'text/beowulf') {
if(script[i].src) {
src.push(load(script[i].src));
} else {
src.push(script[i].innerHTML);
}
}
}
if(src.length == 0) {
return;
}
// add classes to scope
src.forEach(function(s) {
new Bwf().create(s);
});
}
// compile
compile();
})();