diffViewer: selecting tab with the latest requested diff
[fnpeditor.git] / modules / documentHistory / documentHistory.js
index 0db2a75..0693469 100644 (file)
@@ -14,6 +14,12 @@ return function(sandbox) {
         itemList: dom.find('.rng-module-documentHistory-itemsList'),\r
     }\r
     var itemViews = [];\r
+    \r
+    \r
+    dom.find('.btn.compare').click(function(e) {\r
+        var selected = historyItems.getSelected();\r
+        sandbox.publish('compare', selected[0], selected[1]);\r
+    });\r
         \r
     var addHistoryItem = function(item, options) {\r
         historyItems.add(item);\r
@@ -27,57 +33,85 @@ return function(sandbox) {
     \r
     var toggleItemViews = function(toggle) {\r
         itemViews.forEach(function(view) {\r
-            view.toggle(toggle);\r
+            if(!historyItems.isSelected(view.item))\r
+                view.toggle(toggle);\r
         });\r
     }\r
     \r
+    var toggleButton = function(btn, toggle) {\r
+        dom.find('button.'+btn).toggleClass('disabled', !toggle);\r
+    }\r
+    \r
     var historyItems = {\r
         _itemsById: {},\r
         _selected: [],\r
-        select: function(id) {\r
+        select: function(item) {\r
             if(this._selected.length < 2) {\r
-                this._selected.push(id);\r
-                if(this._selected.length === 2)\r
-                    toggleItemViews(false);\r
+                this._selected.push(item.version);\r
+                this._updateUI();\r
                 return true;\r
             }\r
             return false;\r
         },\r
         unselect: function(item) {\r
-            this._selected = _.without(this._selected, id);\r
-            if(this._selected.length < 2)\r
-                toggleItemViews(true);\r
+            this._selected = _.without(this._selected, item.version);\r
+            this._updateUI();\r
         },\r
         add: function(item) {\r
-            this._itemsById[item.id] = item;\r
+            this._itemsById[item.version] = item;\r
+        },\r
+        isSelected: function(item) {\r
+            return _.contains(this._selected, item.version);\r
+        },\r
+        getSelected: function() {\r
+            return this._selected;\r
         },\r
-        selected: function(item) {\r
-            return _.contains(_selected, item.id);\r
+        _updateUI: function() {\r
+            var len = this._selected.length;\r
+            if(len === 0) {\r
+                toggleButton('compare', false);\r
+                toggleButton('show2', false);\r
+                toggleButton('restore', false);\r
+            }\r
+            if(len === 1) {\r
+                toggleButton('compare', false);\r
+                toggleButton('show2', true);\r
+                toggleButton('restore', true);\r
+            }\r
+            if(len === 2) {\r
+                toggleItemViews(false);\r
+                toggleButton('compare', true);\r
+                toggleButton('show2', false);\r
+                toggleButton('restore', false);\r
+            } else {\r
+                toggleItemViews(true);\r
+            }\r
         }\r
     };\r
+    historyItems._updateUI();\r
     \r
     var itemView = function(item) {\r
         this.item = item;\r
         this.dom = $(this.template(item));\r
-        this.dom.on('click', this.onItemClicked);\r
+        this.dom.on('click', _.bind(this.onItemClicked, this));\r
     };\r
     itemView.prototype.template = _.template(itemTemplateSrc);\r
     itemView.prototype.onItemClicked = function() {\r
-        if(historyItems.selected(item)) {\r
-            historyItems.unselect(item);\r
+        if(historyItems.isSelected(this.item)) {\r
+            historyItems.unselect(this.item);\r
             this.dimItem();\r
-        } else if(historyItems.select(item)) {\r
+        } else if(historyItems.select(this.item)) {\r
             this.highlightItem();\r
         }            \r
     };\r
     itemView.prototype.highlightItem = function() {\r
-        \r
+        this.dom.addClass('highlighted');\r
     };\r
     itemView.prototype.dimItem = function() {\r
-    \r
+        this.dom.removeClass('highlighted');\r
     };\r
     itemView.prototype.toggle = function(toggle) {\r
-    \r
+        this.dom.toggleClass('disabled', !toggle);\r
     };\r
 \r
     \r