Fixing list removal
[fnpeditor.git] / modules / documentHistory / documentHistory.js
index d5f20d4..d435617 100644 (file)
@@ -1,9 +1,10 @@
 define([\r
 'libs/jquery-1.9.1.min',\r
 'libs/underscore-min',\r
+'./restoreDialog',\r
 'libs/text!./templates/main.html',\r
 'libs/text!./templates/item.html'\r
-], function($, _, mainTemplateSrc, itemTemplateSrc) {\r
+], function($, _, restoreDialog, mainTemplateSrc, itemTemplateSrc) {\r
 \r
 'use strict';\r
     \r
@@ -14,6 +15,25 @@ 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
+    dom.find('.btn.restore').click(function(e) {\r
+        var dialog = restoreDialog.create();\r
+        dialog.on('restore', function(event) {\r
+            sandbox.publish('restoreVersion', {version: historyItems.getSelected()[0], description: event.data.description});\r
+            event.success();\r
+        });\r
+        dialog.show();\r
+    });\r
+    \r
+    dom.find('.btn.display').click(function(e) {\r
+        sandbox.publish('displayVersion', {version: historyItems.getSelected()[0]});\r
+    });\r
         \r
     var addHistoryItem = function(item, options) {\r
         historyItems.add(item);\r
@@ -27,35 +47,62 @@ return function(sandbox) {
     \r
     var toggleItemViews = function(toggle) {\r
         itemViews.forEach(function(view) {\r
-            if(!historyItems.selected(view.item))\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(item) {\r
             if(this._selected.length < 2) {\r
                 this._selected.push(item.version);\r
-                if(this._selected.length === 2)\r
-                    toggleItemViews(false);\r
+                this._updateUI();\r
                 return true;\r
             }\r
             return false;\r
         },\r
         unselect: function(item) {\r
             this._selected = _.without(this._selected, item.version);\r
-            if(this._selected.length < 2)\r
-                toggleItemViews(true);\r
+            this._updateUI();\r
         },\r
         add: function(item) {\r
             this._itemsById[item.version] = item;\r
         },\r
-        selected: function(item) {\r
+        isSelected: function(item) {\r
             return _.contains(this._selected, item.version);\r
+        },\r
+        getSelected: function() {\r
+            return this._selected;\r
+        },\r
+        _updateUI: function() {\r
+            var len = this._selected.length;\r
+            if(len === 0) {\r
+                toggleButton('compare', false);\r
+                toggleButton('display', false);\r
+                toggleButton('restore', false);\r
+            }\r
+            if(len === 1) {\r
+                toggleButton('compare', false);\r
+                toggleButton('display', true);\r
+                toggleButton('restore', true);\r
+            }\r
+            if(len === 2) {\r
+                toggleItemViews(false);\r
+                toggleButton('compare', true);\r
+                toggleButton('display', 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
@@ -64,7 +111,7 @@ return function(sandbox) {
     };\r
     itemView.prototype.template = _.template(itemTemplateSrc);\r
     itemView.prototype.onItemClicked = function() {\r
-        if(historyItems.selected(this.item)) {\r
+        if(historyItems.isSelected(this.item)) {\r
             historyItems.unselect(this.item);\r
             this.dimItem();\r
         } else if(historyItems.select(this.item)) {\r