timeline/js/timeline.js
changeset 85 7a0de7ed7575
parent 84 2448fdcef656
child 87 4dc63e325f48
--- a/timeline/js/timeline.js	Tue Jul 24 12:30:56 2012 +0200
+++ b/timeline/js/timeline.js	Thu Jul 26 17:50:39 2012 +0200
@@ -302,6 +302,10 @@
         _this.drawList();
     }, 150);
     
+    this.throttledShowEditingLinks = _.throttle(function() {
+        _this.showEditingLinks();
+    }, 150);
+    
     this.setLevel(this.level);
     
     this.$.find('.Tl-TopBar-Timescales>div').click(function() {
@@ -522,7 +526,7 @@
             case "occurrence":
                 var _event = ( this.editing_occurrence.just_created ? "Ajout" : "MiseAJour" ) + "OccurrenceTimeline",
                     _data = {
-                        id: ( this.editing_occurrence.just_created ? undefined : this.editing_occurrence.original_id),
+                        id: this.editing_occurrence.original_id,
                         typeOccurrence: "Occurrence" + this.editing_occurrence.type.replace(/^./,function(_l) { return _l.toUpperCase()}),
                         datePublication : Math.floor(this.editing_occurrence.date / 1000),
                         titre : this.editing_occurrence.title,
@@ -577,8 +581,15 @@
 }
 
 Tlns.Classes.Timeline.prototype.onMouseMove = function(_event) {
-    if (this.mouse_down) {
-        this.is_dragging = true;
+    if (this.mouse_down && !this.is_dragging) {
+        var _dx = this.start_pos.x - _event.pageX,
+            _dy = this.start_pos.y - _event.pageY,
+            _sqd = _dx * _dx + _dy * _dy;
+        if (_sqd > 16) {
+            this.is_dragging = true;
+        }
+    }
+    if (this.is_dragging) {
         this.hideTooltip();
         switch (this.dragging_type) {
             case "occurrence":
@@ -679,6 +690,7 @@
     this.$.find(".Ls-To-Date").val(Tlns.Utils.dateFormat(this.end_time, '{{0dayOfMonth}}/{{0monthNumber}}/{{year}}'));
     this.throttledDrawGrid();
     this.throttledDrawList();
+    this.throttledShowEditingLinks();
 }
 
 Tlns.Classes.Timeline.prototype.drawGrid = function() {
@@ -774,6 +786,7 @@
             top: _y + "px"
         });
     this.$.find('.Tl-Overlay-Main').html(_html);
+    
 }
 
 Tlns.Classes.Timeline.prototype.hideTooltip = function() {
@@ -781,15 +794,15 @@
 }
 
 Tlns.Classes.Timeline.prototype.drawOccurrences = function() {
-    var _this = this,
-        _visible = _(this.occurrences).filter(function(_occ) {
-        return (_occ.date >= _this.start_time && _occ.date <= _this.end_time && _occ.status);
-    });
-    _(_visible).each(function(_occ) {
+    var _this = this;
+    _(this.occurrences).each(function(_occ) {
         _occ.x = _this.current_scale * (_occ.date - _this.start_time);
         _occ.y = _occ.univers.y;
         _occ.in_cluster = false;
     });
+    var _visible = _(this.occurrences).filter(function(_occ) {
+        return (_occ.date >= _this.start_time && _occ.date <= _this.end_time && _occ.status);
+    });
     
     var _moved = true;
     while (_moved) {
@@ -846,31 +859,6 @@
         }
     });
     
-    
-    var _links = [];
-    
-    _(_visible).each(function(_occurrence) {
-        _(_occurrence.dependsOn).each(function(_dependance) {
-            var _parent = _(_visible).find(function(_o) {
-                return _o.id == _dependance;
-            });
-            if (typeof _parent !== "undefined") {
-                _links.push({
-                    from_x: _occurrence.x,
-                    from_y: _occurrence.y + Math.floor(_this.univers_height / 2),
-                    to_x: _parent.x,
-                    to_y: _parent.y + Math.floor(_this.univers_height / 2)
-                });
-            }
-        });
-    });
-    
-    var _ctx = this.$.find('.Tl-Canvas')[0].getContext('2d');
-    _ctx.clearRect(0,0,this.main_width, this.main_height);
-    _(_links).each(function(_link) {
-        Tlns.Utils.drawArrow(_ctx, "#505050", _link.from_x,_link.from_y, _link.to_x,_link.to_y);
-    });
-    
     var _html = Mustache.to_html(Tlns.Templates.Occurrence, {
         occurrences:_(_visible).reject(function(_o) {return _o.in_cluster}),
         clusters: _clusters,
@@ -919,6 +907,7 @@
             if (!_this.is_dragging) {
                 var _html = Mustache.to_html(Tlns.Templates.OccurrenceTooltip, _occurrence);
                 _this.showTooltip(_occurrence.x, _occurrence.y, _html, (_event.pageY - _this.dragging_bounds.top) >= (.4 * _this.main_height) );
+//                _this.showLinks(_id);
             }
         }
     }).mouseout(function() {
@@ -928,6 +917,7 @@
             var _occurrence = _this.getOccurrence(_id);
             _this.hideTooltip();
             _this.$.find('.Tl-Link').hide();
+            _this.throttledShowEditingLinks();
         }
     }).mouseup(function() {
         var _el = $(this);
@@ -965,6 +955,7 @@
             _this.open_cluster = _contents;
         }
         _this.throttledDrawGrid();
+        _this.throttledShowEditingLinks();
     })
 }
 
@@ -994,7 +985,7 @@
         Mustache.to_html(
             Tlns.Templates.Occurrence_List,
             {
-                occurrences: this.occurrences.filter(function(_occ) {
+                occurrences: _(this.occurrences).chain().filter(function(_occ) {
                     var _titletest = (!!_occ.title.match(_titleregexp)),
                         _keep = (
                                ( !_title || _titletest )
@@ -1007,7 +998,9 @@
                             && ( !_toDate || _occ.date <= _enddate )
                         );
                     return _keep;
-                })
+                }).sortBy(function(_occ) {
+                    return _occ.date;
+                }).value()
             }
         )
     );
@@ -1027,6 +1020,36 @@
     });
 }
 
+Tlns.Classes.Timeline.prototype.showLinks = function(_id) {
+    var _ctx = this.$.find('.Tl-Canvas')[0].getContext('2d');
+    _ctx.clearRect(0,0,this.main_width, this.main_height);
+    
+    if (typeof _id !== "undefined") {
+        var _links = [],
+            _this = this;
+        _(this.occurrences).each(function(_occurrence) {
+            _(_occurrence.dependsOn).each(function(_dependance) {
+                var _parent = _this.getOccurrence(_dependance);
+                if (typeof _parent !== "undefined" && (_parent.id == _id || _occurrence.id == _id)) {
+                    _links.push({
+                        from_x: _occurrence.x,
+                        from_y: _occurrence.y + Math.floor(_this.univers_height / 2),
+                        to_x: _parent.x,
+                        to_y: _parent.y + Math.floor(_this.univers_height / 2)
+                    });
+                }
+            });
+        });
+    }
+    _(_links).each(function(_link) {
+        Tlns.Utils.drawArrow(_ctx, "#505050", _link.from_x,_link.from_y, _link.to_x,_link.to_y);
+    });
+}
+
+Tlns.Classes.Timeline.prototype.showEditingLinks = function() {
+    this.showLinks(typeof this.editing_occurrence !== "undefined" ? this.editing_occurrence.id : undefined);
+}
+
 Tlns.Classes.Timeline.prototype.getUnivers = function(_id) {
     return _(this.univers).find(function(_univ) {
         return (_univ.id == _id);