1 |
|
2 |
/* |
3 |
Module("my.namespace", function () { |
4 |
Class("Test", { |
5 |
|
6 |
}) |
7 |
}) |
8 |
*/ |
9 |
|
10 |
Class("Joose.Module", { |
11 |
has: { |
12 |
_name: { |
13 |
is: rw |
14 |
}, |
15 |
_elements: { |
16 |
is: rw |
17 |
}, |
18 |
_container: { |
19 |
is: rw |
20 |
} |
21 |
}, |
22 |
classMethods: { |
23 |
setup: function (name, functionThatCreatesClassesAndRoles) { |
24 |
var me = this; |
25 |
var parts = name.split("."); |
26 |
var object = joose.top; |
27 |
var soFar = [] |
28 |
var module; |
29 |
for(var i = 0; i < parts.length; i++) { |
30 |
var part = parts[i]; |
31 |
if(part == "meta") { |
32 |
throw "Module names may not include a part called 'meta'." |
33 |
} |
34 |
cur = object[part]; |
35 |
soFar.push(part) |
36 |
var name = soFar.join(".") |
37 |
if(typeof cur == "undefined") { |
38 |
object[part] = {}; |
39 |
module = new Joose.Module(name) |
40 |
module.setContainer(object[part]) |
41 |
object[part].meta = module |
42 |
Joose.Module._allModules.push(object[part]) |
43 |
|
44 |
} else { |
45 |
module = cur.meta; |
46 |
if(!(module && module.meta && (module.meta.isa(Joose.Module)))) { |
47 |
throw "Trying to setup module "+name+" failed. There is already something else: "+module |
48 |
} |
49 |
} |
50 |
object = object[part] |
51 |
} |
52 |
var before = joose.currentModule |
53 |
joose.currentModule = module |
54 |
if(functionThatCreatesClassesAndRoles) { |
55 |
functionThatCreatesClassesAndRoles(object); |
56 |
} |
57 |
joose.currentModule = before; |
58 |
return object |
59 |
}, |
60 |
|
61 |
getAllModules: function () { |
62 |
return this._allModules |
63 |
} |
64 |
}, |
65 |
methods: { |
66 |
alias: function (destination) { |
67 |
var me = this; |
68 |
|
69 |
if(arguments.length == 0) { |
70 |
return this |
71 |
} |
72 |
|
73 |
Joose.A.each(this.getElements(), function (thing) { |
74 |
var global = me.globalName(thing.meta.className()); |
75 |
|
76 |
if(destination[global] === thing) { // already there |
77 |
return |
78 |
} |
79 |
|
80 |
if(typeof destination[global] != "undefined") { |
81 |
throw "There is already something else in the spot "+global |
82 |
} |
83 |
|
84 |
destination[global] = thing; |
85 |
}) |
86 |
}, |
87 |
|
88 |
globalName: function (name) { |
89 |
var moduleName = this.getName(); |
90 |
if(name.indexOf(moduleName) != 0) { |
91 |
throw "All things inside me should have a name that starts with "+moduleName+". Name is "+name |
92 |
} |
93 |
var rest = name.substr(moduleName.length + 1); // + 1 to remove the trailing dot |
94 |
if(rest.indexOf(".") != -1) { |
95 |
throw "The things inside me should have no more dots in there name. Name is "+rest |
96 |
} |
97 |
return rest |
98 |
}, |
99 |
|
100 |
removeGlobalSymbols: function () { |
101 |
Joose.A.each(this.getElements(), function () { |
102 |
var global = this.globalName(thing.getName()); |
103 |
delete joose.top[global] |
104 |
}) |
105 |
}, |
106 |
|
107 |
initialize: function (name) { |
108 |
this.setElements([]) |
109 |
this.setName(name); |
110 |
}, |
111 |
|
112 |
isEmpty: function () { |
113 |
return this.getElements().length == 0 |
114 |
}, |
115 |
|
116 |
addElement: function (ele) { |
117 |
if(!(ele || ele.meta)) { |
118 |
throw "You may only add things that are Joose objects" |
119 |
} |
120 |
this._elements.push(ele) |
121 |
}, |
122 |
|
123 |
getNames: function () { |
124 |
var names = []; |
125 |
Joose.A.each(this.getElements(), function (ele) { names.push(ele.meta.getName()) }); |
126 |
return names |
127 |
} |
128 |
} |
129 |
}) |
130 |
|
131 |
__global__ = {}; |
132 |
__global__.meta = new Joose.Module(); |
133 |
__global__.meta.setName("__global__"); |
134 |
__global__.meta.setContainer(__global__); |
135 |
|
136 |
Joose.Module._allModules = [__global__]; |
137 |
|
138 |
Module("__global__.nomodule", function () {}) |
139 |
__global__.nomodule.meta._elements = joose.globalObjects |