5 'libs/text!./templates/main.html',
6 'libs/text!./templates/item.html'
7 ], function($, _, restoreDialog, mainTemplateSrc, itemTemplateSrc) {
11 return function(sandbox) {
13 var dom = $(_.template(mainTemplateSrc)());
15 itemList: dom.find('.rng-module-documentHistory-itemsList'),
20 dom.find('.btn.compare').click(function() {
21 var selected = historyItems.getSelected();
22 sandbox.publish('compare', selected[0], selected[1]);
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});
34 dom.find('.btn.display').click(function() {
35 sandbox.publish('displayVersion', {version: historyItems.getSelected()[0]});
38 var addHistoryItem = function(item, options) {
39 historyItems.add(item);
40 var view = new ItemView(item);
42 domNodes.itemList.prepend(view.dom);
44 view.dom.hide().slideDown();
48 var toggleItemViews = function(toggle) {
49 itemViews.forEach(function(view) {
50 if(!historyItems.isSelected(view.item)) {
56 var toggleButton = function(btn, toggle) {
57 dom.find('button.'+btn).toggleClass('disabled', !toggle);
63 select: function(item) {
64 if(this._selected.length < 2) {
65 this._selected.push(item.version);
71 unselect: function(item) {
72 this._selected = _.without(this._selected, item.version);
76 this._itemsById[item.version] = item;
78 isSelected: function(item) {
79 return _.contains(this._selected, item.version);
81 getSelected: function() {
82 return this._selected;
84 _updateUI: function() {
85 var len = this._selected.length;
87 toggleButton('compare', false);
88 toggleButton('display', false);
89 toggleButton('restore', false);
92 toggleButton('compare', false);
93 toggleButton('display', true);
94 toggleButton('restore', true);
97 toggleItemViews(false);
98 toggleButton('compare', true);
99 toggleButton('display', false);
100 toggleButton('restore', false);
102 toggleItemViews(true);
106 historyItems._updateUI();
108 var ItemView = function(item) {
110 this.dom = $(this.template(item));
111 this.dom.on('click', _.bind(this.onItemClicked, this));
113 ItemView.prototype.template = _.template(itemTemplateSrc);
114 ItemView.prototype.onItemClicked = function() {
115 if(historyItems.isSelected(this.item)) {
116 historyItems.unselect(this.item);
118 } else if(historyItems.select(this.item)) {
119 this.highlightItem();
122 ItemView.prototype.highlightItem = function() {
123 this.dom.addClass('highlighted');
125 ItemView.prototype.dimItem = function() {
126 this.dom.removeClass('highlighted');
128 ItemView.prototype.toggle = function(toggle) {
129 this.dom.toggleClass('disabled', !toggle);
135 start: function() { sandbox.publish('ready'); },
136 addHistory: function(history, options) {
137 history.forEach(function(historyItem) {
138 addHistoryItem(historyItem, options || {});
141 getView: function() {