/
foundation.js
262 lines (221 loc) · 8.5 KB
/
foundation.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
(function(_) {
_.mixin({
constructor: function() {
var constructorBasename, containingModule;
var args = extractConstructorArguments(arguments);
if (args.qualifiedConstructorName) {
constructorBasename = args.qualifiedConstructorName.split(".").pop();
containingModule = createModuleContainingConstructor(args.qualifiedConstructorName);
}
var constructor = function() {
if (this.initialize && !constructor._initializeDisabled_) this.initialize.apply(this, arguments);
}
if (constructorBasename) constructor.basename = constructorBasename;
if (args.superconstructor) {
this.inherit(args.superconstructor, constructor);
} else if (_.Object) {
this.inherit(_.Object, constructor);
}
_.each(args.mixins, function(module) {
_.addMethods(constructor.prototype, module);
});
if (constructor.prototype.constructorInitialize) {
if (!constructor.prototype.constructorProperties) constructor.prototype.constructorProperties = {};
constructor.prototype.constructorProperties.initialize = constructor.prototype.constructorInitialize;
delete constructor.prototype.constructorInitialize;
}
if (constructor.prototype.constructorProperties) {
this.addMethods(constructor, constructor.prototype.constructorProperties);
}
if (constructorBasename) containingModule[constructorBasename] = constructor;
if (args.superconstructor && args.superconstructor.inherited) args.superconstructor.inherited(constructor);
if (constructor.initialize) constructor.initialize();
return constructor;
},
module: function(qualifiedModuleName, properties) {
var module = createModulePath(qualifiedModuleName.split("."));
this.addMethods(module, properties);
return module;
},
inherit: function(superconstructor, subconstructor) {
var originalSubconstructorPrototype = subconstructor.prototype;
try {
superconstructor._initializeDisabled_ = true;
subconstructor.superconstructor = superconstructor;
subconstructor.prototype = new superconstructor();
} finally {
delete superconstructor._initializeDisabled_;
}
if (superconstructor.prototype.constructorProperties) {
subconstructor.prototype.constructorProperties = _.clone(superconstructor.prototype.constructorProperties);
}
subconstructor.prototype.constructor = subconstructor;
this.addMethods(subconstructor.prototype, originalSubconstructorPrototype);
return subconstructor;
},
addMethods: function(target, source) {
if (!target) throw new Error("Target module or constructor is null");
if (!source) throw new Error("Source module is null");
definePropertyAccessors(source);
_.each(source, function(value, key) {
if (key == "constructor") return;
if (key == "constructorProperties" && target.constructorProperties) {
_.addMethods(target.constructorProperties, source.constructorProperties);
return;
}
if (target[key] && _.isFunction(target[key]) && _.isFunction(value) && _.argumentNames(value)[0] == "$super") {
value = _.wrapMethod(target[key], value);
}
target[key] = value;
});
return target;
},
assignProperties: function(target, source) {
_.each(source, function(value, key) {
if (target[key] && _.isFunction(target[key]) && target[key]._accessor_) {
target[key](value);
delete source[key];
}
});
_.addMethods(target, source);
return target;
},
argumentNames: function(fn) {
var names = fn.toString().match(/^[\s\(]*function[^(]*\(([^)]*)\)/)[1]
.replace(/\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g, '')
.replace(/\s+/g, '').split(',');
return names.length == 1 && !names[0] ? [] : names;
},
wrapMethod: function(original, wrapper) {
return function() {
return wrapper.apply(this, [_.bind(original, this)].concat(_.toArray(arguments)));
};
}
});
var permittedPropertyAccessorDefinitionKeys = ["reader", "writer", "afterWrite", "afterChange"];
function definePropertyAccessors(module) {
if (module.propertyAccessors) {
var propertyAccessors = module.propertyAccessors;
delete module.propertyAccessors;
_.each(propertyAccessors, function(attrName) {
if (module[attrName] === undefined) module[attrName] = buildPropertyAccessor(attrName)
}, this);
}
_.each(module, function(value, key) {
if (isPropertyAccessorDefinition(value)) {
module[key] = buildPropertyAccessor(key, value.reader, value.writer, value.afterWrite, value.afterChange);
}
});
}
function isPropertyAccessorDefinition(value) {
if (typeof value !== "object") return false;
if (_.isEmpty(value)) return false;
return _.all(_.keys(value), function(key) {
return _.include(permittedPropertyAccessorDefinitionKeys, key)
});
}
function buildPropertyAccessor(name, reader, writer, afterWriteHook, afterChangeHook) {
var fieldName = "_" + name;
if (!reader) reader = function() { return this[fieldName]; };
if (!writer) writer = function(value) { this[fieldName] = value; };
var accessor = function() {
if (arguments.length == 0) {
return reader.call(this);
} else {
var oldValue = this[fieldName];
var newValue = writer.apply(this, arguments) || this[fieldName];
var writeHookReturnVal, changeHookReturnVal;
if (afterWriteHook) writeHookReturnVal = afterWriteHook.call(this, newValue, oldValue);
if (afterChangeHook && newValue !== oldValue) changeHookReturnVal = afterChangeHook.call(this, newValue, oldValue);
return changeHookReturnVal || writeHookReturnVal || newValue;
}
};
accessor._accessor_ = true;
return accessor;
}
function createModuleContainingConstructor(qualifiedConstructorName) {
var qualifiedConstructorPath = qualifiedConstructorName.split(".");
var containingModulePath = qualifiedConstructorPath.slice(0, qualifiedConstructorPath.length - 1);
return createModulePath(containingModulePath);
}
function createModulePath(path) {
var currentModule = window;
_.each(path, function(pathFragment) {
if (!currentModule[pathFragment]) currentModule[pathFragment] = {};
currentModule = currentModule[pathFragment];
});
return currentModule;
}
function extractConstructorArguments(args) {
var constructorArguments = {
mixins: []
};
_.each(args, function(currentArg) {
if (_.isString(currentArg)) {
constructorArguments.qualifiedConstructorName = currentArg;
} else if (_.isFunction(currentArg)) {
constructorArguments.superconstructor = currentArg;
} else {
constructorArguments.mixins.push(currentArg);
}
});
return constructorArguments;
}
_.module("_.Kernel", {
hitch: function() {
var args = _.toArray(arguments);
var methodName = _.first(args);
var otherArgs = _.rest(args);
return _.bind.apply(_, [this[methodName], this].concat(otherArgs));
},
bind: function() {
var args = _.toArray(arguments);
var fn = _.first(args);
var otherArgs = _.rest(args);
return _.bind.apply(_, [fn, this].concat(otherArgs));
},
defer: function(fn) {
return _.defer(this.bind(fn));
},
delay: function(fn, time) {
return _.delay(this.bind(fn), time);
},
isA: function(constructor) {
var currentConstructor = this.constructor;
while (true) {
if (currentConstructor === constructor) return true;
if (currentConstructor.superconstructor) {
currentConstructor = currentConstructor.superconstructor;
} else {
return false;
}
}
}
});
_.constructor("_.Object", {
constructorProperties: _.extend({
delegateConstructorMethods: function() {
var args = _.toArray(arguments);
var delegateName = args.pop();
var newConstructorMethods = {};
this.setupDelegateMethods(newConstructorMethods, args, delegateName);
if (!this.prototype.constructorProperties) this.prototype.constructorProperties = {};
_.extend(this.prototype.constructorProperties, newConstructorMethods);
_.extend(this, newConstructorMethods);
},
delegate: function(source) {
var args = _.toArray(arguments);
var delegateName = args.pop();
this.setupDelegateMethods(this.prototype, args, delegateName);
},
setupDelegateMethods: function(delegator, methodNames, delegateName) {
_.each(methodNames, function(methodName) {
delegator[methodName] = function() {
var delegate = this[delegateName];
return delegate[methodName].apply(delegate, arguments);
};
});
}
}, _.Kernel)
}, _.Kernel);
})(_);