editor: bring back restore dialog after integration
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 17 Dec 2013 16:09:51 +0000 (17:09 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 17 Dec 2013 16:09:51 +0000 (17:09 +0100)
Both save and restore dialog are now handled by more generalized
dialog object, based on previous dialog object implementation.

12 files changed:
src/editor/modules/data/data.js
src/editor/modules/data/data.less
src/editor/modules/data/dialog.html [new file with mode: 0644]
src/editor/modules/data/dialog.js [new file with mode: 0644]
src/editor/modules/data/dialog.less [new file with mode: 0644]
src/editor/modules/data/saveDialog.html [deleted file]
src/editor/modules/data/saveDialog.js [deleted file]
src/editor/modules/data/saveDialog.less [deleted file]
src/editor/modules/documentHistory/documentHistory.js
src/editor/modules/documentHistory/restoreDialog.js [deleted file]
src/editor/modules/documentHistory/templates/restoreDialog.html [deleted file]
src/editor/modules/rng/rng.js

index bce9582..4a9bc72 100644 (file)
@@ -1,12 +1,12 @@
 define([
     'libs/jquery',
-    './saveDialog',
+    './dialog',
     'wlxml/wlxml',
     'wlxml/extensions/list/list'
-
-], function($, saveDialog, wlxml, listExtension) {
+], function($, Dialog, wlxml, listExtension) {
 
 'use strict';
+/* global gettext */
 
 return function(sandbox) {
 
@@ -75,7 +75,11 @@ return function(sandbox) {
                     },
                     sandbox.getConfig().documentSaveForm
                 ),
-                dialog = saveDialog.create({fields: documentSaveForm.fields});
+                dialog = Dialog.create({
+                    fields: documentSaveForm.fields,
+                    title: gettext('Save Document'),
+                    submitButtonText: gettext('Save')
+                });
             
             dialog.on('save', function(event) {
                 sandbox.publish('savingStarted');
@@ -120,22 +124,41 @@ return function(sandbox) {
                 },
             });
         },
-        restoreVersion: function(options) {
-            if(options.version && options.description) {
-                sandbox.publish('restoringStarted', {version: options.version});
+        restoreVersion: function(version) {
+            var documentRestoreForm = $.extend({
+                        fields: [],
+                        version_field_name: 'version'
+                    },
+                    sandbox.getConfig().documentRestoreForm
+                ),
+                dialog = Dialog.create({
+                    fields: documentRestoreForm.fields,
+                    title: gettext('Restore Version'),
+                    submitButtonText: gettext('Restore')
+                });
+
+            dialog.on('save', function(event) {
+                var formData = event.formData;
+                formData[documentRestoreForm.version_field_name] = version;
+                sandbox.publish('restoringStarted', {version: version});
+                if(sandbox.getConfig().jsonifySentData) {
+                    formData = JSON.stringify(formData);
+                }
                 $.ajax({
                     method: 'post',
                     dataType: 'json',
                     url: sandbox.getConfig().documentRestoreUrl(document_id),
-                    data: JSON.stringify(options),
+                    data: formData,
                     success: function(data) {
-                        document_version = data.current_version;
+                        document_version = data.version;
                         reloadHistory();
                         wlxmlDocument.loadXML(data.document);
-                        sandbox.publish('documentReverted', data);
+                        sandbox.publish('documentReverted', data.version);
+                        event.success();
                     },
                 });
-            }
+            });
+            dialog.show();
         },
         getDocumentId: function() {
             return document_id;
index 1beb090..2a21630 100644 (file)
@@ -1 +1 @@
-@import 'saveDialog.less';
\ No newline at end of file
+@import 'dialog.less';
\ No newline at end of file
diff --git a/src/editor/modules/data/dialog.html b/src/editor/modules/data/dialog.html
new file mode 100644 (file)
index 0000000..844a6ff
--- /dev/null
@@ -0,0 +1,12 @@
+<div class="rng-dialog modal hide static">
+    <div class="modal-header">
+        <button type="button" class="close">&times;</button>
+        <h1><%= title %></h1>
+    </div>
+    <div class="modal-body">
+    </div>
+    <div class="modal-footer">
+        <a href="#" class="btn btn-info btn-mini save-btn"><%= submitButtonText %></a>
+        <a href="#" class="btn btn-danger btn-mini cancel-btn">Anuluj</a>
+    </div>
+</div>
\ No newline at end of file
diff --git a/src/editor/modules/data/dialog.js b/src/editor/modules/data/dialog.js
new file mode 100644 (file)
index 0000000..0ad87e5
--- /dev/null
@@ -0,0 +1,83 @@
+define(function(require) {
+
+    'use strict';
+
+    var _ = require('libs/underscore'),
+        Backbone = require('libs/backbone'),
+        saveDialogTemplate = require('libs/text!./dialog.html'),
+        fieldTemplates = {};
+        fieldTemplates.checkbox = require('libs/text!./templates/checkbox.html');
+        fieldTemplates.select = require('libs/text!./templates/select.html');
+        fieldTemplates.textarea = require('libs/text!./templates/textarea.html');
+        fieldTemplates.input = require('libs/text!./templates/input.html');
+
+
+
+    var DialogView = Backbone.View.extend({
+        template: _.template(saveDialogTemplate),
+        events: {
+            'click .save-btn': 'onSave',
+            'click .cancel-btn': 'close',
+            'click .close': 'close'
+        },
+        initialize: function() {
+            _.bindAll(this);
+            this.actionsDisabled = false;
+        },
+        show: function() {
+            this.setElement(this.template(this.options));
+
+            var body = this.$('.modal-body');
+            this.options.fields.forEach(function(field) {
+                var template = fieldTemplates[field.type];
+                if(!template) {
+                    throw new Error('Field type {type} not recognized.'.replace('{type}', field.type));
+                }
+                body.append(
+                    _.template(template)(_.extend({description: ''}, field))
+                );
+            });
+
+            this.$el.modal({backdrop: 'static'});
+            this.$el.modal('show');
+            this.$('textarea').focus();
+        },
+        onSave: function(e) {
+            e.preventDefault();
+            var view = this,
+                formData = {};
+
+            this.options.fields.forEach(function(field) {
+                var widget = view.$('[name=' + field.name +']');
+                formData[field.name] = widget.val();
+            });
+
+            this.trigger('save', {
+                formData: formData,
+                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(config) {
+            return new DialogView(config);
+        }
+    };
+
+});
\ No newline at end of file
diff --git a/src/editor/modules/data/dialog.less b/src/editor/modules/data/dialog.less
new file mode 100644 (file)
index 0000000..8128162
--- /dev/null
@@ -0,0 +1,25 @@
+.rng-dialog {
+    textarea {
+        padding: 3px 3px;
+        margin: 5px auto;
+        width: 95%;
+        display: block;
+    }
+    
+    h1, label {
+        font-size: 12px;
+        line-height: 12px;
+
+    }
+    
+    h1 {
+        margin: 2px 5px;
+        font-weight: bold;
+    }
+
+    .description {
+        font-size: .8em;
+    }
+
+    width: 620px;
+}
\ No newline at end of file
diff --git a/src/editor/modules/data/saveDialog.html b/src/editor/modules/data/saveDialog.html
deleted file mode 100644 (file)
index d0238e0..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-<div class="rng-module-data-saveDialog modal hide static">
-    <div class="modal-header">
-        <button type="button" class="close">&times;</button>
-        <h1>Zapisz dokument</h1>
-    </div>
-    <div class="modal-body">
-    </div>
-    <div class="modal-footer">
-        <a href="#" class="btn btn-info btn-mini save-btn">Zapisz</a>
-        <a href="#" class="btn btn-danger btn-mini cancel-btn">Anuluj</a>
-    </div>
-</div>
\ No newline at end of file
diff --git a/src/editor/modules/data/saveDialog.js b/src/editor/modules/data/saveDialog.js
deleted file mode 100644 (file)
index 3aa17ec..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-define(function(require) {
-
-    'use strict';
-
-    var _ = require('libs/underscore'),
-        Backbone = require('libs/backbone'),
-        saveDialogTemplate = require('libs/text!./saveDialog.html'),
-        fieldTemplates = {};
-        fieldTemplates.checkbox = require('libs/text!./templates/checkbox.html');
-        fieldTemplates.select = require('libs/text!./templates/select.html');
-        fieldTemplates.textarea = require('libs/text!./templates/textarea.html');
-        fieldTemplates.input = require('libs/text!./templates/input.html');
-
-
-
-    var DialogView = Backbone.View.extend({
-        template: _.template(saveDialogTemplate),
-        events: {
-            'click .save-btn': 'onSave',
-            'click .cancel-btn': 'close',
-            'click .close': 'close'
-        },
-        initialize: function() {
-            _.bindAll(this);
-            this.actionsDisabled = false;
-        },
-        show: function() {
-            this.setElement(this.template());
-
-            var body = this.$('.modal-body');
-            this.options.fields.forEach(function(field) {
-                var template = fieldTemplates[field.type];
-                if(!template) {
-                    throw new Error('Field type {type} not recognized.'.replace('{type}', field.type));
-                }
-                body.append(
-                    _.template(template)(_.extend({description: ''}, field))
-                );
-            });
-
-            this.$el.modal({backdrop: 'static'});
-            this.$el.modal('show');
-            this.$('textarea').focus();
-        },
-        onSave: function(e) {
-            e.preventDefault();
-            var view = this,
-                formData = {};
-
-            this.options.fields.forEach(function(field) {
-                var widget = view.$('[name=' + field.name +']');
-                formData[field.name] = widget.val();
-            });
-
-            this.trigger('save', {
-                formData: formData,
-                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(config) {
-            return new DialogView(config);
-        }
-    };
-
-});
\ No newline at end of file
diff --git a/src/editor/modules/data/saveDialog.less b/src/editor/modules/data/saveDialog.less
deleted file mode 100644 (file)
index e0530e5..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-.rng-module-data-saveDialog {
-    textarea {
-        padding: 3px 3px;
-        margin: 5px auto;
-        width: 95%;
-        display: block;
-    }
-    
-    h1, label {
-        font-size: 12px;
-        line-height: 12px;
-
-    }
-    
-    h1 {
-        margin: 2px 5px;
-        font-weight: bold;
-    }
-
-    .description {
-        font-size: .8em;
-    }
-
-    width: 620px;
-}
\ No newline at end of file
index b90fc59..7bbf32f 100644 (file)
@@ -1,10 +1,9 @@
 define([
 'libs/jquery',
 'libs/underscore',
-'./restoreDialog',
 'libs/text!./templates/main.html',
 'libs/text!./templates/item.html'
-], function($, _, restoreDialog, mainTemplateSrc, itemTemplateSrc) {
+], function($, _, mainTemplateSrc, itemTemplateSrc) {
 
 'use strict';
     
@@ -23,12 +22,7 @@ return function(sandbox) {
     });
     
     dom.find('.btn.restore').click(function() {
-        var dialog = restoreDialog.create();
-        dialog.on('restore', function(event) {
-            sandbox.publish('restoreVersion', {version: historyItems.getSelected()[0], description: event.data.description});
-            event.success();
-        });
-        dialog.show();
+        sandbox.publish('restoreVersion', historyItems.getSelected()[0]);
     });
     
     dom.find('.btn.display').click(function() {
diff --git a/src/editor/modules/documentHistory/restoreDialog.js b/src/editor/modules/documentHistory/restoreDialog.js
deleted file mode 100644 (file)
index a05b50f..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-define([
-'libs/text!./templates/restoreDialog.html',
-'libs/underscore',
-'libs/backbone'
-], function(restoreDialogTemplate, _, Backbone) {
-
-    'use strict';
-
-
-    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');
-            this.$('textarea').focus();
-        },
-        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/src/editor/modules/documentHistory/templates/restoreDialog.html b/src/editor/modules/documentHistory/templates/restoreDialog.html
deleted file mode 100644 (file)
index ded0a11..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-<div class="rng-module-data-saveDialog modal hide static">
-    <div class="modal-header">
-        <button type="button" class="close">&times;</button>
-        <h1>Przywróć wersję</h1>
-    </div>
-    <div class="modal-body">
-        <label>Opisz powód przywrócenia</label>
-        <textarea rows="5"></textarea>
-    </div>
-    <div class="modal-footer">
-        <a href="#" class="btn btn-info btn-mini restore-btn">Przywróć</a>
-        <a href="#" class="btn btn-danger btn-mini cancel-btn">Anuluj</a>
-    </div>
-</div>
\ No newline at end of file
index a5432c0..4e7ff0c 100644 (file)
@@ -108,10 +108,10 @@ return function(sandbox) {
         diffFetched: function(diff) {
             sandbox.getModule('diffViewer').setDiff(diff);
         },
-        documentReverted: function(event) {
+        documentReverted: function(version) {
             sandbox.getModule('mainBar').setCommandEnabled('save', true);
-            sandbox.getModule('indicator').clearMessage({message:'Wersja ' + event.reverted_version + ' przywrócona'});
-            sandbox.getModule('mainBar').setVersion(event.current_version);
+            sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
+            sandbox.getModule('mainBar').setVersion(version);
         }
     };
     
@@ -224,8 +224,8 @@ return function(sandbox) {
         compare: function(ver1, ver2) {
             sandbox.getModule('data').fetchDiff(ver1, ver2);
         },
-        restoreVersion: function(event) {
-            sandbox.getModule('data').restoreVersion(event);
+        restoreVersion: function(version) {
+            sandbox.getModule('data').restoreVersion(version);
         },
         displayVersion: function(event) {
             /* globals window */