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 var addHistoryItem = function(item, options) {
\r
35 historyItems.add(item);
\r
36 var view = new itemView(item);
\r
37 itemViews.push(view);
\r
38 domNodes.itemList.prepend(view.dom);
\r
39 if(options.animate) {
\r
40 view.dom.hide().slideDown();
\r
44 var toggleItemViews = function(toggle) {
\r
45 itemViews.forEach(function(view) {
\r
46 if(!historyItems.isSelected(view.item))
\r
47 view.toggle(toggle);
\r
51 var toggleButton = function(btn, toggle) {
\r
52 dom.find('button.'+btn).toggleClass('disabled', !toggle);
\r
55 var historyItems = {
\r
58 select: function(item) {
\r
59 if(this._selected.length < 2) {
\r
60 this._selected.push(item.version);
\r
66 unselect: function(item) {
\r
67 this._selected = _.without(this._selected, item.version);
\r
70 add: function(item) {
\r
71 this._itemsById[item.version] = item;
\r
73 isSelected: function(item) {
\r
74 return _.contains(this._selected, item.version);
\r
76 getSelected: function() {
\r
77 return this._selected;
\r
79 _updateUI: function() {
\r
80 var len = this._selected.length;
\r
82 toggleButton('compare', false);
\r
83 toggleButton('show2', false);
\r
84 toggleButton('restore', false);
\r
87 toggleButton('compare', false);
\r
88 toggleButton('show2', true);
\r
89 toggleButton('restore', true);
\r
92 toggleItemViews(false);
\r
93 toggleButton('compare', true);
\r
94 toggleButton('show2', false);
\r
95 toggleButton('restore', false);
\r
97 toggleItemViews(true);
\r
101 historyItems._updateUI();
\r
103 var itemView = function(item) {
\r
105 this.dom = $(this.template(item));
\r
106 this.dom.on('click', _.bind(this.onItemClicked, this));
\r
108 itemView.prototype.template = _.template(itemTemplateSrc);
\r
109 itemView.prototype.onItemClicked = function() {
\r
110 if(historyItems.isSelected(this.item)) {
\r
111 historyItems.unselect(this.item);
\r
113 } else if(historyItems.select(this.item)) {
\r
114 this.highlightItem();
\r
117 itemView.prototype.highlightItem = function() {
\r
118 this.dom.addClass('highlighted');
\r
120 itemView.prototype.dimItem = function() {
\r
121 this.dom.removeClass('highlighted');
\r
123 itemView.prototype.toggle = function(toggle) {
\r
124 this.dom.toggleClass('disabled', !toggle);
\r
130 start: function() { sandbox.publish('ready'); },
131 addHistory: function(history, options) {
\r
132 history.forEach(function(historyItem) {
\r
133 addHistoryItem(historyItem, options || {});
\r
136 getView: function() {
\r