1 | /** |
||
2 | * Inji js core |
||
3 | * |
||
4 | * @author Alexey Krupskiy <[email protected]> |
||
5 | * @link http://inji.ru/ |
||
6 | * @copyright 2015 Alexey Krupskiy |
||
7 | * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
||
8 | */ |
||
9 | |||
10 | function Inji() { |
||
11 | this.options = { |
||
12 | appRoot: '' |
||
13 | }; |
||
14 | this.onLoadCallbacks = []; |
||
15 | this.loaded = false; |
||
16 | this.listeners = {}; |
||
17 | this.loadedScripts = {}; |
||
18 | } |
||
19 | |||
20 | Inji.prototype.onLoad = function (callback, start) { |
||
21 | if (typeof callback === 'function') { |
||
22 | if (this.loaded) { |
||
23 | callback(); |
||
24 | } else { |
||
25 | if (start) { |
||
26 | this.onLoadCallbacks.unshift(callback); |
||
27 | } else { |
||
28 | this.onLoadCallbacks.push(callback); |
||
29 | } |
||
30 | } |
||
31 | } |
||
32 | }; |
||
33 | Inji.prototype.startCallbacks = function () { |
||
34 | console.log('inji start onload'); |
||
0 ignored issues
–
show
Debugging Code
introduced
by
![]() |
|||
35 | var callback; |
||
36 | while (callback = this.onLoadCallbacks.shift()) { |
||
37 | if (typeof callback == 'function') { |
||
38 | callback(); |
||
39 | } |
||
40 | } |
||
41 | if (this.onLoadCallbacks.length != 0) { |
||
42 | this.startCallbacks(); |
||
43 | } |
||
44 | var indicator = document.getElementById('loading-indicator'); |
||
45 | if (indicator) { |
||
46 | indicator.style.display = 'none'; |
||
47 | } |
||
48 | inji.loaded = true; |
||
49 | console.log('inji start complete'); |
||
50 | }; |
||
51 | Inji.prototype.start = function (options) { |
||
52 | console.log('Inji start'); |
||
0 ignored issues
–
show
|
|||
53 | for (var key in options.compresedScripts) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
![]() |
|||
54 | inji.loadedScripts[options.compresedScripts[key]] = true; |
||
55 | } |
||
56 | this.options = options; |
||
57 | if (options.onLoadModules) { |
||
58 | this.onLoad(function () { |
||
59 | for (var key in options.onLoadModules) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
![]() |
|||
60 | if (typeof inji[key] == 'undefined') { |
||
61 | inji[key] = new window[key](); |
||
62 | if (typeof (inji[key].init) == 'function') { |
||
63 | console.log(key + ' init'); |
||
0 ignored issues
–
show
|
|||
64 | inji[key].init(); |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | }, true) |
||
69 | } |
||
70 | if (options.scripts.length > 0) { |
||
71 | this.loadScripts(options.scripts, 0); |
||
72 | } else { |
||
73 | inji.startCallbacks(); |
||
74 | } |
||
75 | }; |
||
76 | Inji.prototype.loadScripts = function (scripts, key) { |
||
77 | this.addScript(scripts[key], function () { |
||
78 | if (typeof (scripts[key].name) != 'undefined') { |
||
79 | inji.loadedScripts[scripts[key].file] = true; |
||
80 | if (typeof inji[scripts[key].name] == 'undefined') { |
||
81 | console.log('js ' + scripts[key].name + '(' + scripts[key].file + ') loaded'); |
||
0 ignored issues
–
show
|
|||
82 | inji[scripts[key].name] = new window[scripts[key].name](); |
||
83 | if (typeof (inji[scripts[key].name].init) == 'function') { |
||
84 | inji[scripts[key].name].init(); |
||
85 | } |
||
86 | } |
||
87 | } else if (typeof (scripts[key].file) != 'undefined') { |
||
88 | inji.loadedScripts[scripts[key].file] = true; |
||
89 | console.log('js ' + scripts[key].file + ' loaded'); |
||
90 | |||
91 | } else { |
||
92 | inji.loadedScripts[scripts[key]] = true; |
||
93 | console.log('js ' + scripts[key] + ' loaded'); |
||
94 | inji.event('loadScript', scripts[key]); |
||
95 | } |
||
96 | if (typeof (scripts[key + 1]) != 'undefined') { |
||
97 | inji.loadScripts(scripts, key + 1); |
||
98 | } else { |
||
99 | console.log('All scripts loaded'); |
||
100 | inji.startCallbacks(); |
||
101 | } |
||
102 | }); |
||
103 | }; |
||
104 | Inji.prototype.addScript = function (script, callback) { |
||
105 | var element = document.createElement('script'); |
||
106 | var src = ''; |
||
107 | if (typeof (script.file) != 'undefined') { |
||
108 | src = script.file; |
||
109 | } else { |
||
110 | src = script; |
||
111 | } |
||
112 | if (inji.loadedScripts[src]) { |
||
113 | if (typeof (callback) == 'function') { |
||
114 | callback(); |
||
115 | } |
||
116 | return true; |
||
117 | } |
||
118 | element.src = src; |
||
119 | element.type = 'text/javascript'; |
||
120 | if (typeof (callback) == 'function') { |
||
121 | element.onload = callback; |
||
122 | } |
||
123 | document.head.appendChild(element); |
||
124 | |||
125 | |||
126 | }; |
||
127 | Inji.prototype.on = function (eventType, callback) { |
||
128 | if (typeof this.listeners[eventType] == 'undefined') { |
||
129 | this.listeners[eventType] = []; |
||
130 | } |
||
131 | this.listeners[eventType].push(callback); |
||
132 | } |
||
133 | Inji.prototype.event = function (eventType, object) { |
||
134 | if (typeof this.listeners[eventType] != 'undefined') { |
||
135 | for (var key in this.listeners[eventType]) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
![]() |
|||
136 | this.listeners[eventType][key](eventType, object); |
||
137 | } |
||
138 | } |
||
139 | }; |
||
140 | Inji.prototype.randomString = function (length) { |
||
141 | if (!length) { |
||
142 | length = 20; |
||
143 | } |
||
144 | var text = ""; |
||
145 | var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
||
146 | |||
147 | for (var i = 0; i < length; i++) |
||
148 | text += chars.charAt(Math.floor(Math.random() * chars.length)); |
||
149 | |||
150 | return text; |
||
151 | }; |
||
152 | Inji.prototype.numberFormat = function (number, decimals, dec_point, thousands_sep) { |
||
153 | //// Format a number with grouped thousands |
||
154 | // |
||
155 | // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) |
||
156 | // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) |
||
157 | // + bugfix by: Michael White (http://crestidg.com) |
||
158 | |||
159 | var i, j, kw, kd, km; |
||
160 | |||
161 | // input sanitation & defaults |
||
162 | if (isNaN(decimals = Math.abs(decimals))) { |
||
163 | decimals = 2; |
||
164 | } |
||
165 | if (dec_point == undefined) { |
||
166 | dec_point = ","; |
||
167 | } |
||
168 | if (thousands_sep == undefined) { |
||
169 | thousands_sep = "."; |
||
170 | } |
||
171 | |||
172 | i = parseInt(number = (+number || 0).toFixed(decimals)) + ""; |
||
173 | |||
174 | if ((j = i.length) > 3) { |
||
175 | j = j % 3; |
||
176 | } else { |
||
177 | j = 0; |
||
178 | } |
||
179 | |||
180 | km = (j ? i.substr(0, j) + thousands_sep : ""); |
||
181 | kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep); |
||
182 | //kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : ""); |
||
183 | kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : ""); |
||
184 | |||
185 | |||
186 | return km + kw + kd; |
||
187 | }; |
||
188 | Inji.prototype.get = function (query) { |
||
189 | var element = document.querySelector(query); |
||
190 | if (element) { |
||
191 | return new function () { |
||
192 | this.element = element; |
||
193 | this.attr = function (name) { |
||
194 | for (var key in this.element.attributes) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
![]() |
|||
195 | var attr = element.attributes[key]; |
||
196 | if (attr.name == name) { |
||
197 | return attr.value; |
||
198 | } |
||
199 | } |
||
200 | return null; |
||
201 | }; |
||
202 | this.data = function (name) { |
||
203 | var data = this.attr('data-' + name); |
||
204 | try { |
||
205 | return JSON.parse(data); |
||
206 | } catch (e) { |
||
207 | return data; |
||
208 | } |
||
209 | |||
210 | } |
||
211 | }; |
||
212 | } |
||
213 | }; |
||
214 | Inji.prototype.parseQueryString = function (string) { |
||
215 | var result = {}; |
||
0 ignored issues
–
show
|
|||
216 | |||
217 | string = string.indexOf('?') === 0 ? string.substr(1) : string; |
||
218 | if (!string.length) { |
||
219 | return {}; |
||
220 | } |
||
221 | string = decodeURIComponent(string.replace(/\[[\"\']/g, '[').replace(/[\"\']\]/g, ']')); |
||
222 | var result = {}; |
||
223 | var params = string.split('&'); |
||
224 | for (var key in params) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
![]() |
|||
225 | var item = params[key]; |
||
226 | var itemPaths = item.match(/\[([^\]]*)\]/g); |
||
227 | if (itemPaths === null) { |
||
228 | result[item.split('=')[0]] = item.split('=')[1]; |
||
229 | continue; |
||
230 | } |
||
231 | var rootParam = item.substr(0, item.indexOf('[')); |
||
232 | if (typeof result[rootParam] == 'undefined') { |
||
233 | result[rootParam] = {}; |
||
234 | } |
||
235 | var resultCurPath = result[rootParam]; |
||
236 | for (var itemPathKey in itemPaths) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
![]() |
|||
237 | var itemPath = itemPaths[itemPathKey].replace('[', '').replace(']', ''); |
||
238 | if (itemPath == '') { |
||
239 | itemPath = Object.keys(resultCurPath).length; |
||
240 | } |
||
241 | if (typeof resultCurPath[itemPath] == 'undefined') { |
||
242 | resultCurPath[itemPath] = parseInt(itemPathKey) + 1 < itemPaths.length ? {} : item.split('=')[1]; |
||
243 | } |
||
244 | if (parseInt(itemPathKey) + 1 < itemPaths.length) { |
||
245 | resultCurPath = resultCurPath[itemPath]; |
||
246 | } |
||
247 | } |
||
248 | |||
249 | } |
||
250 | return result; |
||
251 | }; |
||
252 | var inji = new Inji(); |