integration wip: saving document
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 10 Dec 2013 21:50:23 +0000 (22:50 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 15 Dec 2013 21:32:50 +0000 (22:32 +0100)
- configurable save form
- configurable api urls

src/editor/modules/data/data.js
src/editor/modules/data/saveDialog.html
src/editor/modules/data/saveDialog.js
src/editor/modules/data/saveDialog.less
src/editor/modules/data/templates/checkbox.html [new file with mode: 0644]
src/editor/modules/data/templates/input.html [new file with mode: 0644]
src/editor/modules/data/templates/select.html [new file with mode: 0644]
src/editor/modules/data/templates/textarea.html [new file with mode: 0644]

index 006bf0f..8757389 100644 (file)
@@ -53,7 +53,7 @@ return function(sandbox) {
     var reloadHistory = function() {
         $.ajax({
             method: 'get',
     var reloadHistory = function() {
         $.ajax({
             method: 'get',
-            url: '/' + gettext('editor') + '/' + document_id + '/history',
+            url: sandbox.getConfig().documentHistoryUrl(document_id),
             success: function(data) {
                 history = data;
                 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
             success: function(data) {
                 history = data;
                 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
@@ -69,18 +69,34 @@ return function(sandbox) {
             return wlxmlDocument;
         },
         saveDocument: function() {
             return wlxmlDocument;
         },
         saveDocument: function() {
-
-            var dialog = saveDialog.create();
+            var documentSaveForm = $.extend({
+                        fields: [],
+                        content_field_name: 'text',
+                        version_field_name: 'version'
+                    },
+                    sandbox.getConfig().documentSaveForm
+                ),
+                dialog = saveDialog.create({fields: documentSaveForm.fields});
+            
             dialog.on('save', function(event) {
                 sandbox.publish('savingStarted');
             dialog.on('save', function(event) {
                 sandbox.publish('savingStarted');
+
+                var formData = event.formData;
+                formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
+                formData[documentSaveForm.version_field_name] = document_version;
+                if(sandbox.getConfig().jsonifySentData) {
+                    formData = JSON.stringify(formData);
+                }
+
                 dialog.toggleButtons(false);
                 $.ajax({
                     method: 'post',
                 dialog.toggleButtons(false);
                 $.ajax({
                     method: 'post',
-                    url: '/' + gettext('editor') + '/' + document_id,
-                    data: JSON.stringify({document:wlxmlDocument.toXML(), description: event.data.description}),
-                    success: function() {
+                    url: sandbox.getConfig().documentSaveUrl(document_id),
+                    data: formData,
+                    success: function(data) {
                         event.success();
                         event.success();
-                        sandbox.publish('savingEnded', 'success');
+                        sandbox.publish('savingEnded', 'success', data.version);
+                        document_version = data.version;
                         reloadHistory();
                     },
                     error: function() {event.error(); sandbox.publish('savingEnded', 'error');}
                         reloadHistory();
                     },
                     error: function() {event.error(); sandbox.publish('savingEnded', 'error');}
@@ -98,7 +114,7 @@ return function(sandbox) {
         fetchDiff: function(ver1, ver2) {
             $.ajax({
                 method: 'get',
         fetchDiff: function(ver1, ver2) {
             $.ajax({
                 method: 'get',
-                url: '/' + gettext('editor') + '/' + document_id + '/diff',
+                url: '/' + gettext('editor') + '/diff/' + document_id,
                 data: {from: ver1, to: ver2},
                 success: function(data) {
                     sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
                 data: {from: ver1, to: ver2},
                 success: function(data) {
                     sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
index 0846910..d0238e0 100644 (file)
@@ -4,8 +4,6 @@
         <h1>Zapisz dokument</h1>
     </div>
     <div class="modal-body">
         <h1>Zapisz dokument</h1>
     </div>
     <div class="modal-body">
-        <label>Opisz swoje zmiany</label>
-        <textarea rows="5"></textarea>
     </div>
     <div class="modal-footer">
         <a href="#" class="btn btn-info btn-mini save-btn">Zapisz</a>
     </div>
     <div class="modal-footer">
         <a href="#" class="btn btn-info btn-mini save-btn">Zapisz</a>
index e1f49b4..3aa17ec 100644 (file)
@@ -1,11 +1,18 @@
-define([
-'libs/text!./saveDialog.html',
-'libs/underscore',
-'libs/backbone'
-], function(saveDialogTemplate, _, Backbone) {
+define(function(require) {
 
     'use strict';
 
 
     '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: {
     var DialogView = Backbone.View.extend({
         template: _.template(saveDialogTemplate),
         events: {
@@ -19,16 +26,34 @@ define([
         },
         show: function() {
             this.setElement(this.template());
         },
         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();
             this.$el.modal({backdrop: 'static'});
             this.$el.modal('show');
             this.$('textarea').focus();
-
         },
         onSave: function(e) {
             e.preventDefault();
         },
         onSave: function(e) {
             e.preventDefault();
-            var view = this;
+            var view = this,
+                formData = {};
+
+            this.options.fields.forEach(function(field) {
+                var widget = view.$('[name=' + field.name +']');
+                formData[field.name] = widget.val();
+            });
+
             this.trigger('save', {
             this.trigger('save', {
-                data: {description: view.$el.find('textarea').val()},
+                formData: formData,
                 success: function() { view.actionsDisabled = false; view.close(); },
                 error: function() { view.actionsDisabled = false; view.close(); },
             });
                 success: function() { view.actionsDisabled = false; view.close(); },
                 error: function() { view.actionsDisabled = false; view.close(); },
             });
@@ -50,8 +75,8 @@ define([
     });
 
     return {
     });
 
     return {
-        create: function() {
-            return new DialogView();
+        create: function(config) {
+            return new DialogView(config);
         }
     };
 
         }
     };
 
index e863e93..e0530e5 100644 (file)
         margin: 2px 5px;
         font-weight: bold;
     }
         margin: 2px 5px;
         font-weight: bold;
     }
+
+    .description {
+        font-size: .8em;
+    }
+
+    width: 620px;
 }
\ No newline at end of file
 }
\ No newline at end of file
diff --git a/src/editor/modules/data/templates/checkbox.html b/src/editor/modules/data/templates/checkbox.html
new file mode 100644 (file)
index 0000000..3c14092
--- /dev/null
@@ -0,0 +1,5 @@
+<p>
+    <div style="float: left; width:100px;"><%= label %>:</div>
+    <input type="checkbox" name="<%= name %>"/> 
+    <span class="description"><%= description %></span>
+</p>
\ No newline at end of file
diff --git a/src/editor/modules/data/templates/input.html b/src/editor/modules/data/templates/input.html
new file mode 100644 (file)
index 0000000..f60f2f6
--- /dev/null
@@ -0,0 +1,10 @@
+<p>
+    <div style="float: left; width:100px;">
+    <%= label %>:
+    </div>
+    <div>
+        <input type="input" name="<%= name %>"/>
+        <span class="description"><%= description %></span>
+    </div>
+    
+</p>
\ No newline at end of file
diff --git a/src/editor/modules/data/templates/select.html b/src/editor/modules/data/templates/select.html
new file mode 100644 (file)
index 0000000..4b5a1de
--- /dev/null
@@ -0,0 +1,9 @@
+<p>
+    <div style="float: left; width:100px;"><%= label %>:</div>
+    <select name="<%= name %>">
+    <% options.forEach(function(option) { %>
+        <option value="<%= option.value %>"><%= option.text %></option>
+    <% }); %>
+    </select>
+    <span class="description"><%= description %></span>
+</p>
\ No newline at end of file
diff --git a/src/editor/modules/data/templates/textarea.html b/src/editor/modules/data/templates/textarea.html
new file mode 100644 (file)
index 0000000..50b7c50
--- /dev/null
@@ -0,0 +1,5 @@
+<p>
+    <label><%= label %></label>
+    <textarea name="<%= name %>" rows="5"></textarea>
+    <span class="description"><%= description %></span>
+</p>