This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sApi.js
executable file
·232 lines (210 loc) · 6.4 KB
/
sApi.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
/**
* $s-API
*
* @version 202203.01
*/
(function (w, d, pvn)
{
/**
* @typedef $s
* @type {{extend: extend, onApiReady: onApiReady}}
*/
let $s = {
//readyFunctions are deleted after execution
_rF: [],
//placeholder
_includeModulesPlaceHolder: {},
//to check which version of the api is running
version: 202203.01,
/**
* extends smdApi with object
*
* @typedef $s.extend
* @param object
*/
extend: function(object)
{
let completeNewApiName = [];
/**
* Goes through smd or a subobject and adds newObjectToAdd to it.
*
* @param objectToParse
* @param newObjectToAdd
* @param completeNewApiName
* @private
*/
function _parseSObject(objectToParse, newObjectToAdd, completeNewApiName) {
for (let i in newObjectToAdd) {
completeNewApiName.push(i);
if (typeof objectToParse[i] === "undefined") {
objectToParse[i] = newObjectToAdd[i];
$s.ready(function() {
$s._isapi(objectToParse, i, completeNewApiName);
});
} else if (typeof newObjectToAdd[i] === "object" && Object.keys(newObjectToAdd[i]).length > 0) {
_parseSObject(objectToParse[i], newObjectToAdd[i], completeNewApiName);
}
}
}
_parseSObject($s, object, completeNewApiName);
},
/**
* Workaround if body not fully loaded...
*
* @param event
* @returns {boolean}
* @private
*/
_dispatchReadyEvent: function (event)
{
if (document.body !== null) {
document.body.dispatchEvent(event);
} else {
$s.ready(function() {
$s._dispatchReadyEvent(event);
});
}
return true;
},
/**
* Initializes an api on $s or on a subobject of $s
*
* @param api
* @param apiName
* @param completeNewApiName
* @private
*/
_isapi: function(api, apiName, completeNewApiName)
{
let typeOfApiName = typeof api[apiName];
if (typeOfApiName !== "undefined" && !(api[apiName] instanceof HTMLElement) && api[apiName].constructor !== Array) {
/**
* creates an CustomEvent dispatch and calls _isapi on all sub elements
* @param _apiToSetReady
* @param _completeNewApiName
* @private
*/
var _readyFunction = function(_apiToSetReady, _completeNewApiName)
{
_apiToSetReady._objectReady = true;
let event = new CustomEvent("$s." + _completeNewApiName.join(".") + "::ready");
$s._dispatchReadyEvent(event);
if (typeof _apiToSetReady === "object" && _apiToSetReady.constructor !== Array && Object.keys(_apiToSetReady).length > 0) {
for (let i in _apiToSetReady) {
//All done... execute the waiting functions...
if (_apiToSetReady[i] !== null && i.substr(0,1) !== "_" && (typeof _apiToSetReady[i] === 'object' || typeof _apiToSetReady[i] === 'function')) {
_completeNewApiName.push(i);
/*
timing fuckup in javascript ?! needs to be debugged;
if there is no setTimeout here, subobject like smd.ui.loadMoreDatasets
could not call smd.onApiReady or smd.userSettings.exec inside the init() function.
*/
setTimeout(function() { $s._isapi(_apiToSetReady, i, _completeNewApiName);},1);
}
}
}
};
// must be var! let will not contain the right return of the init function in chrome
var _makeAutoObjectReady = true;
// just objects will get the possability to make a custom init function...
// on functions we will automaticly generate a CustomEvent dispatch...
if (typeOfApiName === "object") {
if (typeof api[apiName]._objectReady === "undefined") {
api[apiName]._objectReady = false;
}
if (api[apiName]._objectReady !== true) {
if (typeof api[apiName].init === "function") {
_makeAutoObjectReady = api[apiName].init();
delete (api[apiName].init);
}
} else {
_makeAutoObjectReady = false;
}
}
// CustomEvents can only be dispatched if the dom is ready.
// So if there is _readyFunction in the $s-Object,
// the dom is not ready and the CustomEvent dispatch must wait...
if (_makeAutoObjectReady !== false) {
if (typeof $s._rF !== "undefined") {
$s._rF.push(function ()
{
_readyFunction(api[apiName], completeNewApiName);
});
} else {
_readyFunction(api[apiName], completeNewApiName);
}
}
}
},
/**
* Checks if apiName (function or object) is registered and initialiszed and executes callback
*
* @typedef $s.onApiReady
* @param apiName
* @param callback
*/
onApiReady: function(apiName, callback)
{
let splittedApiName = apiName.split(".");
// The real magic is done here.
// This function goes deeper in each step of the api-chain.
function _onApiReady(api, apiStackToCheck, apiName)
{
let apiToCheck = apiStackToCheck.splice(0, 1);
// Are there any chain segements to go deeper?
if (apiStackToCheck.length === 0) { // no, we must check at the current depth.
if (
(typeof api[apiToCheck] === "object" && api[apiToCheck]._objectReady === true) ||
typeof api[apiToCheck] === "function"
) {
// there is a object or a function with this name (apiToCheck) and it is ready.
callback();
} else {
// there isn't a function or an object or the object isn't ready => CustomEvent.
if (typeof $s._rF !== "undefined") {
$s._rF.push(function ()
{
document.body.addEventListener("$s." + apiName + "::ready", callback);
});
} else {
document.body.addEventListener("$s." + apiName + "::ready", callback);
}
}
}
if (apiStackToCheck.length > 0) {
// we must go deeper...
if (typeof api[apiToCheck] === "object") {
// the object is there... lets do deeper
_onApiReady(api[apiToCheck], apiStackToCheck, apiName);
} else if (typeof api[apiToCheck] === "undefined") {
// the object isn't there => CustomEvent...
document.body.addEventListener("$s." + apiName + "::ready", callback);
}
}
}
_onApiReady($s, splittedApiName, apiName);
}
};
/**
* Initializes all ready functions of $s' subobjects.
*/
$s.ready(function()
{
for (let i in $s._rF) {
if (!isNaN(i) && typeof $s._rF[i] === "function") {
$s._rF[i]();
}
}
delete($s._rF);
});
/**
* Initializes all subobjects of $s.
*/
for (let i in $s) {
if (typeof $s[i] === "object" && i !== "_rF") {
$s._isapi($s, i, [i]);
}
}
// default name is $s
w[pvn || "$s"] = $s;
}(window, document));