js2 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. data/Changelog +3 -0
  2. data/Manifest.txt +13 -28
  3. data/Rakefile +19 -19
  4. data/bin/js2 +48 -41
  5. data/lib/javascript/sel_marker.js2 +3 -3
  6. data/lib/js2/config.rb +6 -1
  7. data/lib/js2/{process/file_handler.rb → file_handler.rb} +15 -19
  8. data/lib/js2/foo.js2.haml +3 -0
  9. data/lib/js2/{process/haml_engine.rb → haml_engine.rb} +1 -1
  10. data/lib/js2/{parser/haml.rb → haml_parser.rb} +20 -7
  11. data/lib/js2/js2.js +108 -0
  12. data/lib/js2/js2bootstrap.js2 +455 -0
  13. data/lib/js2/parser.rb +3542 -0
  14. data/lib/js2/processor.rb +14 -65
  15. data/lib/js2/replace.rb +106 -0
  16. data/lib/js2/{decorator/test.rb → sel_decorator.rb} +11 -14
  17. data/{meta/c_tokenizer.rl.erb → lib/js2/tokenizer.rl.erb} +155 -123
  18. data/lib/js2/tree.rb +340 -0
  19. data/lib/js2/universe.rb +101 -0
  20. data/lib/js2.rb +31 -77
  21. data/lib/tasks/js2.rake +1 -1
  22. metadata +15 -29
  23. data/lib/js2/ast/class_node.rb +0 -105
  24. data/lib/js2/ast/comment_node.rb +0 -2
  25. data/lib/js2/ast/haml_node.rb +0 -22
  26. data/lib/js2/ast/include_node.rb +0 -11
  27. data/lib/js2/ast/inherited_node.rb +0 -7
  28. data/lib/js2/ast/member_node.rb +0 -18
  29. data/lib/js2/ast/method_node.rb +0 -29
  30. data/lib/js2/ast/module_node.rb +0 -6
  31. data/lib/js2/ast/node.rb +0 -14
  32. data/lib/js2/ast/nodes.rb +0 -123
  33. data/lib/js2/ast/stuff_node.rb +0 -6
  34. data/lib/js2/decorator/app.rb +0 -7
  35. data/lib/js2/decorator/cleanser.rb +0 -54
  36. data/lib/js2/decorator/standard.rb +0 -103
  37. data/lib/js2/parser/fast.rb +0 -3959
  38. data/lib/js2/process/universe.rb +0 -89
  39. data/lib/js2/test/selenium.rb +0 -109
  40. data/lib/js2/test/selenium_element.rb +0 -234
  41. data/lib/js2/test/selenium_helper.rb +0 -27
  42. data/lib/js2bootstrap.js2 +0 -274
  43. data/meta/replace.rb +0 -126
@@ -0,0 +1,455 @@
1
+ (function (scope) {
2
+ var JS2 = {};
3
+ scope.JS2 = JS2;
4
+
5
+ function noInit () { };
6
+
7
+ var ooUtils = {
8
+ 'extends': function (par) {
9
+ this.parent = par;
10
+ var newProto = par.oo('instance');
11
+ var proto = this.prototype;
12
+
13
+ var members = this.oo.members;
14
+ for (var k in proto) {
15
+ if (proto.hasOwnProperty(k)) newProto[k] = proto[k];
16
+ }
17
+
18
+ this.prototype = newProto;
19
+ this.prototype['class'] = this;
20
+ },
21
+
22
+ 'instance': function () {
23
+ var proto = this.prototype;
24
+
25
+ var init = null;
26
+ if (this.oo.members.initialize) {
27
+ init = proto.initialize;
28
+ }
29
+
30
+ this.prototype.initialize = noInit;
31
+ var ret = new this();
32
+
33
+ if (init) {
34
+ this.prototype.initialize = init;
35
+ ret.initialize = init;
36
+ } else {
37
+ delete this.prototype['initialize'];
38
+ }
39
+
40
+ return ret;
41
+ },
42
+
43
+ 'include': function (mod) {
44
+ var hash = mod.prototype;
45
+ var members = this.oo.members;
46
+ var modMembers = mod.oo.members;
47
+
48
+ for (var k in modMembers) {
49
+ if (!(k in members)) {
50
+ this.prototype[k] = hash[k];
51
+ }
52
+ }
53
+ },
54
+
55
+ 'member': function (name, member) {
56
+ this.oo.members[name] = true;
57
+ this.prototype[name] = member;
58
+ },
59
+
60
+ 'method': function (name, method) {
61
+ this.oo.members[name] = true;
62
+ this.prototype[name] = method;
63
+ method._name = name;
64
+ method._class = this;
65
+ },
66
+
67
+ 'modularize': function () {
68
+ this.isModule = true;
69
+ },
70
+
71
+ 'staticMember': function (name, member) {
72
+ this[name] = member;
73
+ },
74
+
75
+ 'setHTMLCache': function (hash) {
76
+ // create temp class
77
+ var tempClass = function () {};
78
+
79
+ // look for super htmlCache
80
+ var par = this.parent;
81
+ if (par) {
82
+ var parCache = par.prototype.htmlCache;
83
+ if (parCache) tempClass.prototype = parCache;
84
+ }
85
+
86
+ var htmlCache = new tempClass();
87
+ for (var k in hash) htmlCache[k] = hash[k];
88
+ this.oo('member', 'htmlCache', htmlCache);
89
+ },
90
+
91
+ 'super': function (member) {
92
+ return this.parent.prototype[member];
93
+ },
94
+
95
+ 'property': function (names) {
96
+ for (var i=0; i<names.length; i++) {
97
+ var name = names[i];
98
+ var getter = 'get' + name.charAt(0).toUpperCase() + name.substr(1);
99
+ var setter = 'set' + name.charAt(0).toUpperCase() + name.substr(1);
100
+
101
+ var members = this.oo.members;
102
+ if (! (getter in members))
103
+ this.oo('method', getter, (function (n) { return function () { return this[n] }})(name));
104
+ if (! (setter in members))
105
+ this.oo('method', setter, (function (n) { return function (val) { return this[n] = val }})(name));
106
+ }
107
+ },
108
+
109
+ 'accessor': function (names) {
110
+ for (var i=0; i<names.length; i++) {
111
+ var name = names[i];
112
+ if (! (name in this.oo.members))
113
+ this.oo('method', name,
114
+ function () {
115
+ if (arguments.length) { return this['_' + name] = arguments[0]; }
116
+ else { return this['_' + name]; }
117
+ });
118
+ }
119
+ },
120
+
121
+ 'ancestors': function (names) {
122
+ var ret = [];
123
+ var k = this.parent;
124
+ while (k) {
125
+ ret.push(k);
126
+ k = k.parent;
127
+ }
128
+ return ret;
129
+ }
130
+ };
131
+
132
+
133
+ function createClass (name, par) {
134
+ var K = function () { if (this.initialize) this.initialize.apply(this, arguments); };
135
+
136
+ K.prototype['class'] = K;
137
+ K.prototype['klass'] = K;
138
+
139
+ K.oo = function (method, param1, param2) {
140
+ return ooUtils[method].apply(K, [ param1, param2 ]);
141
+ };
142
+
143
+ K.oo.includes = [];
144
+ K.oo.members = {};
145
+
146
+ return K;
147
+ }
148
+
149
+ function createNamespace (space, currentScope) {
150
+ var currentScope = currentScope || scope;
151
+
152
+ var splitted = space.split('.');
153
+ var name = [];
154
+ while (splitted.length) {
155
+ var part = splitted.shift();
156
+ name.push(part);
157
+
158
+ if (! currentScope[part]) {
159
+ var K = createClass();
160
+ K.package = currentScope;
161
+ currentScope[part] = K;
162
+ }
163
+
164
+ currentScope = currentScope[part];
165
+ currentScope.className = name.join('.');
166
+ }
167
+
168
+ return currentScope;
169
+ }
170
+
171
+ JS2.OO = {};
172
+ JS2.OO.createClass = createNamespace;
173
+ JS2.OO.createModule = function (name) { createNamespace(name).oo('modularize') };
174
+ JS2.OO.get = function (name, scope) {
175
+ scope = scope || window;
176
+ var cur = scope;
177
+ var names = name.split(/\./);
178
+ while (names.length) cur = cur[names.shift()];
179
+ return cur;
180
+ };
181
+
182
+ JS2.OO['super'] = function () {
183
+ var method = arguments.callee.caller;
184
+ var name = method._name;
185
+ var klass = method._class;
186
+ var self = arguments[0];
187
+
188
+ var method = klass.parent.prototype[name];
189
+ if (! method) return;
190
+
191
+ var args = [];
192
+ for (var i=1,len=arguments.length;i<len;i++) {
193
+ args.push(arguments[i]);
194
+ }
195
+ return method.apply(self, args);
196
+ }
197
+
198
+ })(window);
199
+
200
+ class JS2.Observer {
201
+ function initialize (owner) {
202
+ this.lookupBefore = {};
203
+ this.lookupAfter = {};
204
+
205
+ this.replaced = {};
206
+ this.replacedValues = {};
207
+ }
208
+
209
+ function replaceFunction (owner, eventName) {
210
+ if (this.replaced[eventName]) return;
211
+
212
+ this.replacedValues[eventName] = owner[eventName];
213
+ this.replaced[eventName] = true;
214
+ owner[eventName] = this.getTrigger(eventName);
215
+ }
216
+
217
+ function trigger (owner, eventName, args) {
218
+ var beforeChain = this.lookupBefore[eventName];
219
+ if (beforeChain) this.executeChain(beforeChain, args);
220
+
221
+ var funct = this.replacedValues[eventName];
222
+ if (funct) funct.apply(owner, args);
223
+
224
+ var afterChain = this.lookupAfter[eventName];
225
+ if (afterChain) this.executeChain(afterChain, args);
226
+ }
227
+
228
+ function addListener (eventName, funct, before) {
229
+ var lookup = before ? this.lookupBefore : this.lookupAfter;
230
+
231
+ var chain = lookup[eventName] = lookup[eventName] || [];
232
+ chain.push(funct);
233
+ }
234
+
235
+ private
236
+
237
+ function getTrigger (eventName) {
238
+ return function () { this.__observer.trigger(this, eventName, arguments); };
239
+ }
240
+
241
+ function executeChain (chain, args) {
242
+ foreach (var f:i in chain) if (f) f.apply(this, args);
243
+ }
244
+ }
245
+
246
+ module JS2.Observable {
247
+ function addListener (eventName, funct, before) {
248
+ if (! this.__observer) this.__observer = new Factual.Core.Observer();
249
+
250
+ var id = this.__observer.addListener(eventName, funct, before);
251
+ this.__observer.replaceFunction(this, eventName);
252
+ return id;
253
+ }
254
+
255
+ function triggerEvent (eventName, args) {
256
+ if (this.__observer) this.__observer.trigger(this, eventName, args);
257
+ }
258
+ }
259
+
260
+
261
+ class JS2.App.Notifier {
262
+ var autoInc = 1;
263
+
264
+ function initialize () {
265
+ this.chains = {};
266
+ this.autoInc = 1;
267
+ this.id = this['class'].prototype.autoInc;
268
+ this['class'].prototype.autoInc++;
269
+ }
270
+
271
+ function register (comp) {
272
+ if (! comp.__notifier_ids) {
273
+ comp.__notifier_ids = {};
274
+ }
275
+
276
+ if (! comp.__notifier_ids[this.id]) {
277
+ comp.__notifier_ids[this.id] = this.autoInc;
278
+ this.autoInc++;
279
+ }
280
+
281
+ for (var key in comp) {
282
+ if (key.indexOf('e_') == 0) {
283
+ var eventType = key.substr(2);
284
+ if (! this.chains[eventType]) this.chains[eventType] = [];
285
+ this.chains[eventType].push([ comp, comp[key] ]);
286
+ }
287
+ }
288
+
289
+ comp.notify = curry with (this) {
290
+ self.notify.apply(self, arguments);
291
+ };
292
+ }
293
+
294
+ function remove (comp) {
295
+ var id = comp.__notifier_id;
296
+ for (var key in this.chains) {
297
+ var newChain = [];
298
+ foreach (var ele:j in chain) {
299
+ if (ele[0].__notifier_id[this.id] != id) {
300
+ newChain.push(ele);
301
+ }
302
+ }
303
+
304
+ this.chains[key] = newChain;
305
+ }
306
+ }
307
+
308
+ function registerListener (listener) {
309
+ for (var key in listener) {
310
+ var funct = listener[key];
311
+ if (typeof funct != 'function') continue;
312
+ if (! this.chains[key]) this.chains[key] = [];
313
+ this.chains[key].push([ listener, funct ]);
314
+ }
315
+ }
316
+
317
+ function notify () {
318
+ var eventType = arguments[0];
319
+ var args;
320
+
321
+ // optimize for 1 argument
322
+ if (arguments.length == 2) {
323
+ args = [ arguments[1] ];
324
+ } else {
325
+ args = [];
326
+ for (var i=1; i<=arguments.length; i++) args.push(arguments[i]);
327
+ }
328
+
329
+ var chain = this.chains[eventType];
330
+ if (chain) {
331
+ for (var i=0,pair; pair=chain[i++];) {
332
+ pair[1].apply(pair[0], args);
333
+ }
334
+ }
335
+ }
336
+ }
337
+
338
+
339
+
340
+ class JS2.App {
341
+ include JS2.Observer;
342
+
343
+ function start (options) {
344
+ // hack to get notifier
345
+ this.getNotifier();
346
+
347
+ this.build();
348
+ this.notify('setOptions', options || {});
349
+ this.notify('initHTML');
350
+ this.notify('registerEvents');
351
+ this.notify('finalize');
352
+ }
353
+
354
+
355
+ function register (comp) {
356
+ this.getNotifier().register(comp);
357
+ }
358
+
359
+ function getNotifier () {
360
+ if (! this._notifier) {
361
+ this._notifier = new JS2.App.Notifier();
362
+ this._notifier.register(this);
363
+ }
364
+
365
+ return this._notifier;
366
+ }
367
+
368
+ function build () {
369
+ var components = { main: this };
370
+
371
+ var classes = [];
372
+ var klass = this['class'];
373
+
374
+ while (klass) {
375
+ classes.unshift(klass);
376
+ klass = klass.parent;
377
+ }
378
+
379
+ var template = [];
380
+ var already = {};
381
+ var runningIdx = 0;
382
+
383
+ foreach (var c:i in classes) {
384
+ var toAdd = c.prototype.getTemplate();
385
+ foreach (var t:j in toAdd) {
386
+ if (already[t.name] != undefined) {
387
+ template[already[t.name]] = t;
388
+ } else {
389
+ already[t.name] = runningIdx;
390
+ runningIdx += 1;
391
+ template.push(t);
392
+ }
393
+ }
394
+ }
395
+
396
+ // instantiate all components
397
+ components['main'] = this;
398
+ foreach (var config:i in template) {
399
+ if (!config['class']) alert("Invalid class defined for " + name + ':' + config['class']);
400
+ var klass = JS2.OO.get(config['class']);
401
+
402
+ components[config.name] = new klass();
403
+ this.register(components[config.name]);
404
+ }
405
+
406
+ foreach (var config:i in template) {
407
+ var name = config.name;
408
+ var comp = components[name];
409
+
410
+ // inject set dependencies as an array
411
+ if (config.dependencies instanceof Array) {
412
+ foreach (var dep:j in config.dependencies) {
413
+ comp[dep] = components[dep];
414
+ }
415
+ }
416
+
417
+ // as a hash... for use when nickname is not the dependency name
418
+ else if (config.dependencies instanceof Object) {
419
+ for (var key in config.dependencies) {
420
+ comp[key] = components[config.dependencies[key]];
421
+ }
422
+ }
423
+ }
424
+
425
+ this.notify('initBaseHTML');
426
+
427
+ // handle selectors as root elements
428
+ foreach (var config:i in template) {
429
+ var name = config.name;
430
+ var comp = components[name];
431
+
432
+ if (config.selector) comp.$root = this.htmlSelect(this.$root, config.selector);
433
+ if (config.globalSelector) comp.$root = this.htmlSelect(config.globalSelector);
434
+ }
435
+ }
436
+
437
+ function htmlSelect (root, text) {
438
+ alert('html selector not implemented');
439
+ }
440
+
441
+ function getTemplate () {
442
+ return [];
443
+ }
444
+ }
445
+
446
+ class JS2.App.JQuery extends JS2.App {
447
+ function htmlSelect ($root, text) {
448
+ if (text) {
449
+ return $root.find(text);
450
+ } else {
451
+ return $(root);
452
+ }
453
+ }
454
+ }
455
+