timeline/js/timeline.js
changeset 90 50e3c4ba168a
parent 89 6b1a038edbf7
child 91 5b770cf1a6e3
equal deleted inserted replaced
89:6b1a038edbf7 90:50e3c4ba168a
   154     central_time: 0,
   154     central_time: 0,
   155     sync_now: true,
   155     sync_now: true,
   156     api_endpoint: "",
   156     api_endpoint: "",
   157     api_method: "searchForTimelineEdito",
   157     api_method: "searchForTimelineEdito",
   158     occurrences: [],
   158     occurrences: [],
   159     cluster_spacing: 12,
   159     grid_spacing: 12,
   160     tooltip_date_format: '{{dayOfMonth}} {{shortMonthName}} {{year}} {{0hours}}:{{0minutes}}',
   160     tooltip_date_format: '{{dayOfMonth}} {{shortMonthName}} {{year}} {{0hours}}:{{0minutes}}',
   161     statuses: {
   161     statuses: {
   162         "valide": "Validée",
   162         "valide": "Validée",
   163         "a_valider": "A valider",
   163         "a_valider": "A valider",
   164         "a_realiser": "A réaliser"
   164         "a_realiser": "A réaliser"
   177     + '</div>'
   177     + '</div>'
   178     + '<div class="Tl-Overlay-Container"><div class="Tl-Overlay-Box"><div class="Tl-Overlay"><div class="Tl-Overlay-Tip-Top"></div><div class="Tl-Overlay-Main"></div><div class="Tl-Overlay-Tip-Bottom"></div></div></div></div></div></div>'
   178     + '<div class="Tl-Overlay-Container"><div class="Tl-Overlay-Box"><div class="Tl-Overlay"><div class="Tl-Overlay-Tip-Top"></div><div class="Tl-Overlay-Main"></div><div class="Tl-Overlay-Tip-Bottom"></div></div></div></div></div></div>'
   179     
   179     
   180 Tlns.Templates.Univers = '<div class="Tl-UniversText">{{title}}</div>';
   180 Tlns.Templates.Univers = '<div class="Tl-UniversText">{{title}}</div>';
   181 
   181 
   182 Tlns.Templates.Occurrence = '{{#clusters}}<div class="Tl-Cluster" style="left: {{x}}px; top: {{y}}px;" cluster-contents="{{contents}}">'
   182 Tlns.Templates.Occurrence =
   183     + '<div class="Tl-ClusterCount">{{occurrences.length}}</div></div>{{/clusters}}'
   183     '{{#occurrences}}<div class="Tl-Occurrence Tl-OccOnGrid Tl-Occ{{type}} Tl-Occ{{status}}" occurrence-id="{{id}}" style="left: {{x}}px; top: {{y}}px;"></div>{{/occurrences}}';
   184     + '{{#occurrences}}<div class="Tl-Occurrence Tl-OccOnGrid Tl-Occ{{type}} Tl-Occ{{status}}{{#editing}} Tl-Editing{{/editing}}" occurrence-id="{{id}}" style="left: {{x}}px; top: {{y}}px;">'
       
   185 //  + '{{#locked}}<div class="Tl-Locked"></div>{{/locked}}'
       
   186     + '</div>{{/occurrences}}{{#open_cluster}}<div class="Tl-ClusterOverlay" style="left: {{x}}px; top: {{y}}px;">'
       
   187     + '{{#occurrences}}<div class="Tl-Occurrence Tl-OccInCluster Tl-Occ{{type}} Tl-Occ{{status}}{{#editing}} Tl-Editing{{/editing}}" occurrence-id="{{id}}">'
       
   188     + '{{#locked}}<div class="Tl-Locked"></div>{{/locked}}</div>{{/occurrences}}</div>{{/open_cluster}}';
       
   189 
   184 
   190 Tlns.Templates.OccurrenceTooltip = '<h3 class="Tl-Tooltip-Title">{{title}}</h3><p class="Tl-Tooltip-Date">{{formatted_date}} &mdash; {{translated_status}} &mdash; {{#jt}}Au JT{{/jt}}{{^jt}}Hors JT{{/jt}}{{#format}} &mdash; {{format}}{{/format}}</p>'
   185 Tlns.Templates.OccurrenceTooltip = '<h3 class="Tl-Tooltip-Title">{{title}}</h3><p class="Tl-Tooltip-Date">{{formatted_date}} &mdash; {{translated_status}} &mdash; {{#jt}}Au JT{{/jt}}{{^jt}}Hors JT{{/jt}}{{#format}} &mdash; {{format}}{{/format}}</p>'
   191     + '<p class="Tl-Tooltip-Description">{{description}}</p>'
   186     + '<p class="Tl-Tooltip-Description">{{description}}</p>'
   192 //    + '<p class="Tl-Tooltip-Characters">{{univers.mainCharacter}}{{#characters}}, {{.}}{{/characters}}</p>'
   187 //    + '<p class="Tl-Tooltip-Characters">{{univers.mainCharacter}}{{#characters}}, {{.}}{{/characters}}</p>'
   193 
   188 
   452 Tlns.Classes.Timeline.prototype.drawOccurrences = function() {
   447 Tlns.Classes.Timeline.prototype.drawOccurrences = function() {
   453     var _this = this;
   448     var _this = this;
   454     _(this.occurrences).each(function(_occ) {
   449     _(this.occurrences).each(function(_occ) {
   455         _occ.x = _this.current_scale * (_occ.date - _this.start_time);
   450         _occ.x = _this.current_scale * (_occ.date - _this.start_time);
   456         _occ.y = _occ.univers.y;
   451         _occ.y = _occ.univers.y;
   457         _occ.in_cluster = false;
       
   458     });
   452     });
   459     var _visible = _(this.occurrences).filter(function(_occ) {
   453     var _visible = _(this.occurrences).filter(function(_occ) {
   460         return (_occ.date >= _this.start_time && _occ.date <= _this.end_time && _occ.status);
   454         return (_occ.date >= _this.start_time && _occ.date <= _this.end_time && _occ.status);
   461     });
   455     });
   462     
   456     
   463     var _moved = true;
   457     var _moved = true, l = 0;
   464     while (_moved) {
   458     while (_moved && l < 200) {
       
   459         l++;
   465         _moved = false;
   460         _moved = false;
   466         for (var _i = 0; _i < _visible.length; _i++) {
   461         for (var _i = 0; _i < _visible.length; _i++) {
   467             for (var _j = 0; _j < _i; _j++) {
   462             for (var _j = 0; _j < _i; _j++) {
       
   463                 var delta = Math.abs(_visible[_j].x-_visible[_i].x);
   468                 if (_visible[_j].univers_id == _visible[_i].univers_id
   464                 if (_visible[_j].univers_id == _visible[_i].univers_id
   469                     && _visible[_j].x != _visible[_i].x
   465                     && delta < this.grid_spacing
   470                     && Math.abs(_visible[_j].x-_visible[_i].x) < this.cluster_spacing
       
   471                 ) {
   466                 ) {
       
   467                     var sign = _visible[_i].x < _visible[_j].x ? 1 : -1,
       
   468                         add = sign * (this.cluster_spacing - delta) / 2;
   472                     _moved = true;
   469                     _moved = true;
   473                     _visible[_i].x = this.cluster_spacing * Math.round(_visible[_i].x / this.cluster_spacing);
   470                     _visible[_i].x -= add;
   474                     _visible[_j].x = this.cluster_spacing * Math.round(_visible[_j].x / this.cluster_spacing);
   471                     _visible[_j].x += add;
   475                 }
   472                 }
   476             }
   473             }
   477         }
   474         }
   478     }
   475     }
   479     var _clusters = [],
       
   480         _openCluster = false;
       
   481     for (var _i = 0; _i < _visible.length; _i++) {
       
   482         for (var _j = 0; _j < _i; _j++) {
       
   483             if (_visible[_j].univers_id == _visible[_i].univers_id && _visible[_j].x == _visible[_i].x) {
       
   484                 _visible[_j].in_cluster = true;
       
   485                 _visible[_i].in_cluster = true;
       
   486                 var _x = _visible[_j].x,
       
   487                     _y = _visible[_j].y;
       
   488                     _cluster = _(_clusters).find(function(_c) { return _c.x == _x && _c.y == _y });
       
   489                 if (typeof _cluster === "undefined") {
       
   490                     _cluster = { x: _x, y: _y, occurrences: [] };
       
   491                     _clusters.push(_cluster);
       
   492                 }
       
   493                 if ("undefined" === typeof _(_cluster.occurrences).find(function(_o) {
       
   494                     return _o.type == _visible[_j].type && _o.id == _visible[_j].id;
       
   495                 })) {
       
   496                     _cluster.occurrences.push(_visible[_j]);
       
   497                 }
       
   498                 if ("undefined" === typeof _(_cluster.occurrences).find(function(_o) {
       
   499                     return _o.type == _visible[_i].type && _o.id == _visible[_i].id;
       
   500                 })) {
       
   501                     _cluster.occurrences.push(_visible[_i]);
       
   502                 }
       
   503             }
       
   504         }
       
   505     }
       
   506     _(_clusters).each(function(_cluster) {
       
   507         _cluster.occurrences = _(_cluster.occurrences).sortBy(function(_o) {
       
   508             return _o.date;
       
   509         });
       
   510         _cluster.contents = _cluster.occurrences.map(function(_o) {
       
   511             return _o.id;
       
   512         }).join("|");
       
   513         if (_cluster.contents == _this.open_cluster) {
       
   514             _openCluster = _cluster;
       
   515         }
       
   516     });
       
   517     
   476     
   518     var _html = Mustache.to_html(Tlns.Templates.Occurrence, {
   477     var _html = Mustache.to_html(Tlns.Templates.Occurrence, {
   519         occurrences:_(_visible).reject(function(_o) {return _o.in_cluster}),
   478         occurrences: _visible
   520         clusters: _clusters,
       
   521         open_cluster: _openCluster
       
   522     });
   479     });
   523     this.$.find('.Tl-Occurrences').html(_html);
   480     this.$.find('.Tl-Occurrences').html(_html);
   524     
   481         
   525     
       
   526     if (_openCluster) {
       
   527         var _w = this.$.find('.Tl-Occurrence').width(),
       
   528             _ww = _w * _openCluster.occurrences.length;
       
   529         this.$.find('.Tl-ClusterOverlay').css({
       
   530             "margin-left": - Math.floor(_ww/2) + "px",
       
   531             width: _ww
       
   532         });
       
   533         _(_openCluster.occurrences).each(function(_o, _i) {
       
   534             _o.y = _o.y - 20;
       
   535             _o.x = _o.x - (_ww / 2) + ((_i + .5) * _w);
       
   536         });
       
   537     }
       
   538     
       
   539     this.$.find('.Tl-Occurrence').mousedown(function() {
   482     this.$.find('.Tl-Occurrence').mousedown(function() {
   540         var _el = $(this),
   483         var _el = $(this),
   541             _id = _el.attr("occurrence-id");
   484             _id = _el.attr("occurrence-id");
   542         if (typeof _id !== "undefined") {
   485         if (typeof _id !== "undefined") {
   543             _this.editing_occurrence = _this.getOccurrence(_id);
   486             _this.editing_occurrence = _this.getOccurrence(_id);
   568         if (typeof _id !== "undefined") {
   511         if (typeof _id !== "undefined") {
   569             var _occurrence = _this.getOccurrence(_id);
   512             var _occurrence = _this.getOccurrence(_id);
   570             _this.hideTooltip();
   513             _this.hideTooltip();
   571         }
   514         }
   572     });
   515     });
   573         
   516     
   574     this.$.find('.Tl-Cluster').click(function() {
       
   575         var _el = $(this),
       
   576             _contents = _el.attr("cluster-contents");
       
   577         if (_this.open_cluster == _contents) {
       
   578             _this.open_cluster = false;
       
   579         } else {
       
   580             _this.open_cluster = _contents;
       
   581         }
       
   582         _this.throttledDrawGrid();
       
   583     })
       
   584 }
   517 }
   585 
   518 
   586 Tlns.Classes.Timeline.prototype.getUnivers = function(_id) {
   519 Tlns.Classes.Timeline.prototype.getUnivers = function(_id) {
   587     return _(this.univers).find(function(_univ) {
   520     return _(this.univers).find(function(_univ) {
   588         return (_univ.id == _id);
   521         return (_univ.id == _id);
   622     this.original_data = _data;
   555     this.original_data = _data;
   623     this.id = _data.id;
   556     this.id = _data.id;
   624     this.date = new Date(1000 * (_data.dateFirstPublication || _data.dateCreate) || Date.now);
   557     this.date = new Date(1000 * (_data.dateFirstPublication || _data.dateCreate) || Date.now);
   625     this.formatted_date = Tlns.Utils.dateFormat(this.date,Tlns.Defaults.Timeline.tooltip_date_format);
   558     this.formatted_date = Tlns.Utils.dateFormat(this.date,Tlns.Defaults.Timeline.tooltip_date_format);
   626     this.title = _data.title;
   559     this.title = _data.title;
   627     this.univers_id = this.timeline.univers[Math.floor(this.timeline.univers.length * Math.random())].id;
   560     this.univers_id = this.timeline.univers[0].id;
   628     this.univers = this.timeline.getUnivers(this.univers_id);
   561     this.univers = this.timeline.getUnivers(this.univers_id);
   629     this.type = "publication";
   562     this.type = "publication";
   630     if (typeof _data.statut !== "undefined" || typeof this.status === "undefined") {
   563     if (typeof _data.statut !== "undefined" || typeof this.status === "undefined") {
   631         switch(_data.statut) {
   564         switch(_data.statut) {
   632             case "Validée":
   565             case "Validée":