61eec9e1742c7f9db3d02539c35f56ac2c7a5e7a
[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             } else {\r
78                 toggleItemViews(true);\r
79             }\r
80         }\r
81     };\r
82     historyItems._updateUI();\r
83     \r
84     var itemView = function(item) {\r
85         this.item = item;\r
86         this.dom = $(this.template(item));\r
87         this.dom.on('click', _.bind(this.onItemClicked, this));\r
88     };\r
89     itemView.prototype.template = _.template(itemTemplateSrc);\r
90     itemView.prototype.onItemClicked = function() {\r
91         if(historyItems.selected(this.item)) {\r
92             historyItems.unselect(this.item);\r
93             this.dimItem();\r
94         } else if(historyItems.select(this.item)) {\r
95             this.highlightItem();\r
96         }            \r
97     };\r
98     itemView.prototype.highlightItem = function() {\r
99         this.dom.addClass('highlighted');\r
100     };\r
101     itemView.prototype.dimItem = function() {\r
102         this.dom.removeClass('highlighted');\r
103     };\r
104     itemView.prototype.toggle = function(toggle) {\r
105         this.dom.toggleClass('disabled', !toggle);\r
106     };\r
107 \r
108     \r
109     \r
110     return {\r
111         start: function() { sandbox.publish('ready'); },
112         addHistory: function(history, options) {\r
113             history.forEach(function(historyItem) {\r
114                 addHistoryItem(historyItem, options || {});\r
115             });\r
116         },\r
117         getView: function() {\r
118             return dom;\r
119         }\r
120     }\r
121 }\r
122 \r
123 });