From e8c45077de2d4ada496850fd77fa5035f07f8a4d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Mon, 17 Jun 2013 11:04:50 +0200 Subject: [PATCH 1/1] Restoring previous version of a publication --- modules/data/data.js | 16 ++++++ modules/documentHistory/documentHistory.js | 12 +++- modules/documentHistory/restoreDialog.js | 55 +++++++++++++++++++ .../templates/restoreDialog.html | 14 +++++ modules/rng/rng.js | 35 +++++++++--- 5 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 modules/documentHistory/restoreDialog.js create mode 100644 modules/documentHistory/templates/restoreDialog.html diff --git a/modules/data/data.js b/modules/data/data.js index 9b2b0cb..8c35436 100644 --- a/modules/data/data.js +++ b/modules/data/data.js @@ -102,6 +102,22 @@ return function(sandbox) { sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2}) }, }); + }, + restoreVersion: function(options) { + if(options.version && options.description) { + sandbox.publish('restoringStarted', {version: options.version}); + $.ajax({ + method: 'post', + dataType: 'json', + url: '/' + gettext('editor') + '/' + document_id + '/revert', + data: JSON.stringify(options), + success: function(data) { + doc = data.document; + reloadHistory(); + sandbox.publish('documentReverted', {document: data.document, version: data.version}); + }, + }); + } } } }; diff --git a/modules/documentHistory/documentHistory.js b/modules/documentHistory/documentHistory.js index 0693469..9a01f16 100644 --- a/modules/documentHistory/documentHistory.js +++ b/modules/documentHistory/documentHistory.js @@ -1,9 +1,10 @@ define([ 'libs/jquery-1.9.1.min', 'libs/underscore-min', +'./restoreDialog', 'libs/text!./templates/main.html', 'libs/text!./templates/item.html' -], function($, _, mainTemplateSrc, itemTemplateSrc) { +], function($, _, restoreDialog, mainTemplateSrc, itemTemplateSrc) { 'use strict'; @@ -20,6 +21,15 @@ return function(sandbox) { var selected = historyItems.getSelected(); sandbox.publish('compare', selected[0], selected[1]); }); + + dom.find('.btn.restore').click(function(e) { + var dialog = restoreDialog.create(); + dialog.on('restore', function(event) { + sandbox.publish('restoreVersion', {version: historyItems.getSelected()[0], description: event.data.description}); + event.success(); + }); + dialog.show(); + }); var addHistoryItem = function(item, options) { historyItems.add(item); diff --git a/modules/documentHistory/restoreDialog.js b/modules/documentHistory/restoreDialog.js new file mode 100644 index 0000000..543a546 --- /dev/null +++ b/modules/documentHistory/restoreDialog.js @@ -0,0 +1,55 @@ +define([ +'libs/text!./templates/restoreDialog.html', +'libs/underscore-min', +'libs/backbone-min', +'libs/jquery-1.9.1.min' +], function(restoreDialogTemplate, _, Backbone, $) { + + var DialogView = Backbone.View.extend({ + template: _.template(restoreDialogTemplate), + events: { + 'click .restore-btn': 'onSave', + 'click .cancel-btn': 'close', + 'click .close': 'close' + }, + initialize: function() { + _.bindAll(this); + this.actionsDisabled = false; + }, + show: function() { + this.setElement(this.template()); + this.$el.modal({backdrop: 'static'}); + this.$el.modal('show'); + + }, + onSave: function(e) { + e.preventDefault(); + var view = this; + this.trigger('restore', { + data: {description: view.$el.find('textarea').val()}, + success: function() { view.actionsDisabled = false; view.close(); }, + error: function() { view.actionsDisabled = false; view.close(); }, + }) + }, + close: function(e) { + if(e) + e.preventDefault(); + if(!this.actionsDisabled) { + this.$el.modal('hide'); + this.$el.remove(); + } + }, + toggleButtons: function(toggle) { + this.$('.btn, button').toggleClass('disabled', !toggle); + this.$('textarea').attr('disabled', !toggle); + this.actionsDisabled = !toggle; + } + }); + + return { + create: function() { + return new DialogView(); + } + } + +}); \ No newline at end of file diff --git a/modules/documentHistory/templates/restoreDialog.html b/modules/documentHistory/templates/restoreDialog.html new file mode 100644 index 0000000..72fea22 --- /dev/null +++ b/modules/documentHistory/templates/restoreDialog.html @@ -0,0 +1,14 @@ + \ No newline at end of file diff --git a/modules/rng/rng.js b/modules/rng/rng.js index a13cfce..cd7122c 100644 --- a/modules/rng/rng.js +++ b/modules/rng/rng.js @@ -68,6 +68,19 @@ return function(sandbox) { sandbox.getModule('nodeFamilyTree').setNode(wlxmlNode); sandbox.getModule('nodeBreadCrumbs').setNode(wlxmlNode); + }, + resetDocument: function(document, reason) { + var modules = []; + if(reason === 'source_edit') + modules = ['documentCanvas', 'metadataEditor']; + else if (reason === 'edit') + modules = ['sourceEditor']; + else if (reason === 'revert') + modules = ['documentCanvas', 'metadataEditor', 'sourceEditor']; + + modules.forEach(function(moduleName) { + sandbox.getModule(moduleName).setDocument(document); + }); } } @@ -122,15 +135,7 @@ return function(sandbox) { }); }, documentChanged: function(document, reason) { - var modules = []; - if(reason === 'source_edit') - modules = ['documentCanvas', 'metadataEditor']; - else if (reason === 'edit') - modules = ['sourceEditor']; - - modules.forEach(function(moduleName) { - sandbox.getModule(moduleName).setDocument(document); - }); + commands.resetDocument(document, reason); }, savingStarted: function() { sandbox.getModule('mainBar').setCommandEnabled('save', false); @@ -140,11 +145,20 @@ return function(sandbox) { sandbox.getModule('mainBar').setCommandEnabled('save', true); sandbox.getModule('indicator').clearMessage({message:'Dokument zapisany'}); }, + restoringStarted: function(event) { + sandbox.getModule('mainBar').setCommandEnabled('save', false); + sandbox.getModule('indicator').showMessage(gettext('Restoring version ' + event.version + '...')); + }, historyItemAdded: function(item) { sandbox.getModule('documentHistory').addHistory([item], {animate: true}); }, diffFetched: function(diff) { sandbox.getModule('diffViewer').setDiff(diff); + }, + documentReverted: function(event) { + commands.resetDocument(event.document, 'revert'); + sandbox.getModule('mainBar').setCommandEnabled('save', true); + sandbox.getModule('indicator').clearMessage({message:'Wersja ' + event.version + ' przywrócona'}); } } @@ -268,6 +282,9 @@ return function(sandbox) { }, compare: function(ver1, ver2) { sandbox.getModule('data').fetchDiff(ver1, ver2); + }, + restoreVersion: function(event) { + sandbox.getModule('data').restoreVersion(event); } } -- 2.20.1