-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShit.js
216 lines (168 loc) · 6.78 KB
/
Shit.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
! function(root, undefined) {
var
isInitializing = false,
shitApps = [];
function construct(constructor, args) {
function Dependency() {
return constructor.apply(this, args);
}
Dependency.prototype = constructor.prototype;
return new Dependency();
}
function isPromise(object) {
return object.hasOwnProperty('then');
}
function isInstance(object) {
return "object" == typeof object;
}
function expandAndResolveArgumentsFromFunctionString(argumentList, targetScope) {
var argumentArray = argumentList.map(function(value) {
var
targetTypeName = value ? value.trim() : null,
scopeType = targetTypeName === '$scope' ? targetScope : void 0,
requestedType = scopeType || targetScope[targetTypeName] || targetScope.cache[targetScope._typeName][targetTypeName];
if (!targetTypeName || !requestedType)
return void 0;
return isInstance(requestedType) || isPromise(requestedType) ? requestedType : inject.call(targetScope, requestedType); // instance, promise, singleton, or recursive inject
});
return [].slice.call(argumentArray);
}
function module(a, fn) {
return (window.define && window.define.amd) ? define(a, [], fn) : this[a] = fn;
}
function appScope(appName, applicationType) {
if (shitApps[appName]) {
for (var prop in applicationType.prototype) {
shitApps[appName][prop] = applicationType.prototype[prop];
}
} else {
var inst = nestedScope(appName, applicationType);
inst.prototype._typeName = appName;
shitApps[appName] = new inst();
}
return shitApps[appName];
}
function nestedScope(n, fn, parentNamespace) {
if ( !! parentNamespace) {
return module.call(shitApps[parentNamespace], n, fn);
} else {
root[n] = fn;
return fn;
}
}
function ViewModel(name, fn) {
fn.prototype = Object.create(fn.prototype, {
_typeName: {
value: name
},
_type: {
value: 'ViewModel'
}
});
fn.constructor = fn;
this.cache[this._typeName][name] = fn;
return nestedScope(name, fn, this._typeName);
}
function Service(name, fn) {
fn.prototype = Object.create(fn.prototype, {
_typeName: {
value: name
},
_type: {
value: 'Service'
}
});
fn.constructor = fn;
this.cache[this._typeName][name] = fn;
return nestedScope(name, fn, this._typeName);
}
function inject(fn) {
// inject stuff defined as arguments to fn
var
o = this,
dep = this.cache[this._typeName],
target = fn,
rx_args = /^function\s*[^\(]*\(\s*([^\)]*)\)/mi,
text = target.toString(),
args = text.match(rx_args)[1].split(','),
expandedArguments = expandAndResolveArgumentsFromFunctionString(args, o);
// use the construct() fn to new up the resource with a correct prototype chain and arguments that are to be injected
return !args.length || !args[0] ? new target() : construct(target, expandedArguments)
}
function typeResolver(svc, rootElement){
var instance = this,
requestedType = shitApps[instance._typeName][svc];
// dependecy injection
if ( !requestedType ) return;
// if already an instance return existing. else ask inject to new it up
var target = isInstance(requestedType) ?
requestedType :
requestedType = inject.call(instance, requestedType);
// in case it's a viewmodel. let root-app databind it.
if (target._type === "ViewModel")
instance.databind.call(instance, target, rootElement);
return target;
}
function resolveByDataAttributes() {
var instance = this;
// parse the DOM for data-view and data-app on dom ready
if (instance.resolved) return;
[].slice.call(document.querySelectorAll("[data-view]")).forEach(function(el) {
var vm = el.getAttribute("data-view");
if (!vm || !vm.length) return;
el.dataset.viewmodel = instance.resolve(vm, el);
});
instance.resolved = true;
}
function createDefaultApplicationProperties(applicationType, appName, instance){
applicationType.prototype.inject = applicationType.prototype.inject || [];
applicationType.prototype.databind = applicationType.prototype.databind || function() {};
applicationType.prototype.activate = applicationType.prototype.activate || function() {};
applicationType.prototype.ViewModel = ViewModel.bind(instance);
applicationType.prototype.Service = Service.bind(instance);
applicationType.prototype.cache = applicationType.prototype.cache || {};
applicationType.prototype.cache[appName] = {};
applicationType.prototype.resolved = false;
applicationType.prototype.resolve = typeResolver.bind(instance);
}
function injectAppScopeDependenciesFor(instance){
instance.inject.forEach(function(res) {
instance.resolve(res);
});
}
var Application = function(nameOrProt, proto) {
var
instance,
applicationName,
actualPrototype,
applicationType = function() {};
if (typeof(nameOrProt) === "string") {
// name, prototype passed in
applicationName = nameOrProt;
actualPrototype = proto;
} else {
// only proto passed in, use a default app name
applicationName = !root.document ? "$Application" : (root.document.querySelector("[data-app]").getAttribute("data-app") || "$Application");
actualPrototype = nameOrProt;
}
applicationType.prototype = actualPrototype;
instance = new appScope(applicationName, applicationType);
createDefaultApplicationProperties(applicationType, applicationName, instance);
if(isInitializing === true)
return instance;
injectAppScopeDependenciesFor(instance);
instance.activate.call(instance, instance.cache[applicationName]);
window.addEventListener('DOMContentLoaded', resolveByDataAttributes.bind(instance) , false);
return instance;
}
return function addLibraryToRuntime() {
isInitializing = true;
root.$hit = Application(function() {});
root.$hit.Application = Application;
isInitializing = false;
if (window && window.define && window.define.amd) window.define("shitjs", [], function() {
return root;
});
return root;
}();
}(window);