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