65
|
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.SetDefaults = function(_object, _defaults, _options) { |
|
15 |
var _options = _options || {}; |
|
16 |
_(_defaults).each(function(_v, _k) { |
|
17 |
if(/^m(in|ax)_/.test(_k)) { |
|
18 |
var _tab = _k.split('_') |
|
19 |
if( typeof _object[_tab[1]] !== "undefined") { |
|
20 |
var _fn = (_tab[0] === "min" ? Math.max : Math.min); |
|
21 |
_object[_tab[1]] = _fn(_object[_tab[1]], _v); |
|
22 |
} |
|
23 |
} else { |
|
24 |
if( typeof _options[_k] !== "undefined") { |
|
25 |
_object[_k] = _options[_k]; |
|
26 |
} else { |
|
27 |
_object[_k] = _v; |
|
28 |
} |
|
29 |
} |
|
30 |
}); |
|
31 |
} |
|
32 |
|
|
33 |
Tlns.Utils.dateFormat = function(_date, _template) { |
|
34 |
if (typeof _data !== "object") { |
|
35 |
_date = new Date(_date); |
|
36 |
} |
|
37 |
function zeroPad(_n) { |
|
38 |
return (_n < 10 ? "0" : "") + _n |
|
39 |
} |
|
40 |
var _params = { |
|
41 |
hours: _date.getHours(), |
|
42 |
"0hours": zeroPad(_date.getHours()), |
|
43 |
minutes: _date.getMinutes(), |
|
44 |
"0minutes": zeroPad(_date.getMinutes()), |
|
45 |
seconds: _date.getSeconds(), |
|
46 |
"0seconds": zeroPad(_date.getSeconds()), |
|
47 |
dayOfWeek: ["dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi"][_date.getDay()], |
|
48 |
shortDayOfWeek: ["Dim","Lun","Mar","Mer","Jeu","Ven","Sam"][_date.getDay()], |
|
49 |
dayOfMonth: _date.getDate(), |
|
50 |
"0dayOfMonth": zeroPad(_date.getDate()), |
|
51 |
monthNumber: 1+_date.getMonth(), |
|
52 |
"0monthNumber": zeroPad(1+_date.getMonth()), |
|
53 |
monthName: ["janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre"][_date.getMonth()], |
|
54 |
shortMonthName: ["jan","fev","mar","avr","mai","jun","jul","aou","sep","oct","nov","dec"][_date.getMonth()], |
|
55 |
year: _date.getFullYear() |
|
56 |
} |
|
57 |
return Mustache.to_html(_template, _params); |
|
58 |
} |
|
59 |
|
|
60 |
/* Defaults */ |
|
61 |
|
|
62 |
Tlns.Defaults.Timeline = { |
|
63 |
container : "timeline", |
|
64 |
width : 950, |
|
65 |
height : 200, |
|
66 |
url_univers : '', |
|
67 |
min_width : 400, |
|
68 |
min_height : 100, |
|
69 |
main_width : 800, |
|
70 |
timescales : [{ |
|
71 |
label : "Mois", |
|
72 |
span : 32 * 86400 * 1000, |
|
73 |
grid_interval : 5 * 86400 * 1000, |
|
74 |
grid_date_format : '{{dayOfMonth}} {{shortMonthName}}', |
|
75 |
start_date_format : '{{dayOfMonth}} {{shortMonthName}}', |
|
76 |
end_date_format : '{{dayOfMonth}} {{shortMonthName}} {{year}}' |
|
77 |
}, { |
|
78 |
label : "Semaine", |
|
79 |
span : 8 * 86400 * 1000, |
|
80 |
grid_interval : 86400 * 1000, |
|
81 |
grid_date_format : '{{shortDayOfWeek}} {{0dayOfMonth}}/{{0monthNumber}}', |
|
82 |
start_date_format : '{{dayOfMonth}} {{shortMonthName}}', |
|
83 |
end_date_format : '{{dayOfMonth}} {{shortMonthName}}' |
|
84 |
}, { |
|
85 |
label : "2 jours", |
|
86 |
span : 2 * 86400 * 1000, |
|
87 |
grid_interval : 8 * 3600 * 1000, |
|
88 |
grid_date_format : '{{shortDayOfWeek}} {{0dayOfMonth}}/{{0monthNumber}} {{hours}}h', |
|
89 |
start_date_format : '{{dayOfMonth}} {{shortMonthName}}', |
|
90 |
end_date_format : '{{dayOfMonth}} {{shortMonthName}}' |
|
91 |
}, { |
|
92 |
label : "Demi-Journée", |
|
93 |
span : 12 * 3600 * 1000, |
|
94 |
grid_interval : 2 * 3600 * 1000, |
|
95 |
grid_date_format : '{{hours}}h', |
|
96 |
start_date_format : '{{dayOfMonth}} {{shortMonthName}} {{hours}}h', |
|
97 |
end_date_format : '{{dayOfMonth}} {{shortMonthName}} {{hours}}h' |
|
98 |
}, { |
|
99 |
label : "3 Heures", |
|
100 |
span : 3 * 3600 * 1000, |
|
101 |
grid_interval : 30 * 60 * 1000, |
|
102 |
grid_date_format : '{{0hours}}:{{0minutes}}', |
|
103 |
start_date_format : '{{dayOfMonth}} {{shortMonthName}} {{0hours}}:{{0minutes}}', |
|
104 |
end_date_format : '{{0hours}}:{{0minutes}}' |
|
105 |
}, { |
|
106 |
label : "1 Heure", |
66
|
107 |
span : 60 * 60 * 1000, |
65
|
108 |
grid_interval : 15 * 60 * 1000, |
|
109 |
grid_date_format : '{{0hours}}:{{0minutes}}', |
|
110 |
start_date_format : '{{dayOfMonth}} {{shortMonthName}} {{0hours}}:{{0minutes}}', |
|
111 |
end_date_format : '{{0hours}}:{{0minutes}}' |
|
112 |
}], |
|
113 |
level: 0, |
|
114 |
central_time: 0, |
|
115 |
sync_now: true, |
|
116 |
url_occurrences: '', |
66
|
117 |
occurrences: [], |
|
118 |
cluster_spacing: 9 |
65
|
119 |
} |
|
120 |
|
|
121 |
for (var _i = 0; _i < Tlns.Defaults.Timeline.timescales.length; _i++) { |
|
122 |
Tlns.Defaults.Timeline.timescales[_i].level = _i; |
|
123 |
} |
|
124 |
|
|
125 |
/* Templates */ |
|
126 |
|
|
127 |
Tlns.Templates.Timeline = '<div class="Tl-TopBar"><div class="Tl-TopBar-Button Tl-Border-Right"><div class="Tl-TopBar-AddButton"></div></div><div class="Tl-TopBar-Spacer Tl-Border-Right"></div>' |
|
128 |
+ '<div class="Tl-TopBar-Button Tl-Border-Right"><div class="Tl-TopBar-PreviousButton"></div></div><div class="Tl-TopBar-TimeSpan Tl-TopBar-TextBtn Tl-Border-Right">--/--</div>' |
|
129 |
+ '<div class="Tl-TopBar-Button Tl-Border-Right"><div class="Tl-TopBar-SyncButton"></div></div><div class="Tl-TopBar-Button Tl-Border-Right"><div class="Tl-TopBar-NextButton"></div></div><div class="Tl-TopBar-Spacer Tl-Border-Right"></div>' |
|
130 |
+ '<div class="Tl-TopBar-Timescales">{{#timescales}}<div class="Tl-TopBar-Button Tl-TopBar-TextBtn Tl-Border-Right" data-level="{{level}}">{{label}}</div>{{/timescales}}</div></div>' |
66
|
131 |
+ '<div class="Tl-BottomPart"><ul class="Tl-UniversLabels"></ul><div class="Tl-MainPart"><div class="Tl-Layer Tl-Grid"></div><div class="Tl-Layer Tl-Occurrences"></div></div>' |
|
132 |
+ '<div class="Tl-Overlay-Container"><div class="Tl-Overlay-Box"><div class="Tl-Overlay"><div class="Tl-Overlay-Main"></div><div class="Tl-Overlay-Tip"></div></div></div></div></div>'; |
65
|
133 |
|
66
|
134 |
Tlns.Templates.Univers = '<span class="Tl-UniversText">{{title}}</span>'; |
|
135 |
|
|
136 |
Tlns.Templates.Occurrence = '{{#clusters}}<div class="Tl-Cluster Tl-Occ{{type}}" style="left: {{x}}px; top: {{y}}px;" cluster-contents="{{#occurrences}}{{type}}|{{id}},{{/occurrences}}"><div class="Tl-ClusterCount">{{occurrences.length}}</div></div>{{/clusters}}{{#occurrences}}<div class="Tl-Occurrence Tl-Occ{{type}}" occurrence-type="{{type}}" occurrence-id="{{id}}" style="left: {{x}}px; top: {{univers.y}}px;"></div>{{/occurrences}}'; |
65
|
137 |
|
|
138 |
/* Classes */ |
|
139 |
|
|
140 |
Tlns.Classes.Timeline = function(_options) { |
|
141 |
|
|
142 |
/* Setting Defaults */ |
|
143 |
Tlns.Utils.SetDefaults(this, Tlns.Defaults.Timeline, _options); |
|
144 |
|
|
145 |
/* Setting container CSS */ |
|
146 |
this.$ = $('#' + this.container); |
|
147 |
this.$.addClass('Tl-Main'); |
|
148 |
this.$.css({ |
|
149 |
width : this.width + "px", |
|
150 |
height : this.height + "px" |
|
151 |
}); |
|
152 |
this.$.html(Mustache.to_html(Tlns.Templates.Timeline, this)); |
|
153 |
|
|
154 |
this.main_height = this.height - this.$.find('.Tl-TopBar').outerHeight(); |
|
155 |
this.$.find('.Tl-BottomPart').css("height", this.main_height + "px"); |
|
156 |
this.$.find('.Tl-MainPart').css("width", this.main_width + "px"); |
66
|
157 |
this.$.find('.Tl-Overlay-Container').css("left", Math.floor(this.$.find('.Tl-BottomPart').outerWidth() - this.main_width / 2) + "px"); |
65
|
158 |
var _o = this.$.find('.Tl-MainPart').offset(); |
|
159 |
this.dragging_bounds = { |
|
160 |
left: _o.left, |
|
161 |
top: _o.top, |
|
162 |
right: _o.left + this.$.find('.Tl-MainPart').outerWidth(), |
|
163 |
bottom: _o.top + this.$.find('.Tl-MainPart').outerHeight(), |
|
164 |
}; |
|
165 |
|
|
166 |
this.setLevel(this.level); |
|
167 |
|
|
168 |
var _this = this; |
|
169 |
this.$.find('.Tl-TopBar-Timescales>div').click(function() { |
|
170 |
_this.setLevel($(this).attr("data-level")); |
|
171 |
}); |
|
172 |
|
|
173 |
this.$.find('.Tl-TopBar-SyncButton').click(function() { |
|
174 |
_this.sync_now = !_this.sync_now; |
|
175 |
_this.drawGrid(); |
|
176 |
}) |
|
177 |
|
|
178 |
this.$.find('.Tl-TopBar-PreviousButton').click(function() { |
|
179 |
_this.offsetTime(-_this.timescales[_this.level].span / 4); |
|
180 |
}); |
|
181 |
|
|
182 |
this.$.find('.Tl-TopBar-NextButton').click(function() { |
|
183 |
_this.offsetTime(_this.timescales[_this.level].span / 4); |
|
184 |
}); |
|
185 |
|
|
186 |
this.$.find('.Tl-MainPart').mousedown(function(_event) { |
|
187 |
_this.onMouseDown(_event); |
|
188 |
return false; |
|
189 |
}); |
|
190 |
|
|
191 |
this.$.find('.Tl-MainPart').mousemove(function(_event) { |
|
192 |
_this.onMouseMove(_event); |
|
193 |
return false; |
|
194 |
}); |
|
195 |
|
|
196 |
this.$.find('.Tl-MainPart').mouseup(function(_event) { |
|
197 |
_this.onMouseUp(_event); |
|
198 |
return false; |
|
199 |
}); |
|
200 |
|
66
|
201 |
this.$.find('.Tl-Overlay-Box').mouseover(function(_event) { |
|
202 |
$(this).show(); |
|
203 |
}).mouseout(function(_event) { |
|
204 |
$(this).hide(); |
|
205 |
}) |
|
206 |
|
65
|
207 |
this.throttledSetTime = _.throttle(function(_time) { |
|
208 |
_this.setTime(_time) |
|
209 |
}, 150); |
|
210 |
|
|
211 |
/* Loading Univers */ |
|
212 |
$.getJSON(this.url_univers, function(_data) { |
|
213 |
_this.onUniversLoaded(_data); |
|
214 |
}); |
|
215 |
} |
|
216 |
|
|
217 |
Tlns.Classes.Timeline.prototype.onMouseDown = function(_event) { |
|
218 |
this.mouse_down = true; |
|
219 |
this.is_dragging = false; |
66
|
220 |
this.time_at_start = this.central_time; |
65
|
221 |
this.start_pos = { |
|
222 |
x: _event.pageX, |
|
223 |
y: _event.pageY |
|
224 |
}; |
66
|
225 |
if (typeof this.dragging_type === "undefined") { |
|
226 |
this.dragging_type = "timeline"; |
65
|
227 |
} |
|
228 |
} |
|
229 |
|
|
230 |
Tlns.Classes.Timeline.prototype.onMouseUp = function(_event) { |
|
231 |
this.mouse_down = false; |
|
232 |
this.is_dragging = false; |
66
|
233 |
this.dragging_type = undefined; |
65
|
234 |
} |
|
235 |
|
|
236 |
Tlns.Classes.Timeline.prototype.onMouseMove = function(_event) { |
|
237 |
if (this.mouse_down) { |
|
238 |
this.is_dragging = true; |
66
|
239 |
this.$.find('.Tl-Overlay-Box').hide(); |
65
|
240 |
if (_event.pageX > this.dragging_bounds.left |
|
241 |
&& _event.pageX < this.dragging_bounds.right |
|
242 |
&& _event.pageY > this.dragging_bounds.top |
66
|
243 |
&& _event.pageY < this.dragging_bounds.bottom |
|
244 |
) { |
|
245 |
var _newTime = Math.floor(this.time_at_start + ( this.start_pos.x - _event.pageX ) / this.current_scale); |
65
|
246 |
switch (this.dragging_type) { |
|
247 |
case "timeline": |
|
248 |
this.throttledSetTime(_newTime); |
|
249 |
break; |
|
250 |
} |
|
251 |
} else { |
|
252 |
this.onMouseUp(_event); |
|
253 |
} |
|
254 |
} |
|
255 |
} |
|
256 |
|
|
257 |
Tlns.Classes.Timeline.prototype.onUniversLoaded = function(_data) { |
|
258 |
this.univers = []; |
|
259 |
if(_data.length) { |
|
260 |
this.univers_height = Math.floor(this.main_height / _data.length); |
|
261 |
} |
|
262 |
for(var _i = 0; _i < _data.length; _i++) { |
|
263 |
this.univers.push(new Tlns.Classes.Univers(_data[_i], this, _i)); |
|
264 |
} |
66
|
265 |
this.loadOccurrences(); |
65
|
266 |
} |
|
267 |
|
|
268 |
Tlns.Classes.Timeline.prototype.offsetTime = function(_timeOffset) { |
|
269 |
this.setTime(this.central_time + _timeOffset); |
|
270 |
} |
|
271 |
|
|
272 |
Tlns.Classes.Timeline.prototype.setTime = function(_centralTime) { |
|
273 |
this.sync_now = false; |
|
274 |
this.central_time = _centralTime; |
|
275 |
this.drawGrid(); |
|
276 |
} |
|
277 |
|
|
278 |
Tlns.Classes.Timeline.prototype.setLevel = function(_level) { |
|
279 |
if (_level >= 0 && _level < this.timescales.length) { |
|
280 |
this.$.find('.Tl-TopBar-Timescales>div').each(function() { |
|
281 |
var _el = $(this); |
|
282 |
if (_el.attr("data-level") == _level) { |
|
283 |
_el.addClass("active"); |
|
284 |
} else { |
|
285 |
_el.removeClass("active"); |
|
286 |
} |
|
287 |
}); |
|
288 |
this.level = _level; |
|
289 |
this.drawGrid(); |
|
290 |
} |
|
291 |
} |
|
292 |
|
|
293 |
Tlns.Classes.Timeline.prototype.drawGrid = function() { |
|
294 |
var _now = new Date().valueOf(); |
|
295 |
if (this.sync_now) { |
|
296 |
this.central_time = _now; |
|
297 |
this.$.find('.Tl-TopBar-SyncButton').addClass("active"); |
|
298 |
} else { |
|
299 |
this.$.find('.Tl-TopBar-SyncButton').removeClass("active"); |
|
300 |
} |
|
301 |
var _timescale = this.timescales[this.level], |
|
302 |
_offset = new Date().getTimezoneOffset() * 60000; |
66
|
303 |
this.current_scale = this.main_width / (_timescale.span) |
|
304 |
this.start_time = this.central_time - (_timescale.span / 2); |
|
305 |
this.end_time = this.central_time + (_timescale.span / 2); |
|
306 |
var _grid_width = Math.floor(_timescale.grid_interval * this.current_scale), |
|
307 |
_roundstart = Math.floor((this.start_time - _offset) / _timescale.grid_interval) * _timescale.grid_interval + _offset, |
65
|
308 |
_html = ''; |
66
|
309 |
this.$.find('.Tl-TopBar-TimeSpan').html(Tlns.Utils.dateFormat(this.start_time, _timescale.start_date_format) + ' - ' + Tlns.Utils.dateFormat(this.end_time, _timescale.end_date_format)); |
|
310 |
for (var _t = _roundstart; _t < this.end_time; _t += _timescale.grid_interval) { |
65
|
311 |
_html += '<div class="Tl-Grid-Column" style="width:' + _grid_width + 'px; left: ' + this.current_scale * (_t - this.central_time) + 'px">' |
|
312 |
+ '<div class="Tl-Grid-Label">' + Tlns.Utils.dateFormat(_t, _timescale.grid_date_format) + '</div></div>'; |
|
313 |
} |
|
314 |
/* |
|
315 |
|
|
316 |
for (var _t = _roundstart; _t < _tmax; _t += _timescale.grid_interval) { |
|
317 |
var _isMajor = !(Math.floor((_t - _offset) / _timescale.grid_interval) % _timescale.major_interval); |
|
318 |
_html += '<div class="Tl-Grid-Column' + ( _isMajor ? ' Tl-Grid-Major' : '' ) + '" style="width:' + _grid_width + 'px; left: ' + _scale * (_t - this.central_time) + 'px">' |
|
319 |
+ ( _isMajor ? '<div class="Tl-Grid-Label">' + Tlns.Utils.dateFormat(_t, _timescale.date_format) + '</div>' : '' ) + '</div>'; |
|
320 |
} |
|
321 |
*/ |
66
|
322 |
if (this.start_time <= _now && this.end_time >= _now) { |
65
|
323 |
_html += '<div class="Tl-Grid-Now" style="left: ' + this.current_scale * (_now - this.central_time) + 'px"></div>' |
|
324 |
} |
|
325 |
this.$.find('.Tl-Grid').html(_html); |
66
|
326 |
this.drawOccurrences(); |
|
327 |
} |
|
328 |
|
|
329 |
Tlns.Classes.Timeline.prototype.loadOccurrences = function() { |
|
330 |
var _url = Mustache.to_html(this.url_occurrences, { |
|
331 |
from_time: this.start_time, |
|
332 |
to_time: this.end_time |
|
333 |
}), |
|
334 |
_this = this; |
|
335 |
$.getJSON(_url, function(_data) { |
|
336 |
_this.onOccurrencesLoaded(_data); |
|
337 |
}); |
|
338 |
} |
|
339 |
|
|
340 |
Tlns.Classes.Timeline.prototype.onOccurrencesLoaded = function(_data) { |
|
341 |
if (typeof _data.occurrencesNarratives === "object" && _data.occurrencesNarratives !== null) { |
|
342 |
for (var _i = 0; _i < _data.occurrencesNarratives.length; _i++) { |
|
343 |
this.createOrUpdateOccurrence("narrative", _data.occurrencesNarratives[_i]); |
|
344 |
} |
|
345 |
for (var _i = 0; _i < _data.occurrencesProduction.length; _i++) { |
|
346 |
this.createOrUpdateOccurrence("production", _data.occurrencesProduction[_i]); |
|
347 |
} |
|
348 |
} |
|
349 |
if (!this.mouse_down) { |
|
350 |
this.drawOccurrences(); |
|
351 |
} |
|
352 |
} |
|
353 |
|
|
354 |
Tlns.Classes.Timeline.prototype.getOccurrence = function(_type, _id) { |
|
355 |
return _(this.occurrences).find(function(_occ) { |
|
356 |
return (_occ.type == _type && _occ.id == _id); |
|
357 |
}); |
|
358 |
} |
|
359 |
|
|
360 |
Tlns.Classes.Timeline.prototype.createOrUpdateOccurrence = function(_type, _data) { |
|
361 |
var _occurrence = this.getOccurrence(_type, _data.id); |
|
362 |
if (typeof _occurrence === "undefined") { |
|
363 |
_occurrence = new Tlns.Classes.Occurrence(this); |
|
364 |
this.occurrences.push(_occurrence); |
|
365 |
} |
|
366 |
_occurrence.update(_type, _data); |
|
367 |
} |
|
368 |
|
|
369 |
Tlns.Classes.Timeline.prototype.showTooltip = function(_x, _y, _html) { |
|
370 |
this.$.find('.Tl-Overlay-Box').show() |
|
371 |
.css({ |
|
372 |
left: _x + "px", |
|
373 |
top: _y + "px" |
|
374 |
}); |
|
375 |
this.$.find('.Tl-Overlay-Main').html(_html); |
|
376 |
} |
|
377 |
|
|
378 |
Tlns.Classes.Timeline.prototype.hideTooltip = function() { |
|
379 |
this.$.find('.Tl-Overlay-Box').hide(); |
65
|
380 |
} |
|
381 |
|
|
382 |
Tlns.Classes.Timeline.prototype.drawOccurrences = function() { |
66
|
383 |
var _this = this, |
|
384 |
_visible = _(this.occurrences).filter(function(_occ) { |
|
385 |
return (_occ.date >= _this.start_time && _occ.date <= _this.end_time && _occ.published); |
|
386 |
}); |
|
387 |
_(_visible).each(function(_occ) { |
|
388 |
_occ.x = _this.current_scale * (_occ.date - _this.central_time); |
|
389 |
_occ.in_cluster = false; |
|
390 |
}); |
65
|
391 |
|
66
|
392 |
var _moved = true; |
|
393 |
while (_moved) { |
|
394 |
_moved = false; |
|
395 |
for (var _i = 0; _i < _visible.length; _i++) { |
|
396 |
for (var _j = 0; _j < _i; _j++) { |
|
397 |
if (_visible[_j].univers_id == _visible[_i].univers_id |
|
398 |
&& _visible[_j].x != _visible[_i].x |
|
399 |
&& Math.abs(_visible[_j].x-_visible[_i].x) < this.cluster_spacing |
|
400 |
) { |
|
401 |
_moved = true; |
|
402 |
_visible[_i].x = this.cluster_spacing * Math.round(_visible[_i].x / this.cluster_spacing); |
|
403 |
_visible[_j].x = this.cluster_spacing * Math.round(_visible[_j].x / this.cluster_spacing); |
|
404 |
} |
|
405 |
} |
|
406 |
} |
|
407 |
} |
|
408 |
var _clusters = []; |
|
409 |
for (var _i = 0; _i < _visible.length; _i++) { |
|
410 |
for (var _j = 0; _j < _i; _j++) { |
|
411 |
if (_visible[_j].univers_id == _visible[_i].univers_id && _visible[_j].x == _visible[_i].x) { |
|
412 |
_visible[_j].in_cluster = true; |
|
413 |
_visible[_i].in_cluster = true; |
|
414 |
var _x = _visible[_j].x, |
|
415 |
_y = _visible[_j].univers.y; |
|
416 |
_cluster = _(_clusters).find(function(_c) { return _c.x == _x && _c.y == _y }); |
|
417 |
if (typeof _cluster === "undefined") { |
|
418 |
_cluster = { x: _x, y: _y, occurrences: [] }; |
|
419 |
_clusters.push(_cluster); |
|
420 |
} |
|
421 |
if ("undefined" === typeof _(_cluster.occurrences).find(function(_o) { |
|
422 |
return _o.type == _visible[_j].type && _o.id == _visible[_j].id; |
|
423 |
})) { |
|
424 |
_cluster.occurrences.push({type: _visible[_j].type, id: _visible[_j].id}); |
|
425 |
} |
|
426 |
if ("undefined" === typeof _(_cluster.occurrences).find(function(_o) { |
|
427 |
return _o.type == _visible[_i].type && _o.id == _visible[_i].id; |
|
428 |
})) { |
|
429 |
_cluster.occurrences.push({type: _visible[_i].type, id: _visible[_i].id}); |
|
430 |
} |
|
431 |
} |
|
432 |
} |
|
433 |
} |
|
434 |
_(_clusters).each(function(_cluster) { |
|
435 |
_cluster.type = _cluster.occurrences[0].type; |
|
436 |
for (var _i = 1; _i < _cluster.occurrences.length; _i++) { |
|
437 |
if (_cluster.occurrences[_i].type !== _cluster.type) { |
|
438 |
_cluster.type = "both"; |
|
439 |
break; |
|
440 |
} |
|
441 |
} |
|
442 |
}); |
|
443 |
|
|
444 |
var _html = Mustache.to_html(Tlns.Templates.Occurrence, { |
|
445 |
occurrences:_(_visible).reject(function(_o) {return _o.in_cluster}), |
|
446 |
clusters: _clusters |
|
447 |
}); |
|
448 |
this.$.find('.Tl-Occurrences').html(_html); |
|
449 |
this.$.find('.Tl-Occurrence').mousedown(function() { |
|
450 |
_this.dragging_type = "occurrence" |
|
451 |
}).mouseover(function() { |
|
452 |
var _el = $(this), |
|
453 |
_occurrence = _this.getOccurrence(_el.attr("occurrence-type"),_el.attr("occurrence-id")); |
|
454 |
_this.showTooltip(_occurrence.x, _occurrence.univers.y, _occurrence.title); |
|
455 |
}).mouseout(function() { |
|
456 |
_this.hideTooltip(); |
|
457 |
}); |
65
|
458 |
} |
|
459 |
|
66
|
460 |
Tlns.Classes.Timeline.prototype.getUnivers = function(_id) { |
|
461 |
return _(this.univers).find(function(_univ) { |
|
462 |
return (_univ.id == _id); |
|
463 |
}); |
|
464 |
} |
|
465 |
|
|
466 |
/* |
|
467 |
* Univers |
|
468 |
*/ |
|
469 |
|
65
|
470 |
Tlns.Classes.Univers = function(_data, _timeline, _index) { |
|
471 |
this.id = _data.id; |
|
472 |
this.index = _index; |
|
473 |
this.title = _data.nom; |
|
474 |
this.mainCharacter = _data.personnage; |
66
|
475 |
this.y = (_timeline.univers_height * _index); |
65
|
476 |
|
|
477 |
this.$label = $('<li>').css({ |
|
478 |
height : _timeline.univers_height + "px" |
|
479 |
}).html(Mustache.to_html(Tlns.Templates.Univers, this)) |
66
|
480 |
.addClass((_index % 2) ? 'Tl-Line-Odd' : 'Tl-Line-Even'); |
65
|
481 |
|
|
482 |
_timeline.$.find('.Tl-UniversLabels').append(this.$label); |
|
483 |
var _txt = _data.nom, |
|
484 |
_span = this.$label.find('span'); |
|
485 |
|
|
486 |
while (_span.outerWidth() > (_timeline.width - _timeline.main_width) && _txt) { |
|
487 |
_txt = _txt.substr(0, _txt.length - 1); |
|
488 |
_span.html(_txt + '…'); |
|
489 |
} |
|
490 |
} |
66
|
491 |
|
|
492 |
/* |
|
493 |
* Occurrence |
|
494 |
*/ |
|
495 |
|
|
496 |
Tlns.Classes.Occurrence = function(_timeline) { |
|
497 |
this.timeline = _timeline; |
|
498 |
} |
|
499 |
|
|
500 |
Tlns.Classes.Occurrence.prototype.update = function(_type, _data) { |
|
501 |
this.type = _type; |
|
502 |
this.id = _data.id || _.uniqueId(); |
|
503 |
this.date = _data.date || _data.datePublication; |
|
504 |
this.title = _data.titre || "<untitled>"; |
|
505 |
this.univers_id = _data.univers; |
|
506 |
this.univers = this.timeline.getUnivers(this.univers_id); |
|
507 |
this.status = _data.statut; |
|
508 |
this.published = _data.publie || false; |
|
509 |
this.locked = _data.verrouille || false; |
|
510 |
this.characters = _data.personnagesSecondaires || []; |
|
511 |
this.dependsOn = _data.dependDe || []; |
|
512 |
} |
|
513 |
|
|
514 |
Tlns.Classes.Occurrence.prototype.toString = function() { |
|
515 |
return "Occurrence " + this.type + ': "' + this.title + '"'; |
|
516 |
} |