author | veltr |
Fri, 04 Oct 2013 15:39:08 +0200 | |
changeset 102 | 2f1ef2ded30c |
parent 101 | 05c1161fa501 |
child 103 | b9a417017e19 |
permissions | -rw-r--r-- |
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; |
|
101 | 16 |
}; |
81 | 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)) { |
|
101 | 22 |
var _tab = _k.split('_'); |
81 | 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() |
101 | 58 |
}; |
81 | 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) |
|
101 | 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 |
|
101 | 109 |
}; |
97 | 110 |
}; |
81 | 111 |
|
112 |
/* Defaults */ |
|
113 |
||
101 | 114 |
/* TOUTE LA CONFIGURATION DE L'APPLI SE FAIT ICI */ |
115 |
||
81 | 116 |
Tlns.Defaults.Timeline = { |
88 | 117 |
email: "", |
118 |
token: "", |
|
81 | 119 |
container : "timeline", |
97 | 120 |
width : 790, |
88 | 121 |
height : 225, |
81 | 122 |
min_width : 400, |
123 |
min_height : 100, |
|
96 | 124 |
main_width : 726, |
125 |
linelabels : [ |
|
126 |
"RDV", |
|
127 |
"Actu", |
|
128 |
"Appels", |
|
129 |
"Persos" |
|
130 |
], |
|
131 |
picto_url: "img/", |
|
81 | 132 |
timescales : [{ |
133 |
label : "Semaine", |
|
89 | 134 |
span : 7 * 86400 * 1000, |
81 | 135 |
grid_interval : 86400 * 1000, |
89 | 136 |
grid_date_format : '{{dayOfMonth}} {{monthName}}', |
102
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
137 |
min_importance : 3 //0 //pour les tests, mettre à 0 pour tout afficher |
81 | 138 |
}, { |
89 | 139 |
label : "3 jours", |
140 |
span : 3 * 86400 * 1000, |
|
141 |
grid_interval : 6 * 3600 * 1000, |
|
142 |
grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}', |
|
102
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
143 |
min_importance : 2 //0 //pour les tests, mettre à 0 pour tout afficher |
81 | 144 |
}, { |
89 | 145 |
label : "Journée", |
146 |
span : 86400 * 1000, |
|
81 | 147 |
grid_interval : 2 * 3600 * 1000, |
89 | 148 |
grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}', |
102
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
149 |
min_importance : 1 //0 //pour les tests, mettre à 0 pour tout afficher |
81 | 150 |
}, { |
89 | 151 |
label : "Demi-Journée", |
152 |
span : 6 * 3600 * 1000, |
|
153 |
grid_interval : 3600 * 1000, |
|
154 |
grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}', |
|
93 | 155 |
min_importance : 0 |
81 | 156 |
}], |
157 |
level: 0, |
|
158 |
central_time: 0, |
|
159 |
sync_now: true, |
|
88 | 160 |
api_endpoint: "", |
89 | 161 |
api_method: "searchForTimelineEdito", |
81 | 162 |
occurrences: [], |
90 | 163 |
grid_spacing: 12, |
98 | 164 |
tooltip_date_format: '{{dayOfMonth}} {{monthName}} {{year}} à {{0hours}}:{{0minutes}}', |
96 | 165 |
class_info: { |
166 |
"Cms\\Previously": { |
|
167 |
label: "Précédemment", |
|
168 |
univers_id: 0, |
|
169 |
picto: "previously.png", |
|
170 |
show: true |
|
171 |
}, |
|
172 |
"Cms\\Chapter": { |
|
173 |
label: "Roman", |
|
174 |
univers_id: 0, |
|
175 |
picto: "roman.png", |
|
176 |
show: true |
|
177 |
}, |
|
178 |
"Cms\\FlashTrash": { |
|
179 |
label: "JT", |
|
180 |
univers_id: 0, |
|
181 |
picto: "flash.png", |
|
182 |
show: true |
|
183 |
}, |
|
184 |
"Cms\\Article": { |
|
185 |
label: "Article", |
|
186 |
univers_id: 1, |
|
187 |
show: true |
|
188 |
}, |
|
189 |
"Cms\\Poll": { |
|
190 |
label: "Sondage", |
|
191 |
univers_id: 2, |
|
192 |
picto: "sondage.png", |
|
193 |
show: true |
|
194 |
}, |
|
195 |
"Cms\\CallWitness": { |
|
196 |
label: "Appel à Témoins", |
|
197 |
univers_id: 2, |
|
198 |
picto: "temoignage.png", |
|
199 |
show: true |
|
200 |
}, |
|
201 |
"Cms\\Rule": { |
|
202 |
label: "Règle du jour", |
|
203 |
univers_id: 2, |
|
204 |
picto: "regle.png", |
|
205 |
show: true |
|
206 |
}, |
|
207 |
"Cms\\CallCharacter": { |
|
208 |
label: "Appel à Personnage", |
|
209 |
univers_id: 3, |
|
210 |
show: true |
|
211 |
}, |
|
212 |
"Cms\\SweetCadaver": { |
|
213 |
label: "Cadavre exquis", |
|
214 |
univers_id: 3, |
|
215 |
show: true |
|
216 |
} |
|
93 | 217 |
}, |
94 | 218 |
maxtime: false, |
219 |
url_base: "" |
|
97 | 220 |
}; |
81 | 221 |
|
222 |
for (var _i = 0; _i < Tlns.Defaults.Timeline.timescales.length; _i++) { |
|
223 |
Tlns.Defaults.Timeline.timescales[_i].level = _i; |
|
224 |
} |
|
225 |
||
226 |
/* Templates */ |
|
227 |
||
89 | 228 |
Tlns.Templates.Timeline = '<div class="Tl-Main"><div class="Tl-Grid"></div><div class="Tl-TopBar"></div>' |
97 | 229 |
+ '<div class="Tl-BottomPart"><div class="Tl-AnotherGroup"><ul class="Tl-UniversLabels"></ul>' |
89 | 230 |
+ '<div class="Tl-MainPart"><div class="Tl-Occurrences"></div>' |
97 | 231 |
+ '</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>' |
232 |
+ '<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>' |
|
233 |
+ '<div class="Tl-Details"></div>'; |
|
81 | 234 |
|
89 | 235 |
Tlns.Templates.Univers = '<div class="Tl-UniversText">{{title}}</div>'; |
83 | 236 |
|
90 | 237 |
Tlns.Templates.Occurrence = |
96 | 238 |
'{{#occurrences}}<div class="Tl-Occurrence Tl-OccOnGrid Tl-Occ{{type}}{{#editing}} Tl-Editing{{/editing}}" occurrence-id="{{id}}" style="left: {{x}}px; top: {{y}}px;">' |
102
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
239 |
+ '{{#image}}<img src="{{image}}" />{{/image}}{{#isFavorite}}<div class="Tl-Occurrence-Favorite"></div>{{/isFavorite}}' |
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
240 |
+ '{{#participationCount}}<div class="Tl-Occurrence-Participation">{{participationCount}}</div>{{/participationCount}}</div>{{/occurrences}}'; |
81 | 241 |
|
95 | 242 |
Tlns.Templates.OccurrenceTooltip = '<h3 class="Tl-Tooltip-Title">{{title}}</h3>'; |
81 | 243 |
|
102
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
244 |
Tlns.Templates.OccurrenceDetails = |
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
245 |
'<div class="Tl-Detail"><div class="Tl-Detail-X"></div><div class="Tl-Detail-Favorite{{#isFavorite}} Tl-Detail-isFavorite{{/isFavorite}}"></div>' |
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
246 |
+ '<div class="Tl-Detail-Image-Wrapper"><img class="Tl-Detail-Image" src="{{detail_image}}" />' |
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
247 |
+ '{{#participationCount}}<div class="Tl-Detail-Participation">{{participationCount}}<span class="Tl-Participation-Icon"></span></div>{{/participationCount}}</div>' |
94 | 248 |
+ '<h2 class="Tl-Detail-Title">{{title}}</h2><p class="Tl-Detail-Description">{{detail_description}}</p>' |
98 | 249 |
+ '<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 | 250 |
|
81 | 251 |
/* Classes */ |
252 |
||
253 |
Tlns.Classes.Timeline = function(_options) { |
|
254 |
||
255 |
/* Setting Defaults */ |
|
256 |
Tlns.Utils.SetDefaults(this, Tlns.Defaults.Timeline, _options); |
|
257 |
||
258 |
/* Setting container CSS */ |
|
259 |
this.$ = $('#' + this.container).html(Mustache.to_html(Tlns.Templates.Timeline, this)); |
|
260 |
|
|
261 |
this.$.find('.Tl-Main').css({ |
|
262 |
width : this.width + "px", |
|
263 |
height : this.height + "px" |
|
264 |
}); |
|
89 | 265 |
this.top_height = this.$.find('.Tl-TopBar').outerHeight(); |
266 |
this.main_height = this.height - this.top_height; |
|
82 | 267 |
//this.main_height = this.height - 27; |
97 | 268 |
var labelsWidth = this.$.find('.Tl-UniversLabels').width(); |
269 |
this.main_width = this.width - labelsWidth - this.$.find('.Tl-Slider-Container').width(); |
|
81 | 270 |
this.$.find('.Tl-BottomPart').css("height", this.main_height + "px"); |
97 | 271 |
this.$.find('.Tl-MainPart').css("width", this.main_width + "px"); |
272 |
this.$.find('.Tl-Grid').css({ |
|
273 |
"left": labelsWidth + "px", |
|
274 |
"width": this.main_width + "px" |
|
275 |
}); |
|
81 | 276 |
this.$.find('.Tl-Overlay-Container').css("left", (this.$.find('.Tl-BottomPart').outerWidth() - this.main_width) + "px"); |
97 | 277 |
this.$slider = this.$.find('.Tl-Slider'); |
88 | 278 |
|
97 | 279 |
var $mainpart = this.$.find('.Tl-MainPart'), |
280 |
_o = $mainpart.offset(); |
|
81 | 281 |
this.dragging_bounds = { |
282 |
left: _o.left, |
|
283 |
top: _o.top, |
|
97 | 284 |
right: _o.left + $mainpart.outerWidth(), |
285 |
bottom: _o.top + $mainpart.outerHeight(), |
|
81 | 286 |
}; |
287 |
|
|
288 |
var _this = this; |
|
289 |
|
|
290 |
this.throttledDrawGrid = _.throttle(function() { |
|
291 |
_this.drawGrid(); |
|
292 |
}, 150); |
|
293 |
|
|
97 | 294 |
var $scrollgroup = this.$.find('.Tl-AnotherGroup'); |
89 | 295 |
|
97 | 296 |
$scrollgroup.mousedown(function(_event) { |
81 | 297 |
_this.onMouseDown(_event); |
298 |
return false; |
|
299 |
}); |
|
300 |
|
|
97 | 301 |
$scrollgroup.mousemove(function(_event) { |
81 | 302 |
_this.onMouseMove(_event); |
303 |
return false; |
|
304 |
}); |
|
305 |
|
|
97 | 306 |
$scrollgroup.mouseup(function(_event) { |
81 | 307 |
_this.onMouseUp(_event); |
308 |
return false; |
|
309 |
}); |
|
310 |
|
|
97 | 311 |
$scrollgroup.mousewheel(function(_event, _delta) { |
81 | 312 |
var _newLevel = Math.max(0,Math.min(_this.timescales.length-1, (_delta < 0 ? -1 : 1) + parseInt(_this.level))); |
313 |
if (_newLevel != _this.level) { |
|
314 |
_this.hideTooltip(); |
|
315 |
var _deltaX = _event.pageX - _this.dragging_bounds.left, |
|
316 |
_tAtMouse = _this.timeFromMouse(_event.pageX), |
|
317 |
_newScale = _this.main_width / (_this.timescales[_newLevel].span), |
|
93 | 318 |
_newStart = _tAtMouse - _deltaX / _newScale, |
319 |
_newTime = _newStart + _this.timescales[_newLevel].span / 2; |
|
320 |
_this.central_time = _this.maxtime ? Math.min(_newTime, _this.maxtime) : _newTime; |
|
81 | 321 |
_this.setLevel(_newLevel); |
322 |
} |
|
323 |
return false; |
|
324 |
}); |
|
325 |
|
|
326 |
this.$.find('.Tl-Overlay-Box').mouseover(function(_event) { |
|
327 |
$(this).show(); |
|
328 |
}).mouseout(function(_event) { |
|
329 |
$(this).hide(); |
|
330 |
}); |
|
331 |
|
|
97 | 332 |
this.$slider.slider({ |
333 |
orientation: "vertical", |
|
334 |
min: 0, |
|
335 |
max: this.timescales.length - 1, |
|
336 |
value: this.level, |
|
337 |
slide: function(e, ui) { |
|
338 |
_this.setLevel(ui.value); |
|
339 |
} |
|
340 |
}); |
|
341 |
this.$.find('.Tl-Slider-Container').mousewheel(function(_event, _delta) { |
|
342 |
var _newLevel = Math.max(0,Math.min(_this.timescales.length-1, (_delta < 0 ? -1 : 1) + parseInt(_this.level))); |
|
343 |
if (_newLevel != _this.level) { |
|
344 |
_this.hideTooltip(); |
|
345 |
_this.setLevel(_newLevel); |
|
346 |
} |
|
347 |
return false; |
|
348 |
}); |
|
349 |
|
|
350 |
$(".Tl-Slider-Zoom-In").click(function() { |
|
351 |
_this.setLevel(Math.min(_this.timescales.length-1,parseInt(_this.level)+1)); |
|
352 |
return false; |
|
353 |
}); |
|
354 |
$(".Tl-Slider-Zoom-Out").click(function() { |
|
355 |
_this.setLevel(Math.max(0,parseInt(_this.level)-1)); |
|
356 |
return false; |
|
357 |
}); |
|
358 |
|
|
359 |
this.setLevel(this.level); |
|
87 | 360 |
|
88 | 361 |
this.onUniversLoaded(this.linelabels); |
81 | 362 |
|
97 | 363 |
}; |
81 | 364 |
|
365 |
Tlns.Classes.Timeline.prototype.onMouseDown = function(_event) { |
|
366 |
this.mouse_down = true; |
|
367 |
this.is_dragging = false; |
|
368 |
this.start_pos = { |
|
369 |
x: _event.pageX, |
|
370 |
y: _event.pageY |
|
371 |
}; |
|
102
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
372 |
this.time_at_start = this.central_time; |
97 | 373 |
}; |
81 | 374 |
|
375 |
Tlns.Classes.Timeline.prototype.onMouseUp = function(_event) { |
|
376 |
this.mouse_down = false; |
|
377 |
this.is_dragging = false; |
|
97 | 378 |
}; |
81 | 379 |
|
380 |
Tlns.Classes.Timeline.prototype.timeFromX = function(_x) { |
|
93 | 381 |
return this.start_time + _x / this.current_scale; |
97 | 382 |
}; |
81 | 383 |
|
384 |
Tlns.Classes.Timeline.prototype.timeFromMouse = function(_pageX) { |
|
385 |
return this.timeFromX(_pageX - this.dragging_bounds.left); |
|
97 | 386 |
}; |
81 | 387 |
|
388 |
Tlns.Classes.Timeline.prototype.universFromY = function(_y) { |
|
97 | 389 |
return Math.max(0,Math.min(this.univers.length, Math.floor(_y / this.univers_height))); |
390 |
}; |
|
81 | 391 |
|
392 |
Tlns.Classes.Timeline.prototype.universFromMouse = function(_pageY) { |
|
393 |
return this.universFromY(_pageY - this.dragging_bounds.top); |
|
97 | 394 |
}; |
81 | 395 |
|
396 |
Tlns.Classes.Timeline.prototype.onMouseMove = function(_event) { |
|
85 | 397 |
if (this.mouse_down && !this.is_dragging) { |
398 |
var _dx = this.start_pos.x - _event.pageX, |
|
399 |
_dy = this.start_pos.y - _event.pageY, |
|
400 |
_sqd = _dx * _dx + _dy * _dy; |
|
401 |
if (_sqd > 16) { |
|
402 |
this.is_dragging = true; |
|
403 |
} |
|
404 |
} |
|
405 |
if (this.is_dragging) { |
|
81 | 406 |
this.hideTooltip(); |
102
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
407 |
this.setTime(this.time_at_start + Math.floor(( this.start_pos.x - _event.pageX ) / this.current_scale)); |
81 | 408 |
} |
97 | 409 |
}; |
81 | 410 |
|
411 |
Tlns.Classes.Timeline.prototype.onUniversLoaded = function(_data) { |
|
412 |
this.univers = []; |
|
413 |
if(_data.length) { |
|
414 |
this.univers_height = Math.floor(this.main_height / _data.length); |
|
415 |
} |
|
416 |
for(var _i = 0; _i < _data.length; _i++) { |
|
417 |
this.univers.push(new Tlns.Classes.Univers(_data[_i], this, _i)); |
|
418 |
} |
|
419 |
|
|
420 |
this.loadOccurrences(); |
|
97 | 421 |
}; |
81 | 422 |
|
423 |
Tlns.Classes.Timeline.prototype.offsetTime = function(_timeOffset) { |
|
424 |
this.setTime(this.central_time + _timeOffset); |
|
97 | 425 |
}; |
81 | 426 |
|
427 |
Tlns.Classes.Timeline.prototype.setTime = function(_centralTime) { |
|
428 |
this.sync_now = false; |
|
93 | 429 |
this.central_time = this.maxtime ? Math.min(_centralTime, this.maxtime) : _centralTime; |
81 | 430 |
this.changeSpan(); |
97 | 431 |
}; |
81 | 432 |
|
433 |
Tlns.Classes.Timeline.prototype.setLevel = function(_level) { |
|
434 |
if (_level >= 0 && _level < this.timescales.length) { |
|
435 |
this.level = _level; |
|
97 | 436 |
this.$slider.slider("value", _level); |
81 | 437 |
this.changeSpan(); |
438 |
} |
|
97 | 439 |
}; |
81 | 440 |
|
441 |
Tlns.Classes.Timeline.prototype.changeSpan = function() { |
|
442 |
var _now = new Date().valueOf(); |
|
443 |
if (this.sync_now) { |
|
444 |
this.central_time = _now; |
|
445 |
} |
|
446 |
var _timescale = this.timescales[this.level]; |
|
447 |
this.current_scale = this.main_width / (_timescale.span); |
|
448 |
this.start_time = this.central_time - (_timescale.span / 2); |
|
449 |
this.end_time = this.central_time + (_timescale.span / 2); |
|
450 |
this.throttledDrawGrid(); |
|
97 | 451 |
}; |
81 | 452 |
|
453 |
Tlns.Classes.Timeline.prototype.drawGrid = function() { |
|
454 |
var _now = new Date().valueOf(), |
|
455 |
_timescale = this.timescales[this.level], |
|
456 |
_offset = new Date().getTimezoneOffset() * 60000, |
|
457 |
_grid_width = Math.floor(_timescale.grid_interval * this.current_scale), |
|
458 |
_roundstart = Math.floor((this.start_time - _offset) / _timescale.grid_interval) * _timescale.grid_interval + _offset, |
|
459 |
_html = ''; |
|
460 |
for (var _t = _roundstart; _t < this.end_time; _t += _timescale.grid_interval) { |
|
97 | 461 |
var _x = this.current_scale * (_t - this.start_time), |
462 |
isMajor = !((_t - _offset)%(24*60*60*1000)); |
|
81 | 463 |
if (_x > 0) { |
97 | 464 |
_html += '<div class="Tl-Grid-Column' + (isMajor ? ' Tl-Grid-Major':'') + '" style="width:' + _grid_width + 'px; left: ' + _x + 'px">' |
81 | 465 |
+ '<div class="Tl-Grid-Label">' + Tlns.Utils.dateFormat(_t, _timescale.grid_date_format) + '</div></div>'; |
466 |
} |
|
467 |
} |
|
468 |
if (this.start_time <= _now && this.end_time >= _now) { |
|
97 | 469 |
_html += '<div class="Tl-Grid-Now" style="left: ' + this.current_scale * (_now - this.start_time) + 'px"></div>'; |
470 |
} |
|
471 |
if (this.editing_occurrence && this.editing_occurrence.date <= this.end_time && this.editing_occurrence.date >= this.start_time) { |
|
472 |
_html += '<div class="Tl-Grid-Editing" style="left: ' + this.editing_occurrence.x + 'px"></div>'; |
|
81 | 473 |
} |
474 |
this.$.find('.Tl-Grid').html(_html); |
|
475 |
this.drawOccurrences(); |
|
97 | 476 |
}; |
81 | 477 |
|
478 |
Tlns.Classes.Timeline.prototype.loadOccurrences = function() { |
|
479 |
var _this = this; |
|
99 | 480 |
|
481 |
function getData(cursor) { |
|
482 |
$.getJSON(_this.api_endpoint, { |
|
483 |
method: _this.api_method, |
|
484 |
api_key: _this.token, |
|
485 |
mail: _this.email, |
|
486 |
cursor: cursor |
|
487 |
}, function(_data) { |
|
488 |
_this.onOccurrencesLoaded(_data); |
|
489 |
if (_data.cursor.hasNext) { |
|
490 |
getData(_data.cursor.next); |
|
491 |
} |
|
492 |
}); |
|
493 |
} |
|
494 |
|
|
495 |
getData(); |
|
81 | 496 |
|
97 | 497 |
}; |
81 | 498 |
|
88 | 499 |
Tlns.Classes.Timeline.prototype.onOccurrencesLoaded = function(_data) { |
500 |
for (var _i = 0; _i < _data.data.length; _i++) { |
|
501 |
this.createOrUpdateOccurrence(_data.data[_i]); |
|
81 | 502 |
} |
503 |
if (!this.mouse_down) { |
|
504 |
this.drawOccurrences(); |
|
505 |
} |
|
97 | 506 |
}; |
81 | 507 |
|
508 |
Tlns.Classes.Timeline.prototype.deleteOccurrence = function(_id) { |
|
509 |
this.occurrences = _(this.occurrences).reject(function(_occ) { |
|
510 |
return _occ.id == _id; |
|
511 |
}); |
|
97 | 512 |
}; |
81 | 513 |
|
514 |
Tlns.Classes.Timeline.prototype.getOccurrence = function(_id) { |
|
515 |
return _(this.occurrences).find(function(_occ) { |
|
516 |
return _occ.id == _id; |
|
517 |
}); |
|
97 | 518 |
}; |
81 | 519 |
|
88 | 520 |
Tlns.Classes.Timeline.prototype.createOrUpdateOccurrence = function(_data) { |
521 |
var _id = _data.id, |
|
96 | 522 |
_occurrence = this.getOccurrence(_id), |
523 |
typeinfo = this.class_info[_data.__CLASS__]; |
|
524 |
if (typeinfo && typeinfo.show) { |
|
525 |
if (typeof _occurrence === "undefined") { |
|
526 |
_occurrence = new Tlns.Classes.Occurrence(this); |
|
527 |
this.occurrences.push(_occurrence); |
|
528 |
} |
|
529 |
_occurrence.update(_data); |
|
81 | 530 |
} |
531 |
return _occurrence; |
|
97 | 532 |
}; |
81 | 533 |
|
95 | 534 |
Tlns.Classes.Timeline.prototype.showTooltip = function(_x, _y, _html) { |
81 | 535 |
this.$.find('.Tl-Overlay-Box') |
536 |
.show() |
|
537 |
.css({ |
|
538 |
left: _x + "px", |
|
539 |
top: _y + "px" |
|
540 |
}); |
|
541 |
this.$.find('.Tl-Overlay-Main').html(_html); |
|
85 | 542 |
|
97 | 543 |
}; |
81 | 544 |
|
545 |
Tlns.Classes.Timeline.prototype.hideTooltip = function() { |
|
546 |
this.$.find('.Tl-Overlay-Box').hide(); |
|
97 | 547 |
}; |
81 | 548 |
|
549 |
Tlns.Classes.Timeline.prototype.drawOccurrences = function() { |
|
85 | 550 |
var _this = this; |
551 |
_(this.occurrences).each(function(_occ) { |
|
81 | 552 |
_occ.x = _this.current_scale * (_occ.date - _this.start_time); |
553 |
_occ.y = _occ.univers.y; |
|
554 |
}); |
|
96 | 555 |
var minT = this.timeFromX(-32), |
93 | 556 |
minI = this.timescales[this.level].min_importance; |
85 | 557 |
var _visible = _(this.occurrences).filter(function(_occ) { |
101 | 558 |
_occ.visible = (_occ.date >= minT && _occ.date <= _this.end_time && (_occ.importance >= minI)); |
559 |
return _occ.visible; |
|
85 | 560 |
}); |
81 | 561 |
|
101 | 562 |
/* FILTRAGE SI TROP D'OCCURRENCES PAR UNITE DE TEMPS */ |
102
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
563 |
/* Commenter la partie ci-dessous pour les tests */ |
101 | 564 |
|
565 |
var _timescale = this.timescales[this.level], |
|
566 |
_offset = new Date().getTimezoneOffset() * 60000, |
|
567 |
_grid_width = Math.floor(_timescale.grid_interval * this.current_scale), |
|
568 |
_roundstart = Math.floor((this.start_time - _offset) / _timescale.grid_interval) * _timescale.grid_interval + _offset, |
|
569 |
_html = ''; |
|
570 |
for (var _t = _roundstart; _t < this.end_time; _t += _timescale.grid_interval) { |
|
571 |
var items = _(_visible).filter(function(_occ) { |
|
572 |
return _occ.date >= _t && _occ.date < _t + _timescale.grid_interval; |
|
573 |
}); |
|
574 |
if (items && items.length > 1) { |
|
575 |
_(items).chain().rest().each(function(_occ) { |
|
576 |
_occ.visible = false; |
|
577 |
}); |
|
578 |
} |
|
579 |
} |
|
580 |
var _visible = _(_visible).filter(function(_occ) { return _occ.visible; }); |
|
581 |
|
|
582 |
/* FIN FILTRAGE */ |
|
583 |
|
|
584 |
/* REORGANISATION DES PICTOS SI TROP NOMBREUX */ |
|
585 |
|
|
90 | 586 |
var _moved = true, l = 0; |
91 | 587 |
while (_moved && l < 10) { |
90 | 588 |
l++; |
81 | 589 |
_moved = false; |
590 |
for (var _i = 0; _i < _visible.length; _i++) { |
|
591 |
for (var _j = 0; _j < _i; _j++) { |
|
90 | 592 |
var delta = Math.abs(_visible[_j].x-_visible[_i].x); |
81 | 593 |
if (_visible[_j].univers_id == _visible[_i].univers_id |
90 | 594 |
&& delta < this.grid_spacing |
81 | 595 |
) { |
90 | 596 |
var sign = _visible[_i].x < _visible[_j].x ? 1 : -1, |
91 | 597 |
add = sign * (this.grid_spacing - delta) / 2; |
81 | 598 |
_moved = true; |
90 | 599 |
_visible[_i].x -= add; |
600 |
_visible[_j].x += add; |
|
81 | 601 |
} |
602 |
} |
|
603 |
} |
|
604 |
} |
|
605 |
|
|
101 | 606 |
/* FIN REORGANISATION */ |
607 |
|
|
81 | 608 |
var _html = Mustache.to_html(Tlns.Templates.Occurrence, { |
90 | 609 |
occurrences: _visible |
81 | 610 |
}); |
611 |
this.$.find('.Tl-Occurrences').html(_html); |
|
90 | 612 |
|
102
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
613 |
this.$.find('.Tl-Occurrence').mousedown(function() { // Clic sur un contenu |
81 | 614 |
var _el = $(this), |
615 |
_id = _el.attr("occurrence-id"); |
|
616 |
if (typeof _id !== "undefined") { |
|
617 |
_this.editing_occurrence = _this.getOccurrence(_id); |
|
83 | 618 |
if (!_this.editing_occurrence.editing) { |
619 |
_(_this.occurrences).each(function(_occ) { |
|
620 |
_occ.editing = false; |
|
621 |
}); |
|
81 | 622 |
_this.editing_occurrence.editing = true; |
94 | 623 |
_this.$.find(".Tl-Details").html(Mustache.to_html(Tlns.Templates.OccurrenceDetails, _this.editing_occurrence)); |
81 | 624 |
} |
83 | 625 |
_this.throttledDrawGrid(); |
81 | 626 |
} |
102
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
627 |
}).mouseover(function(_event) { // Hover sur un contenu |
81 | 628 |
var _el = $(this), |
629 |
_id = _el.attr("occurrence-id"); |
|
630 |
if (typeof _id !== "undefined") { |
|
631 |
var _occurrence = _this.getOccurrence(_id); |
|
632 |
if (!_this.is_dragging) { |
|
95 | 633 |
var _html = Mustache.to_html(Tlns.Templates.OccurrenceTooltip, _occurrence); |
97 | 634 |
_this.showTooltip(_occurrence.x, _occurrence.y, _html); |
81 | 635 |
} |
636 |
} |
|
637 |
}).mouseout(function() { |
|
638 |
var _el = $(this), |
|
639 |
_id = _el.attr("occurrence-id"); |
|
640 |
if (typeof _id !== "undefined") { |
|
641 |
var _occurrence = _this.getOccurrence(_id); |
|
642 |
_this.hideTooltip(); |
|
643 |
} |
|
644 |
}); |
|
90 | 645 |
|
98 | 646 |
if (this.editing_occurrence) { |
647 |
$(".Tl-Grid-Editing, .Tl-Detail-X").css("left", this.editing_occurrence.x); |
|
648 |
if (this.editing_occurrence.date > this.end_time || this.editing_occurrence.date < this.start_time) { |
|
649 |
$(".Tl-Detail-X").hide(); |
|
650 |
} else { |
|
651 |
$(".Tl-Detail-X").show(); |
|
652 |
} |
|
653 |
} |
|
654 |
|
|
97 | 655 |
}; |
81 | 656 |
|
657 |
Tlns.Classes.Timeline.prototype.getUnivers = function(_id) { |
|
658 |
return _(this.univers).find(function(_univ) { |
|
659 |
return (_univ.id == _id); |
|
660 |
}); |
|
97 | 661 |
}; |
81 | 662 |
|
663 |
/* |
|
664 |
* Univers |
|
665 |
*/ |
|
666 |
||
667 |
Tlns.Classes.Univers = function(_data, _timeline, _index) { |
|
92 | 668 |
this.id = this.index = _index; |
88 | 669 |
this.title = _data; |
81 | 670 |
// this.mainCharacter = _data.personnage; |
671 |
this.y = (_timeline.univers_height * _index); |
|
672 |
||
673 |
this.$label = $('<li>').css({ |
|
674 |
height : _timeline.univers_height + "px" |
|
88 | 675 |
}).html(Mustache.to_html(Tlns.Templates.Univers, this)); |
81 | 676 |
|
677 |
_timeline.$.find('.Tl-UniversLabels').append(this.$label); |
|
89 | 678 |
|
679 |
var txtdiv = this.$label.find(".Tl-UniversText"); |
|
680 |
txtdiv.css("margin-top", Math.floor((_timeline.univers_height - txtdiv.height()) / 2)); |
|
97 | 681 |
}; |
81 | 682 |
|
683 |
/* |
|
684 |
* Occurrence |
|
685 |
*/ |
|
686 |
||
687 |
Tlns.Classes.Occurrence = function(_timeline) { |
|
688 |
this.timeline = _timeline; |
|
97 | 689 |
}; |
81 | 690 |
|
88 | 691 |
Tlns.Classes.Occurrence.prototype.update = function(_data) { |
102
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
692 |
|
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
693 |
/* Récupération des propriétés du JSON */ |
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
694 |
|
88 | 695 |
this.original_data = _data; |
696 |
this.id = _data.id; |
|
697 |
this.date = new Date(1000 * (_data.dateFirstPublication || _data.dateCreate) || Date.now); |
|
98 | 698 |
this.formatted_date = Tlns.Utils.dateFormat(this.date,this.timeline.tooltip_date_format); |
88 | 699 |
this.title = _data.title; |
92 | 700 |
this.type = _data.__CLASS__; |
93 | 701 |
this.importance = _data.importance; |
96 | 702 |
var typeinfo = this.timeline.class_info[_data.__CLASS__]; |
703 |
this.univers_id = typeinfo.univers_id; |
|
97 | 704 |
var media = _(_data.contentHasMedias).find(function(m) { |
705 |
return !!m.media.carre; |
|
706 |
}); |
|
707 |
if (media) { |
|
708 |
this.image = media.media.carre.replace(/carre\/[\d]+\/[\d]+/,'carre/32/32'); |
|
709 |
this.detail_image = media.media.carre.replace(/carre\/[\d]+\/[\d]+/,'carre/135/135'); |
|
96 | 710 |
} |
711 |
if (typeinfo.picto) { |
|
712 |
this.image = this.timeline.picto_url + typeinfo.picto; |
|
92 | 713 |
} |
714 |
this.univers = this.timeline.univers[this.univers_id]; |
|
96 | 715 |
this.format = typeinfo.label; |
102
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
716 |
|
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
717 |
/* Données temporaires aléatoires */ |
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
718 |
this.isFavorite = (Math.random() > 1/2); // A random Boolean ;-) |
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
719 |
if (this.univers_id >= 2) { |
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
720 |
this.participationCount = Math.floor(12*Math.random()); |
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
721 |
} |
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
722 |
/* End Temporary Data */ |
2f1ef2ded30c
Added favorites display and participation count with random data
veltr
parents:
101
diff
changeset
|
723 |
|
88 | 724 |
var _tmp = $('<p>').html(_data.resume || ""); |
94 | 725 |
var trimmedDesc = _tmp.text().trim().replace(/(\n|\r|\r\n)/mg,' '); |
726 |
this.description = trimmedDesc.replace(/(^.{60,80})[\s].+$/m,'$1…'); |
|
727 |
this.detail_description = trimmedDesc.replace(/(^.{360,380})[\s].+$/m,'$1…'); |
|
728 |
this.url = this.timeline.url_base + _data.url; |
|
97 | 729 |
}; |
81 | 730 |
|
731 |
Tlns.Classes.Occurrence.prototype.toString = function() { |
|
732 |
return "Occurrence " + this.type + ': "' + this.title + '"'; |
|
97 | 733 |
}; |