-
Notifications
You must be signed in to change notification settings - Fork 1
/
Wirup.js
395 lines (380 loc) · 13.2 KB
/
Wirup.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"use strict";
var wirup = function () {};
wirup.prototype = (function () {
var _getElement = function (element_id) {
return document.getElementById(element_id);
},
_wijax = (callType, url, contentType, callback) => {
return new Promise((resolve, reject) => {
const httpRequest = new XMLHttpRequest();
httpRequest.open(callType, url, true);
httpRequest.setRequestHeader("Content-Type", contentType);
httpRequest.setRequestHeader("cache-control", "no-cache");
httpRequest.setRequestHeader("expires", "0");
httpRequest.setRequestHeader("expires", "Tue, 01 Jan 1980 1:00:00 GMT");
httpRequest.setRequestHeader("pragma", "no-cache");
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState !== 4) {
return;
}
if (httpRequest.status === 200) {
resolve(callback(httpRequest.responseText));
} else {
const error =
httpRequest.statusText || "Your ajax request threw an error.";
reject(error);
}
};
httpRequest.send();
});
},
_appLocation = window.location.pathname,
_views,
_registerViews = () => {
return _wijax(
"GET",
"views/views.json",
"application/json; charset=UTF-8",
_getViews
);
},
_getViews = (data) => {
_views = JSON.parse(data)["views"];
},
_getTemplate = (target) => {
return new Promise((resolve, reject) => {
let _index;
// Fallback mechanism: if _appLocation is not defined, default to root "/"
const _url = _appLocation || "/";
// Handle edge cases where _views might be undefined or empty
if (!_views || _views.length === 0) {
reject("Views are undefined or empty, cannot load template.");
return;
}
// Find the index of the view matching the current URL (_appLocation)
for (let i = 0; i < _views.length; i++) {
if (_views[i]["url"] === _url) {
_index = i;
break;
}
}
// Handle the case where the URL does not match any view
if (_index === undefined) {
console.warn(
`No view found for the URL: ${_url}. Redirecting to default view.`
);
_index = 0; // You can redirect to a "Not Found" page or the default page.
}
// Fill the view with the found index
_fillView(_index, target)
.then(() => resolve())
.catch((error) => {
console.error(`Error filling view for URL ${_url}: ${error}`);
reject(`Failed to fill view for ${_url}`);
});
}).catch((error) => {
console.error(`Error in _getTemplate: ${error}`);
console.error("View may not have been defined.");
});
},
_fillView = function (index, targetElementId) {
const htmlFilePath = "views/" + _views[index]["viewFile"];
return new Promise((resolve, reject) => {
fetch(htmlFilePath + "?")
.then((response) => {
if (response.ok) {
return response.text();
} else {
reject(`Failed to load HTML view: ${htmlFilePath}`);
}
})
.then((htmlContent) => {
const targetElement = _getElement(targetElementId);
if (targetElement) {
targetElement.innerHTML = htmlContent;
resolve(); // Resolve the promise if the HTML content is successfully injected
} else {
reject(`Target element with ID "${targetElementId}" not found`);
}
})
.catch((error) => {
reject(error);
});
});
},
_bindRouter = () => {
window.onpopstate = (event) => {
_appLocation = window.location.pathname; // Handle back/forward button
_init();
};
},
_navigateTo = (url) => {
history.pushState(null, null, url); // Update URL without reloading
_appLocation = url; // Update internal app location
_getTemplate("contentBody").then(() => {
_renderViewComponents();
});
},
_dataStore = {},
_components = {},
_loadObserver = {},
_registerComponent = (componentName, template) => {
_components[componentName] = template;
},
_registerData = (dataItemName, value) => {
Object.defineProperty(_dataStore, dataItemName, {
get: function () {
return this["_" + dataItemName];
},
set: function (newValue) {
var _currentVal = this["_" + dataItemName];
this["_" + dataItemName] = newValue;
_updateComponentsByDataSourceName(dataItemName);
},
});
_dataStore[dataItemName] = value;
},
_onLoad = (value) => {
Object.defineProperty(_loadObserver, "trigger", {
get: function () {
return this["_trigger"];
},
set: function (newValue) {
let _currentVal = this["_trigger"];
this["_trigger"] = newValue;
window[newValue]();
},
});
_loadObserver["trigger"] = value;
},
_addData = (dataItemName, newData) => {
let _tempArray = [];
_tempArray = _dataStore[dataItemName];
_tempArray.push(newData);
_dataStore[dataItemName] = _tempArray;
_tempArray = [];
},
_findIndexByKey = function (dataSourceName, keyName, keyValue) {
if (!this.dataStore[dataSourceName]) {
throw new Error(
`Data source '${dataSourceName}' not found in dataStore`
);
}
const index = this.dataStore[dataSourceName].findIndex((dataItem) => {
return dataItem[keyName] === keyValue;
});
return index;
},
_updateData = (dataItemName, position, newData) => {
let _tempArray = [];
_tempArray = _dataStore[dataItemName];
position = position - 1 < 0 ? 0 : position - 1;
_tempArray[position] = newData;
_dataStore[dataItemName] = _tempArray;
_tempArray = [];
},
_removeData = (dataSourceName, position) => {
let _tempArray = [];
_tempArray = _dataStore[dataSourceName];
position = position - 1 < 0 ? 0 : position - 1;
_tempArray.splice(position, 1);
_dataStore[dataSourceName] = _tempArray;
_tempArray = [];
},
_renderViewComponents = () => {
return new Promise(function (resolve, reject) {
let _viewComponents =
_getElement("contentBody").querySelectorAll("[datasource]");
[].forEach.call(_viewComponents, (_component) => {
_component.innerHTML = _buildComponent(
_component.tagName.toLowerCase(),
_component.getAttribute("datasource")
);
});
_registerAction("Switched View", "CBody", "NA");
resolve();
});
},
_updateComponentsByDataSourceName = (dataSourceName) => {
_getElement("contentBody")
.querySelectorAll('[datasource="' + dataSourceName + '"]')
.forEach((elem) => {
elem.innerHTML = _buildComponent(
elem.tagName.toLowerCase(),
dataSourceName
);
_registerAction(
"Updated Data.",
elem.tagName.toLowerCase(),
"No Comment."
);
});
},
_buildComponent = (componentName, datasourceName) => {
let output = [];
const dataSource = _dataStore[datasourceName];
const dataSourceType = typeof dataSource;
switch (dataSourceType) {
case "object":
if (Array.isArray(dataSource)) {
// Handle the case where it's an array
output = dataSource.map((item) => {
return _components[componentName](item);
});
} else {
// Handle the case where it's an object (non-array)
output.push(_components[componentName](dataSource));
}
break;
case "string":
// Handle the case where it's a string
output.push(_components[componentName](dataSource));
break;
default:
throw new Error(`Unsupported data type: ${dataSourceType}`);
}
// If output is an array, join it. Otherwise, return it directly.
return Array.isArray(output) ? output.join("") : output;
},
_buildComponents = (componentNames, datasourceNames) => {
let output = [];
componentNames.forEach((componentName, index) => {
const dataSource = _dataStore[datasourceNames[index]];
const dataSourceType = typeof dataSource;
switch (dataSourceType) {
case "object":
if (Array.isArray(dataSource)) {
output = output.concat(
dataSource.map((item) => _components[componentName](item))
);
} else {
output.push(_components[componentName](dataSource));
}
break;
case "string":
output.push(_components[componentName](dataSource));
break;
default:
throw new Error(`Unsupported data type: ${dataSourceType}`);
}
});
// Return joined output if it's an array
return Array.isArray(output) ? output.join("") : output;
},
_jsonize = function (objectName) {
try {
return JSON.parse(window[objectName]);
} catch (e) {
return window[objectName];
}
},
_loadScript = function (scriptPath) {
return new Promise((resolve, reject) => {
let _newScript = document.createElement("script");
_newScript.type = "text/javascript";
_newScript.src = scriptPath;
document.getElementsByTagName("head")[0].appendChild(_newScript);
_newScript.onload = () => {
resolve();
};
_newScript.onabort = () => {
reject();
};
});
},
_loadScriptsFromFolder = (folderPath, scriptNames) => {
return new Promise((resolve, reject) => {
const promises = scriptNames.map((scriptName) => {
return new Promise((resolveScript, rejectScript) => {
const _newScript = document.createElement("script");
_newScript.type = "text/javascript";
_newScript.src = `${folderPath}/${scriptName}`;
document.getElementsByTagName("head")[0].appendChild(_newScript);
_newScript.onload = () => resolveScript();
_newScript.onerror = () =>
rejectScript(`Failed to load script: ${scriptName}`);
});
});
Promise.all(promises)
.then(resolve)
.catch((err) => {
console.error(`Error loading scripts from folder: ${err}`);
reject(err);
});
}).catch((error) => {
console.error(`Error in _loadScriptsFromFolder: ${error}`);
});
},
_profile = "",
_registerAction = (actionName, actionElement, comment) => {
let date = new Date();
let timestamp = date.getTime();
let action = {
name: actionName,
element: actionElement,
comment: comment,
profile: _profile,
timestamp: timestamp,
};
let _actions = localStorage.getItem("wuActions");
localStorage.setItem(
"wuActions",
_actions + "," + JSON.stringify(action)
);
},
_registerProfile = (profile) => {
_profile = profile;
},
_onContentLoad = (functionName) => {
return window[functionName]();
},
_renderAll = (config) => {
return new Promise(function (resolve, reject) {
let componentsFiles = ["components.js"];
if (
config?.components &&
Array.isArray(config.components) &&
config.components.length > 0
) {
componentsFiles = config.components;
}
_loadScriptsFromFolder("components", componentsFiles).then(() => {
_registerViews().then(() => {
_getTemplate("contentBody").then(() => {
_renderViewComponents().then(() => {
_bindRouter();
if (typeof config.callbackName !== "undefined") {
_onContentLoad(config.callbackName);
}
});
});
});
});
});
},
_init = (config) => {
_renderAll(config);
};
return {
wx: _getElement,
wijax: _wijax,
jsonize: _jsonize,
loadScript: _loadScript,
registerComponent: _registerComponent,
registerProfile: _registerProfile,
registerAction: _registerAction,
buildComponent: _buildComponent,
buildComponents: _buildComponents,
navigateTo: _navigateTo,
registerData: _registerData,
addData: _addData,
findIndexByKey: _findIndexByKey,
updateData: _updateData,
removeData: _removeData,
dataStore: _dataStore,
onLoad: _onLoad,
components: _components,
init: _init,
};
})();
window.wu = new wirup();