-
Notifications
You must be signed in to change notification settings - Fork 0
/
candi.js
207 lines (178 loc) · 6.22 KB
/
candi.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
;(function (window) {
window.candi = (function(){
// $$injector closure - dependency injection
var $$injector = (function() {
// $$util - utility service
var $$util = (function () {
// function reflection utility
var fn = (function() {
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m,
FN_ARG_SPLIT = /,/,
FN_ARG = /^\s*(_?)(\S+?)\1\s*$/,
STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
return {
arguments: function(fn) {
return this.text(fn).match(FN_ARGS)[1].split(FN_ARG_SPLIT);
},
text: function(fn) {
return fn.toString().replace(STRIP_COMMENTS, '');
},
FN_ARGS: FN_ARGS,
FN_ARG_SPLIT: FN_ARG_SPLIT,
FN_ARG: FN_ARG,
STRIP_COMMENTS: STRIP_COMMENTS
};
})();
return {
format: function (template) {
var args = Array.prototype.slice.call(arguments, 1);
return template.replace(/\{\d+\}/g, function (match) {
var index = match.slice(1, -1);
return typeof args[index] !== 'undefined' ? args[index] : match;
});
},
checkable: function (element) {
return (/radio|checkBox/i).test(element.type);
},
isFunction: function (obj) {
return typeof obj === 'function';
},
isArray: function(obj) {
return obj instanceof Array;
},
isString: function(obj) {
return typeof obj === 'string';
},
isUndefined: function(obj) {
return typeof obj === 'undefined';
},
exists: function (obj, emptyStrings) {
return (!emptyStrings && typeof obj !== 'undefined') || (emptyStrings && typeof obj !== 'undefined' && (typeof obj === 'string' && obj.length));
},
forEach: function(array, fn) {
if(!this.isArray(array)) throw $$err('$$util')("Invalid argument. 'array' must be an instance of Array.");
if(!this.isFunction(fn)) throw $$err('$$util')("Invalid argument. 'fn' must be be a Function.");
for(var i = 0; i < array.length; i++) {
fn(array[i], i, array);
}
},
fn: fn
};
})();
// $$err - base error handling service
var $$err = function (module) {
return function (type, template) {
var templateArgs = Array.prototype.slice.call(arguments, 2),
prefix = $$util.format('[{0} : {1}] ', module || '?', type || '?'),
message = prefix + $$util.format.apply({}, [template].concat(templateArgs));
return new Error(message);
};
};
//$$injector - dependency injector
var $$injector = (function () {
var deps = {},
$$injectorErr = $$err('$$injector'),
_ = $$util;
function apply(fn, $inject, scope, args, type) {
try {
return fn.apply(scope || {}, resolve($inject, args, fn.length));
} catch (e) {
throw $$injectorErr(type, 'Invocation error.\n{0}', e);
}
}
function has(name) {
return deps.hasOwnProperty(name) && typeof deps[name] !== 'undefined';
}
function resolve(names, args, fnLength) {
var locals = [], nonDeps, errorCount, returnSingle = _.isString(names);
if(!names || !names.length) return;
if(!_.isArray(names)) names = [].concat(names);
nonDeps = names.slice(0);
for(var i = names.length - 1; i >= 0; i--) {
if(has(names[i])) {
nonDeps.splice(i, 1);
locals.unshift(deps[names[i]]);
continue;
}
if(args && (names.length - args.length) <= i) locals.unshift(args[i - (names.length - args.length)]);
}
// throw errors for all dependencies that we couldn't find a match for
fnLength = fnLength || (args ? args.length : 0);
if(nonDeps.length && (errorCount = nonDeps.length - fnLength) > 0) {
_.forEach(nonDeps.slice(0, errorCount), function(name) {
throw $$injectorErr('resolve', "'{0}' is not a registered dependency.", name);
});
}
return returnSingle ? locals[0] : locals;
}
function precompile (fn) {
if(typeof fn !== 'function') return false;
var args = [];
if(fn.length) {
_.forEach(_.fn.arguments(fn), function(arg) {
arg.replace(_.fn.FN_ARG, function(all, underscore, name) {
args.push(name);
});
});
}
return args;
}
function assert(name, existance, type) {
if (!existance && has(name))
throw $$injectorErr(type || '?', "'{0}' is already a registered dependency.", name);
if (existance && !has(name))
throw $$injectorErr(type || '?', "'{0}' is an invalid dependency and has not been registered.", name);
return true;
}
return {
define: function (name, dependency) {
// let's make sure the dependency doesn't exist
assert(name, false);
// now return our new dependency
return (deps[name] = dependency);
},
resolve: resolve,
has: has,
invoke: function(fn, scope, args, type) {
if (typeof fn !== 'function') return false;
return apply(fn, precompile(fn), scope, args, type);
},
instantiate: function(fn, args, type) {
if(!_.isFunction(fn)) return false;
var instance = Object.create(fn.prototype);
this.invoke(fn, instance, args, type);
return instance;
},
assert: assert
};
})();
// define our dependencies so we can inject them in later services through the $$provider
$$injector.define('$util', $$util);
$$injector.define('$err', $$err);
$$injector.define('$invoke', $$injector.invoke);
return $$injector;
})();
// $$provider - handles object/function definition with dependency injection being it's main purpose
var $$provider = (function ($$injector) {
var $$providerErr = $$injector.resolve('$err')('$$provider'),
_ = $$injector.resolve('$util');
return {
singleton: function (name, factory, scope) {
return $$injector.define(name, $$injector.invoke(factory, scope, undefined, _.format('singleton|{0}', name)));
},
instance: function (name, factory) {
return $$injector.define(name, function() {
return $$injector.instantiate(factory, Array.prototype.slice.call(arguments), _.format('instance|{0}', name));
});
},
variable: function(name, value) {
return $$injector.define(name, value);
}
};
})($$injector);
return {
injector: $$injector,
provider: $$provider
};
})();
})(window);