1
|
|
|
var Apiunit = function () { |
2
|
|
|
|
3
|
|
|
// this.router = router; |
4
|
|
|
this.cfg = {}; |
5
|
|
|
this.cfg.router = {}; |
6
|
|
|
this.cfg.event = {}; |
7
|
|
|
this.cfg.listener = {}; |
8
|
|
|
this.cfg.env = {}; |
9
|
|
|
|
10
|
|
|
// this.config = config; |
11
|
|
|
|
12
|
|
|
this.included = []; |
13
|
|
|
|
14
|
|
|
this.success = function (elem) { |
15
|
|
|
console.log("Api Unit Success".elem); |
|
|
|
|
16
|
|
|
}; |
17
|
|
|
|
18
|
|
|
this.error = function (elem) { |
|
|
|
|
19
|
|
|
console.error("Api Unit Problem"); |
20
|
|
|
}; |
21
|
|
|
|
22
|
|
|
var apiunit = this; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
this.listener = function (listener) { |
26
|
|
|
|
27
|
|
|
apiunit.cfg.listener = listener; |
28
|
|
|
|
29
|
|
|
return this; |
30
|
|
|
}; |
31
|
|
|
|
32
|
|
|
this.env = function (event) { |
33
|
|
|
apiunit.cfg.env = event; |
34
|
|
|
apiunit.loadPlugin(obj, error, success); |
|
|
|
|
35
|
|
|
|
36
|
|
|
return this; |
37
|
|
|
}; |
38
|
|
|
|
39
|
|
|
this.event = function (event) { |
40
|
|
|
apiunit.cfg.event = event; |
41
|
|
|
apiunit.loadPlugin(obj, error, success); |
|
|
|
|
42
|
|
|
|
43
|
|
|
return this; |
44
|
|
|
}; |
45
|
|
|
|
46
|
|
|
this.loadEvent = function (obj, error, success) { |
47
|
|
|
|
48
|
|
|
if (typeof success !== 'function') { |
49
|
|
|
success = this.success; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
if (typeof error !== 'function') { |
52
|
|
|
error = this.error; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
obj = this.fromJsonStringToObj(obj); |
56
|
|
|
|
57
|
|
|
var listener = this.cfg.listener; |
58
|
|
|
if (typeof obj !== 'object') { |
59
|
|
|
console.error('! apiunit. listener', listener); |
60
|
|
|
} |
61
|
|
|
var exe = {}; |
|
|
|
|
62
|
|
|
|
63
|
|
|
if (typeof obj === 'object') { |
64
|
|
|
//console.log('obj:', obj); |
65
|
|
|
|
66
|
|
|
for (var source in obj) { |
|
|
|
|
67
|
|
|
|
68
|
|
|
var data = obj[source]; |
69
|
|
|
|
70
|
|
|
console.log('source data', source, data); |
|
|
|
|
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
exe = listener[source](data); |
74
|
|
|
console.log('apiunit.router[source] =', source, exe); |
75
|
|
|
} catch (err) { |
76
|
|
|
console.error('! apiunit.router[source]', source, err, obj); |
77
|
|
|
} |
78
|
|
|
// } |
79
|
|
|
//console.log(target, type, value); |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
|
|
console.error('apiunit obj: is not object:', obj); |
83
|
|
|
} |
84
|
|
|
// console.log(target, type, value); |
85
|
|
|
// } |
86
|
|
|
console.log('apiunit.included:', apiunit.included); |
87
|
|
|
|
88
|
|
|
return this; |
89
|
|
|
}; |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
this.router = function (router, error, success) { |
|
|
|
|
93
|
|
|
// if (typeof success !== 'function') { |
94
|
|
|
// success = this.success; |
95
|
|
|
// } |
96
|
|
|
// if (typeof error !== 'function') { |
97
|
|
|
// error = this.error; |
98
|
|
|
// } |
99
|
|
|
apiunit.cfg.router = router; |
100
|
|
|
|
101
|
|
|
return this; |
102
|
|
|
}; |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
this.url = function (path, error, success) { |
106
|
|
|
if (typeof success !== 'function') { |
107
|
|
|
success = this.success; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
if (typeof error !== 'function') { |
110
|
|
|
error = this.error; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
if (typeof path === 'string') { |
113
|
|
|
this.loadJSON(path, function (obj) { |
114
|
|
|
// Parse JSON string into object |
115
|
|
|
// console.log('obj', obj); |
116
|
|
|
apiunit.loadPlugin(obj); |
117
|
|
|
}); |
118
|
|
|
} else { |
119
|
|
|
for (var i in path) { |
|
|
|
|
120
|
|
|
this.loadJSON(path[i], function (obj) { |
121
|
|
|
// Parse JSON string into object |
122
|
|
|
// console.log('obj', obj); |
123
|
|
|
apiunit.loadPlugin(obj); |
124
|
|
|
}); |
125
|
|
|
// apiunit.loadPlugin(obj[i]); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return this; |
130
|
|
|
}; |
131
|
|
|
|
132
|
|
|
this.json = function (obj, delay, error, success) { |
133
|
|
|
// if (typeof success !== 'function') { |
134
|
|
|
// success = this.success; |
135
|
|
|
// } |
136
|
|
|
// if (typeof error !== 'function') { |
137
|
|
|
// error = this.error; |
138
|
|
|
// } |
139
|
|
|
// console.log(typeof delay); |
140
|
|
|
// return this; |
141
|
|
|
|
142
|
|
|
if (typeof delay === 'number') { |
143
|
|
|
// var delay = 100; |
144
|
|
|
setTimeout(function () { |
145
|
|
|
apiunit.loadPlugin(obj, error, success); |
146
|
|
|
}, |
147
|
|
|
delay); |
148
|
|
|
} else { |
149
|
|
|
apiunit.loadPlugin(obj, error, success); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return this; |
153
|
|
|
}; |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
this.loadJSON = function (file, callback) { |
157
|
|
|
var xobj = new XMLHttpRequest(); |
158
|
|
|
xobj.overrideMimeType("application/json"); |
159
|
|
|
xobj.open('GET', file, true); |
160
|
|
|
// Replace 'my_data' with the path to your file |
161
|
|
|
xobj.onreadystatechange = function () { |
162
|
|
|
if (xobj.readyState === 4 && xobj.status === 200) { |
163
|
|
|
// Required use of an anonymous callback |
164
|
|
|
// as .open() will NOT return a value but simply returns undefined in asynchronous mode |
165
|
|
|
callback(xobj.responseText); |
166
|
|
|
} else { |
167
|
|
|
console.error(file, xobj); |
168
|
|
|
} |
169
|
|
|
}; |
170
|
|
|
xobj.send(null); |
171
|
|
|
return this; |
172
|
|
|
}; |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
this.loadPlugin = function (obj, error, success) { |
176
|
|
|
|
177
|
|
|
if (typeof success !== 'function') { |
178
|
|
|
success = this.success; |
|
|
|
|
179
|
|
|
} |
180
|
|
|
if (typeof error !== 'function') { |
181
|
|
|
error = this.error; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
obj = this.fromJsonStringToObj(obj); |
185
|
|
|
|
186
|
|
|
var router = this.cfg.router; |
187
|
|
|
var exe = {}; |
|
|
|
|
188
|
|
|
// for (var target in obj) { |
189
|
|
|
|
190
|
|
|
//type = obj[target]; |
191
|
|
|
//value = obj[target][type]; |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
//console.log(typeof obj[target]); |
195
|
|
|
//console.log(obj[target]); |
196
|
|
|
//console.log('target:', target); |
197
|
|
|
|
198
|
|
|
// router = new Router(target, error, success); |
199
|
|
|
|
200
|
|
|
if (typeof obj === 'object') { |
201
|
|
|
//console.log('obj:', obj); |
202
|
|
|
|
203
|
|
|
for (var source in obj) { |
|
|
|
|
204
|
|
|
|
205
|
|
|
//console.log('source:', source); |
206
|
|
|
//console.log('obj[source]:', typeof obj[source]); |
207
|
|
|
//console.log('obj[source]:', obj[source]); |
208
|
|
|
//for (i = 0; i < obj.length; i++) { |
209
|
|
|
|
210
|
|
|
// console.log(typeof obj[source]); |
211
|
|
|
// console.log(obj[source]); |
212
|
|
|
|
213
|
|
|
// if (typeof obj[source] !== 'object') { |
214
|
|
|
// obj[source] = [obj[source]]; |
215
|
|
|
// } |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
// for (var i in obj[source]) { |
219
|
|
|
|
220
|
|
|
var data = obj[source]; |
221
|
|
|
|
222
|
|
|
console.log('source data', source, data); |
|
|
|
|
223
|
|
|
|
224
|
|
|
try { |
225
|
|
|
exe = router[source](data); |
226
|
|
|
console.log('apiunit.router[source] =', source, exe); |
227
|
|
|
} catch (err) { |
228
|
|
|
console.error('! apiunit.router[source]', source, err, obj); |
229
|
|
|
} |
230
|
|
|
// } |
231
|
|
|
//console.log(target, type, value); |
232
|
|
|
} |
233
|
|
|
} else { |
234
|
|
|
console.error('apiunit obj: is not object:', obj); |
235
|
|
|
} |
236
|
|
|
// console.log(target, type, value); |
237
|
|
|
// } |
238
|
|
|
console.log('apiunit.included:', apiunit.included); |
239
|
|
|
|
240
|
|
|
//return output; |
241
|
|
|
|
242
|
|
|
// for(i = 0; i < obj.length; i++) { |
243
|
|
|
//out += '<a href="' + arr[i].data + '">' + arr[i].display + '</a><br>'; |
244
|
|
|
//var router = new IncludeToId('home-plugins'); |
245
|
|
|
// console.log(obj[i]); |
246
|
|
|
// } |
247
|
|
|
//document.getElementById("id01").innerHTML = out; |
248
|
|
|
return this; |
249
|
|
|
}; |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
this.fromJsonStringToObj = function (obj) { |
253
|
|
|
if (typeof obj === 'string') { |
254
|
|
|
try { |
255
|
|
|
//const user = JSON.parse(data) |
256
|
|
|
//console.log('fromJsonStringToObj before', obj); |
257
|
|
|
obj = JSON.parse(obj); |
258
|
|
|
console.log('fromJsonStringToObj after', obj); |
|
|
|
|
259
|
|
|
} catch (err) { |
260
|
|
|
console.error('!fromJsonStringToObj błąd formatu JSON, sprawdź czy nie ma przecinka na koncu ostatniego elementu listy', err, obj); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
return obj; |
264
|
|
|
}; |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
}; |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
//var loadToHeader = new IncludeToId('home-plugins'); |
271
|
|
|
/* |
272
|
|
|
var setting = { |
273
|
|
|
"id": "home-plugins", |
274
|
|
|
"style": ["/visitor/newsletter/css/black.css"], |
275
|
|
|
"script": ["/visitor/newsletter/js/foot.js"], |
276
|
|
|
"html": ["/visitor/newsletter/plugin/create.html", "/visitor/newsletter/plugin/delete.html"], |
277
|
|
|
}; |
278
|
|
|
*/ |
279
|
|
|
// TODO : id / tag, class |
280
|
|
|
// var setting = { |
281
|
|
|
// "head": { |
282
|
|
|
// "style": "/visitor/newsletter/css/black.css", |
283
|
|
|
// "script": "/visitor/newsletter/js/foot.js" |
284
|
|
|
// }, |
285
|
|
|
// "home-plugins": { |
286
|
|
|
// "html": [ |
287
|
|
|
// "/visitor/home/plugin/messages.html", |
288
|
|
|
// "/visitor/newsletter/plugin/create.html", |
289
|
|
|
// "/visitor/newsletter/plugin/delete.html" |
290
|
|
|
// ] |
291
|
|
|
// }, |
292
|
|
|
// }; |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
/* |
296
|
|
|
if (source === 'docs') { |
297
|
|
|
// for (var doc_name in url) { |
298
|
|
|
// var doc_val = obj[source][i][doc_name]; |
299
|
|
|
router.comment(i, url); |
300
|
|
|
// } |
301
|
|
|
} else if (source === 'image') { |
302
|
|
|
if (!exist_in_apiunit) { |
303
|
|
|
router.image(url); |
304
|
|
|
apiunit.included.push(url); |
305
|
|
|
} else { |
306
|
|
|
console.error('!exist: ', url); |
307
|
|
|
} |
308
|
|
|
} else if (source === 'html') { |
309
|
|
|
if (!exist_in_apiunit) { |
310
|
|
|
router.html(url); |
311
|
|
|
apiunit.included.push(url); |
312
|
|
|
} else { |
313
|
|
|
console.error('!exist: ', url); |
314
|
|
|
} |
315
|
|
|
} else if (source === 'script') { |
316
|
|
|
if (!exist_in_apiunit) { |
317
|
|
|
router.script(url); |
318
|
|
|
apiunit.included.push(url); |
319
|
|
|
} else { |
320
|
|
|
console.error('!exist: ', url); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
} else if (source === 'script-onload') { |
324
|
|
|
if (!exist_in_apiunit) { |
325
|
|
|
router.script_onload(url); |
326
|
|
|
apiunit.included.push(url); |
327
|
|
|
} else { |
328
|
|
|
console.error('!exist: ', url); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
} else if (source === 'script-delay') { |
332
|
|
|
if (!exist_in_apiunit) { |
333
|
|
|
router.script_delay(url); |
334
|
|
|
apiunit.included.push(url); |
335
|
|
|
} else { |
336
|
|
|
console.error('!exist: ', url); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
} else if (source === 'style') { |
340
|
|
|
if (!exist_in_apiunit) { |
341
|
|
|
router.style(url); |
342
|
|
|
apiunit.included.push(url); |
343
|
|
|
} else { |
344
|
|
|
console.error('!exist: ', url); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
} else if (source === 'style-onload') { |
348
|
|
|
if (!exist_in_apiunit) { |
349
|
|
|
router.style_onload(url); |
350
|
|
|
apiunit.included.push(url); |
351
|
|
|
} else { |
352
|
|
|
console.error('!exist: ', url); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
} else { |
356
|
|
|
|
357
|
|
|
} |
358
|
|
|
*/ |
359
|
|
|
|
360
|
|
|
|