-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApplicationPrototype.js
More file actions
executable file
·316 lines (303 loc) · 9.77 KB
/
ApplicationPrototype.js
File metadata and controls
executable file
·316 lines (303 loc) · 9.77 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/// <reference path="index.d.ts" />
/* jshint -W054, -W018, undef:true */
/* globals APP_BUILDER_GLOBAL, window, module, process, console, global, setImmediate, setTimeout */
var isBrowser=new Function("try {return this===window;}catch(e){ return false;}");
var isNode=new Function("var isBrowser = false; try { isBrowser = this===window;}catch(e){ isBrowser = false;}; try {return !isBrowser && ( this ===global );}catch(e){console.error(e); return false;}");
var ApplicationPrototype = ((function (window, undefined) {
window.ApplicationPrototype = (function (builder) {
var methods = {};
var private_methods = [];
var public_methods = {};
var vars = {};
var config = {};
var crudEvents = function (methods, public_methods, private_methods) {
var events = {};
var nextTick;
// @ts-ignore
if (typeof(setImmediate) === "function") {
// @ts-ignore
nextTick = setImmediate;
// @ts-ignore
} else if (typeof(process) === "object" && process && typeof(process.nextTick) === "function") {
// @ts-ignore
nextTick = setImmediate;
} else {
nextTick = setTimeout;
}
var bind_method = function (v, f, conf) {
var method = "" + v;
var methodNameCamel = (method + "").replace(/^./, function (m) { return m.toUpperCase(); });
var method_config;
if (typeof(conf) === "string" && conf !== "all" && conf !== "default") {
method_config = {
listenedBefore : false,
listenedOn : false,
listenedAfter : false,
allowInterruption: false
};
if (conf.indexOf("light") !== -1) { // on
//@ts-ignore
method_config.listenedOn = {
hookname : "on" + methodNameCamel
};
method_config.allowInterruption = true;
}
if (conf.indexOf("af") !== -1) { // after
//@ts-ignore
method_config.listenedAfter = {
hookname : "after" + methodNameCamel
};
}
if (conf.indexOf("on") !== -1) { // on
//@ts-ignore
method_config.listenedOn = {
hookname : "on" + methodNameCamel
};
}
if (conf.indexOf("st") !== -1) { // interrupt
method_config.allowInterruption = true;
}
if (conf.indexOf("before") !== -1 || conf.indexOf("bf") !== -1) { // before
//@ts-ignore
method_config.listenedBefore = {
hookname : "before" + methodNameCamel
};
}
} else {
method_config = {
listenedBefore : { hookname : "before" + methodNameCamel },
listenedOn : { hookname : "on" + methodNameCamel },
listenedAfter : { hookname : "after" + methodNameCamel },
allowInterruption: true
};
}
if (conf && typeof(conf) === "object") {
((function (config, conf) {
var i;
for (i in config) if (i in conf) {
if (typeof(conf[i]) === typeof(config[i])) {
config[i] = conf[i];
}
}
})(method_config, conf));
}
public_methods[method] = function () {
if (
method === 'on' || method === 'once' || method === 'off' || method === 'emit' || method === 'bind' || (
method_config.listenedBefore === false
&& method_config.listenedOn === false
&& method_config.listenedAfter === false
)
) {
return ( f || methods[method] ).apply(public_methods, arguments);
}
if (method_config.listenedBefore !== false) {
if (methods.emit(method_config.listenedBefore.hookname, arguments, false, !method_config.allowInterruption) === false) {
return false;
}
}
if (method_config.listenedOn !== false) {
if (!methods.emit(method_config.listenedOn.hookname, arguments, false, !method_config.allowInterruption) === false) {
return false;
}
}
var response = ( f || methods[method] ).apply(public_methods, arguments);
if (method_config.listenedAfter !== false) {
var args = arguments;
nextTick(function () {
if (!methods.emit(method_config.listenedAfter.hookname, args, false, !method_config.allowInterruption) === false) {
return false;
}
}, 0);
}
return response;
};
return public_methods;
};
var handlerIdIndex = 1;
methods.on = function (eventName, handler, handlerId) {
if (eventName.match(/\s*\,\s*/)) {
eventName.split(/\s*\,\s*/).forEach(function (eventName) {
eventName = eventName.replace(/^\s+/, '').replace(/\s+$/, '');
if (!eventName) return;
methods.on(eventName, handler, handlerId);
});
return;
} else {
eventName = eventName.replace(/^\s+/, '').replace(/\s+$/, '');
if (!eventName) return;
}
if (typeof(handler) !== "function")
return false;
if (!(eventName in events)) {
events[eventName] = {};
}
if (!handlerId) {
handlerIdIndex++;
handlerId = "s-" + handlerIdIndex;
}
if (handlerId in events[eventName]) {
delete events[eventName][handlerId];
}
events[eventName][handlerId] = handler;
return handlerId;
};
methods.once = function (eventName, handler, handlerId) {
var id;
id = methods.on(eventName, function () {
methods.off(eventName, id);
handler.apply(this, arguments);
}, handlerId);
return id;
};
methods.off = function (eventName, handlerId) {
if (eventName.match(/\s*\,\s*/)) {
eventName.split(/\s*\,\s*/).forEach(function (eventName) {
eventName = eventName.replace(/^\s+/, '').replace(/\s+$/, '');
if (!eventName) return;
methods.off(eventName, handlerId);
});
return;
} else {
eventName = eventName.replace(/^\s+/, '').replace(/\s+$/, '');
if (!eventName) return;
}
if (handlerId) {
if (eventName in events) {
if (handlerId in events[eventName]) {
delete events[eventName][handlerId];
return true;
}
}
} else if (eventName in events) {
var b = false;
for (handlerId in events[eventName]) {
delete events[eventName][handlerId];
b = true;
}
return b;
}
return false;
};
methods.emit = function (eventName, args, track, noSkipStopReturn) {
if (eventName in events) {
var handlerId;
for(handlerId in events[eventName]) {
try {
if (events[eventName][handlerId].apply(( track ? public_methods : methods ), (args || [])) === false && !noSkipStopReturn) {
return false;
}
} catch(err) {
console.error("Error detected: ", err);
}
}
}
};
methods.bind = function (method, callback, conf) {
if (typeof(method) === "function") {
conf = callback;
callback = method;
method = callback.name;
}
return bind_method(method, callback, conf);
};
methods.property = function (propName, getter, setter, config) {
if (typeof(propName) === "function") {
config = setter || getter;
getter = propName;
setter = propName || getter;
propName = propName.name || null;
}
config = config || {};
if (propName) {
var _lastValue = null;
if (!setter && getter) {
setter = getter;
}
if (!getter) getter = function () {
return _lastValue;
};
if (!setter) setter = function ( newValue) {
_lastValue = newValue;
return _lastValue;
};
[
methods,
public_methods
].forEach(function (methods) {
Object.defineProperty(
methods,
propName,
Object.assign(
{
configurable: ( typeof(config.configurable) === "boolean" ? config.configurable : true ),
enumerable: ( typeof(config.enumerable) === "boolean" ? config.enumerable : true )
},
getter ? {
get: function () {
var value = getter.apply(methods, [ undefined, _lastValue, false ]);
var validate = methods.emit('__onGet', [ propName, value, _lastValue ]);
if (validate !== undefined) value = validate;
validate = methods.emit('__onGet::' + propName, [ value, _lastValue ]);
if (validate !== undefined) value = validate;
methods.emit('__afterGet', [propName, value, _lastValue]);
methods.emit('__afterGet::' + propName, [value, _lastValue]);
return value;
}
} : {},
setter ? {
set: function (newValue) {
var value = setter.apply(methods, [ newValue, _lastValue, true ]);
var validate = methods.emit('__onSet', [ propName, value, _lastValue ]);
if (validate !== undefined) value = validate;
validate = methods.emit('__onSet::' + propName, [ value, _lastValue ]);
if (validate !== undefined) value = validate;
var _lastValue2 = _lastValue;
_lastValue = value;
methods.emit('__afterSet', [propName, value, _lastValue2]);
methods.emit('__afterSet::' + propName, [value, _lastValue2]);
return value;
}
} : {}
)
);
});
}
};
var method;
for (method in methods) {
if (private_methods.indexOf(method) === -1) {
((bind_method)(method));
}
}
};
methods.crudEvents = function () {
return crudEvents.apply(methods, arguments);
};
if (typeof(builder) === "function") {
if (builder(config, vars, methods, public_methods, private_methods) !== false) {
crudEvents(methods, public_methods, private_methods);
} else {
return methods;
}
} else {
crudEvents(methods, public_methods, private_methods);
}
return public_methods;
});
return window.ApplicationPrototype;
})((function () {
if (typeof(window) !== "undefined") { return window;
// @ts-ignore
} else if (typeof(global) !== "undefined") { return global;
// @ts-ignore
} else if (typeof(APP_BUILDER_GLOBAL) === "undefined") {
throw new Error("Define APP_BUILDER_GLOBAL as global reference");
} else {
// @ts-ignore
return APP_BUILDER_GLOBAL;
}
})()));
if (isNode()) {
module.exports = ApplicationPrototype;
}