81
|
1 |
/* |
|
2 |
* Main Timeline code |
|
3 |
*/ |
|
4 |
|
|
5 |
window.Tlns = { |
|
6 |
Utils : {}, |
|
7 |
Defaults : {}, |
|
8 |
Templates : {}, |
|
9 |
Classes : {} |
|
10 |
}; |
|
11 |
|
|
12 |
/* Utility Functions */ |
|
13 |
|
|
14 |
Tlns.Utils.zeroPad = function(_n) { |
|
15 |
return (_n < 10 ? "0" : "") + _n; |
|
16 |
} |
|
17 |
|
|
18 |
Tlns.Utils.SetDefaults = function(_object, _defaults, _options) { |
|
19 |
var _options = _options || {}; |
|
20 |
_(_defaults).each(function(_v, _k) { |
|
21 |
if(/^m(in|ax)_/.test(_k)) { |
|
22 |
var _tab = _k.split('_') |
|
23 |
if( typeof _object[_tab[1]] !== "undefined") { |
89
|
24 |
var _fn = Math[_tab[0] === "max" ? "min":"max"]; |
81
|
25 |
_object[_tab[1]] = _fn(_object[_tab[1]], _v); |
|
26 |
} |
|
27 |
} else { |
|
28 |
if( typeof _options[_k] !== "undefined") { |
|
29 |
_object[_k] = _options[_k]; |
|
30 |
} else { |
|
31 |
_object[_k] = _v; |
|
32 |
} |
|
33 |
} |
|
34 |
}); |
97
|
35 |
}; |
81
|
36 |
|
|
37 |
Tlns.Utils.dateFormat = function(_date, _template) { |
|
38 |
if (typeof _date !== "object") { |
|
39 |
_date = new Date(parseInt(_date)); |
|
40 |
} |
|
41 |
var _params = { |
|
42 |
hours: _date.getHours(), |
89
|
43 |
isDayStart: !_date.getHours(), |
81
|
44 |
"0hours": Tlns.Utils.zeroPad(_date.getHours()), |
|
45 |
minutes: _date.getMinutes(), |
|
46 |
"0minutes": Tlns.Utils.zeroPad(_date.getMinutes()), |
|
47 |
seconds: _date.getSeconds(), |
|
48 |
"0seconds": Tlns.Utils.zeroPad(_date.getSeconds()), |
|
49 |
dayOfWeek: ["dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi"][_date.getDay()], |
|
50 |
shortDayOfWeek: ["Dim","Lun","Mar","Mer","Jeu","Ven","Sam"][_date.getDay()], |
|
51 |
dayOfMonth: _date.getDate(), |
|
52 |
"0dayOfMonth": Tlns.Utils.zeroPad(_date.getDate()), |
|
53 |
monthNumber: 1+_date.getMonth(), |
|
54 |
"0monthNumber": Tlns.Utils.zeroPad(1+_date.getMonth()), |
|
55 |
monthName: ["janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre"][_date.getMonth()], |
97
|
56 |
shortMonthName: ["jan.","fev.","mar.","avr.","mai","jun.","jul.","aou.","sep.","oct.","nov.","dec."][_date.getMonth()], |
81
|
57 |
year: _date.getFullYear() |
|
58 |
} |
|
59 |
return Mustache.to_html(_template, _params); |
97
|
60 |
}; |
81
|
61 |
|
|
62 |
Tlns.Utils.guid = function() { |
|
63 |
return 'xxxx-xxxx-xxxx-xxxx'.replace(/x/g,function() { |
|
64 |
return Math.floor(Math.random()*16).toString(16); |
|
65 |
}); |
97
|
66 |
}; |
81
|
67 |
|
|
68 |
Tlns.Utils.timeFieldProcess = function(_val) { |
|
69 |
var _h = 0, |
|
70 |
_m = 0, |
|
71 |
_matches = _val.match(/(\d+)/g); |
|
72 |
if (_matches && _matches.length) { |
|
73 |
_h = Math.min(23, +(_matches[0])); |
|
74 |
if (_matches.length > 1) { |
|
75 |
_m = Math.min(59, +(_matches[1])); |
|
76 |
} |
|
77 |
} |
|
78 |
return { |
|
79 |
hours: _h, |
|
80 |
minutes: _m, |
|
81 |
text: Tlns.Utils.zeroPad(_h) + ':' + Tlns.Utils.zeroPad(_m) |
|
82 |
} |
97
|
83 |
}; |
81
|
84 |
|
|
85 |
Tlns.Utils.dateFieldProcess = function(_val) { |
|
86 |
var _now = new Date(), |
|
87 |
_y = _now.getFullYear(), |
|
88 |
_m = 1 + _now.getMonth(), |
|
89 |
_d = _now.getDate(), |
|
90 |
_matches = _val.match(/(\d+)/g); |
|
91 |
if (_matches && _matches.length) { |
|
92 |
_d = Math.min(31, +(_matches[0])); |
|
93 |
if (_matches.length > 1) { |
|
94 |
_m = Math.min(12, +(_matches[1])); |
|
95 |
} |
|
96 |
if (_matches.length > 2) { |
|
97 |
_y = parseInt(_matches[2]); |
|
98 |
if (_y < 2000) { |
|
99 |
_y += 2000; |
|
100 |
} |
|
101 |
_y = Math.min(2020, Math.max(2000, _y)); |
|
102 |
} |
|
103 |
} |
|
104 |
return { |
|
105 |
year: _y, |
|
106 |
month: _m, |
|
107 |
date: _d, |
|
108 |
text: Tlns.Utils.zeroPad(_d) + '/' + Tlns.Utils.zeroPad(_m) + '/' + _y |
|
109 |
} |
97
|
110 |
}; |
81
|
111 |
|
|
112 |
/* Defaults */ |
|
113 |
|
|
114 |
Tlns.Defaults.Timeline = { |
88
|
115 |
email: "", |
|
116 |
token: "", |
81
|
117 |
container : "timeline", |
97
|
118 |
width : 790, |
88
|
119 |
height : 225, |
81
|
120 |
min_width : 400, |
|
121 |
min_height : 100, |
96
|
122 |
main_width : 726, |
|
123 |
linelabels : [ |
|
124 |
"RDV", |
|
125 |
"Actu", |
|
126 |
"Appels", |
|
127 |
"Persos" |
|
128 |
], |
|
129 |
picto_url: "img/", |
81
|
130 |
timescales : [{ |
|
131 |
label : "Semaine", |
89
|
132 |
span : 7 * 86400 * 1000, |
81
|
133 |
grid_interval : 86400 * 1000, |
89
|
134 |
grid_date_format : '{{dayOfMonth}} {{monthName}}', |
94
|
135 |
min_importance : 0 |
81
|
136 |
}, { |
89
|
137 |
label : "3 jours", |
|
138 |
span : 3 * 86400 * 1000, |
|
139 |
grid_interval : 6 * 3600 * 1000, |
|
140 |
grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}', |
93
|
141 |
min_importance : 0 |
81
|
142 |
}, { |
89
|
143 |
label : "Journée", |
|
144 |
span : 86400 * 1000, |
81
|
145 |
grid_interval : 2 * 3600 * 1000, |
89
|
146 |
grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}', |
93
|
147 |
min_importance : 0 |
81
|
148 |
}, { |
89
|
149 |
label : "Demi-Journée", |
|
150 |
span : 6 * 3600 * 1000, |
|
151 |
grid_interval : 3600 * 1000, |
|
152 |
grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}', |
93
|
153 |
min_importance : 0 |
81
|
154 |
}], |
|
155 |
level: 0, |
|
156 |
central_time: 0, |
|
157 |
sync_now: true, |
88
|
158 |
api_endpoint: "", |
89
|
159 |
api_method: "searchForTimelineEdito", |
81
|
160 |
occurrences: [], |
90
|
161 |
grid_spacing: 12, |
98
|
162 |
tooltip_date_format: '{{dayOfMonth}} {{monthName}} {{year}} à {{0hours}}:{{0minutes}}', |
96
|
163 |
class_info: { |
|
164 |
"Cms\\Previously": { |
|
165 |
label: "Précédemment", |
|
166 |
univers_id: 0, |
|
167 |
picto: "previously.png", |
|
168 |
show: true |
|
169 |
}, |
|
170 |
"Cms\\Chapter": { |
|
171 |
label: "Roman", |
|
172 |
univers_id: 0, |
|
173 |
picto: "roman.png", |
|
174 |
show: true |
|
175 |
}, |
|
176 |
"Cms\\FlashTrash": { |
|
177 |
label: "JT", |
|
178 |
univers_id: 0, |
|
179 |
picto: "flash.png", |
|
180 |
show: true |
|
181 |
}, |
|
182 |
"Cms\\Article": { |
|
183 |
label: "Article", |
|
184 |
univers_id: 1, |
|
185 |
show: true |
|
186 |
}, |
|
187 |
"Cms\\Poll": { |
|
188 |
label: "Sondage", |
|
189 |
univers_id: 2, |
|
190 |
picto: "sondage.png", |
|
191 |
show: true |
|
192 |
}, |
|
193 |
"Cms\\CallWitness": { |
|
194 |
label: "Appel à Témoins", |
|
195 |
univers_id: 2, |
|
196 |
picto: "temoignage.png", |
|
197 |
show: true |
|
198 |
}, |
|
199 |
"Cms\\Rule": { |
|
200 |
label: "Règle du jour", |
|
201 |
univers_id: 2, |
|
202 |
picto: "regle.png", |
|
203 |
show: true |
|
204 |
}, |
|
205 |
"Cms\\CallCharacter": { |
|
206 |
label: "Appel à Personnage", |
|
207 |
univers_id: 3, |
|
208 |
show: true |
|
209 |
}, |
|
210 |
"Cms\\SweetCadaver": { |
|
211 |
label: "Cadavre exquis", |
|
212 |
univers_id: 3, |
|
213 |
show: true |
|
214 |
} |
93
|
215 |
}, |
94
|
216 |
maxtime: false, |
|
217 |
url_base: "" |
97
|
218 |
}; |
81
|
219 |
|
|
220 |
for (var _i = 0; _i < Tlns.Defaults.Timeline.timescales.length; _i++) { |
|
221 |
Tlns.Defaults.Timeline.timescales[_i].level = _i; |
|
222 |
} |
|
223 |
|
|
224 |
/* Templates */ |
|
225 |
|
89
|
226 |
Tlns.Templates.Timeline = '<div class="Tl-Main"><div class="Tl-Grid"></div><div class="Tl-TopBar"></div>' |
97
|
227 |
+ '<div class="Tl-BottomPart"><div class="Tl-AnotherGroup"><ul class="Tl-UniversLabels"></ul>' |
89
|
228 |
+ '<div class="Tl-MainPart"><div class="Tl-Occurrences"></div>' |
97
|
229 |
+ '</div></div><div class="Tl-Slider-Container"><a class="Tl-Slider-Zoom-In" href="#"></a><div class="Tl-Slider"></div><a class="Tl-Slider-Zoom-Out" href="#"></a></div>' |
|
230 |
+ '<div class="Tl-Overlay-Container"><div class="Tl-Overlay-Box"><div class="Tl-Overlay"><div class="Tl-Overlay-Main"></div></div></div></div></div></div>' |
|
231 |
+ '<div class="Tl-Details"></div>'; |
81
|
232 |
|
89
|
233 |
Tlns.Templates.Univers = '<div class="Tl-UniversText">{{title}}</div>'; |
83
|
234 |
|
90
|
235 |
Tlns.Templates.Occurrence = |
96
|
236 |
'{{#occurrences}}<div class="Tl-Occurrence Tl-OccOnGrid Tl-Occ{{type}}{{#editing}} Tl-Editing{{/editing}}" occurrence-id="{{id}}" style="left: {{x}}px; top: {{y}}px;">' |
91
|
237 |
+ '{{#image}}<img src="{{image}}" />{{/image}}' |
|
238 |
+ '</div>{{/occurrences}}'; |
81
|
239 |
|
95
|
240 |
Tlns.Templates.OccurrenceTooltip = '<h3 class="Tl-Tooltip-Title">{{title}}</h3>'; |
81
|
241 |
|
98
|
242 |
Tlns.Templates.OccurrenceDetails = '<div class="Tl-Detail"><div class="Tl-Detail-X"></div><img class="Tl-Detail-Image" src="{{detail_image}}" />' |
94
|
243 |
+ '<h2 class="Tl-Detail-Title">{{title}}</h2><p class="Tl-Detail-Description">{{detail_description}}</p>' |
98
|
244 |
+ '<div class="Tl-Detail-Bottom"><span class="Tl-Detail-Date">Publié le {{formatted_date}}</span><a class="Tl-Detail-Read" href="{{url}}" target="_blank">Lire la suite</a></div></div>'; |
94
|
245 |
|
81
|
246 |
/* Classes */ |
|
247 |
|
|
248 |
Tlns.Classes.Timeline = function(_options) { |
|
249 |
|
|
250 |
/* Setting Defaults */ |
|
251 |
Tlns.Utils.SetDefaults(this, Tlns.Defaults.Timeline, _options); |
|
252 |
|
|
253 |
/* Setting container CSS */ |
|
254 |
this.$ = $('#' + this.container).html(Mustache.to_html(Tlns.Templates.Timeline, this)); |
|
255 |
|
|
256 |
this.$.find('.Tl-Main').css({ |
|
257 |
width : this.width + "px", |
|
258 |
height : this.height + "px" |
|
259 |
}); |
89
|
260 |
this.top_height = this.$.find('.Tl-TopBar').outerHeight(); |
|
261 |
this.main_height = this.height - this.top_height; |
82
|
262 |
//this.main_height = this.height - 27; |
97
|
263 |
var labelsWidth = this.$.find('.Tl-UniversLabels').width(); |
|
264 |
this.main_width = this.width - labelsWidth - this.$.find('.Tl-Slider-Container').width(); |
81
|
265 |
this.$.find('.Tl-BottomPart').css("height", this.main_height + "px"); |
97
|
266 |
this.$.find('.Tl-MainPart').css("width", this.main_width + "px"); |
|
267 |
this.$.find('.Tl-Grid').css({ |
|
268 |
"left": labelsWidth + "px", |
|
269 |
"width": this.main_width + "px" |
|
270 |
}); |
81
|
271 |
this.$.find('.Tl-Overlay-Container').css("left", (this.$.find('.Tl-BottomPart').outerWidth() - this.main_width) + "px"); |
97
|
272 |
this.$slider = this.$.find('.Tl-Slider'); |
88
|
273 |
|
97
|
274 |
var $mainpart = this.$.find('.Tl-MainPart'), |
|
275 |
_o = $mainpart.offset(); |
81
|
276 |
this.dragging_bounds = { |
|
277 |
left: _o.left, |
|
278 |
top: _o.top, |
97
|
279 |
right: _o.left + $mainpart.outerWidth(), |
|
280 |
bottom: _o.top + $mainpart.outerHeight(), |
81
|
281 |
}; |
|
282 |
|
|
283 |
var _this = this; |
|
284 |
|
|
285 |
this.throttledDrawGrid = _.throttle(function() { |
|
286 |
_this.drawGrid(); |
|
287 |
}, 150); |
|
288 |
|
97
|
289 |
var $scrollgroup = this.$.find('.Tl-AnotherGroup'); |
89
|
290 |
|
97
|
291 |
$scrollgroup.mousedown(function(_event) { |
81
|
292 |
_this.onMouseDown(_event); |
|
293 |
return false; |
|
294 |
}); |
|
295 |
|
97
|
296 |
$scrollgroup.mousemove(function(_event) { |
81
|
297 |
_this.onMouseMove(_event); |
|
298 |
return false; |
|
299 |
}); |
|
300 |
|
97
|
301 |
$scrollgroup.mouseup(function(_event) { |
81
|
302 |
_this.onMouseUp(_event); |
|
303 |
return false; |
|
304 |
}); |
|
305 |
|
97
|
306 |
$scrollgroup.mousewheel(function(_event, _delta) { |
81
|
307 |
var _newLevel = Math.max(0,Math.min(_this.timescales.length-1, (_delta < 0 ? -1 : 1) + parseInt(_this.level))); |
|
308 |
if (_newLevel != _this.level) { |
|
309 |
_this.hideTooltip(); |
|
310 |
var _deltaX = _event.pageX - _this.dragging_bounds.left, |
|
311 |
_tAtMouse = _this.timeFromMouse(_event.pageX), |
|
312 |
_newScale = _this.main_width / (_this.timescales[_newLevel].span), |
93
|
313 |
_newStart = _tAtMouse - _deltaX / _newScale, |
|
314 |
_newTime = _newStart + _this.timescales[_newLevel].span / 2; |
|
315 |
_this.central_time = _this.maxtime ? Math.min(_newTime, _this.maxtime) : _newTime; |
81
|
316 |
_this.setLevel(_newLevel); |
|
317 |
} |
|
318 |
return false; |
|
319 |
}); |
|
320 |
|
|
321 |
this.$.find('.Tl-Overlay-Box').mouseover(function(_event) { |
|
322 |
$(this).show(); |
|
323 |
}).mouseout(function(_event) { |
|
324 |
$(this).hide(); |
|
325 |
}); |
|
326 |
|
97
|
327 |
this.$slider.slider({ |
|
328 |
orientation: "vertical", |
|
329 |
min: 0, |
|
330 |
max: this.timescales.length - 1, |
|
331 |
value: this.level, |
|
332 |
slide: function(e, ui) { |
|
333 |
_this.setLevel(ui.value); |
|
334 |
} |
|
335 |
}); |
|
336 |
this.$.find('.Tl-Slider-Container').mousewheel(function(_event, _delta) { |
|
337 |
var _newLevel = Math.max(0,Math.min(_this.timescales.length-1, (_delta < 0 ? -1 : 1) + parseInt(_this.level))); |
|
338 |
if (_newLevel != _this.level) { |
|
339 |
_this.hideTooltip(); |
|
340 |
_this.setLevel(_newLevel); |
|
341 |
} |
|
342 |
return false; |
|
343 |
}); |
|
344 |
|
|
345 |
$(".Tl-Slider-Zoom-In").click(function() { |
|
346 |
_this.setLevel(Math.min(_this.timescales.length-1,parseInt(_this.level)+1)); |
|
347 |
return false; |
|
348 |
}); |
|
349 |
$(".Tl-Slider-Zoom-Out").click(function() { |
|
350 |
_this.setLevel(Math.max(0,parseInt(_this.level)-1)); |
|
351 |
return false; |
|
352 |
}); |
|
353 |
|
|
354 |
this.setLevel(this.level); |
87
|
355 |
|
88
|
356 |
this.onUniversLoaded(this.linelabels); |
81
|
357 |
|
97
|
358 |
}; |
81
|
359 |
|
|
360 |
Tlns.Classes.Timeline.prototype.onMouseDown = function(_event) { |
|
361 |
this.mouse_down = true; |
|
362 |
this.is_dragging = false; |
|
363 |
this.start_pos = { |
|
364 |
x: _event.pageX, |
|
365 |
y: _event.pageY |
|
366 |
}; |
|
367 |
if (typeof this.dragging_type === "undefined") { |
|
368 |
this.time_at_start = this.central_time; |
|
369 |
this.dragging_type = "timeline"; |
|
370 |
} |
97
|
371 |
}; |
81
|
372 |
|
|
373 |
Tlns.Classes.Timeline.prototype.onMouseUp = function(_event) { |
|
374 |
this.mouse_down = false; |
|
375 |
this.is_dragging = false; |
|
376 |
this.dragging_type = undefined; |
97
|
377 |
}; |
81
|
378 |
|
|
379 |
Tlns.Classes.Timeline.prototype.timeFromX = function(_x) { |
93
|
380 |
return this.start_time + _x / this.current_scale; |
97
|
381 |
}; |
81
|
382 |
|
|
383 |
Tlns.Classes.Timeline.prototype.timeFromMouse = function(_pageX) { |
|
384 |
return this.timeFromX(_pageX - this.dragging_bounds.left); |
97
|
385 |
}; |
81
|
386 |
|
|
387 |
Tlns.Classes.Timeline.prototype.universFromY = function(_y) { |
97
|
388 |
return Math.max(0,Math.min(this.univers.length, Math.floor(_y / this.univers_height))); |
|
389 |
}; |
81
|
390 |
|
|
391 |
Tlns.Classes.Timeline.prototype.universFromMouse = function(_pageY) { |
|
392 |
return this.universFromY(_pageY - this.dragging_bounds.top); |
97
|
393 |
}; |
81
|
394 |
|
|
395 |
Tlns.Classes.Timeline.prototype.onMouseMove = function(_event) { |
85
|
396 |
if (this.mouse_down && !this.is_dragging) { |
|
397 |
var _dx = this.start_pos.x - _event.pageX, |
|
398 |
_dy = this.start_pos.y - _event.pageY, |
|
399 |
_sqd = _dx * _dx + _dy * _dy; |
|
400 |
if (_sqd > 16) { |
|
401 |
this.is_dragging = true; |
|
402 |
} |
|
403 |
} |
|
404 |
if (this.is_dragging) { |
81
|
405 |
this.hideTooltip(); |
|
406 |
switch (this.dragging_type) { |
|
407 |
case "timeline": |
|
408 |
this.setTime(this.time_at_start + Math.floor(( this.start_pos.x - _event.pageX ) / this.current_scale)); |
|
409 |
break; |
|
410 |
} |
|
411 |
} |
97
|
412 |
}; |
81
|
413 |
|
|
414 |
Tlns.Classes.Timeline.prototype.onUniversLoaded = function(_data) { |
|
415 |
this.univers = []; |
|
416 |
if(_data.length) { |
|
417 |
this.univers_height = Math.floor(this.main_height / _data.length); |
|
418 |
} |
|
419 |
for(var _i = 0; _i < _data.length; _i++) { |
|
420 |
this.univers.push(new Tlns.Classes.Univers(_data[_i], this, _i)); |
|
421 |
} |
|
422 |
|
|
423 |
this.loadOccurrences(); |
97
|
424 |
}; |
81
|
425 |
|
|
426 |
Tlns.Classes.Timeline.prototype.offsetTime = function(_timeOffset) { |
|
427 |
this.setTime(this.central_time + _timeOffset); |
97
|
428 |
}; |
81
|
429 |
|
|
430 |
Tlns.Classes.Timeline.prototype.setTime = function(_centralTime) { |
|
431 |
this.sync_now = false; |
93
|
432 |
this.central_time = this.maxtime ? Math.min(_centralTime, this.maxtime) : _centralTime; |
81
|
433 |
this.changeSpan(); |
97
|
434 |
}; |
81
|
435 |
|
|
436 |
Tlns.Classes.Timeline.prototype.setLevel = function(_level) { |
|
437 |
if (_level >= 0 && _level < this.timescales.length) { |
|
438 |
this.level = _level; |
97
|
439 |
this.$slider.slider("value", _level); |
81
|
440 |
this.changeSpan(); |
|
441 |
} |
97
|
442 |
}; |
81
|
443 |
|
|
444 |
Tlns.Classes.Timeline.prototype.changeSpan = function() { |
|
445 |
var _now = new Date().valueOf(); |
|
446 |
if (this.sync_now) { |
|
447 |
this.central_time = _now; |
|
448 |
} |
|
449 |
var _timescale = this.timescales[this.level]; |
|
450 |
this.current_scale = this.main_width / (_timescale.span); |
|
451 |
this.start_time = this.central_time - (_timescale.span / 2); |
|
452 |
this.end_time = this.central_time + (_timescale.span / 2); |
|
453 |
this.throttledDrawGrid(); |
97
|
454 |
}; |
81
|
455 |
|
|
456 |
Tlns.Classes.Timeline.prototype.drawGrid = function() { |
|
457 |
var _now = new Date().valueOf(), |
|
458 |
_timescale = this.timescales[this.level], |
|
459 |
_offset = new Date().getTimezoneOffset() * 60000, |
|
460 |
_grid_width = Math.floor(_timescale.grid_interval * this.current_scale), |
|
461 |
_roundstart = Math.floor((this.start_time - _offset) / _timescale.grid_interval) * _timescale.grid_interval + _offset, |
|
462 |
_html = ''; |
|
463 |
for (var _t = _roundstart; _t < this.end_time; _t += _timescale.grid_interval) { |
97
|
464 |
var _x = this.current_scale * (_t - this.start_time), |
|
465 |
isMajor = !((_t - _offset)%(24*60*60*1000)); |
81
|
466 |
if (_x > 0) { |
97
|
467 |
_html += '<div class="Tl-Grid-Column' + (isMajor ? ' Tl-Grid-Major':'') + '" style="width:' + _grid_width + 'px; left: ' + _x + 'px">' |
81
|
468 |
+ '<div class="Tl-Grid-Label">' + Tlns.Utils.dateFormat(_t, _timescale.grid_date_format) + '</div></div>'; |
|
469 |
} |
|
470 |
} |
|
471 |
if (this.start_time <= _now && this.end_time >= _now) { |
97
|
472 |
_html += '<div class="Tl-Grid-Now" style="left: ' + this.current_scale * (_now - this.start_time) + 'px"></div>'; |
|
473 |
} |
|
474 |
if (this.editing_occurrence && this.editing_occurrence.date <= this.end_time && this.editing_occurrence.date >= this.start_time) { |
|
475 |
_html += '<div class="Tl-Grid-Editing" style="left: ' + this.editing_occurrence.x + 'px"></div>'; |
81
|
476 |
} |
|
477 |
this.$.find('.Tl-Grid').html(_html); |
|
478 |
this.drawOccurrences(); |
97
|
479 |
}; |
81
|
480 |
|
|
481 |
Tlns.Classes.Timeline.prototype.loadOccurrences = function() { |
|
482 |
var _this = this; |
99
|
483 |
|
|
484 |
function getData(cursor) { |
|
485 |
$.getJSON(_this.api_endpoint, { |
|
486 |
method: _this.api_method, |
|
487 |
api_key: _this.token, |
|
488 |
mail: _this.email, |
|
489 |
cursor: cursor |
|
490 |
}, function(_data) { |
|
491 |
_this.onOccurrencesLoaded(_data); |
|
492 |
if (_data.cursor.hasNext) { |
|
493 |
getData(_data.cursor.next); |
|
494 |
} |
|
495 |
}); |
|
496 |
} |
|
497 |
|
|
498 |
getData(); |
81
|
499 |
|
97
|
500 |
}; |
81
|
501 |
|
88
|
502 |
Tlns.Classes.Timeline.prototype.onOccurrencesLoaded = function(_data) { |
|
503 |
for (var _i = 0; _i < _data.data.length; _i++) { |
|
504 |
this.createOrUpdateOccurrence(_data.data[_i]); |
81
|
505 |
} |
|
506 |
if (!this.mouse_down) { |
|
507 |
this.drawOccurrences(); |
|
508 |
} |
97
|
509 |
}; |
81
|
510 |
|
|
511 |
Tlns.Classes.Timeline.prototype.deleteOccurrence = function(_id) { |
|
512 |
this.occurrences = _(this.occurrences).reject(function(_occ) { |
|
513 |
return _occ.id == _id; |
|
514 |
}); |
97
|
515 |
}; |
81
|
516 |
|
|
517 |
Tlns.Classes.Timeline.prototype.getOccurrence = function(_id) { |
|
518 |
return _(this.occurrences).find(function(_occ) { |
|
519 |
return _occ.id == _id; |
|
520 |
}); |
97
|
521 |
}; |
81
|
522 |
|
88
|
523 |
Tlns.Classes.Timeline.prototype.createOrUpdateOccurrence = function(_data) { |
|
524 |
var _id = _data.id, |
96
|
525 |
_occurrence = this.getOccurrence(_id), |
|
526 |
typeinfo = this.class_info[_data.__CLASS__]; |
|
527 |
if (typeinfo && typeinfo.show) { |
|
528 |
if (typeof _occurrence === "undefined") { |
|
529 |
_occurrence = new Tlns.Classes.Occurrence(this); |
|
530 |
this.occurrences.push(_occurrence); |
|
531 |
} |
|
532 |
_occurrence.update(_data); |
81
|
533 |
} |
|
534 |
return _occurrence; |
97
|
535 |
}; |
81
|
536 |
|
95
|
537 |
Tlns.Classes.Timeline.prototype.showTooltip = function(_x, _y, _html) { |
81
|
538 |
this.$.find('.Tl-Overlay-Box') |
|
539 |
.show() |
|
540 |
.css({ |
|
541 |
left: _x + "px", |
|
542 |
top: _y + "px" |
|
543 |
}); |
|
544 |
this.$.find('.Tl-Overlay-Main').html(_html); |
85
|
545 |
|
97
|
546 |
}; |
81
|
547 |
|
|
548 |
Tlns.Classes.Timeline.prototype.hideTooltip = function() { |
|
549 |
this.$.find('.Tl-Overlay-Box').hide(); |
97
|
550 |
}; |
81
|
551 |
|
|
552 |
Tlns.Classes.Timeline.prototype.drawOccurrences = function() { |
85
|
553 |
var _this = this; |
|
554 |
_(this.occurrences).each(function(_occ) { |
81
|
555 |
_occ.x = _this.current_scale * (_occ.date - _this.start_time); |
|
556 |
_occ.y = _occ.univers.y; |
|
557 |
}); |
96
|
558 |
var minT = this.timeFromX(-32), |
93
|
559 |
minI = this.timescales[this.level].min_importance; |
85
|
560 |
var _visible = _(this.occurrences).filter(function(_occ) { |
96
|
561 |
return (_occ.date >= minT && _occ.date <= _this.end_time && (_occ.importance >= minI)); |
85
|
562 |
}); |
81
|
563 |
|
90
|
564 |
var _moved = true, l = 0; |
91
|
565 |
while (_moved && l < 10) { |
90
|
566 |
l++; |
81
|
567 |
_moved = false; |
|
568 |
for (var _i = 0; _i < _visible.length; _i++) { |
|
569 |
for (var _j = 0; _j < _i; _j++) { |
90
|
570 |
var delta = Math.abs(_visible[_j].x-_visible[_i].x); |
81
|
571 |
if (_visible[_j].univers_id == _visible[_i].univers_id |
90
|
572 |
&& delta < this.grid_spacing |
81
|
573 |
) { |
90
|
574 |
var sign = _visible[_i].x < _visible[_j].x ? 1 : -1, |
91
|
575 |
add = sign * (this.grid_spacing - delta) / 2; |
81
|
576 |
_moved = true; |
90
|
577 |
_visible[_i].x -= add; |
|
578 |
_visible[_j].x += add; |
81
|
579 |
} |
|
580 |
} |
|
581 |
} |
|
582 |
} |
|
583 |
|
|
584 |
var _html = Mustache.to_html(Tlns.Templates.Occurrence, { |
90
|
585 |
occurrences: _visible |
81
|
586 |
}); |
|
587 |
this.$.find('.Tl-Occurrences').html(_html); |
90
|
588 |
|
81
|
589 |
this.$.find('.Tl-Occurrence').mousedown(function() { |
|
590 |
var _el = $(this), |
|
591 |
_id = _el.attr("occurrence-id"); |
|
592 |
if (typeof _id !== "undefined") { |
|
593 |
_this.editing_occurrence = _this.getOccurrence(_id); |
|
594 |
if (typeof _this.dragging_type === "undefined" && typeof _this.editing_occurrence !== "undefined" /* && !_this.editing_occurrence.locked */ ) { |
|
595 |
_this.dragging_type = "occurrence"; |
83
|
596 |
} |
|
597 |
if (!_this.editing_occurrence.editing) { |
|
598 |
_(_this.occurrences).each(function(_occ) { |
|
599 |
_occ.editing = false; |
|
600 |
}); |
81
|
601 |
_this.editing_occurrence.editing = true; |
94
|
602 |
_this.$.find(".Tl-Details").html(Mustache.to_html(Tlns.Templates.OccurrenceDetails, _this.editing_occurrence)); |
81
|
603 |
} |
83
|
604 |
_this.throttledDrawGrid(); |
81
|
605 |
} |
|
606 |
}).mouseover(function(_event) { |
|
607 |
var _el = $(this), |
|
608 |
_id = _el.attr("occurrence-id"); |
|
609 |
if (typeof _id !== "undefined") { |
|
610 |
var _occurrence = _this.getOccurrence(_id); |
|
611 |
if (!_this.is_dragging) { |
95
|
612 |
var _html = Mustache.to_html(Tlns.Templates.OccurrenceTooltip, _occurrence); |
97
|
613 |
_this.showTooltip(_occurrence.x, _occurrence.y, _html); |
81
|
614 |
} |
|
615 |
} |
|
616 |
}).mouseout(function() { |
|
617 |
var _el = $(this), |
|
618 |
_id = _el.attr("occurrence-id"); |
|
619 |
if (typeof _id !== "undefined") { |
|
620 |
var _occurrence = _this.getOccurrence(_id); |
|
621 |
_this.hideTooltip(); |
|
622 |
} |
|
623 |
}); |
90
|
624 |
|
98
|
625 |
if (this.editing_occurrence) { |
|
626 |
$(".Tl-Grid-Editing, .Tl-Detail-X").css("left", this.editing_occurrence.x); |
|
627 |
if (this.editing_occurrence.date > this.end_time || this.editing_occurrence.date < this.start_time) { |
|
628 |
$(".Tl-Detail-X").hide(); |
|
629 |
} else { |
|
630 |
$(".Tl-Detail-X").show(); |
|
631 |
} |
|
632 |
} |
|
633 |
|
97
|
634 |
}; |
81
|
635 |
|
|
636 |
Tlns.Classes.Timeline.prototype.getUnivers = function(_id) { |
|
637 |
return _(this.univers).find(function(_univ) { |
|
638 |
return (_univ.id == _id); |
|
639 |
}); |
97
|
640 |
}; |
81
|
641 |
|
|
642 |
/* |
|
643 |
* Univers |
|
644 |
*/ |
|
645 |
|
|
646 |
Tlns.Classes.Univers = function(_data, _timeline, _index) { |
92
|
647 |
this.id = this.index = _index; |
88
|
648 |
this.title = _data; |
81
|
649 |
// this.mainCharacter = _data.personnage; |
|
650 |
this.y = (_timeline.univers_height * _index); |
|
651 |
|
|
652 |
this.$label = $('<li>').css({ |
|
653 |
height : _timeline.univers_height + "px" |
88
|
654 |
}).html(Mustache.to_html(Tlns.Templates.Univers, this)); |
81
|
655 |
|
|
656 |
_timeline.$.find('.Tl-UniversLabels').append(this.$label); |
89
|
657 |
|
|
658 |
var txtdiv = this.$label.find(".Tl-UniversText"); |
|
659 |
txtdiv.css("margin-top", Math.floor((_timeline.univers_height - txtdiv.height()) / 2)); |
97
|
660 |
}; |
81
|
661 |
|
|
662 |
/* |
|
663 |
* Occurrence |
|
664 |
*/ |
|
665 |
|
|
666 |
Tlns.Classes.Occurrence = function(_timeline) { |
|
667 |
this.timeline = _timeline; |
97
|
668 |
}; |
81
|
669 |
|
88
|
670 |
Tlns.Classes.Occurrence.prototype.update = function(_data) { |
|
671 |
this.original_data = _data; |
|
672 |
this.id = _data.id; |
|
673 |
this.date = new Date(1000 * (_data.dateFirstPublication || _data.dateCreate) || Date.now); |
98
|
674 |
this.formatted_date = Tlns.Utils.dateFormat(this.date,this.timeline.tooltip_date_format); |
88
|
675 |
this.title = _data.title; |
92
|
676 |
this.type = _data.__CLASS__; |
93
|
677 |
this.importance = _data.importance; |
96
|
678 |
var typeinfo = this.timeline.class_info[_data.__CLASS__]; |
|
679 |
this.univers_id = typeinfo.univers_id; |
97
|
680 |
var media = _(_data.contentHasMedias).find(function(m) { |
|
681 |
return !!m.media.carre; |
|
682 |
}); |
|
683 |
if (media) { |
|
684 |
this.image = media.media.carre.replace(/carre\/[\d]+\/[\d]+/,'carre/32/32'); |
|
685 |
this.detail_image = media.media.carre.replace(/carre\/[\d]+\/[\d]+/,'carre/135/135'); |
96
|
686 |
} |
|
687 |
if (typeinfo.picto) { |
|
688 |
this.image = this.timeline.picto_url + typeinfo.picto; |
92
|
689 |
} |
|
690 |
this.univers = this.timeline.univers[this.univers_id]; |
96
|
691 |
this.format = typeinfo.label; |
81
|
692 |
// this.published = (_data.publication && _data.publication == "En ligne"); |
|
693 |
// this.locked = _data.verrouille || false; |
|
694 |
// this.characters = _data.personnagesSecondaires || []; |
88
|
695 |
var _tmp = $('<p>').html(_data.resume || ""); |
94
|
696 |
var trimmedDesc = _tmp.text().trim().replace(/(\n|\r|\r\n)/mg,' '); |
|
697 |
this.description = trimmedDesc.replace(/(^.{60,80})[\s].+$/m,'$1…'); |
|
698 |
this.detail_description = trimmedDesc.replace(/(^.{360,380})[\s].+$/m,'$1…'); |
|
699 |
this.url = this.timeline.url_base + _data.url; |
97
|
700 |
}; |
81
|
701 |
|
|
702 |
Tlns.Classes.Occurrence.prototype.toString = function() { |
|
703 |
return "Occurrence " + this.type + ': "' + this.title + '"'; |
97
|
704 |
}; |