From: Aleksander Łukasz Date: Tue, 17 Dec 2013 16:09:51 +0000 (+0100) Subject: editor: bring back restore dialog after integration X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/e68778f27b18c21c23387d7609d825ab3a192af7 editor: bring back restore dialog after integration Both save and restore dialog are now handled by more generalized dialog object, based on previous dialog object implementation. --- diff --git a/src/editor/modules/data/data.js b/src/editor/modules/data/data.js index bce9582..4a9bc72 100644 --- a/src/editor/modules/data/data.js +++ b/src/editor/modules/data/data.js @@ -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; diff --git a/src/editor/modules/data/data.less b/src/editor/modules/data/data.less index 1beb090..2a21630 100644 --- a/src/editor/modules/data/data.less +++ b/src/editor/modules/data/data.less @@ -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 index 0000000..844a6ff --- /dev/null +++ b/src/editor/modules/data/dialog.html @@ -0,0 +1,12 @@ + \ 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 index 0000000..0ad87e5 --- /dev/null +++ b/src/editor/modules/data/dialog.js @@ -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 index 0000000..8128162 --- /dev/null +++ b/src/editor/modules/data/dialog.less @@ -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 index d0238e0..0000000 --- a/src/editor/modules/data/saveDialog.html +++ /dev/null @@ -1,12 +0,0 @@ - \ 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 index 3aa17ec..0000000 --- a/src/editor/modules/data/saveDialog.js +++ /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 index e0530e5..0000000 --- a/src/editor/modules/data/saveDialog.less +++ /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 diff --git a/src/editor/modules/documentHistory/documentHistory.js b/src/editor/modules/documentHistory/documentHistory.js index b90fc59..7bbf32f 100644 --- a/src/editor/modules/documentHistory/documentHistory.js +++ b/src/editor/modules/documentHistory/documentHistory.js @@ -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 index a05b50f..0000000 --- a/src/editor/modules/documentHistory/restoreDialog.js +++ /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 index ded0a11..0000000 --- a/src/editor/modules/documentHistory/templates/restoreDialog.html +++ /dev/null @@ -1,14 +0,0 @@ - \ No newline at end of file diff --git a/src/editor/modules/rng/rng.js b/src/editor/modules/rng/rng.js index a5432c0..4e7ff0c 100644 --- a/src/editor/modules/rng/rng.js +++ b/src/editor/modules/rng/rng.js @@ -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 */