4 'libs/text!./templates/main.html',
5 'libs/text!./templates/item.html'
6 ], function($, _, mainTemplateSrc, itemTemplateSrc) {
10 return function(sandbox) {
12 var dom = $(_.template(mainTemplateSrc)());
14 itemList: dom.find('.rng-module-documentHistory-itemsList'),
19 dom.find('.btn.compare').click(function() {
20 var selected = historyItems.getSelected();
21 sandbox.publish('compare', selected[0], selected[1]);
24 dom.find('.btn.restore').click(function() {
25 sandbox.publish('restoreVersion', historyItems.getSelected()[0]);
28 dom.find('.btn.display').click(function() {
29 sandbox.publish('displayVersion', {version: historyItems.getSelected()[0]});
32 var addHistoryItem = function(item, options) {
33 historyItems.add(item);
34 var view = new ItemView(item);
36 domNodes.itemList.prepend(view.dom);
38 view.dom.hide().slideDown();
42 var toggleItemViews = function(toggle) {
43 itemViews.forEach(function(view) {
44 if(!historyItems.isSelected(view.item)) {
50 var toggleButton = function(btn, toggle) {
51 dom.find('button.'+btn).toggleClass('disabled', !toggle);
57 select: function(item) {
58 if(this._selected.length < 2) {
59 this._selected.push(item.version);
65 unselect: function(item) {
66 this._selected = _.without(this._selected, item.version);
70 this._itemsById[item.version] = item;
72 isSelected: function(item) {
73 return _.contains(this._selected, item.version);
75 getSelected: function() {
76 return this._selected;
78 _updateUI: function() {
79 var len = this._selected.length;
81 toggleButton('compare', false);
82 toggleButton('display', false);
83 toggleButton('restore', false);
86 toggleButton('compare', false);
87 toggleButton('display', true);
88 toggleButton('restore', true);
91 toggleItemViews(false);
92 toggleButton('compare', true);
93 toggleButton('display', false);
94 toggleButton('restore', false);
96 toggleItemViews(true);
100 historyItems._updateUI();
102 var ItemView = function(item) {
104 this.dom = $(this.template(item));
105 this.dom.on('click', _.bind(this.onItemClicked, this));
107 ItemView.prototype.template = _.template(itemTemplateSrc);
108 ItemView.prototype.onItemClicked = function() {
109 if(historyItems.isSelected(this.item)) {
110 historyItems.unselect(this.item);
112 } else if(historyItems.select(this.item)) {
113 this.highlightItem();
116 ItemView.prototype.highlightItem = function() {
117 this.dom.addClass('highlighted');
119 ItemView.prototype.dimItem = function() {
120 this.dom.removeClass('highlighted');
122 ItemView.prototype.toggle = function(toggle) {
123 this.dom.toggleClass('disabled', !toggle);
129 start: function() { sandbox.publish('ready'); },
130 addHistory: function(history, options) {
131 history.forEach(function(historyItem) {
132 addHistoryItem(historyItem, options || {});
135 getView: function() {