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 |
}); |
|
35 |
} |
|
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()], |
|
56 |
shortMonthName: ["jan","fev","mar","avr","mai","jun","jul","aou","sep","oct","nov","dec"][_date.getMonth()], |
|
57 |
year: _date.getFullYear() |
|
58 |
} |
|
59 |
return Mustache.to_html(_template, _params); |
|
60 |
} |
|
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 |
}); |
|
66 |
} |
|
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 |
} |
|
83 |
} |
|
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 |
} |
|
110 |
} |
|
111 |
|
|
112 |
/* Defaults */ |
|
113 |
|
|
114 |
Tlns.Defaults.Timeline = { |
88
|
115 |
email: "", |
|
116 |
token: "", |
81
|
117 |
container : "timeline", |
88
|
118 |
width : 780, |
|
119 |
height : 225, |
81
|
120 |
min_width : 400, |
|
121 |
min_height : 100, |
88
|
122 |
main_width : 700, |
|
123 |
linelabels : [], |
81
|
124 |
timescales : [{ |
|
125 |
label : "Semaine", |
89
|
126 |
span : 7 * 86400 * 1000, |
81
|
127 |
grid_interval : 86400 * 1000, |
89
|
128 |
grid_date_format : '{{dayOfMonth}} {{monthName}}', |
94
|
129 |
min_importance : 0 |
81
|
130 |
}, { |
89
|
131 |
label : "3 jours", |
|
132 |
span : 3 * 86400 * 1000, |
|
133 |
grid_interval : 6 * 3600 * 1000, |
|
134 |
grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}', |
93
|
135 |
min_importance : 0 |
81
|
136 |
}, { |
89
|
137 |
label : "Journée", |
|
138 |
span : 86400 * 1000, |
81
|
139 |
grid_interval : 2 * 3600 * 1000, |
89
|
140 |
grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}', |
93
|
141 |
min_importance : 0 |
81
|
142 |
}, { |
89
|
143 |
label : "Demi-Journée", |
|
144 |
span : 6 * 3600 * 1000, |
|
145 |
grid_interval : 3600 * 1000, |
|
146 |
grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}', |
93
|
147 |
min_importance : 0 |
81
|
148 |
}], |
|
149 |
level: 0, |
|
150 |
central_time: 0, |
|
151 |
sync_now: true, |
88
|
152 |
api_endpoint: "", |
89
|
153 |
api_method: "searchForTimelineEdito", |
81
|
154 |
occurrences: [], |
90
|
155 |
grid_spacing: 12, |
81
|
156 |
tooltip_date_format: '{{dayOfMonth}} {{shortMonthName}} {{year}} {{0hours}}:{{0minutes}}', |
|
157 |
statuses: { |
|
158 |
"valide": "Validée", |
|
159 |
"a_valider": "A valider", |
|
160 |
"a_realiser": "A réaliser" |
92
|
161 |
}, |
|
162 |
class_labels: { |
|
163 |
"Cms\\Previously": "Précédemment", |
|
164 |
"Cms\\Chapter": "Chapitre", |
|
165 |
"Cms\\Article": "Article", |
|
166 |
"Cms\\Poll": "Sondage", |
|
167 |
"Cms\\CallWitness": "Appel à Témoins", |
|
168 |
"Cms\\CallCharacter": "Appel à Personnage", |
|
169 |
"Cms\\Message": "Message", |
|
170 |
"Cms\\Rule": "Règle", |
|
171 |
"Cms\\Video": "Vidéo", |
|
172 |
"Cms\\SweetCadaver": "Cadavre exquis" |
93
|
173 |
}, |
94
|
174 |
maxtime: false, |
|
175 |
url_base: "" |
81
|
176 |
} |
|
177 |
|
|
178 |
for (var _i = 0; _i < Tlns.Defaults.Timeline.timescales.length; _i++) { |
|
179 |
Tlns.Defaults.Timeline.timescales[_i].level = _i; |
|
180 |
} |
|
181 |
|
|
182 |
/* Templates */ |
|
183 |
|
89
|
184 |
Tlns.Templates.Timeline = '<div class="Tl-Main"><div class="Tl-Grid"></div><div class="Tl-TopBar"></div>' |
81
|
185 |
+ '<div class="Tl-BottomPart"><ul class="Tl-UniversLabels"></ul>' |
89
|
186 |
+ '<div class="Tl-MainPart"><div class="Tl-Occurrences"></div>' |
87
|
187 |
+ '</div>' |
95
|
188 |
+ '<div class="Tl-Overlay-Container"><div class="Tl-Overlay-Box"><div class="Tl-Overlay"><div class="Tl-Overlay-Tip-Top"></div><div class="Tl-Overlay-Main"></div></div></div></div></div></div>' |
94
|
189 |
+ '<div class="Tl-Details"></div>' |
81
|
190 |
|
89
|
191 |
Tlns.Templates.Univers = '<div class="Tl-UniversText">{{title}}</div>'; |
83
|
192 |
|
90
|
193 |
Tlns.Templates.Occurrence = |
91
|
194 |
'{{#occurrences}}<div class="Tl-Occurrence Tl-OccOnGrid Tl-Occ{{type}} Tl-Occ{{status}}{{#editing}} Tl-Editing{{/editing}}" occurrence-id="{{id}}" style="left: {{x}}px; top: {{y}}px;">' |
|
195 |
+ '{{#image}}<img src="{{image}}" />{{/image}}' |
|
196 |
+ '</div>{{/occurrences}}'; |
81
|
197 |
|
95
|
198 |
Tlns.Templates.OccurrenceTooltip = '<h3 class="Tl-Tooltip-Title">{{title}}</h3>'; |
81
|
199 |
|
94
|
200 |
Tlns.Templates.OccurrenceDetails = '<div class="Tl-Detail"><img class="Tl-Detail-Image" src="{{detail_image}}" />' |
|
201 |
+ '<h2 class="Tl-Detail-Title">{{title}}</h2><p class="Tl-Detail-Description">{{detail_description}}</p>' |
|
202 |
+ '<div class="Tl-Detail-Bottom"><span class="Tl-Detail-Date">{{formatted_date}}</span><a class="Tl-Detail-Read" href="{{url}}" target="_blank">Lire la suite</a></div></div>'; |
|
203 |
|
81
|
204 |
/* Classes */ |
|
205 |
|
|
206 |
Tlns.Classes.Timeline = function(_options) { |
|
207 |
|
|
208 |
/* Setting Defaults */ |
|
209 |
Tlns.Utils.SetDefaults(this, Tlns.Defaults.Timeline, _options); |
|
210 |
|
|
211 |
/* Setting container CSS */ |
|
212 |
this.$ = $('#' + this.container).html(Mustache.to_html(Tlns.Templates.Timeline, this)); |
|
213 |
|
|
214 |
this.$.find('.Tl-Main').css({ |
|
215 |
width : this.width + "px", |
|
216 |
height : this.height + "px" |
|
217 |
}); |
89
|
218 |
this.top_height = this.$.find('.Tl-TopBar').outerHeight(); |
|
219 |
this.main_height = this.height - this.top_height; |
82
|
220 |
//this.main_height = this.height - 27; |
81
|
221 |
this.$.find('.Tl-BottomPart').css("height", this.main_height + "px"); |
89
|
222 |
this.$.find('.Tl-MainPart, .Tl-Grid').css("width", this.main_width + "px"); |
81
|
223 |
this.$.find('.Tl-Overlay-Container').css("left", (this.$.find('.Tl-BottomPart').outerWidth() - this.main_width) + "px"); |
88
|
224 |
|
81
|
225 |
var _o = this.$.find('.Tl-MainPart').offset(); |
|
226 |
this.dragging_bounds = { |
|
227 |
left: _o.left, |
|
228 |
top: _o.top, |
|
229 |
right: _o.left + this.$.find('.Tl-MainPart').outerWidth(), |
|
230 |
bottom: _o.top + this.$.find('.Tl-MainPart').outerHeight(), |
|
231 |
}; |
88
|
232 |
this.$.find('.Tl-UniversLabels').css({ |
|
233 |
width: this.width - this.main_width |
|
234 |
}); |
81
|
235 |
|
|
236 |
var _this = this; |
|
237 |
|
|
238 |
this.throttledDrawGrid = _.throttle(function() { |
|
239 |
_this.drawGrid(); |
|
240 |
}, 150); |
|
241 |
|
|
242 |
this.setLevel(this.level); |
89
|
243 |
|
81
|
244 |
this.$.find('.Tl-MainPart').mousedown(function(_event) { |
|
245 |
_this.onMouseDown(_event); |
|
246 |
return false; |
|
247 |
}); |
|
248 |
|
|
249 |
this.$.find('.Tl-MainPart').mousemove(function(_event) { |
|
250 |
_this.onMouseMove(_event); |
|
251 |
return false; |
|
252 |
}); |
|
253 |
|
|
254 |
this.$.find('.Tl-MainPart').mouseup(function(_event) { |
|
255 |
_this.onMouseUp(_event); |
|
256 |
return false; |
|
257 |
}); |
|
258 |
|
|
259 |
this.$.find('.Tl-MainPart').mousewheel(function(_event, _delta) { |
|
260 |
var _newLevel = Math.max(0,Math.min(_this.timescales.length-1, (_delta < 0 ? -1 : 1) + parseInt(_this.level))); |
|
261 |
if (_newLevel != _this.level) { |
|
262 |
_this.hideTooltip(); |
|
263 |
var _deltaX = _event.pageX - _this.dragging_bounds.left, |
|
264 |
_tAtMouse = _this.timeFromMouse(_event.pageX), |
|
265 |
_newScale = _this.main_width / (_this.timescales[_newLevel].span), |
93
|
266 |
_newStart = _tAtMouse - _deltaX / _newScale, |
|
267 |
_newTime = _newStart + _this.timescales[_newLevel].span / 2; |
|
268 |
_this.central_time = _this.maxtime ? Math.min(_newTime, _this.maxtime) : _newTime; |
81
|
269 |
_this.setLevel(_newLevel); |
|
270 |
} |
|
271 |
return false; |
|
272 |
}); |
|
273 |
|
|
274 |
this.$.find('.Tl-Overlay-Box').mouseover(function(_event) { |
|
275 |
$(this).show(); |
|
276 |
}).mouseout(function(_event) { |
|
277 |
$(this).hide(); |
|
278 |
}); |
|
279 |
|
87
|
280 |
|
88
|
281 |
this.onUniversLoaded(this.linelabels); |
81
|
282 |
|
|
283 |
} |
|
284 |
|
|
285 |
Tlns.Classes.Timeline.prototype.onMouseDown = function(_event) { |
|
286 |
this.mouse_down = true; |
|
287 |
this.is_dragging = false; |
|
288 |
this.start_pos = { |
|
289 |
x: _event.pageX, |
|
290 |
y: _event.pageY |
|
291 |
}; |
|
292 |
if (typeof this.dragging_type === "undefined") { |
|
293 |
this.time_at_start = this.central_time; |
|
294 |
this.dragging_type = "timeline"; |
|
295 |
} |
|
296 |
} |
|
297 |
|
|
298 |
Tlns.Classes.Timeline.prototype.onMouseUp = function(_event) { |
|
299 |
this.mouse_down = false; |
|
300 |
this.is_dragging = false; |
|
301 |
this.dragging_type = undefined; |
|
302 |
} |
|
303 |
|
|
304 |
Tlns.Classes.Timeline.prototype.timeFromX = function(_x) { |
93
|
305 |
return this.start_time + _x / this.current_scale; |
81
|
306 |
} |
|
307 |
|
|
308 |
Tlns.Classes.Timeline.prototype.timeFromMouse = function(_pageX) { |
|
309 |
return this.timeFromX(_pageX - this.dragging_bounds.left); |
|
310 |
} |
|
311 |
|
|
312 |
Tlns.Classes.Timeline.prototype.universFromY = function(_y) { |
|
313 |
return Math.max(0,Math.min(this.univers.length, Math.floor(_y / this.univers_height))) |
|
314 |
} |
|
315 |
|
|
316 |
Tlns.Classes.Timeline.prototype.universFromMouse = function(_pageY) { |
|
317 |
return this.universFromY(_pageY - this.dragging_bounds.top); |
|
318 |
} |
|
319 |
|
|
320 |
Tlns.Classes.Timeline.prototype.onMouseMove = function(_event) { |
85
|
321 |
if (this.mouse_down && !this.is_dragging) { |
|
322 |
var _dx = this.start_pos.x - _event.pageX, |
|
323 |
_dy = this.start_pos.y - _event.pageY, |
|
324 |
_sqd = _dx * _dx + _dy * _dy; |
|
325 |
if (_sqd > 16) { |
|
326 |
this.is_dragging = true; |
|
327 |
} |
|
328 |
} |
|
329 |
if (this.is_dragging) { |
81
|
330 |
this.hideTooltip(); |
|
331 |
switch (this.dragging_type) { |
|
332 |
case "timeline": |
|
333 |
this.setTime(this.time_at_start + Math.floor(( this.start_pos.x - _event.pageX ) / this.current_scale)); |
|
334 |
break; |
|
335 |
} |
|
336 |
} |
|
337 |
} |
|
338 |
|
|
339 |
Tlns.Classes.Timeline.prototype.onUniversLoaded = function(_data) { |
|
340 |
this.univers = []; |
|
341 |
if(_data.length) { |
|
342 |
this.univers_height = Math.floor(this.main_height / _data.length); |
|
343 |
} |
|
344 |
for(var _i = 0; _i < _data.length; _i++) { |
|
345 |
this.univers.push(new Tlns.Classes.Univers(_data[_i], this, _i)); |
|
346 |
} |
|
347 |
|
|
348 |
this.loadOccurrences(); |
|
349 |
} |
|
350 |
|
|
351 |
Tlns.Classes.Timeline.prototype.offsetTime = function(_timeOffset) { |
|
352 |
this.setTime(this.central_time + _timeOffset); |
|
353 |
} |
|
354 |
|
|
355 |
Tlns.Classes.Timeline.prototype.setTime = function(_centralTime) { |
|
356 |
this.sync_now = false; |
93
|
357 |
this.central_time = this.maxtime ? Math.min(_centralTime, this.maxtime) : _centralTime; |
81
|
358 |
this.changeSpan(); |
|
359 |
} |
|
360 |
|
|
361 |
Tlns.Classes.Timeline.prototype.setLevel = function(_level) { |
|
362 |
if (_level >= 0 && _level < this.timescales.length) { |
|
363 |
this.level = _level; |
|
364 |
this.changeSpan(); |
|
365 |
} |
|
366 |
} |
|
367 |
|
|
368 |
Tlns.Classes.Timeline.prototype.changeSpan = function() { |
|
369 |
var _now = new Date().valueOf(); |
|
370 |
if (this.sync_now) { |
|
371 |
this.central_time = _now; |
|
372 |
} |
|
373 |
var _timescale = this.timescales[this.level]; |
|
374 |
this.current_scale = this.main_width / (_timescale.span); |
|
375 |
this.start_time = this.central_time - (_timescale.span / 2); |
|
376 |
this.end_time = this.central_time + (_timescale.span / 2); |
|
377 |
this.throttledDrawGrid(); |
|
378 |
} |
|
379 |
|
|
380 |
Tlns.Classes.Timeline.prototype.drawGrid = function() { |
|
381 |
var _now = new Date().valueOf(), |
|
382 |
_timescale = this.timescales[this.level], |
|
383 |
_offset = new Date().getTimezoneOffset() * 60000, |
|
384 |
_grid_width = Math.floor(_timescale.grid_interval * this.current_scale), |
|
385 |
_roundstart = Math.floor((this.start_time - _offset) / _timescale.grid_interval) * _timescale.grid_interval + _offset, |
|
386 |
_html = ''; |
|
387 |
for (var _t = _roundstart; _t < this.end_time; _t += _timescale.grid_interval) { |
|
388 |
var _x = this.current_scale * (_t - this.start_time); |
|
389 |
if (_x > 0) { |
|
390 |
_html += '<div class="Tl-Grid-Column" style="width:' + _grid_width + 'px; left: ' + _x + 'px">' |
|
391 |
+ '<div class="Tl-Grid-Label">' + Tlns.Utils.dateFormat(_t, _timescale.grid_date_format) + '</div></div>'; |
|
392 |
} |
|
393 |
} |
|
394 |
if (this.start_time <= _now && this.end_time >= _now) { |
|
395 |
_html += '<div class="Tl-Grid-Now" style="left: ' + this.current_scale * (_now - this.start_time) + 'px"></div>' |
|
396 |
} |
|
397 |
this.$.find('.Tl-Grid').html(_html); |
|
398 |
this.drawOccurrences(); |
|
399 |
} |
|
400 |
|
|
401 |
Tlns.Classes.Timeline.prototype.loadOccurrences = function() { |
|
402 |
var _this = this; |
88
|
403 |
$.getJSON(this.api_endpoint, { |
|
404 |
method: this.api_method, |
|
405 |
api_key: this.token, |
|
406 |
mail: this.email |
|
407 |
}, function(_data) { |
|
408 |
console.log(_data); |
|
409 |
_this.onOccurrencesLoaded(_data); |
81
|
410 |
}); |
|
411 |
|
|
412 |
} |
|
413 |
|
88
|
414 |
Tlns.Classes.Timeline.prototype.onOccurrencesLoaded = function(_data) { |
|
415 |
for (var _i = 0; _i < _data.data.length; _i++) { |
|
416 |
this.createOrUpdateOccurrence(_data.data[_i]); |
81
|
417 |
} |
|
418 |
if (!this.mouse_down) { |
|
419 |
this.drawOccurrences(); |
|
420 |
} |
|
421 |
} |
|
422 |
|
|
423 |
Tlns.Classes.Timeline.prototype.deleteOccurrence = function(_id) { |
|
424 |
this.occurrences = _(this.occurrences).reject(function(_occ) { |
|
425 |
return _occ.id == _id; |
|
426 |
}); |
|
427 |
} |
|
428 |
|
|
429 |
Tlns.Classes.Timeline.prototype.getOccurrence = function(_id) { |
|
430 |
return _(this.occurrences).find(function(_occ) { |
|
431 |
return _occ.id == _id; |
|
432 |
}); |
|
433 |
} |
|
434 |
|
88
|
435 |
Tlns.Classes.Timeline.prototype.createOrUpdateOccurrence = function(_data) { |
|
436 |
var _id = _data.id, |
81
|
437 |
_occurrence = this.getOccurrence(_id); |
92
|
438 |
switch (_data.__CLASS__) { |
|
439 |
case "Cms\\Previously": |
|
440 |
case "Cms\\Chapter": |
|
441 |
case "Cms\\Article": |
95
|
442 |
case "Cms\\Video": |
92
|
443 |
case "Cms\\Poll": |
95
|
444 |
case "Cms\\Rule": |
92
|
445 |
case "Cms\\CallWitness": |
|
446 |
case "Cms\\CallCharacter": |
|
447 |
case "Cms\\SweetCadaver": |
|
448 |
if (typeof _occurrence === "undefined") { |
|
449 |
_occurrence = new Tlns.Classes.Occurrence(this); |
|
450 |
this.occurrences.push(_occurrence); |
|
451 |
} |
|
452 |
_occurrence.update(_data); |
|
453 |
break; |
81
|
454 |
} |
|
455 |
return _occurrence; |
|
456 |
} |
|
457 |
|
95
|
458 |
Tlns.Classes.Timeline.prototype.showTooltip = function(_x, _y, _html) { |
81
|
459 |
this.$.find('.Tl-Overlay-Box') |
|
460 |
.show() |
|
461 |
.css({ |
|
462 |
left: _x + "px", |
|
463 |
top: _y + "px" |
|
464 |
}); |
|
465 |
this.$.find('.Tl-Overlay-Main').html(_html); |
85
|
466 |
|
81
|
467 |
} |
|
468 |
|
|
469 |
Tlns.Classes.Timeline.prototype.hideTooltip = function() { |
|
470 |
this.$.find('.Tl-Overlay-Box').hide(); |
|
471 |
} |
|
472 |
|
|
473 |
Tlns.Classes.Timeline.prototype.drawOccurrences = function() { |
85
|
474 |
var _this = this; |
|
475 |
_(this.occurrences).each(function(_occ) { |
81
|
476 |
_occ.x = _this.current_scale * (_occ.date - _this.start_time); |
|
477 |
_occ.y = _occ.univers.y; |
|
478 |
}); |
95
|
479 |
var minT = this.timeFromX(-38), |
93
|
480 |
minI = this.timescales[this.level].min_importance; |
85
|
481 |
var _visible = _(this.occurrences).filter(function(_occ) { |
93
|
482 |
return (_occ.date >= minT && _occ.date <= _this.end_time && _occ.status && (_occ.importance >= minI)); |
85
|
483 |
}); |
81
|
484 |
|
90
|
485 |
var _moved = true, l = 0; |
91
|
486 |
while (_moved && l < 10) { |
90
|
487 |
l++; |
81
|
488 |
_moved = false; |
|
489 |
for (var _i = 0; _i < _visible.length; _i++) { |
|
490 |
for (var _j = 0; _j < _i; _j++) { |
90
|
491 |
var delta = Math.abs(_visible[_j].x-_visible[_i].x); |
81
|
492 |
if (_visible[_j].univers_id == _visible[_i].univers_id |
90
|
493 |
&& delta < this.grid_spacing |
81
|
494 |
) { |
90
|
495 |
var sign = _visible[_i].x < _visible[_j].x ? 1 : -1, |
91
|
496 |
add = sign * (this.grid_spacing - delta) / 2; |
81
|
497 |
_moved = true; |
90
|
498 |
_visible[_i].x -= add; |
|
499 |
_visible[_j].x += add; |
81
|
500 |
} |
|
501 |
} |
|
502 |
} |
|
503 |
} |
|
504 |
|
|
505 |
var _html = Mustache.to_html(Tlns.Templates.Occurrence, { |
90
|
506 |
occurrences: _visible |
81
|
507 |
}); |
|
508 |
this.$.find('.Tl-Occurrences').html(_html); |
90
|
509 |
|
81
|
510 |
this.$.find('.Tl-Occurrence').mousedown(function() { |
|
511 |
var _el = $(this), |
|
512 |
_id = _el.attr("occurrence-id"); |
|
513 |
if (typeof _id !== "undefined") { |
|
514 |
_this.editing_occurrence = _this.getOccurrence(_id); |
|
515 |
if (typeof _this.dragging_type === "undefined" && typeof _this.editing_occurrence !== "undefined" /* && !_this.editing_occurrence.locked */ ) { |
|
516 |
_this.dragging_type = "occurrence"; |
83
|
517 |
} |
|
518 |
if (!_this.editing_occurrence.editing) { |
|
519 |
_(_this.occurrences).each(function(_occ) { |
|
520 |
_occ.editing = false; |
|
521 |
}); |
81
|
522 |
_this.editing_occurrence.editing = true; |
94
|
523 |
_this.$.find(".Tl-Details").html(Mustache.to_html(Tlns.Templates.OccurrenceDetails, _this.editing_occurrence)); |
81
|
524 |
} |
83
|
525 |
_this.throttledDrawGrid(); |
81
|
526 |
} |
|
527 |
}).mouseover(function(_event) { |
|
528 |
var _el = $(this), |
|
529 |
_id = _el.attr("occurrence-id"); |
|
530 |
if (typeof _id !== "undefined") { |
|
531 |
var _occurrence = _this.getOccurrence(_id); |
|
532 |
if (!_this.is_dragging) { |
95
|
533 |
var _html = Mustache.to_html(Tlns.Templates.OccurrenceTooltip, _occurrence); |
|
534 |
_this.showTooltip(_occurrence.x + 19, _occurrence.y + 20, _html); |
81
|
535 |
} |
|
536 |
} |
|
537 |
}).mouseout(function() { |
|
538 |
var _el = $(this), |
|
539 |
_id = _el.attr("occurrence-id"); |
|
540 |
if (typeof _id !== "undefined") { |
|
541 |
var _occurrence = _this.getOccurrence(_id); |
|
542 |
_this.hideTooltip(); |
|
543 |
} |
|
544 |
}); |
90
|
545 |
|
81
|
546 |
} |
|
547 |
|
|
548 |
Tlns.Classes.Timeline.prototype.getUnivers = function(_id) { |
|
549 |
return _(this.univers).find(function(_univ) { |
|
550 |
return (_univ.id == _id); |
|
551 |
}); |
|
552 |
} |
|
553 |
|
|
554 |
/* |
|
555 |
* Univers |
|
556 |
*/ |
|
557 |
|
|
558 |
Tlns.Classes.Univers = function(_data, _timeline, _index) { |
92
|
559 |
this.id = this.index = _index; |
88
|
560 |
this.title = _data; |
81
|
561 |
// this.mainCharacter = _data.personnage; |
|
562 |
this.y = (_timeline.univers_height * _index); |
|
563 |
|
|
564 |
this.$label = $('<li>').css({ |
|
565 |
height : _timeline.univers_height + "px" |
88
|
566 |
}).html(Mustache.to_html(Tlns.Templates.Univers, this)); |
81
|
567 |
|
|
568 |
_timeline.$.find('.Tl-UniversLabels').append(this.$label); |
89
|
569 |
|
|
570 |
var txtdiv = this.$label.find(".Tl-UniversText"); |
|
571 |
txtdiv.css("margin-top", Math.floor((_timeline.univers_height - txtdiv.height()) / 2)); |
81
|
572 |
} |
|
573 |
|
|
574 |
/* |
|
575 |
* Occurrence |
|
576 |
*/ |
|
577 |
|
|
578 |
Tlns.Classes.Occurrence = function(_timeline) { |
|
579 |
this.timeline = _timeline; |
|
580 |
} |
|
581 |
|
88
|
582 |
Tlns.Classes.Occurrence.prototype.update = function(_data) { |
|
583 |
this.original_data = _data; |
|
584 |
this.id = _data.id; |
|
585 |
this.date = new Date(1000 * (_data.dateFirstPublication || _data.dateCreate) || Date.now); |
81
|
586 |
this.formatted_date = Tlns.Utils.dateFormat(this.date,Tlns.Defaults.Timeline.tooltip_date_format); |
88
|
587 |
this.title = _data.title; |
92
|
588 |
this.type = _data.__CLASS__; |
93
|
589 |
this.importance = _data.importance; |
92
|
590 |
switch(_data.__CLASS__) { |
|
591 |
case "Cms\\Previously": |
|
592 |
case "Cms\\Chapter": |
95
|
593 |
case "Cms\\Video": |
92
|
594 |
this.univers_id = 0; |
|
595 |
break; |
|
596 |
case "Cms\\Article": |
|
597 |
this.univers_id = 1; |
|
598 |
break; |
|
599 |
case "Cms\\Poll": |
95
|
600 |
case "Cms\\Rule": |
92
|
601 |
case "Cms\\CallWitness": |
|
602 |
this.univers_id = 2; |
|
603 |
break; |
95
|
604 |
case "Cms\\CallCharacter": |
92
|
605 |
case "Cms\\SweetCadaver": |
|
606 |
this.univers_id = 3; |
|
607 |
break; |
|
608 |
} |
|
609 |
this.univers = this.timeline.univers[this.univers_id]; |
81
|
610 |
if (typeof _data.statut !== "undefined" || typeof this.status === "undefined") { |
|
611 |
switch(_data.statut) { |
|
612 |
case "Validée": |
|
613 |
case "valide": |
|
614 |
this.status = "valide" |
|
615 |
break; |
|
616 |
case "A réaliser": |
|
617 |
case "a_realiser": |
|
618 |
this.status = "a_realiser"; |
|
619 |
break; |
84
|
620 |
case "A valider": |
|
621 |
case "a_valider": |
81
|
622 |
default: |
84
|
623 |
this.status = "a_valider"; |
81
|
624 |
} |
|
625 |
} |
92
|
626 |
this.format = this.timeline.class_labels[this.type]; |
91
|
627 |
if (_data.contentHasMedias && _data.contentHasMedias.length) { |
95
|
628 |
this.image = _data.contentHasMedias[0].media.carre.replace(/carre\/[\d]+\/[\d]+/,'carre/38/38'); |
94
|
629 |
this.detail_image = _data.contentHasMedias[0].media.carre.replace(/carre\/[\d]+\/[\d]+/,'carre/135/135'); |
91
|
630 |
} |
81
|
631 |
this.translated_status = Tlns.Defaults.Timeline.statuses[this.status]; |
|
632 |
// this.published = (_data.publication && _data.publication == "En ligne"); |
|
633 |
// this.locked = _data.verrouille || false; |
|
634 |
// this.characters = _data.personnagesSecondaires || []; |
88
|
635 |
var _tmp = $('<p>').html(_data.resume || ""); |
94
|
636 |
var trimmedDesc = _tmp.text().trim().replace(/(\n|\r|\r\n)/mg,' '); |
|
637 |
this.description = trimmedDesc.replace(/(^.{60,80})[\s].+$/m,'$1…'); |
|
638 |
this.detail_description = trimmedDesc.replace(/(^.{360,380})[\s].+$/m,'$1…'); |
|
639 |
this.url = this.timeline.url_base + _data.url; |
81
|
640 |
} |
|
641 |
|
|
642 |
Tlns.Classes.Occurrence.prototype.addDependency = function(_id) { |
|
643 |
if (_(this.dependsOn).indexOf(_id) == -1) { |
|
644 |
this.dependsOn.push(_id); |
|
645 |
} |
|
646 |
} |
|
647 |
|
|
648 |
Tlns.Classes.Occurrence.prototype.removeDependency = function(_id) { |
|
649 |
this.dependsOn = _(this.dependsOn).reject(function(_n) { |
|
650 |
return _n == _id; |
|
651 |
}); |
|
652 |
} |
|
653 |
|
|
654 |
Tlns.Classes.Occurrence.prototype.toString = function() { |
|
655 |
return "Occurrence " + this.type + ': "' + this.title + '"'; |
|
656 |
} |