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', historyItems.getSelected()[0]);
32 dom.find('.btn.publish').click(function() {
33 sandbox.publish('publishVersion', historyItems.getSelected()[0]);
36 var addHistoryItem = function(item, options) {
37 historyItems.add(item);
38 var view = new ItemView(item);
40 domNodes.itemList.prepend(view.dom);
42 view.dom.hide().slideDown();
46 var toggleItemViews = function(toggle) {
47 itemViews.forEach(function(view) {
48 if(!historyItems.isSelected(view.item)) {
54 var toggleButton = function(btn, toggle) {
55 dom.find('button.'+btn).toggleClass('disabled', !toggle);
61 select: function(item) {
62 if(this._selected.length < 2) {
63 this._selected.push(item.revision);
69 unselect: function(item) {
70 this._selected = _.without(this._selected, item.revision);
74 this._itemsById[item.version] = item;
76 isSelected: function(item) {
77 return _.contains(this._selected, item.revision);
79 getSelected: function() {
80 return this._selected;
82 _updateUI: function() {
83 var len = this._selected.length;
85 toggleButton('compare', false);
86 toggleButton('display', false);
87 toggleButton('restore', false);
88 toggleButton('publish', false);
91 toggleButton('compare', false);
92 toggleButton('display', true);
93 toggleButton('restore', true);
94 toggleButton('publish', true);
97 toggleItemViews(false);
98 toggleButton('compare', true);
99 toggleButton('display', false);
100 toggleButton('restore', false);
101 toggleButton('publish', false);
103 toggleItemViews(true);
107 historyItems._updateUI();
109 var ItemView = function(item) {
111 this.dom = $(this.template(item));
112 this.dom.on('click', _.bind(this.onItemClicked, this));
114 ItemView.prototype.template = _.template(itemTemplateSrc);
115 ItemView.prototype.onItemClicked = function() {
116 if(historyItems.isSelected(this.item)) {
117 historyItems.unselect(this.item);
119 } else if(historyItems.select(this.item)) {
120 this.highlightItem();
123 ItemView.prototype.highlightItem = function() {
124 this.dom.addClass('highlighted');
126 ItemView.prototype.dimItem = function() {
127 this.dom.removeClass('highlighted');
129 ItemView.prototype.toggle = function(toggle) {
130 this.dom.toggleClass('disabled', !toggle);
136 start: function() { sandbox.publish('ready'); },
137 addHistory: function(history, options) {
138 history.forEach(function(historyItem) {
139 addHistoryItem(historyItem, options || {});
142 getView: function() {