| 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 |
1
1
14
19
19
7
4
4
4
4
4
4
4
3
21
23
21
4
4
4
4
4
4
4
21
21
2
21
19
2
21
15
18
27
27
27
1
27
1
27
2
1
1
27
27
1
27
23
23
23
1
1
1
1
1
1
1
1
1
27
3
3
3
24
19
19
18
1
1
17
3
1
1
3
1
3
17
1
1
17
18
18
19
14
14
1
14
1
14
1
14
2
2
1
1
1
14
1
1
1
6
6
1
1
1
14
14
2
14
1
1
| /*global require, define, document, $, console, window */
/*jslint nomen: true, debug: true, bitwise: true, todo: true*/
/**
* @module App
*/
define([
"framework",
"underscore",
"backbone",
"modules/mainPack"
],
/**
* Widget constructor class
* @name ElascticnodeWidget
* @requires framework {Object} jQuery|Zepto
* @requires underscore {Object} Underscore
* @requires backbone {Object} Backbone
* @requires module:mainPack {Object} Loader dependent modules
* @class Widget
* @param config {Object} Init config object
* @constructor
* @return {Object} Widget constructor
* @see module:App
*/
function ($, _, Backbone) {
"use strict";
return function (config) {
/**
* @name module:App#config
* @memberof module:App
* @inner
* @example config = {
* type: "exams",
* interval: 10,
* host: "school.elasticnode.ru",
* container: "body",
* title: "My title",
* }
* */
var self = this,
status = 0,
id = 'enw' + (100000 * Math.random() | 0),
path,
pack,
view = {},
setStatus = function (val) {
/*
* -1 error
* 0 waiting
* 1 rendered
* 2 data fetched
*/
status = view.status = val;
return val;
},
/**
* @name module:App#renderer
* @description View render function
* @memberof module:App
* @function renderer
* @inner
*/
renderer,
/**
* @name module:App#flushView
* @description Assigned view destructor
* @memberof module:App
* @function flushView
* @param view {Object} Backbone.View instance
* @inner
*/
flushView = function (view) {
if (view && view.$el) {
view.undelegateEvents();
view.$el.unbind();
//Removes view from DOM
view.$el.remove();
view.remove();
Backbone.View.prototype.remove.call(view);
view = {};
return true;
}
return false;
},
/**
* @name module:App#loadRenderer
* @description Loads the view.render instance as renderer
* @param path {String} Module related path
* @memberof module:App
* @function loadRenderer
* @inner
*/
loadRenderer = function (path, pack) {
var cb = function () {
if (path) {
cb = function (viewConstructor) {
/* istanbul ignore else */
Eif (viewConstructor) {
// Previous view annihilation
flushView(view);
setStatus(0);
view = new (viewConstructor.extend({
el: config.$container.get(0),
id: id,
status: status,
host: config.host,
title: config.title || viewConstructor.prototype.title,
setStatus: setStatus,
delayedSetModeArgs: [].concat(config.mode || view.delayedSetModeArgs)
}))();
renderer = $.proxy(view.render, view.self);
renderer();
}
return renderer;
};
require([path], cb);
return cb;
}
return;
};
// If view module is a part of some package
if (pack) {
require([pack], cb);
} else {
cb();
}
return cb;
},
/**
* @name module:App#Loader
* @description Returns or creates a new one renderer instance
* @memberof module:App
* @function Loader
* @inner
*/
Loader = function () {
return function () {
return loadRenderer(path, pack);
};
},
/**
* @name module:App#validateConfig
* @description Validates config object and handles its errors
* @memberof module:App
* @function validateConfig
* @param [_config] {Object} Config object. If undefined actual config will be used
* @inner
* @returns {Object} Correct config or error object
*/
validateConfig = function (_config) {
_config = _.extend({}, config, _config);
var errCode = null,
allowedTypes = ["gia", "ege", "sou", "population"];
// At first we need to check out if widget type is allowed
if (!!_config.type && !_.contains(allowedTypes, _config.type)) {
errCode = 1;
}
// If host is undefined
if (!_config.host) {
errCode = 3;
}
// Self-update interval should be correct
if (_config.interval && _config.interval > 0) {
if (_config.interval < 5) {
_config.interval = 5;
console.warn("Too short interval. It should be at least 5m or 0 if you don't need autoupdate");
}
}
_config.$container = $(_config.container).eq(0);
// Parent existence check
if (!_config.$container.length) {
errCode = 2;
}
switch (_config.type) {
case "gia":
path = 'views/exams/giaExamsView';
pack = "modules/examsModule";
break;
case "ege":
path = 'views/exams/examsView';
pack = "modules/examsModule";
break;
case "sou":
path = 'views/sou/souView';
pack = "modules/souModule";
break;
case "population":
path = 'views/population/populationView';
pack = "modules/populationModule";
break;
}
if (errCode) {
setStatus(-1);
console.warn("Invalid config. See elasticnode.ru/widgets/#error" + errCode + " for details");
return {error: errCode};
}
return _config;
},
/**
* @name module:App#applyConfig
* @description Applies new config to current widget
* @memberof module:App
* @function applyConfig
* @param [config] {Object} Config object
* @inner
*/
applyConfig = function (_config, view) {
_config = validateConfig(_config);
if (!_config.hasOwnProperty("error") && !_.isEqual(_config, config)) {
// console.log("Config looks fine (id: " + id + ")");
// In case of type mismatch widget should be completely re-rendered
if (config.type !== _config.type) {
config = _config;
renderer = new Loader();
} else {
// if rendered
if (view.$el) {
// It title was changed
if (_config.title && view.title !== _config.title) {
view.title = _config.title;
view.renderTitle();
}
// Container was changed
if (config.$container.get(0) !== _config.$container.get(0)) {
_config.$container.after(view.$el.detach());
}
// Todo: timeouts should be defined
view.interval = _config.interval;
}
// config.host influences on view.model
if (_config.host && config.host !== _config.host) {
view.host = _config.host;
/**
* @see module:defaultView#modelInit
*/
view.modelInit();
}
config = _config;
}
// Delayed mode set
view.delayedSetModeArgs = [].concat(_config.mode);
// view.render() link
renderer();
}
return _config;
};
renderer = new Loader();
// Public methods goes here
/**
* @name module:App#getStatus
* @description Returns current widget status
* @memberof module:App
* @function getStatus
*/
self.getStatus = function () {
return status;
};
/**
* @name module:App#getNode
* @description Returns view.$el if exists or null
* @memberof module:App
* @function getNode
* @returns {object|null} $container or null
*/
self.getNode = function () {
return view.$el || null;
};
/**
* @name module:App#setConfig
* @description Sets a new widget config
* @memberof module:App
* @function setConfig
* @param config {Object} Config object
*/
self.setConfig = function (_config) {
return applyConfig(_config, view);
};
/**
* @name module:App#setMode
* @description Asynchronous widget mode set
* @memberof module:App
* @see module:defaultView#setMode
* @function
* @param arguments {*} Any arguments
* @returns {boolean} True if .switchModeDelayed() method defined
*/
self.setMode = function () {
view.delayedSetModeArgs = arguments;
if (view.switchModeDelayed) {
view.switchModeDelayed();
return true;
}
return false;
};
// Widget destructor
/**
* @name module:App#destroy
* @description Widget annihilation
* @memberof module:App
* @function destroy
* @returns {string} Message
*/
self.destroy = function () {
flushView();
var key;
//TODO clearTimeout
for (key in this) {
/* istanbul ignore else */
Eif (this.hasOwnProperty(key)) {
delete this[key];
}
}
self.ptototype = {};
setStatus(-1);
// NB: destroyed object cannot be used anymore
return "Completely destroyed view and object methods (id: " + id + ")";
};
// Alive!
applyConfig(undefined, view);
// UNIT TEST HELPERS
self.__testonly__ = {
applyConfig: applyConfig,
validateConfig: validateConfig,
setView: function (_view) { view = _view; return view; },
getView: function () {return view; },
getRenderer: function () {return renderer; },
getConfig: function () {return config; },
Loader: Loader,
loadRenderer: loadRenderer,
flushView: flushView,
setStatus: setStatus
};
};
}); |