2 'libs/jquery-1.9.1.min',
\r
3 'libs/underscore-min',
\r
5 'libs/text!./templates/main.html',
\r
6 'libs/text!./templates/item.html'
\r
7 ], function($, _, restoreDialog, mainTemplateSrc, itemTemplateSrc) {
\r
11 return function(sandbox) {
\r
13 var dom = $(_.template(mainTemplateSrc)());
\r
15 itemList: dom.find('.rng-module-documentHistory-itemsList'),
\r
20 dom.find('.btn.compare').click(function(e) {
\r
21 var selected = historyItems.getSelected();
\r
22 sandbox.publish('compare', selected[0], selected[1]);
\r
25 dom.find('.btn.restore').click(function(e) {
\r
26 var dialog = restoreDialog.create();
\r
27 dialog.on('restore', function(event) {
\r
28 sandbox.publish('restoreVersion', {version: historyItems.getSelected()[0], description: event.data.description});
\r
34 dom.find('.btn.display').click(function(e) {
\r
35 sandbox.publish('displayVersion', {version: historyItems.getSelected()[0]});
\r
38 var addHistoryItem = function(item, options) {
\r
39 historyItems.add(item);
\r
40 var view = new itemView(item);
\r
41 itemViews.push(view);
\r
42 domNodes.itemList.prepend(view.dom);
\r
43 if(options.animate) {
\r
44 view.dom.hide().slideDown();
\r
48 var toggleItemViews = function(toggle) {
\r
49 itemViews.forEach(function(view) {
\r
50 if(!historyItems.isSelected(view.item))
\r
51 view.toggle(toggle);
\r
55 var toggleButton = function(btn, toggle) {
\r
56 dom.find('button.'+btn).toggleClass('disabled', !toggle);
\r
59 var historyItems = {
\r
62 select: function(item) {
\r
63 if(this._selected.length < 2) {
\r
64 this._selected.push(item.version);
\r
70 unselect: function(item) {
\r
71 this._selected = _.without(this._selected, item.version);
\r
74 add: function(item) {
\r
75 this._itemsById[item.version] = item;
\r
77 isSelected: function(item) {
\r
78 return _.contains(this._selected, item.version);
\r
80 getSelected: function() {
\r
81 return this._selected;
\r
83 _updateUI: function() {
\r
84 var len = this._selected.length;
\r
86 toggleButton('compare', false);
\r
87 toggleButton('display', false);
\r
88 toggleButton('restore', false);
\r
91 toggleButton('compare', false);
\r
92 toggleButton('display', true);
\r
93 toggleButton('restore', true);
\r
96 toggleItemViews(false);
\r
97 toggleButton('compare', true);
\r
98 toggleButton('display', false);
\r
99 toggleButton('restore', false);
\r
101 toggleItemViews(true);
\r
105 historyItems._updateUI();
\r
107 var itemView = function(item) {
\r
109 this.dom = $(this.template(item));
\r
110 this.dom.on('click', _.bind(this.onItemClicked, this));
\r
112 itemView.prototype.template = _.template(itemTemplateSrc);
\r
113 itemView.prototype.onItemClicked = function() {
\r
114 if(historyItems.isSelected(this.item)) {
\r
115 historyItems.unselect(this.item);
\r
117 } else if(historyItems.select(this.item)) {
\r
118 this.highlightItem();
\r
121 itemView.prototype.highlightItem = function() {
\r
122 this.dom.addClass('highlighted');
\r
124 itemView.prototype.dimItem = function() {
\r
125 this.dom.removeClass('highlighted');
\r
127 itemView.prototype.toggle = function(toggle) {
\r
128 this.dom.toggleClass('disabled', !toggle);
\r
134 start: function() { sandbox.publish('ready'); },
135 addHistory: function(history, options) {
\r
136 history.forEach(function(historyItem) {
\r
137 addHistoryItem(historyItem, options || {});
\r
140 getView: function() {
\r