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