modjs-architecture 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ var test = 'test';
@@ -0,0 +1 @@
1
+ var test = 'test';
@@ -0,0 +1,11 @@
1
+ (function(app) {
2
+ var m = app.add_module("foo-elements");
3
+
4
+ //= require "../elements/foo-elements.elements"
5
+
6
+ m.actions = function() {
7
+
8
+ }
9
+
10
+ m.run();
11
+ })(myapp);
data/spec/tmp/foo.js ADDED
@@ -0,0 +1,9 @@
1
+ (function(app) {
2
+ var m = app.add_module("foo");
3
+
4
+ m.actions = function() {
5
+
6
+ }
7
+
8
+ m.run();
9
+ })(myapp);
@@ -0,0 +1,13 @@
1
+ (function(app) {
2
+ var m = app.add_module("foo_all");
3
+
4
+ //= require "../elements/foo_all.elements"
5
+
6
+ //= require "../models/foo_all.model"
7
+
8
+ m.actions = function() {
9
+
10
+ }
11
+
12
+ m.run();
13
+ })(myapp);
@@ -0,0 +1,11 @@
1
+ (function(app) {
2
+ var m = app.add_module("foo_elements");
3
+
4
+ //= require "../elements/foo_elements.elements"
5
+
6
+ m.actions = function() {
7
+
8
+ }
9
+
10
+ m.run();
11
+ })(myapp);
@@ -0,0 +1,11 @@
1
+ (function(app) {
2
+ var m = app.add_module("foo_model");
3
+
4
+ //= require "../models/foo_model.model"
5
+
6
+ m.actions = function() {
7
+
8
+ }
9
+
10
+ m.run();
11
+ })(myapp);
@@ -0,0 +1,320 @@
1
+ is_defined = function(suspect) {
2
+ return typeof suspect == "undefined" ? false : true;
3
+ };
4
+
5
+ is_undefined = function(suspect) {
6
+ return typeof suspect == "undefined" ? true : false;
7
+ };
8
+
9
+ is_typeof = function(type, suspect) {
10
+ if (is_undefined(type)) {
11
+ throw new Error("is_typeof(Type, suspect): type is undefined");
12
+ }
13
+
14
+ if (is_undefined(suspect)) {
15
+ throw new Error("is_typeof(Type, suspect): suspect is undefined");
16
+ }
17
+
18
+ return suspect.constructor == type ? true : false;
19
+ };
20
+
21
+ is_numeric = function(suspect) {
22
+ if (isNaN(suspect)) {
23
+ return false;
24
+ }
25
+ return !isNaN(parseFloat(suspect)) && isFinite(suspect);
26
+ };
27
+
28
+ is_string = function(suspect) {
29
+ return is_typeof(String, suspect);
30
+ };
31
+
32
+ is_array = function(suspect) {
33
+ return is_typeof(Array, suspect);
34
+ };
35
+
36
+ is_number = function(suspect) {
37
+ return is_typeof(Number, suspect);
38
+ };
39
+
40
+ is_date = function(suspect) {
41
+ return is_typeof(Date, suspect);
42
+ };
43
+
44
+ is_bool = function(suspect) {
45
+ return is_typeof(Boolean, suspect);
46
+ };
47
+
48
+ is_regex = function(suspect) {
49
+ return is_typeof(RegExp, suspect);
50
+ };
51
+
52
+ is_empty = function(suspect) {
53
+ if (is_undefined(suspect)) {
54
+ return true;
55
+ }
56
+
57
+ return suspect.length === 0;
58
+ };
59
+
60
+ is_not_empty = function(suspect) {
61
+ return suspect.length >= 1;
62
+ };
63
+ if (!Element.prototype.addEventListener) {
64
+ var oListeners = {};
65
+
66
+ function runListeners(oEvent) {
67
+ if (!oEvent) {
68
+ oEvent = window.event;
69
+ }
70
+
71
+ for (var iLstId = 0, iElId = 0, oEvtListeners = oListeners[oEvent.type]; iElId < oEvtListeners.aEls.length; iElId++) {
72
+ if (oEvtListeners.aEls[iElId] === this) {
73
+ for (iLstId; iLstId < oEvtListeners.aEvts[iElId].length; iLstId++) {
74
+ oEvtListeners.aEvts[iElId][iLstId].call(this, oEvent);
75
+ }
76
+
77
+ break;
78
+ }
79
+ }
80
+ }
81
+
82
+ Element.prototype.addEventListener = function (sEventType, fListener) {
83
+ if (oListeners.hasOwnProperty(sEventType)) {
84
+ var oEvtListeners = oListeners[sEventType];
85
+
86
+ for (var nElIdx = -1, iElId = 0; iElId < oEvtListeners.aEls.length; iElId++) {
87
+ if (oEvtListeners.aEls[iElId] === this) {
88
+ nElIdx = iElId; break;
89
+ }
90
+ }
91
+
92
+ if (nElIdx === -1) {
93
+ oEvtListeners.aEls.push(this);
94
+ oEvtListeners.aEvts.push([fListener]);
95
+ this["on" + sEventType] = runListeners;
96
+ }
97
+ else {
98
+ var aElListeners = oEvtListeners.aEvts[nElIdx];
99
+
100
+ if (this["on" + sEventType] !== runListeners) {
101
+ aElListeners.splice(0);
102
+ this["on" + sEventType] = runListeners;
103
+ }
104
+
105
+ for (var iLstId = 0; iLstId < aElListeners.length; iLstId++) {
106
+ if (aElListeners[iLstId] === fListener) {
107
+ return;
108
+ }
109
+ }
110
+
111
+ aElListeners.push(fListener);
112
+ }
113
+ }
114
+ else {
115
+ oListeners[sEventType] = {
116
+ aEls: [this],
117
+ aEvts: [[fListener]]
118
+ };
119
+
120
+ this["on" + sEventType] = runListeners;
121
+ }
122
+ };
123
+
124
+ Element.prototype.removeEventListener = function (sEventType) {
125
+ if (!oListeners.hasOwnProperty(sEventType)) {
126
+ return;
127
+ }
128
+
129
+ var oEvtListeners = oListeners[sEventType];
130
+
131
+ for (var nElIdx = -1, iElId = 0; iElId < oEvtListeners.aEls.length; iElId++) {
132
+ if (oEvtListeners.aEls[iElId] === this) {
133
+ nElIdx = iElId;
134
+ break;
135
+ }
136
+ }
137
+
138
+ if (nElIdx === -1) {
139
+ return;
140
+ }
141
+
142
+ for (var iLstId = 0, aElListeners = oEvtListeners.aEvts[nElIdx]; iLstId < aElListeners.length; iLstId++) {
143
+ aElListeners.splice(iLstId, 1);
144
+ }
145
+ };
146
+ }
147
+
148
+ var Mod = {};
149
+
150
+ Mod.DOM = (function() {
151
+
152
+ var timer,
153
+ queue = [];
154
+
155
+ function DOM() {
156
+ this.cache = {};
157
+ this.is_ready = false;
158
+ }
159
+
160
+ DOM.prototype.add_event = function(element, type, fn, capture) {
161
+ if (is_undefined(capture)) {
162
+ capture = false;
163
+ }
164
+
165
+ if (is_string(element)) {
166
+ element = this.cache[element];
167
+ }
168
+
169
+ element.addEventListener(type, fn, capture);
170
+ };
171
+
172
+ DOM.prototype.remove_event = function(element, type, fn, capture) {
173
+ if (is_undefined(capture)) {
174
+ capture = false;
175
+ }
176
+
177
+ if (is_string(element)) {
178
+ element = this.cache[element];
179
+ }
180
+
181
+ element.removeEventListener(type, fn, capture);
182
+ };
183
+
184
+ DOM.prototype.call_when_ready = function(func) {
185
+ if (this.is_ready) {
186
+ return func();
187
+ }
188
+
189
+ if (timer) {
190
+ queue.push(func);
191
+ }
192
+ else {
193
+ this.add_event(window, 'load', this.execute_ready_queue);
194
+ queue.push(func);
195
+ timer = setInterval(this.execute_ready_queue, 13);
196
+ }
197
+ };
198
+
199
+ DOM.prototype.execute_ready_queue = function() {
200
+ if (this.is_ready) {
201
+ return false;
202
+ }
203
+
204
+ if (document && document.getElementsByTagName && document.getElementById && document.body) {
205
+ clearInterval(timer);
206
+ timer = null;
207
+ for(var i = 0, j = queue.length; i < j; i++) {
208
+ queue[i]();
209
+ }
210
+
211
+ queue = [];
212
+ this.is_ready = true;
213
+ }
214
+ };
215
+
216
+ DOM.prototype.add_elements = function(elements) {
217
+ for(var key in elements) {
218
+ if (elements.hasOwnProperty(key)) {
219
+ this.add_element(key, elements[key]);
220
+ }
221
+ }
222
+ };
223
+
224
+ DOM.prototype.add_element = function(key, element) {
225
+ this.cache[key] = element;
226
+ };
227
+
228
+ return DOM;
229
+ })();
230
+ Mod.Module = (function() {
231
+
232
+ function Module(name) {
233
+ if (is_undefined(name)) {
234
+ throw new Error("Mod.Module(name): name is undefined");
235
+ }
236
+
237
+ this.dom = new Mod.DOM;
238
+ this.data = {};
239
+ this.name = name;
240
+ }
241
+
242
+ Module.prototype.actions = function() {};
243
+
244
+ Module.prototype.run = function() {
245
+ var mod = this;
246
+ this.dom.call_when_ready(function() {
247
+ mod.execute();
248
+ });
249
+ };
250
+
251
+ Module.prototype.execute = function() {
252
+ this.actions();
253
+ };
254
+
255
+ Module.prototype.elements = function(elements) {
256
+ if (is_undefined(elements)) {
257
+ return this.dom.cache;
258
+ }
259
+
260
+ if (is_string(elements)) {
261
+ var name = elements;
262
+ return this.dom.cache[name];
263
+ }
264
+ else {
265
+ this.dom.add_elements(elements);
266
+ }
267
+ };
268
+
269
+ Module.prototype.set_data = function(key, value) {
270
+ if (is_undefined(key)) {
271
+ throw new Error(this.name + '.set_data(key, value): key is undefined');
272
+ }
273
+
274
+ if (is_typeof(String, key) && is_undefined(value)) {
275
+ throw new SyntaxError(this.name + 'Module.set_data(key, value): value is undefined');
276
+ }
277
+
278
+ if (is_typeof(String, key)) {
279
+ this.data[key] = value;
280
+ }
281
+ else if (is_typeof(Object, key)) {
282
+ var data = key;
283
+ for(var property in data) {
284
+ this.data[property] = data[property];
285
+ }
286
+ }
287
+
288
+ return this;
289
+ };
290
+
291
+ return Module;
292
+ })();
293
+ Mod.Application = (function() {
294
+
295
+ function Application(name) {
296
+ if (is_undefined(name)) {
297
+ throw new Error("new Mod.Application(name): name is undefined");
298
+ }
299
+
300
+ this.name = name
301
+ };
302
+
303
+ Application.prototype.add_module = function(name) {
304
+ if (is_undefined(name)) {
305
+ throw new Error("Mod.Application.add_module(name): name is undefined");
306
+ }
307
+
308
+ if (is_defined(this[name])) {
309
+ throw new Error("Mod.Application.add_module('" + name + "'): '" + name + "' already declared");
310
+ }
311
+
312
+ if (this.name === name) {
313
+ throw new Error("Mod.Application.add_module('" + name + "'): a module cannot have the same name as the application. It's bad idea. Do you really want to write " + name + "." + name + "? It's confusing.'");
314
+ }
315
+
316
+ return this[name] = new Mod.Module(name);
317
+ };
318
+
319
+ return Application;
320
+ })();
@@ -0,0 +1 @@
1
+ var test = 'test';
@@ -0,0 +1,8 @@
1
+ framework: modjs
2
+ src_dir: modules
3
+ build_dir: application
4
+ asset_root: ../
5
+ output: expanded
6
+ dependencies: ["../modules/test.module"]
7
+ autoload: ["../modules/test.module"]
8
+ name: myapp
@@ -0,0 +1,8 @@
1
+ blueprint: modjs
2
+ src_dir: modules
3
+ build_dir: application
4
+ asset_root: ../
5
+ output: compressed
6
+ dependencies: []
7
+ autoload: []
8
+ name: myapp
@@ -0,0 +1,23 @@
1
+ describe("Mod.Application", function() {
2
+
3
+ it("should require a name attribute", function() {
4
+ expect(function() {
5
+ var app = new Mod.Application;
6
+ }).toThrow("new Mod.Application(name): name is undefined");
7
+
8
+ });
9
+
10
+ it("should create a new application instance", function() {
11
+ var app = new Mod.Application('app');
12
+ expect(app).toBeTruthy();
13
+ expect(app.name).toEqual('app');
14
+ });
15
+
16
+ it("should have a module factory method", function() {
17
+ var app = new Mod.Application('app');
18
+ app.add_module('foo');
19
+ expect(app.foo).toBeTruthy();
20
+ expect(app.foo.name).toEqual('foo');
21
+ });
22
+
23
+ });
@@ -0,0 +1,49 @@
1
+ describe("Mod.DOM", function() {
2
+ var dom;
3
+
4
+ beforeEach(function() {
5
+ dom = new Mod.DOM;
6
+ });
7
+
8
+ it("should have a cache", function() {
9
+ expect(dom.cache).toEqual({});
10
+ });
11
+
12
+ it("should have an is_ready property", function() {
13
+ expect(is_bool(dom.is_ready)).toBeTruthy();
14
+ });
15
+
16
+ it("should add an element to the cache", function() {
17
+ dom.add_element('foo', document.getElementById('body'));
18
+ expect(dom.cache.foo).toEqual(document.getElementById('body'));
19
+ });
20
+
21
+ it("should add a hash of elements to the cache", function() {
22
+ dom.add_elements({
23
+ bar: document.getElementsByTagName('html')[0],
24
+ baz: document.getElementsByTagName('head')[0]
25
+ });
26
+
27
+ expect(dom.cache.bar).toEqual(document.getElementsByTagName('html')[0]);
28
+ expect(dom.cache.baz).toEqual(document.getElementsByTagName('head')[0]);
29
+ });
30
+
31
+ it("should have an add_event method", function() {
32
+ expect(dom.add_event).toBeTruthy();
33
+ });
34
+
35
+ it("should have a remove_event method", function() {
36
+ expect(dom.remove_event).toBeTruthy();
37
+ });
38
+
39
+ it("should have a call_when_ready method", function() {
40
+ dom.call_when_ready(function() {
41
+ var body = document.getElementsByTagName('body')[0],
42
+ h1 = document.createElement('h1');
43
+
44
+ h1.setAttribute('id', 'foo');
45
+ body.appendChild(h1);
46
+ expect(document.getElementById('foo')).toBeTruthy();
47
+ });
48
+ });
49
+ });