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