author | veltr |
Fri, 25 May 2012 16:45:40 +0200 | |
changeset 69 | 7bb5f89ad242 |
parent 68 | 4def147b1604 |
child 70 | 43012525c832 |
permissions | -rw-r--r-- |
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: [], |
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
118 |
cluster_spacing: 10 |
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>' |
|
67 | 131 |
+ '<div class="Tl-BottomPart"><ul class="Tl-UniversLabels"></ul><div class="Tl-MainPart"><div class="Tl-Layer Tl-Grid"></div><canvas class="Tl-Layer Tl-Canvas"></canvas><div class="Tl-Layer Tl-Occurrences"></div></div>' |
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
132 |
+ '<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>'; |
65 | 133 |
|
66 | 134 |
Tlns.Templates.Univers = '<span class="Tl-UniversText">{{title}}</span>'; |
135 |
||
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
136 |
Tlns.Templates.Occurrence = '{{#clusters}}<div class="Tl-Cluster Tl-Occ{{type}}" style="left: {{x}}px; top: {{y}}px;" cluster-contents="{{contents}}"><div class="Tl-ClusterCount">{{occurrences.length}}</div></div>{{/clusters}}{{#occurrences}}<div class="Tl-Occurrence Tl-OccOnGrid Tl-Occ{{type}}{{#editing}} Tl-Editing{{/editing}}" occurrence-type="{{type}}" occurrence-id="{{id}}" style="left: {{x}}px; top: {{y}}px;"></div>{{/occurrences}}{{#open_cluster}}<div class="Tl-ClusterOverlay" style="left: {{x}}px; top: {{y}}px;">{{#occurrences}}<div class="Tl-Occurrence Tl-OccInCluster Tl-Occ{{type}}{{#editing}} Tl-Editing{{/editing}}" occurrence-type="{{type}}" occurrence-id="{{id}}"></div>{{/occurrences}}</div>{{/open_cluster}}'; |
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"); |
|
67 | 157 |
this.$.find('.Tl-Overlay-Container').css("left", (this.$.find('.Tl-BottomPart').outerWidth() - this.main_width) + "px"); |
158 |
this.$.find('.Tl-Canvas').attr({ |
|
159 |
width: this.main_width, |
|
160 |
height: this.main_height |
|
161 |
}); |
|
65 | 162 |
var _o = this.$.find('.Tl-MainPart').offset(); |
163 |
this.dragging_bounds = { |
|
164 |
left: _o.left, |
|
165 |
top: _o.top, |
|
166 |
right: _o.left + this.$.find('.Tl-MainPart').outerWidth(), |
|
167 |
bottom: _o.top + this.$.find('.Tl-MainPart').outerHeight(), |
|
168 |
}; |
|
169 |
|
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
170 |
var _this = this; |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
171 |
|
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
172 |
this.throttledDrawGrid = _.throttle(function() { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
173 |
_this.drawGrid(); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
174 |
}, 150); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
175 |
|
65 | 176 |
this.setLevel(this.level); |
177 |
|
|
178 |
this.$.find('.Tl-TopBar-Timescales>div').click(function() { |
|
179 |
_this.setLevel($(this).attr("data-level")); |
|
180 |
}); |
|
181 |
|
|
182 |
this.$.find('.Tl-TopBar-SyncButton').click(function() { |
|
183 |
_this.sync_now = !_this.sync_now; |
|
184 |
_this.drawGrid(); |
|
185 |
}) |
|
186 |
|
|
187 |
this.$.find('.Tl-TopBar-PreviousButton').click(function() { |
|
188 |
_this.offsetTime(-_this.timescales[_this.level].span / 4); |
|
189 |
}); |
|
190 |
|
|
191 |
this.$.find('.Tl-TopBar-NextButton').click(function() { |
|
192 |
_this.offsetTime(_this.timescales[_this.level].span / 4); |
|
193 |
}); |
|
194 |
|
|
195 |
this.$.find('.Tl-MainPart').mousedown(function(_event) { |
|
196 |
_this.onMouseDown(_event); |
|
197 |
return false; |
|
198 |
}); |
|
199 |
|
|
200 |
this.$.find('.Tl-MainPart').mousemove(function(_event) { |
|
201 |
_this.onMouseMove(_event); |
|
202 |
return false; |
|
203 |
}); |
|
204 |
|
|
205 |
this.$.find('.Tl-MainPart').mouseup(function(_event) { |
|
206 |
_this.onMouseUp(_event); |
|
207 |
return false; |
|
208 |
}); |
|
209 |
|
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
210 |
this.$.find('.Tl-MainPart').mousewheel(function(_event, _delta) { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
211 |
var _newLevel = Math.max(0,Math.min(_this.timescales.length-1, (_delta < 0 ? -1 : 1) + parseInt(_this.level))); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
212 |
if (_newLevel != _this.level) { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
213 |
_this.hideTooltip(); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
214 |
var _deltaX = _event.pageX - _this.dragging_bounds.left, |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
215 |
_tAtMouse = _this.timeFromMouse(_event.pageX), |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
216 |
_newScale = _this.main_width / (_this.timescales[_newLevel].span), |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
217 |
_newStart = _tAtMouse - _deltaX / _newScale; |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
218 |
_this.central_time = _newStart + _this.timescales[_newLevel].span / 2; |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
219 |
_this.setLevel(_newLevel); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
220 |
} |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
221 |
return false; |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
222 |
}); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
223 |
|
66 | 224 |
this.$.find('.Tl-Overlay-Box').mouseover(function(_event) { |
225 |
$(this).show(); |
|
226 |
}).mouseout(function(_event) { |
|
227 |
$(this).hide(); |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
228 |
}); |
65 | 229 |
|
230 |
/* Loading Univers */ |
|
231 |
$.getJSON(this.url_univers, function(_data) { |
|
232 |
_this.onUniversLoaded(_data); |
|
233 |
}); |
|
234 |
} |
|
235 |
||
236 |
Tlns.Classes.Timeline.prototype.onMouseDown = function(_event) { |
|
237 |
this.mouse_down = true; |
|
238 |
this.is_dragging = false; |
|
239 |
this.start_pos = { |
|
240 |
x: _event.pageX, |
|
241 |
y: _event.pageY |
|
242 |
}; |
|
66 | 243 |
if (typeof this.dragging_type === "undefined") { |
67 | 244 |
this.time_at_start = this.central_time; |
66 | 245 |
this.dragging_type = "timeline"; |
65 | 246 |
} |
247 |
} |
|
248 |
||
249 |
Tlns.Classes.Timeline.prototype.onMouseUp = function(_event) { |
|
67 | 250 |
if (this.is_dragging) { |
251 |
switch (this.dragging_type) { |
|
252 |
case "occurrence": |
|
253 |
this.editing_occurrence.editing = false; |
|
254 |
this.throttledDrawGrid(); |
|
255 |
break; |
|
256 |
} |
|
257 |
} |
|
65 | 258 |
this.mouse_down = false; |
259 |
this.is_dragging = false; |
|
66 | 260 |
this.dragging_type = undefined; |
65 | 261 |
} |
262 |
||
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
263 |
Tlns.Classes.Timeline.prototype.timeFromX = function(_x) { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
264 |
return Math.max(this.start_time,Math.min(this.end_time, this.start_time + _x / this.current_scale)); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
265 |
} |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
266 |
|
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
267 |
Tlns.Classes.Timeline.prototype.timeFromMouse = function(_pageX) { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
268 |
return this.timeFromX(_pageX - this.dragging_bounds.left); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
269 |
} |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
270 |
|
65 | 271 |
Tlns.Classes.Timeline.prototype.onMouseMove = function(_event) { |
272 |
if (this.mouse_down) { |
|
273 |
this.is_dragging = true; |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
274 |
this.hideTooltip(); |
69 | 275 |
switch (this.dragging_type) { |
276 |
case "occurrence": |
|
277 |
var _d = this.timeFromMouse(_event.pageX); |
|
278 |
this.editing_occurrence.date = _d; |
|
279 |
var _u = Math.max(0,Math.min(this.univers.length, Math.floor((_event.pageY - this.dragging_bounds.top) / this.univers_height))); |
|
280 |
this.editing_occurrence.univers = this.univers[_u]; |
|
281 |
this.editing_occurrence.univers_id = this.univers[_u].id; |
|
282 |
this.throttledDrawGrid(); |
|
283 |
break; |
|
284 |
case "timeline": |
|
285 |
this.setTime(this.time_at_start + Math.floor(( this.start_pos.x - _event.pageX ) / this.current_scale)); |
|
286 |
break; |
|
65 | 287 |
} |
288 |
} |
|
289 |
} |
|
290 |
||
291 |
Tlns.Classes.Timeline.prototype.onUniversLoaded = function(_data) { |
|
292 |
this.univers = []; |
|
293 |
if(_data.length) { |
|
294 |
this.univers_height = Math.floor(this.main_height / _data.length); |
|
295 |
} |
|
296 |
for(var _i = 0; _i < _data.length; _i++) { |
|
297 |
this.univers.push(new Tlns.Classes.Univers(_data[_i], this, _i)); |
|
298 |
} |
|
66 | 299 |
this.loadOccurrences(); |
65 | 300 |
} |
301 |
||
302 |
Tlns.Classes.Timeline.prototype.offsetTime = function(_timeOffset) { |
|
303 |
this.setTime(this.central_time + _timeOffset); |
|
304 |
} |
|
305 |
||
306 |
Tlns.Classes.Timeline.prototype.setTime = function(_centralTime) { |
|
307 |
this.sync_now = false; |
|
308 |
this.central_time = _centralTime; |
|
67 | 309 |
this.throttledDrawGrid(); |
65 | 310 |
} |
311 |
||
312 |
Tlns.Classes.Timeline.prototype.setLevel = function(_level) { |
|
313 |
if (_level >= 0 && _level < this.timescales.length) { |
|
314 |
this.$.find('.Tl-TopBar-Timescales>div').each(function() { |
|
315 |
var _el = $(this); |
|
316 |
if (_el.attr("data-level") == _level) { |
|
317 |
_el.addClass("active"); |
|
318 |
} else { |
|
319 |
_el.removeClass("active"); |
|
320 |
} |
|
321 |
}); |
|
322 |
this.level = _level; |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
323 |
this.throttledDrawGrid(); |
65 | 324 |
} |
325 |
} |
|
326 |
||
327 |
Tlns.Classes.Timeline.prototype.drawGrid = function() { |
|
328 |
var _now = new Date().valueOf(); |
|
329 |
if (this.sync_now) { |
|
330 |
this.central_time = _now; |
|
331 |
this.$.find('.Tl-TopBar-SyncButton').addClass("active"); |
|
332 |
} else { |
|
333 |
this.$.find('.Tl-TopBar-SyncButton').removeClass("active"); |
|
334 |
} |
|
335 |
var _timescale = this.timescales[this.level], |
|
336 |
_offset = new Date().getTimezoneOffset() * 60000; |
|
66 | 337 |
this.current_scale = this.main_width / (_timescale.span) |
338 |
this.start_time = this.central_time - (_timescale.span / 2); |
|
339 |
this.end_time = this.central_time + (_timescale.span / 2); |
|
340 |
var _grid_width = Math.floor(_timescale.grid_interval * this.current_scale), |
|
341 |
_roundstart = Math.floor((this.start_time - _offset) / _timescale.grid_interval) * _timescale.grid_interval + _offset, |
|
65 | 342 |
_html = ''; |
66 | 343 |
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)); |
344 |
for (var _t = _roundstart; _t < this.end_time; _t += _timescale.grid_interval) { |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
345 |
var _x = this.current_scale * (_t - this.start_time); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
346 |
if (_x > 0) { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
347 |
_html += '<div class="Tl-Grid-Column" style="width:' + _grid_width + 'px; left: ' + _x + 'px">' |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
348 |
+ '<div class="Tl-Grid-Label">' + Tlns.Utils.dateFormat(_t, _timescale.grid_date_format) + '</div></div>'; |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
349 |
} |
65 | 350 |
} |
351 |
/* |
|
352 |
|
|
353 |
for (var _t = _roundstart; _t < _tmax; _t += _timescale.grid_interval) { |
|
354 |
var _isMajor = !(Math.floor((_t - _offset) / _timescale.grid_interval) % _timescale.major_interval); |
|
67 | 355 |
_html += '<div class="Tl-Grid-Column' + ( _isMajor ? ' Tl-Grid-Major' : '' ) + '" style="width:' + _grid_width + 'px; left: ' + _scale * (_t - this.start_time) + 'px">' |
65 | 356 |
+ ( _isMajor ? '<div class="Tl-Grid-Label">' + Tlns.Utils.dateFormat(_t, _timescale.date_format) + '</div>' : '' ) + '</div>'; |
357 |
} |
|
358 |
*/ |
|
66 | 359 |
if (this.start_time <= _now && this.end_time >= _now) { |
67 | 360 |
_html += '<div class="Tl-Grid-Now" style="left: ' + this.current_scale * (_now - this.start_time) + 'px"></div>' |
65 | 361 |
} |
362 |
this.$.find('.Tl-Grid').html(_html); |
|
66 | 363 |
this.drawOccurrences(); |
364 |
} |
|
365 |
||
366 |
Tlns.Classes.Timeline.prototype.loadOccurrences = function() { |
|
367 |
var _url = Mustache.to_html(this.url_occurrences, { |
|
368 |
from_time: this.start_time, |
|
369 |
to_time: this.end_time |
|
370 |
}), |
|
371 |
_this = this; |
|
372 |
$.getJSON(_url, function(_data) { |
|
373 |
_this.onOccurrencesLoaded(_data); |
|
374 |
}); |
|
375 |
} |
|
376 |
||
377 |
Tlns.Classes.Timeline.prototype.onOccurrencesLoaded = function(_data) { |
|
378 |
if (typeof _data.occurrencesNarratives === "object" && _data.occurrencesNarratives !== null) { |
|
379 |
for (var _i = 0; _i < _data.occurrencesNarratives.length; _i++) { |
|
380 |
this.createOrUpdateOccurrence("narrative", _data.occurrencesNarratives[_i]); |
|
381 |
} |
|
382 |
for (var _i = 0; _i < _data.occurrencesProduction.length; _i++) { |
|
383 |
this.createOrUpdateOccurrence("production", _data.occurrencesProduction[_i]); |
|
384 |
} |
|
385 |
} |
|
386 |
if (!this.mouse_down) { |
|
387 |
this.drawOccurrences(); |
|
388 |
} |
|
389 |
} |
|
390 |
||
391 |
Tlns.Classes.Timeline.prototype.getOccurrence = function(_type, _id) { |
|
392 |
return _(this.occurrences).find(function(_occ) { |
|
393 |
return (_occ.type == _type && _occ.id == _id); |
|
394 |
}); |
|
395 |
} |
|
396 |
||
397 |
Tlns.Classes.Timeline.prototype.createOrUpdateOccurrence = function(_type, _data) { |
|
398 |
var _occurrence = this.getOccurrence(_type, _data.id); |
|
399 |
if (typeof _occurrence === "undefined") { |
|
400 |
_occurrence = new Tlns.Classes.Occurrence(this); |
|
401 |
this.occurrences.push(_occurrence); |
|
402 |
} |
|
403 |
_occurrence.update(_type, _data); |
|
404 |
} |
|
405 |
||
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
406 |
Tlns.Classes.Timeline.prototype.showTooltip = function(_x, _y, _html, _isUp) { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
407 |
this.$.find('.Tl-Overlay-Box') |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
408 |
.removeClass(_isUp ? 'Tl-Overlay-Down' : 'Tl-Overlay-Up') |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
409 |
.addClass(_isUp ? 'Tl-Overlay-Up' : 'Tl-Overlay-Down') |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
410 |
.show() |
66 | 411 |
.css({ |
412 |
left: _x + "px", |
|
413 |
top: _y + "px" |
|
414 |
}); |
|
415 |
this.$.find('.Tl-Overlay-Main').html(_html); |
|
416 |
} |
|
417 |
||
418 |
Tlns.Classes.Timeline.prototype.hideTooltip = function() { |
|
419 |
this.$.find('.Tl-Overlay-Box').hide(); |
|
65 | 420 |
} |
421 |
||
422 |
Tlns.Classes.Timeline.prototype.drawOccurrences = function() { |
|
66 | 423 |
var _this = this, |
424 |
_visible = _(this.occurrences).filter(function(_occ) { |
|
425 |
return (_occ.date >= _this.start_time && _occ.date <= _this.end_time && _occ.published); |
|
426 |
}); |
|
427 |
_(_visible).each(function(_occ) { |
|
67 | 428 |
_occ.x = _this.current_scale * (_occ.date - _this.start_time); |
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
429 |
_occ.y = _occ.univers.y; |
66 | 430 |
_occ.in_cluster = false; |
431 |
}); |
|
65 | 432 |
|
66 | 433 |
var _moved = true; |
434 |
while (_moved) { |
|
435 |
_moved = false; |
|
436 |
for (var _i = 0; _i < _visible.length; _i++) { |
|
437 |
for (var _j = 0; _j < _i; _j++) { |
|
438 |
if (_visible[_j].univers_id == _visible[_i].univers_id |
|
439 |
&& _visible[_j].x != _visible[_i].x |
|
440 |
&& Math.abs(_visible[_j].x-_visible[_i].x) < this.cluster_spacing |
|
441 |
) { |
|
442 |
_moved = true; |
|
443 |
_visible[_i].x = this.cluster_spacing * Math.round(_visible[_i].x / this.cluster_spacing); |
|
444 |
_visible[_j].x = this.cluster_spacing * Math.round(_visible[_j].x / this.cluster_spacing); |
|
445 |
} |
|
446 |
} |
|
447 |
} |
|
448 |
} |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
449 |
var _clusters = [], |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
450 |
_openCluster = false; |
66 | 451 |
for (var _i = 0; _i < _visible.length; _i++) { |
452 |
for (var _j = 0; _j < _i; _j++) { |
|
453 |
if (_visible[_j].univers_id == _visible[_i].univers_id && _visible[_j].x == _visible[_i].x) { |
|
454 |
_visible[_j].in_cluster = true; |
|
455 |
_visible[_i].in_cluster = true; |
|
456 |
var _x = _visible[_j].x, |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
457 |
_y = _visible[_j].y; |
66 | 458 |
_cluster = _(_clusters).find(function(_c) { return _c.x == _x && _c.y == _y }); |
459 |
if (typeof _cluster === "undefined") { |
|
460 |
_cluster = { x: _x, y: _y, occurrences: [] }; |
|
461 |
_clusters.push(_cluster); |
|
462 |
} |
|
463 |
if ("undefined" === typeof _(_cluster.occurrences).find(function(_o) { |
|
464 |
return _o.type == _visible[_j].type && _o.id == _visible[_j].id; |
|
465 |
})) { |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
466 |
_cluster.occurrences.push(_visible[_j]); |
66 | 467 |
} |
468 |
if ("undefined" === typeof _(_cluster.occurrences).find(function(_o) { |
|
469 |
return _o.type == _visible[_i].type && _o.id == _visible[_i].id; |
|
470 |
})) { |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
471 |
_cluster.occurrences.push(_visible[_i]); |
66 | 472 |
} |
473 |
} |
|
474 |
} |
|
475 |
} |
|
476 |
_(_clusters).each(function(_cluster) { |
|
477 |
_cluster.type = _cluster.occurrences[0].type; |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
478 |
_cluster.contents = _cluster.occurrences.map(function(_o) { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
479 |
return _o.type + ":" + _o.id; |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
480 |
}).join("|"); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
481 |
if (_cluster.contents == _this.open_cluster) { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
482 |
_openCluster = _cluster; |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
483 |
} |
66 | 484 |
for (var _i = 1; _i < _cluster.occurrences.length; _i++) { |
485 |
if (_cluster.occurrences[_i].type !== _cluster.type) { |
|
486 |
_cluster.type = "both"; |
|
487 |
break; |
|
488 |
} |
|
489 |
} |
|
490 |
}); |
|
491 |
|
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
492 |
|
67 | 493 |
var _links = []; |
494 |
|
|
495 |
_(_visible).each(function(_occurrence) { |
|
496 |
_(_occurrence.dependsOn).each(function(_dependance) { |
|
497 |
var _parent = _(_visible).find(function(_o) { |
|
498 |
return _o.type == "narrative" && _o.id == _dependance; |
|
499 |
}); |
|
500 |
if (typeof _parent !== "undefined") { |
|
501 |
_links.push({ |
|
502 |
from_x: _occurrence.x, |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
503 |
from_y: _occurrence.y + Math.floor(_this.univers_height / 2), |
67 | 504 |
to_x: _parent.x, |
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
505 |
to_y: _parent.y + Math.floor(_this.univers_height / 2) |
67 | 506 |
}); |
507 |
} |
|
508 |
}); |
|
509 |
}); |
|
510 |
|
|
511 |
var _ctx = this.$.find('.Tl-Canvas')[0].getContext('2d'); |
|
512 |
_ctx.clearRect(0,0,this.main_width, this.main_height); |
|
513 |
_(_links).each(function(_link) { |
|
514 |
_ctx.beginPath(); |
|
515 |
_ctx.moveTo(_link.from_x,_link.from_y); |
|
516 |
_ctx.lineTo(_link.to_x,_link.to_y); |
|
517 |
_ctx.stroke(); |
|
518 |
}) |
|
519 |
|
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
520 |
console.log(_openCluster); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
521 |
|
66 | 522 |
var _html = Mustache.to_html(Tlns.Templates.Occurrence, { |
523 |
occurrences:_(_visible).reject(function(_o) {return _o.in_cluster}), |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
524 |
clusters: _clusters, |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
525 |
open_cluster: _openCluster |
66 | 526 |
}); |
527 |
this.$.find('.Tl-Occurrences').html(_html); |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
528 |
|
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
529 |
|
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
530 |
if (_openCluster) { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
531 |
var _w = this.$.find('.Tl-Occurrence').width(), |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
532 |
_ww = _w * _openCluster.occurrences.length; |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
533 |
this.$.find('.Tl-ClusterOverlay').css({ |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
534 |
"margin-left": - Math.floor(_ww/2) + "px", |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
535 |
width: _ww |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
536 |
}); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
537 |
_(_openCluster.occurrences).each(function(_o, _i) { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
538 |
_o.y = _o.y - 20; |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
539 |
_o.x = _o.x - (_ww / 2) + ((_i + .5) * _w); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
540 |
}); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
541 |
} |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
542 |
|
66 | 543 |
this.$.find('.Tl-Occurrence').mousedown(function() { |
67 | 544 |
var _el = $(this); |
545 |
_this.editing_occurrence = _occurrence = _this.getOccurrence(_el.attr("occurrence-type"),_el.attr("occurrence-id")); |
|
546 |
if (typeof _this.editing_occurrence !== "undefined") { |
|
547 |
_this.dragging_type = "occurrence"; |
|
548 |
_this.editing_occurrence.editing = true; |
|
549 |
} |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
550 |
}).mouseover(function(_event) { |
66 | 551 |
var _el = $(this), |
552 |
_occurrence = _this.getOccurrence(_el.attr("occurrence-type"),_el.attr("occurrence-id")); |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
553 |
_this.showTooltip(_occurrence.x, _occurrence.y, _occurrence.title, (_event.pageY - _this.dragging_bounds.top) > (_this.main_height / 2) ); |
66 | 554 |
}).mouseout(function() { |
555 |
_this.hideTooltip(); |
|
556 |
}); |
|
68
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
557 |
this.$.find('.Tl-Cluster').click(function() { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
558 |
var _el = $(this), |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
559 |
_contents = _el.attr("cluster-contents"); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
560 |
if (_this.open_cluster == _contents) { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
561 |
_this.open_cluster = false; |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
562 |
} else { |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
563 |
_this.open_cluster = _contents; |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
564 |
} |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
565 |
console.log(_this.open_cluster); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
566 |
_this.throttledDrawGrid(); |
4def147b1604
Added a visualization of the contents of clusters + mousewheel support
veltr
parents:
67
diff
changeset
|
567 |
}) |
65 | 568 |
} |
569 |
||
66 | 570 |
Tlns.Classes.Timeline.prototype.getUnivers = function(_id) { |
571 |
return _(this.univers).find(function(_univ) { |
|
572 |
return (_univ.id == _id); |
|
573 |
}); |
|
574 |
} |
|
575 |
||
576 |
/* |
|
577 |
* Univers |
|
578 |
*/ |
|
579 |
||
65 | 580 |
Tlns.Classes.Univers = function(_data, _timeline, _index) { |
581 |
this.id = _data.id; |
|
582 |
this.index = _index; |
|
583 |
this.title = _data.nom; |
|
584 |
this.mainCharacter = _data.personnage; |
|
66 | 585 |
this.y = (_timeline.univers_height * _index); |
65 | 586 |
|
587 |
this.$label = $('<li>').css({ |
|
588 |
height : _timeline.univers_height + "px" |
|
589 |
}).html(Mustache.to_html(Tlns.Templates.Univers, this)) |
|
66 | 590 |
.addClass((_index % 2) ? 'Tl-Line-Odd' : 'Tl-Line-Even'); |
65 | 591 |
|
592 |
_timeline.$.find('.Tl-UniversLabels').append(this.$label); |
|
593 |
var _txt = _data.nom, |
|
594 |
_span = this.$label.find('span'); |
|
595 |
||
596 |
while (_span.outerWidth() > (_timeline.width - _timeline.main_width) && _txt) { |
|
597 |
_txt = _txt.substr(0, _txt.length - 1); |
|
598 |
_span.html(_txt + '…'); |
|
599 |
} |
|
600 |
} |
|
66 | 601 |
|
602 |
/* |
|
603 |
* Occurrence |
|
604 |
*/ |
|
605 |
||
606 |
Tlns.Classes.Occurrence = function(_timeline) { |
|
607 |
this.timeline = _timeline; |
|
608 |
} |
|
609 |
||
610 |
Tlns.Classes.Occurrence.prototype.update = function(_type, _data) { |
|
611 |
this.type = _type; |
|
612 |
this.id = _data.id || _.uniqueId(); |
|
613 |
this.date = _data.date || _data.datePublication; |
|
614 |
this.title = _data.titre || "<untitled>"; |
|
615 |
this.univers_id = _data.univers; |
|
616 |
this.univers = this.timeline.getUnivers(this.univers_id); |
|
617 |
this.status = _data.statut; |
|
618 |
this.published = _data.publie || false; |
|
619 |
this.locked = _data.verrouille || false; |
|
620 |
this.characters = _data.personnagesSecondaires || []; |
|
621 |
this.dependsOn = _data.dependDe || []; |
|
622 |
} |
|
623 |
||
624 |
Tlns.Classes.Occurrence.prototype.toString = function() { |
|
625 |
return "Occurrence " + this.type + ': "' + this.title + '"'; |
|
626 |
} |