2 'libs/jquery-1.9.1.min',
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(e) {
21 var selected = historyItems.getSelected();
22 sandbox.publish('compare', selected[0], selected[1]);
25 dom.find('.btn.restore').click(function(e) {
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(e) {
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))
55 var toggleButton = function(btn, toggle) {
56 dom.find('button.'+btn).toggleClass('disabled', !toggle);
62 select: function(item) {
63 if(this._selected.length < 2) {
64 this._selected.push(item.version);
70 unselect: function(item) {
71 this._selected = _.without(this._selected, item.version);
75 this._itemsById[item.version] = item;
77 isSelected: function(item) {
78 return _.contains(this._selected, item.version);
80 getSelected: function() {
81 return this._selected;
83 _updateUI: function() {
84 var len = this._selected.length;
86 toggleButton('compare', false);
87 toggleButton('display', false);
88 toggleButton('restore', false);
91 toggleButton('compare', false);
92 toggleButton('display', true);
93 toggleButton('restore', true);
96 toggleItemViews(false);
97 toggleButton('compare', true);
98 toggleButton('display', false);
99 toggleButton('restore', false);
101 toggleItemViews(true);
105 historyItems._updateUI();
107 var ItemView = function(item) {
109 this.dom = $(this.template(item));
110 this.dom.on('click', _.bind(this.onItemClicked, this));
112 ItemView.prototype.template = _.template(itemTemplateSrc);
113 ItemView.prototype.onItemClicked = function() {
114 if(historyItems.isSelected(this.item)) {
115 historyItems.unselect(this.item);
117 } else if(historyItems.select(this.item)) {
118 this.highlightItem();
121 ItemView.prototype.highlightItem = function() {
122 this.dom.addClass('highlighted');
124 ItemView.prototype.dimItem = function() {
125 this.dom.removeClass('highlighted');
127 ItemView.prototype.toggle = function(toggle) {
128 this.dom.toggleClass('disabled', !toggle);
134 start: function() { sandbox.publish('ready'); },
135 addHistory: function(history, options) {
136 history.forEach(function(historyItem) {
137 addHistoryItem(historyItem, options || {});
140 getView: function() {