2c3a3b6eef01d66359f3dcf042226ed115ef6866
[fnpeditor.git] / modules / documentHistory / documentHistory.js
1 define([\r
2 'libs/jquery-1.9.1.min',\r
3 'libs/underscore-min',\r
4 'libs/text!./templates/main.html',\r
5 'libs/text!./templates/item.html'\r
6 ], function($, _, mainTemplateSrc, itemTemplateSrc) {\r
7 \r
8 'use strict';\r
9     \r
10 return function(sandbox) {\r
11     \r
12     var dom = $(_.template(mainTemplateSrc)());\r
13     var domNodes = {\r
14         itemList: dom.find('.rng-module-documentHistory-itemsList'),\r
15     }\r
16     var itemViews = [];\r
17         \r
18     var addHistoryItem = function(item, options) {\r
19         historyItems.add(item);\r
20         var view = new itemView(item);\r
21         itemViews.push(view);\r
22         domNodes.itemList.prepend(view.dom);\r
23         if(options.animate) {\r
24             view.dom.hide().slideDown();\r
25         }\r
26     }\r
27     \r
28     var toggleItemViews = function(toggle) {\r
29         itemViews.forEach(function(view) {\r
30             if(!historyItems.selected(view.item))\r
31                 view.toggle(toggle);\r
32         });\r
33     }\r
34     \r
35     var toggleButton = function(btn, toggle) {\r
36         dom.find('button.'+btn).toggleClass('disabled', !toggle);\r
37     }\r
38     \r
39     var historyItems = {\r
40         _itemsById: {},\r
41         _selected: [],\r
42         select: function(item) {\r
43             if(this._selected.length < 2) {\r
44                 this._selected.push(item.version);\r
45                 this._updateUI();\r
46                 return true;\r
47             }\r
48             return false;\r
49         },\r
50         unselect: function(item) {\r
51             this._selected = _.without(this._selected, item.version);\r
52             this._updateUI();\r
53         },\r
54         add: function(item) {\r
55             this._itemsById[item.version] = item;\r
56         },\r
57         selected: function(item) {\r
58             return _.contains(this._selected, item.version);\r
59         },\r
60         _updateUI: function() {\r
61             var len = this._selected.length;\r
62             if(len === 0) {\r
63                 toggleButton('compare', false);\r
64                 toggleButton('show2', false);\r
65                 toggleButton('restore', false);\r
66             }\r
67             if(len === 1) {\r
68                 toggleButton('compare', false);\r
69                 toggleButton('show2', true);\r
70                 toggleButton('restore', true);\r
71             }\r
72             if(len === 2) {\r
73                 toggleItemViews(false);\r
74                 toggleButton('compare', true);\r
75                 toggleButton('show2', false);\r
76                 toggleButton('restore', false);\r
77             }\r
78         }\r
79     };\r
80     historyItems._updateUI();\r
81     \r
82     var itemView = function(item) {\r
83         this.item = item;\r
84         this.dom = $(this.template(item));\r
85         this.dom.on('click', _.bind(this.onItemClicked, this));\r
86     };\r
87     itemView.prototype.template = _.template(itemTemplateSrc);\r
88     itemView.prototype.onItemClicked = function() {\r
89         if(historyItems.selected(this.item)) {\r
90             historyItems.unselect(this.item);\r
91             this.dimItem();\r
92         } else if(historyItems.select(this.item)) {\r
93             this.highlightItem();\r
94         }            \r
95     };\r
96     itemView.prototype.highlightItem = function() {\r
97         this.dom.addClass('highlighted');\r
98     };\r
99     itemView.prototype.dimItem = function() {\r
100         this.dom.removeClass('highlighted');\r
101     };\r
102     itemView.prototype.toggle = function(toggle) {\r
103         this.dom.toggleClass('disabled', !toggle);\r
104     };\r
105 \r
106     \r
107     \r
108     return {\r
109         start: function() { sandbox.publish('ready'); },
110         addHistory: function(history, options) {\r
111             history.forEach(function(historyItem) {\r
112                 addHistoryItem(historyItem, options || {});\r
113             });\r
114         },\r
115         getView: function() {\r
116             return dom;\r
117         }\r
118     }\r
119 }\r
120 \r
121 });