X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/c66ddb7dbe763b261c7dd4a037239a667f60cd95..195a1011a3ac43b5ff7cec4346b87e7453cee16a:/modules/documentHistory/documentHistory.js?ds=sidebyside diff --git a/modules/documentHistory/documentHistory.js b/modules/documentHistory/documentHistory.js index 0db2a75..0693469 100644 --- a/modules/documentHistory/documentHistory.js +++ b/modules/documentHistory/documentHistory.js @@ -14,6 +14,12 @@ return function(sandbox) { itemList: dom.find('.rng-module-documentHistory-itemsList'), } var itemViews = []; + + + dom.find('.btn.compare').click(function(e) { + var selected = historyItems.getSelected(); + sandbox.publish('compare', selected[0], selected[1]); + }); var addHistoryItem = function(item, options) { historyItems.add(item); @@ -27,57 +33,85 @@ return function(sandbox) { var toggleItemViews = function(toggle) { itemViews.forEach(function(view) { - view.toggle(toggle); + if(!historyItems.isSelected(view.item)) + view.toggle(toggle); }); } + var toggleButton = function(btn, toggle) { + dom.find('button.'+btn).toggleClass('disabled', !toggle); + } + var historyItems = { _itemsById: {}, _selected: [], - select: function(id) { + select: function(item) { if(this._selected.length < 2) { - this._selected.push(id); - if(this._selected.length === 2) - toggleItemViews(false); + this._selected.push(item.version); + this._updateUI(); return true; } return false; }, unselect: function(item) { - this._selected = _.without(this._selected, id); - if(this._selected.length < 2) - toggleItemViews(true); + this._selected = _.without(this._selected, item.version); + this._updateUI(); }, add: function(item) { - this._itemsById[item.id] = item; + this._itemsById[item.version] = item; + }, + isSelected: function(item) { + return _.contains(this._selected, item.version); + }, + getSelected: function() { + return this._selected; }, - selected: function(item) { - return _.contains(_selected, item.id); + _updateUI: function() { + var len = this._selected.length; + if(len === 0) { + toggleButton('compare', false); + toggleButton('show2', false); + toggleButton('restore', false); + } + if(len === 1) { + toggleButton('compare', false); + toggleButton('show2', true); + toggleButton('restore', true); + } + if(len === 2) { + toggleItemViews(false); + toggleButton('compare', true); + toggleButton('show2', false); + toggleButton('restore', false); + } else { + toggleItemViews(true); + } } }; + historyItems._updateUI(); var itemView = function(item) { this.item = item; this.dom = $(this.template(item)); - this.dom.on('click', this.onItemClicked); + this.dom.on('click', _.bind(this.onItemClicked, this)); }; itemView.prototype.template = _.template(itemTemplateSrc); itemView.prototype.onItemClicked = function() { - if(historyItems.selected(item)) { - historyItems.unselect(item); + if(historyItems.isSelected(this.item)) { + historyItems.unselect(this.item); this.dimItem(); - } else if(historyItems.select(item)) { + } else if(historyItems.select(this.item)) { this.highlightItem(); } }; itemView.prototype.highlightItem = function() { - + this.dom.addClass('highlighted'); }; itemView.prototype.dimItem = function() { - + this.dom.removeClass('highlighted'); }; itemView.prototype.toggle = function(toggle) { - + this.dom.toggleClass('disabled', !toggle); };