Added Event communication
authorveltr
Wed, 18 Jul 2012 11:26:58 +0200
changeset 79 be3defb1bbdb
parent 78 3fe54fb581f5
child 80 00a3532ad8cf
Added Event communication
timeline/js/timeline.js
--- a/timeline/js/timeline.js	Mon Jul 16 12:28:31 2012 +0200
+++ b/timeline/js/timeline.js	Wed Jul 18 11:26:58 2012 +0200
@@ -196,8 +196,8 @@
     tooltip_date_format: '{{dayOfMonth}} {{shortMonthName}} {{year}} {{0hours}}:{{0minutes}}',
     statuses: {
         "valide": "Validée",
-        "a_valider": "À valider",
-        "a_realiser": "À réaliser"
+        "a_valider": "A valider",
+        "a_realiser": "A réaliser"
     }
 }
 
@@ -431,6 +431,41 @@
         _this.$.find(".Onglet-Ls").addClass("active");
         _this.throttledDrawList();
     });
+    
+    
+    /* BINDING MEDIADATA EVENTS */
+    $("body").bind("AjoutOccurrenceMediadata MiseAJourOccurrenceMediadata SuppressionOccurrenceMediadata AjoutDependanceMediadata SuppressionDependanceMediadata AjoutDependanceTimeline AjoutOccurrenceTimeline MiseAJourOccurrenceTimeline", function(_event, _data) {
+        console.log(_event.type + " called with data " + JSON.stringify(_data));
+    });
+   
+    $("body").bind("AjoutOccurrenceMediadata MiseAJourOccurrenceMediadata", function(_event, _data) {
+        var _type = _data.typeOccurrence.replace(/^occurrence/i,'').toLowerCase(),
+            _contents = _({ id: _data.id }).extend(_data.contenu);
+        _this.createOrUpdateOccurrence(_type, _contents);
+        _this.throttledDrawGrid();
+    });
+    
+    $("body").bind("SuppressionOccurrenceMediadata", function(_event, _data) {
+        var _id = _data.typeOccurrence.replace(/^occurrence/i,'').toLowerCase() + '_' + _data.id;
+        _this.deleteOccurrence(_id);
+        _this.throttledDrawGrid();
+    });
+    
+    $("body").bind("AjoutDependanceMediadata", function(_event, _data) {
+        var _sourceId = _data.typeOccurrence.replace(/^occurrence/i,'').toLowerCase() + '_' + _data.id,
+            _targetId = _data.typeOccurrenceCible.replace(/^occurrence/i,'').toLowerCase() + '_' + _data.idCible;
+        _this.getOccurrence(_sourceId).addDependency(_targetId);
+        _this.throttledDrawGrid();
+    });
+    
+    $("body").bind("SuppressionDependanceMediadata", function(_event, _data) {
+        var _sourceId = _data.typeOccurrence.replace(/^occurrence/i,'').toLowerCase() + '_' + _data.id,
+            _targetId = _data.typeOccurrenceCible.replace(/^occurrence/i,'').toLowerCase() + '_' + _data.idCible;
+        _this.getOccurrence(_sourceId).addDependency(_targetId);
+        _this.throttledDrawGrid();
+    });
+    
+    
 }
 
 Tlns.Classes.Timeline.prototype.onMouseDown = function(_event) {
@@ -450,6 +485,19 @@
     if (this.is_dragging) {
         switch (this.dragging_type) {
             case "occurrence":
+                var _event = ( this.editing_occurrence.just_created ? "Ajout" : "MiseAJour" ) + "OccurrenceTimeline",
+                    _data = {
+                        id: this.editing_occurrence.original_id,
+                        typeOccurrence: "occurrence" + this.editing_occurrence.type.replace(/^./,function(_l) { return _l.toUpperCase()}),
+                        contenu: {
+                            datePublication : Math.floor(this.editing_occurrence.date / 1000),
+                            titre : this.editing_occurrence.title,
+                            idUnivers: this.editing_occurrence.univers_id,
+                            statut: this.statuses[this.editing_occurrence.status],
+                            JT: +!!this.editing_occurrence.jt
+                        }
+                    }
+                $("body").trigger(_event, _data);
                 this.editing_occurrence.editing = false;
                 this.editing_occurrence.just_created = false;
                 this.throttledDrawGrid();
@@ -463,7 +511,7 @@
         }
     } else {
         if (this.dragging_type == "occurrence" && this.editing_occurrence.just_created) {
-            this.deleteOccurrence(this.editing_occurrence.type, this.editing_occurrence.id);
+            this.deleteOccurrence(this.editing_occurrence.id);
             this.throttledDrawGrid();
         }
     }
@@ -630,7 +678,7 @@
 
 Tlns.Classes.Timeline.prototype.deleteOccurrence = function(_id) {
     this.occurrences = _(this.occurrences).reject(function(_occ) {
-        return occ.id == _id;
+        return _occ.id == _id;
     });
 }
 
@@ -815,7 +863,18 @@
     }).mouseup(function() {
         var _el = $(this);
         if (_this.dragging_type == "link") {
-            _this.editing_occurrence.addDependency(_el.attr("occurrence-id"));
+            var _target = _el.attr("occurrence-id");
+            if (_target != _this.editing_occurrence.id) {
+                _this.editing_occurrence.addDependency(_target);
+                $("body").trigger("AjoutDependanceTimeline",
+                    {
+                        id: _this.editing_occurrence.original_id,
+                        typeOccurrence: "occurrence" + _this.editing_occurrence.type.replace(/^./,function(_l) { return _l.toUpperCase()}),
+                        idCible: _target.replace(/^.*_/,''),
+                        typeOccurrenceCible: "occurrence" + _target.replace(/_.*$/,'').replace(/^./,function(_l) { return _l.toUpperCase()})
+                    }
+                );
+            }
         }
     });
     
@@ -930,30 +989,46 @@
 
 Tlns.Classes.Occurrence.prototype.update = function(_type, _data) {
     this.type = _type;
-    this.original_id = _data.idOccurrencePublication || _data.id || Tlns.Utils.guid();
+    if (typeof _data.idOccurrencePublication !== "undefined" || typeof _data.id !== "undefined" || typeof this.original_id === "undefined") {
+        this.original_id = _data.idOccurrencePublication || _data.id || Tlns.Utils.guid();
+    }
     this.id = _type + "_" + this.original_id;
-    this.date = 1000 * (_data.datePublication || _data.date);
+    if (typeof _data.date !== "undefined" || typeof _data.datePublication !== "undefined") {
+        this.date = 1000 * (_data.datePublication || _data.date);
+    } else {
+        if (typeof this.date === "undefined") {
+            this.date = new Date().valueOf();
+        }
+    }
     this.formatted_date = Tlns.Utils.dateFormat(this.date,Tlns.Defaults.Timeline.tooltip_date_format);
-    this.title = _data.titre || "<untitled>";
-    this.univers_id = _data.idUnivers;
+    if (typeof _data.titre !== "undefined" || typeof this.title === "undefined") {
+        this.title = _data.titre || "<untitled>";
+    }
+    if (typeof _data.idUnivers !== "undefined") {
+        this.univers_id = _data.idUnivers;
+    }
     this.univers = this.timeline.getUnivers(this.univers_id);
-    switch(_data.statut) {
-        case "Validée":
-        case "valide":
-            this.status = "valide"
-        break;
-        case "A valider":
-        case "a_valider":
-            this.status = "a_valider";
-        break;
-        case "A réaliser":
-        case "a_realiser":
-            this.status = "a_realiser";
-        break;
-        default:
-            this.status = false;
+    if (typeof _data.statut !== "undefined" || typeof this.status === "undefined") {
+        switch(_data.statut) {
+            case "Validée":
+            case "valide":
+                this.status = "valide"
+            break;
+            case "A valider":
+            case "a_valider":
+                this.status = "a_valider";
+            break;
+            case "A réaliser":
+            case "a_realiser":
+                this.status = "a_realiser";
+            break;
+            default:
+                this.status = false;
+        }
     }
-    this.jt = !!+_data.JT;
+    if (typeof _data.JT !== "undefined") {
+        this.jt = !!+_data.JT;
+    }
     this.translated_status = Tlns.Defaults.Timeline.statuses[this.status];
 //    this.published = (_data.publication && _data.publication == "En ligne");
 //    this.locked = _data.verrouille || false;
@@ -979,6 +1054,12 @@
     }
 }
 
+Tlns.Classes.Occurrence.prototype.removeDependency = function(_id) {
+    this.dependsOn = _(this.dependsOn).reject(function(_n) {
+        return _n == _id;
+    });
+}
+
 Tlns.Classes.Occurrence.prototype.toString = function() {
     return "Occurrence " + this.type + ': "' + this.title + '"';
 }