Restoring previous version of a publication
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 17 Jun 2013 09:04:50 +0000 (11:04 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 17 Jun 2013 09:04:50 +0000 (11:04 +0200)
modules/data/data.js
modules/documentHistory/documentHistory.js
modules/documentHistory/restoreDialog.js [new file with mode: 0644]
modules/documentHistory/templates/restoreDialog.html [new file with mode: 0644]
modules/rng/rng.js

index 9b2b0cb..8c35436 100644 (file)
@@ -102,6 +102,22 @@ return function(sandbox) {
                     sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2})\r
                 },\r
             });\r
                     sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2})\r
                 },\r
             });\r
+        },\r
+        restoreVersion: function(options) {\r
+            if(options.version && options.description) {\r
+                sandbox.publish('restoringStarted', {version: options.version});\r
+                $.ajax({\r
+                    method: 'post',\r
+                    dataType: 'json',\r
+                    url: '/' + gettext('editor') + '/' + document_id + '/revert',\r
+                    data: JSON.stringify(options),\r
+                    success: function(data) {\r
+                        doc = data.document;\r
+                        reloadHistory();\r
+                        sandbox.publish('documentReverted', {document: data.document, version: data.version});\r
+                    },\r
+                }); \r
+            }\r
         }\r
     }\r
 };\r
         }\r
     }\r
 };\r
index 0693469..9a01f16 100644 (file)
@@ -1,9 +1,10 @@
 define([\r
 'libs/jquery-1.9.1.min',\r
 'libs/underscore-min',\r
 define([\r
 'libs/jquery-1.9.1.min',\r
 'libs/underscore-min',\r
+'./restoreDialog',\r
 'libs/text!./templates/main.html',\r
 'libs/text!./templates/item.html'\r
 'libs/text!./templates/main.html',\r
 'libs/text!./templates/item.html'\r
-], function($, _, mainTemplateSrc, itemTemplateSrc) {\r
+], function($, _, restoreDialog, mainTemplateSrc, itemTemplateSrc) {\r
 \r
 'use strict';\r
     \r
 \r
 'use strict';\r
     \r
@@ -20,6 +21,15 @@ return function(sandbox) {
         var selected = historyItems.getSelected();\r
         sandbox.publish('compare', selected[0], selected[1]);\r
     });\r
         var selected = historyItems.getSelected();\r
         sandbox.publish('compare', selected[0], selected[1]);\r
     });\r
+    \r
+    dom.find('.btn.restore').click(function(e) {\r
+        var dialog = restoreDialog.create();\r
+        dialog.on('restore', function(event) {\r
+            sandbox.publish('restoreVersion', {version: historyItems.getSelected()[0], description: event.data.description});\r
+            event.success();\r
+        });\r
+        dialog.show();\r
+    });\r
         \r
     var addHistoryItem = function(item, options) {\r
         historyItems.add(item);\r
         \r
     var addHistoryItem = function(item, options) {\r
         historyItems.add(item);\r
diff --git a/modules/documentHistory/restoreDialog.js b/modules/documentHistory/restoreDialog.js
new file mode 100644 (file)
index 0000000..543a546
--- /dev/null
@@ -0,0 +1,55 @@
+define([\r
+'libs/text!./templates/restoreDialog.html',\r
+'libs/underscore-min',\r
+'libs/backbone-min',\r
+'libs/jquery-1.9.1.min'\r
+], function(restoreDialogTemplate, _, Backbone, $) {\r
+\r
+    var DialogView = Backbone.View.extend({\r
+        template: _.template(restoreDialogTemplate),\r
+        events: {\r
+            'click .restore-btn': 'onSave',\r
+            'click .cancel-btn': 'close',\r
+            'click .close': 'close'\r
+        },\r
+        initialize: function() {\r
+            _.bindAll(this);\r
+            this.actionsDisabled = false;\r
+        },\r
+        show: function() {           \r
+            this.setElement(this.template());\r
+            this.$el.modal({backdrop: 'static'});\r
+            this.$el.modal('show');\r
+\r
+        },\r
+        onSave: function(e) {\r
+            e.preventDefault();\r
+            var view = this;\r
+            this.trigger('restore', {\r
+                data: {description: view.$el.find('textarea').val()},\r
+                success: function() { view.actionsDisabled = false; view.close(); },\r
+                error: function() { view.actionsDisabled = false; view.close(); },\r
+            })\r
+        },\r
+        close: function(e) {\r
+            if(e)\r
+                e.preventDefault();\r
+            if(!this.actionsDisabled) {\r
+                this.$el.modal('hide');\r
+                this.$el.remove();\r
+            }\r
+        },\r
+        toggleButtons: function(toggle) {\r
+            this.$('.btn, button').toggleClass('disabled', !toggle);\r
+            this.$('textarea').attr('disabled', !toggle);\r
+            this.actionsDisabled = !toggle;\r
+        }\r
+    });\r
+\r
+    return {\r
+        create: function() {\r
+            return new DialogView();\r
+        }\r
+    }\r
+\r
+});
\ No newline at end of file
diff --git a/modules/documentHistory/templates/restoreDialog.html b/modules/documentHistory/templates/restoreDialog.html
new file mode 100644 (file)
index 0000000..72fea22
--- /dev/null
@@ -0,0 +1,14 @@
+<div class="rng-module-data-saveDialog modal hide static">\r
+    <div class="modal-header">\r
+        <button type="button" class="close">&times;</button>\r
+        <h1>Przywróć wersję</h1>\r
+    </div>\r
+    <div class="modal-body">\r
+        <label>Opisz powód przywrócenia</label>\r
+        <textarea rows="5"></textarea>\r
+    </div>\r
+    <div class="modal-footer">\r
+        <a href="#" class="btn btn-info btn-mini restore-btn">Przywróć</a>\r
+        <a href="#" class="btn btn-danger btn-mini cancel-btn">Anuluj</a>\r
+    </div>\r
+</div>
\ No newline at end of file
index a13cfce..cd7122c 100644 (file)
@@ -68,6 +68,19 @@ return function(sandbox) {
             sandbox.getModule('nodeFamilyTree').setNode(wlxmlNode);\r
             sandbox.getModule('nodeBreadCrumbs').setNode(wlxmlNode);\r
             \r
             sandbox.getModule('nodeFamilyTree').setNode(wlxmlNode);\r
             sandbox.getModule('nodeBreadCrumbs').setNode(wlxmlNode);\r
             \r
+        },\r
+        resetDocument: function(document, reason) {\r
+            var modules = [];\r
+            if(reason === 'source_edit')\r
+                modules = ['documentCanvas', 'metadataEditor'];\r
+            else if (reason === 'edit')\r
+                modules = ['sourceEditor'];\r
+            else if (reason === 'revert')\r
+                modules = ['documentCanvas', 'metadataEditor', 'sourceEditor'];\r
+                \r
+            modules.forEach(function(moduleName) {\r
+                sandbox.getModule(moduleName).setDocument(document);\r
+            });\r
         }\r
     }\r
     \r
         }\r
     }\r
     \r
@@ -122,15 +135,7 @@ return function(sandbox) {
             });\r
         },\r
         documentChanged: function(document, reason) {\r
             });\r
         },\r
         documentChanged: function(document, reason) {\r
-            var modules = [];\r
-            if(reason === 'source_edit')\r
-                modules = ['documentCanvas', 'metadataEditor'];\r
-            else if (reason === 'edit')\r
-                modules = ['sourceEditor'];\r
-                \r
-            modules.forEach(function(moduleName) {\r
-                sandbox.getModule(moduleName).setDocument(document);\r
-            });\r
+            commands.resetDocument(document, reason);\r
         },\r
         savingStarted: function() {\r
             sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
         },\r
         savingStarted: function() {\r
             sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
@@ -140,11 +145,20 @@ return function(sandbox) {
             sandbox.getModule('mainBar').setCommandEnabled('save', true);\r
             sandbox.getModule('indicator').clearMessage({message:'Dokument zapisany'});\r
         },\r
             sandbox.getModule('mainBar').setCommandEnabled('save', true);\r
             sandbox.getModule('indicator').clearMessage({message:'Dokument zapisany'});\r
         },\r
+        restoringStarted: function(event) {\r
+            sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
+            sandbox.getModule('indicator').showMessage(gettext('Restoring version ' + event.version + '...'));\r
+        },\r
         historyItemAdded: function(item) {\r
             sandbox.getModule('documentHistory').addHistory([item], {animate: true});\r
         },\r
         diffFetched: function(diff) {\r
             sandbox.getModule('diffViewer').setDiff(diff);\r
         historyItemAdded: function(item) {\r
             sandbox.getModule('documentHistory').addHistory([item], {animate: true});\r
         },\r
         diffFetched: function(diff) {\r
             sandbox.getModule('diffViewer').setDiff(diff);\r
+        },\r
+        documentReverted: function(event) {\r
+            commands.resetDocument(event.document, 'revert');\r
+            sandbox.getModule('mainBar').setCommandEnabled('save', true);\r
+            sandbox.getModule('indicator').clearMessage({message:'Wersja ' + event.version + ' przywrócona'});\r
         }\r
     }\r
     \r
         }\r
     }\r
     \r
@@ -268,6 +282,9 @@ return function(sandbox) {
         },\r
         compare: function(ver1, ver2) {\r
             sandbox.getModule('data').fetchDiff(ver1, ver2);\r
         },\r
         compare: function(ver1, ver2) {\r
             sandbox.getModule('data').fetchDiff(ver1, ver2);\r
+        },\r
+        restoreVersion: function(event) {\r
+            sandbox.getModule('data').restoreVersion(event);\r
         }\r
     }\r
     \r
         }\r
     }\r
     \r