-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathamethyst.js
270 lines (216 loc) · 8.16 KB
/
amethyst.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
// Amethyst.js 1.0
// http://amethyst.io
// (c) 2014 Jonathan James
// Amethyst may be freely distributed under the MIT license.
(function () {
// Setup
// -----
// Establish the root object, `window` in the browser, or `exports` on the server.
var root = this;
// Create the A object for use below.
var A = {};
// Export the A object correctly for client and server use.
// If Node.js -- export with backwards-compatibility for the old `require()` API.
// If other (browser, etc.), add `A` to root as a global object.
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = A;
}
exports.A = A;
} else {
root.A = A;
}
// Utility functions for library.
// ------------------------------
var isObject = function (x) {
return Object.prototype.toString.call(x) === '[object Object]';
};
var isArray = function (x) {
return Object.prototype.toString.call(x) === '[object Array]';
};
var isFunction = function (x) {
return typeof x === 'function';
};
var canUseConsoleLog = function () {
return typeof console !== 'undefined' && console.log;
};
// Subjects Module
// ---------------
// Amethyst components are comprised of subjects.
// They are abstractions of commonly used functionality -- specialized constructors that
// load functionality into a component or decorate existing functionality.
var subjects = (function () {
// Scalars.
// --------
// All subjects are stored in _store so we can load them into components later.
var _store = {};
// Save and load hooks allow us to use reflection easily.
var saveHooks = { before: [], after: [] }
, loadHooks = { before: [], after: [] };
// Utility functions for subjects.
// -------------------------------
var subjectHasName = function (x) {
return x.name !== null;
};
var isInStore = function (name) {
return typeof _store[name] !== "undefined";
};
var thereAreBeforeSaveHooks = function () {
return !!saveHooks.before.length;
};
var thereAreAfterSaveHooks = function () {
return !!saveHooks.after.length;
};
var saveSubject = function (name, options) {
_store[name] = options;
};
// The method to save subjects in _store.
// --------------------------------------
var save = function (/* list of subject objects with name and options */) {
// Iterate through a list of subjects passed as arguments.
// If they validate, add them to _store.
for (var i = arguments.length - 1; i >= 0; i--) {
var name, options = {};
// Validate and assign name.
if (arguments[i].name == null) throw new Error("Subject " + i + " has no name.");
name = arguments[i].name;
// Validate and assign options.
if (!isObject(arguments[i].options))
throw new Error("Subject " + i + " options must be an object.");
options = arguments[i].options;
// Check for existing subjects by the same name.
if (isInStore(name)) throw new Error("A subject named " +
name + " already exists. Please give your subjects unique names.");
// Run before hooks for save.
if (thereAreBeforeSaveHooks()) {
for (var i = saveHooks.before.length - 1; i >= 0; i--) {
saveHooks.before[i](name, options);
};
}
// Save the subject.
saveSubject(name, options);
// Run after hooks for save.
if (thereAreAfterSaveHooks()) {
for (var i = saveHooks.after.length - 1; i >= 0; i--) {
saveHooks.after[i](name, options);
};
}
};
};
// The method to load subjects into a parent using an explicit "subjects" property.
// --------------------------------------------------------------------------------
var _loadDeps = function (context, subjects) {
if (!isObject(subjects))
throw new Error("Subjects " + subjects +
" to be loaded into a parent must be an object!");
// Call _load recursively for each subject.
var args;
for (key in subjects) {
if (!subjects.hasOwnProperty(key)) continue;
if (isArray(subjects[key])) {
args = [key, subjects[key]];
} else {
args = [key, [subjects[key]]];
}
// Name and value.
_load.apply(context, [args]);
};
}
// The method to load subjects into a component.
// ---------------------------------------------
var _load = function (/* list of subjects in string or array form */) {
var subject = {}
, name = ''
, argsArray = []
, loadedResults = []
, error = function (name) {
throw new Error("Subject " + name + " not found.");
};
// console.log("LOADING SUBJECT", arguments);
// Loop through all given subjects and load them into the component.
for (var i = arguments.length - 1; i >= 0; i--) {
// console.log("LOADING ARGUMENT", i, arguments[i]);
// Arguments can either be an array or string.
// An array with two items will have [subjectName, [subjectConstructorArguments]].
if (isArray(arguments[i])) {
name = arguments[i][0];
// Get this subject from `_store` if it exists.
if (!isInStore(name)) error(name);
subject = _store[name];
// Check if we're loading other subjects into this subject. Load them first.
if (subject.subjects) {
_loadDeps(this, subject.subjects);
}
// Apply argsArray to `loaded` method.
if (arguments[i].length > 1) argsArray = arguments[i][1];
if (subject.loaded && isFunction(subject.loaded)) {
loadedResults.push( subject.loaded.apply(this, argsArray) );
}
// Do as above, but for string subject name (without an args array).
} else if (typeof arguments[i] === 'string') {
name = arguments[i];
if (!isInStore(name)) error(name);
subject = _store[name];
if (subject.subjects) {
_loadDeps(this, subject.subjects);
}
if (subject.loaded && isFunction(subject.loaded)) {
loadedResults.push( subject.loaded.call(this) );
}
}
// Add the subject's API to the component. APIs are optional but safe.
if (subject.api) {
if (isFunction(subject.api)) {
this[name] = subject.api.bind(this); // If subjects.api is a function.
} else {
var val
, boundAPI = {};
for (key in subject.api) { // TODO: make recursive?
if (!subject.api.hasOwnProperty(key)) continue;
val = subject.api[key];
boundAPI[key] = isFunction(val) ? val.bind(this) : val;
}
this[name] = boundAPI; // APIs are namespaced by subject when loaded.
};
}
} // End for arguments.
return loadedResults;
};
var load = function (/* context, subject names...*/) {
var s = Array.prototype.slice.call(arguments, 1);
_load.apply(arguments[0], s);
};
// Run a subject's unloaded() function in a given context
var unload = function (/* context, subject name */) {
var s = Array.prototype.slice.call(arguments, 1);
if (!isInStore(s)) throw new Error("Subject " + s + " not found.");
if (_store[s].unloaded && isFunction(_store[s].unloaded))
_store[s].unloaded.call(arguments[0]);
};
var view = function () {
if (canUseConsoleLog()) console.log(_store);
};
// Subjects API
// ------------
return {
before: {
save: function (func) {
if (!isFunction(func)) throw new Error("Hooks must be functions!");
saveHooks.before.push(func);
}
},
after: {
save: function (func) {
if (!isFunction(func)) throw new Error("Hooks must be functions!");
saveHooks.after.push(func);
}
},
save: save,
load: load,
unload: unload,
view: view
};
}());
// Add subjects module to A!
A.subjects = subjects;
}).call(this);