timeline/js/timeline.js
changeset 85 7a0de7ed7575
parent 84 2448fdcef656
child 87 4dc63e325f48
equal deleted inserted replaced
84:2448fdcef656 85:7a0de7ed7575
   300     
   300     
   301     this.throttledDrawList = _.throttle(function() {
   301     this.throttledDrawList = _.throttle(function() {
   302         _this.drawList();
   302         _this.drawList();
   303     }, 150);
   303     }, 150);
   304     
   304     
       
   305     this.throttledShowEditingLinks = _.throttle(function() {
       
   306         _this.showEditingLinks();
       
   307     }, 150);
       
   308     
   305     this.setLevel(this.level);
   309     this.setLevel(this.level);
   306     
   310     
   307     this.$.find('.Tl-TopBar-Timescales>div').click(function() {
   311     this.$.find('.Tl-TopBar-Timescales>div').click(function() {
   308         _this.setLevel($(this).attr("data-level"));
   312         _this.setLevel($(this).attr("data-level"));
   309     });
   313     });
   520     if (this.is_dragging) {
   524     if (this.is_dragging) {
   521         switch (this.dragging_type) {
   525         switch (this.dragging_type) {
   522             case "occurrence":
   526             case "occurrence":
   523                 var _event = ( this.editing_occurrence.just_created ? "Ajout" : "MiseAJour" ) + "OccurrenceTimeline",
   527                 var _event = ( this.editing_occurrence.just_created ? "Ajout" : "MiseAJour" ) + "OccurrenceTimeline",
   524                     _data = {
   528                     _data = {
   525                         id: ( this.editing_occurrence.just_created ? undefined : this.editing_occurrence.original_id),
   529                         id: this.editing_occurrence.original_id,
   526                         typeOccurrence: "Occurrence" + this.editing_occurrence.type.replace(/^./,function(_l) { return _l.toUpperCase()}),
   530                         typeOccurrence: "Occurrence" + this.editing_occurrence.type.replace(/^./,function(_l) { return _l.toUpperCase()}),
   527                         datePublication : Math.floor(this.editing_occurrence.date / 1000),
   531                         datePublication : Math.floor(this.editing_occurrence.date / 1000),
   528                         titre : this.editing_occurrence.title,
   532                         titre : this.editing_occurrence.title,
   529                         idUnivers: this.editing_occurrence.univers_id,
   533                         idUnivers: this.editing_occurrence.univers_id,
   530                         statut: this.statuses[this.editing_occurrence.status],
   534                         statut: this.statuses[this.editing_occurrence.status],
   575 Tlns.Classes.Timeline.prototype.universFromMouse = function(_pageY) {
   579 Tlns.Classes.Timeline.prototype.universFromMouse = function(_pageY) {
   576     return this.universFromY(_pageY - this.dragging_bounds.top);
   580     return this.universFromY(_pageY - this.dragging_bounds.top);
   577 }
   581 }
   578 
   582 
   579 Tlns.Classes.Timeline.prototype.onMouseMove = function(_event) {
   583 Tlns.Classes.Timeline.prototype.onMouseMove = function(_event) {
   580     if (this.mouse_down) {
   584     if (this.mouse_down && !this.is_dragging) {
   581         this.is_dragging = true;
   585         var _dx = this.start_pos.x - _event.pageX,
       
   586             _dy = this.start_pos.y - _event.pageY,
       
   587             _sqd = _dx * _dx + _dy * _dy;
       
   588         if (_sqd > 16) {
       
   589             this.is_dragging = true;
       
   590         }
       
   591     }
       
   592     if (this.is_dragging) {
   582         this.hideTooltip();
   593         this.hideTooltip();
   583         switch (this.dragging_type) {
   594         switch (this.dragging_type) {
   584             case "occurrence":
   595             case "occurrence":
   585                 var _d = this.timeFromMouse(_event.pageX);
   596                 var _d = this.timeFromMouse(_event.pageX);
   586                 this.editing_occurrence.date = _d;
   597                 this.editing_occurrence.date = _d;
   677     this.$.find(".Ls-From-Date").val(Tlns.Utils.dateFormat(this.start_time, '{{0dayOfMonth}}/{{0monthNumber}}/{{year}}'));
   688     this.$.find(".Ls-From-Date").val(Tlns.Utils.dateFormat(this.start_time, '{{0dayOfMonth}}/{{0monthNumber}}/{{year}}'));
   678     this.$.find(".Ls-To-Time").val(Tlns.Utils.dateFormat(this.end_time, '{{0hours}}:{{0minutes}}'));
   689     this.$.find(".Ls-To-Time").val(Tlns.Utils.dateFormat(this.end_time, '{{0hours}}:{{0minutes}}'));
   679     this.$.find(".Ls-To-Date").val(Tlns.Utils.dateFormat(this.end_time, '{{0dayOfMonth}}/{{0monthNumber}}/{{year}}'));
   690     this.$.find(".Ls-To-Date").val(Tlns.Utils.dateFormat(this.end_time, '{{0dayOfMonth}}/{{0monthNumber}}/{{year}}'));
   680     this.throttledDrawGrid();
   691     this.throttledDrawGrid();
   681     this.throttledDrawList();
   692     this.throttledDrawList();
       
   693     this.throttledShowEditingLinks();
   682 }
   694 }
   683 
   695 
   684 Tlns.Classes.Timeline.prototype.drawGrid = function() {
   696 Tlns.Classes.Timeline.prototype.drawGrid = function() {
   685     if (this.sync_now) {
   697     if (this.sync_now) {
   686         this.$.find('.Tl-TopBar-SyncButton').addClass("active");
   698         this.$.find('.Tl-TopBar-SyncButton').addClass("active");
   772         .css({
   784         .css({
   773             left: _x + "px",
   785             left: _x + "px",
   774             top: _y + "px"
   786             top: _y + "px"
   775         });
   787         });
   776     this.$.find('.Tl-Overlay-Main').html(_html);
   788     this.$.find('.Tl-Overlay-Main').html(_html);
       
   789     
   777 }
   790 }
   778 
   791 
   779 Tlns.Classes.Timeline.prototype.hideTooltip = function() {
   792 Tlns.Classes.Timeline.prototype.hideTooltip = function() {
   780     this.$.find('.Tl-Overlay-Box').hide();
   793     this.$.find('.Tl-Overlay-Box').hide();
   781 }
   794 }
   782 
   795 
   783 Tlns.Classes.Timeline.prototype.drawOccurrences = function() {
   796 Tlns.Classes.Timeline.prototype.drawOccurrences = function() {
   784     var _this = this,
   797     var _this = this;
   785         _visible = _(this.occurrences).filter(function(_occ) {
   798     _(this.occurrences).each(function(_occ) {
   786         return (_occ.date >= _this.start_time && _occ.date <= _this.end_time && _occ.status);
       
   787     });
       
   788     _(_visible).each(function(_occ) {
       
   789         _occ.x = _this.current_scale * (_occ.date - _this.start_time);
   799         _occ.x = _this.current_scale * (_occ.date - _this.start_time);
   790         _occ.y = _occ.univers.y;
   800         _occ.y = _occ.univers.y;
   791         _occ.in_cluster = false;
   801         _occ.in_cluster = false;
       
   802     });
       
   803     var _visible = _(this.occurrences).filter(function(_occ) {
       
   804         return (_occ.date >= _this.start_time && _occ.date <= _this.end_time && _occ.status);
   792     });
   805     });
   793     
   806     
   794     var _moved = true;
   807     var _moved = true;
   795     while (_moved) {
   808     while (_moved) {
   796         _moved = false;
   809         _moved = false;
   844         if (_cluster.contents == _this.open_cluster) {
   857         if (_cluster.contents == _this.open_cluster) {
   845             _openCluster = _cluster;
   858             _openCluster = _cluster;
   846         }
   859         }
   847     });
   860     });
   848     
   861     
   849     
       
   850     var _links = [];
       
   851     
       
   852     _(_visible).each(function(_occurrence) {
       
   853         _(_occurrence.dependsOn).each(function(_dependance) {
       
   854             var _parent = _(_visible).find(function(_o) {
       
   855                 return _o.id == _dependance;
       
   856             });
       
   857             if (typeof _parent !== "undefined") {
       
   858                 _links.push({
       
   859                     from_x: _occurrence.x,
       
   860                     from_y: _occurrence.y + Math.floor(_this.univers_height / 2),
       
   861                     to_x: _parent.x,
       
   862                     to_y: _parent.y + Math.floor(_this.univers_height / 2)
       
   863                 });
       
   864             }
       
   865         });
       
   866     });
       
   867     
       
   868     var _ctx = this.$.find('.Tl-Canvas')[0].getContext('2d');
       
   869     _ctx.clearRect(0,0,this.main_width, this.main_height);
       
   870     _(_links).each(function(_link) {
       
   871         Tlns.Utils.drawArrow(_ctx, "#505050", _link.from_x,_link.from_y, _link.to_x,_link.to_y);
       
   872     });
       
   873     
       
   874     var _html = Mustache.to_html(Tlns.Templates.Occurrence, {
   862     var _html = Mustache.to_html(Tlns.Templates.Occurrence, {
   875         occurrences:_(_visible).reject(function(_o) {return _o.in_cluster}),
   863         occurrences:_(_visible).reject(function(_o) {return _o.in_cluster}),
   876         clusters: _clusters,
   864         clusters: _clusters,
   877         open_cluster: _openCluster
   865         open_cluster: _openCluster
   878     });
   866     });
   917             _el.find('.Tl-Link').show();
   905             _el.find('.Tl-Link').show();
   918 //            }
   906 //            }
   919             if (!_this.is_dragging) {
   907             if (!_this.is_dragging) {
   920                 var _html = Mustache.to_html(Tlns.Templates.OccurrenceTooltip, _occurrence);
   908                 var _html = Mustache.to_html(Tlns.Templates.OccurrenceTooltip, _occurrence);
   921                 _this.showTooltip(_occurrence.x, _occurrence.y, _html, (_event.pageY - _this.dragging_bounds.top) >= (.4 * _this.main_height) );
   909                 _this.showTooltip(_occurrence.x, _occurrence.y, _html, (_event.pageY - _this.dragging_bounds.top) >= (.4 * _this.main_height) );
       
   910 //                _this.showLinks(_id);
   922             }
   911             }
   923         }
   912         }
   924     }).mouseout(function() {
   913     }).mouseout(function() {
   925         var _el = $(this),
   914         var _el = $(this),
   926             _id = _el.attr("occurrence-id");
   915             _id = _el.attr("occurrence-id");
   927         if (typeof _id !== "undefined") {
   916         if (typeof _id !== "undefined") {
   928             var _occurrence = _this.getOccurrence(_id);
   917             var _occurrence = _this.getOccurrence(_id);
   929             _this.hideTooltip();
   918             _this.hideTooltip();
   930             _this.$.find('.Tl-Link').hide();
   919             _this.$.find('.Tl-Link').hide();
       
   920             _this.throttledShowEditingLinks();
   931         }
   921         }
   932     }).mouseup(function() {
   922     }).mouseup(function() {
   933         var _el = $(this);
   923         var _el = $(this);
   934         if (_this.dragging_type == "link") {
   924         if (_this.dragging_type == "link") {
   935             var _target = _el.attr("occurrence-id");
   925             var _target = _el.attr("occurrence-id");
   963             _this.open_cluster = false;
   953             _this.open_cluster = false;
   964         } else {
   954         } else {
   965             _this.open_cluster = _contents;
   955             _this.open_cluster = _contents;
   966         }
   956         }
   967         _this.throttledDrawGrid();
   957         _this.throttledDrawGrid();
       
   958         _this.throttledShowEditingLinks();
   968     })
   959     })
   969 }
   960 }
   970 
   961 
   971 Tlns.Classes.Timeline.prototype.drawList = function() {
   962 Tlns.Classes.Timeline.prototype.drawList = function() {
   972     var _universfilter = this.$.find(".Ls-Univers li.Ls-Active").map(function(){return $(this).attr("data")}),
   963     var _universfilter = this.$.find(".Ls-Univers li.Ls-Active").map(function(){return $(this).attr("data")}),
   992     }
   983     }
   993     this.$.find(".Ls-Occurrences").html(
   984     this.$.find(".Ls-Occurrences").html(
   994         Mustache.to_html(
   985         Mustache.to_html(
   995             Tlns.Templates.Occurrence_List,
   986             Tlns.Templates.Occurrence_List,
   996             {
   987             {
   997                 occurrences: this.occurrences.filter(function(_occ) {
   988                 occurrences: _(this.occurrences).chain().filter(function(_occ) {
   998                     var _titletest = (!!_occ.title.match(_titleregexp)),
   989                     var _titletest = (!!_occ.title.match(_titleregexp)),
   999                         _keep = (
   990                         _keep = (
  1000                                ( !_title || _titletest )
   991                                ( !_title || _titletest )
  1001                             && _(_occtypefilter).include(_occ.type)
   992                             && _(_occtypefilter).include(_occ.type)
  1002                             && _(_universfilter).include(_occ.univers_id)
   993                             && _(_universfilter).include(_occ.univers_id)
  1005                             && _(_jtfilter).include(_occ.jt)
   996                             && _(_jtfilter).include(_occ.jt)
  1006                             && ( !_fromDate || _occ.date >= _startdate )
   997                             && ( !_fromDate || _occ.date >= _startdate )
  1007                             && ( !_toDate || _occ.date <= _enddate )
   998                             && ( !_toDate || _occ.date <= _enddate )
  1008                         );
   999                         );
  1009                     return _keep;
  1000                     return _keep;
  1010                 })
  1001                 }).sortBy(function(_occ) {
       
  1002                     return _occ.date;
       
  1003                 }).value()
  1011             }
  1004             }
  1012         )
  1005         )
  1013     );
  1006     );
  1014     if (_title) {
  1007     if (_title) {
  1015         this.$.find(".Ls-Occurrence-Title").each(function() {
  1008         this.$.find(".Ls-Occurrence-Title").each(function() {
  1023                 typeOccurrence: "Occurrence" + _id.replace(/_.*$/,'').replace(/^./,function(_l) { return _l.toUpperCase()})
  1016                 typeOccurrence: "Occurrence" + _id.replace(/_.*$/,'').replace(/^./,function(_l) { return _l.toUpperCase()})
  1024             }
  1017             }
  1025         $("body").trigger("SelectionOccurrenceTimeline", _data);
  1018         $("body").trigger("SelectionOccurrenceTimeline", _data);
  1026         return false;
  1019         return false;
  1027     });
  1020     });
       
  1021 }
       
  1022 
       
  1023 Tlns.Classes.Timeline.prototype.showLinks = function(_id) {
       
  1024     var _ctx = this.$.find('.Tl-Canvas')[0].getContext('2d');
       
  1025     _ctx.clearRect(0,0,this.main_width, this.main_height);
       
  1026     
       
  1027     if (typeof _id !== "undefined") {
       
  1028         var _links = [],
       
  1029             _this = this;
       
  1030         _(this.occurrences).each(function(_occurrence) {
       
  1031             _(_occurrence.dependsOn).each(function(_dependance) {
       
  1032                 var _parent = _this.getOccurrence(_dependance);
       
  1033                 if (typeof _parent !== "undefined" && (_parent.id == _id || _occurrence.id == _id)) {
       
  1034                     _links.push({
       
  1035                         from_x: _occurrence.x,
       
  1036                         from_y: _occurrence.y + Math.floor(_this.univers_height / 2),
       
  1037                         to_x: _parent.x,
       
  1038                         to_y: _parent.y + Math.floor(_this.univers_height / 2)
       
  1039                     });
       
  1040                 }
       
  1041             });
       
  1042         });
       
  1043     }
       
  1044     _(_links).each(function(_link) {
       
  1045         Tlns.Utils.drawArrow(_ctx, "#505050", _link.from_x,_link.from_y, _link.to_x,_link.to_y);
       
  1046     });
       
  1047 }
       
  1048 
       
  1049 Tlns.Classes.Timeline.prototype.showEditingLinks = function() {
       
  1050     this.showLinks(typeof this.editing_occurrence !== "undefined" ? this.editing_occurrence.id : undefined);
  1028 }
  1051 }
  1029 
  1052 
  1030 Tlns.Classes.Timeline.prototype.getUnivers = function(_id) {
  1053 Tlns.Classes.Timeline.prototype.getUnivers = function(_id) {
  1031     return _(this.univers).find(function(_univ) {
  1054     return _(this.univers).find(function(_univ) {
  1032         return (_univ.id == _id);
  1055         return (_univ.id == _id);