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