editor: fix canvas wlxml listener nodeMoved event handling
[fnpeditor.git] / src / editor / modules / data / data.js
1 define([
2     'libs/jquery',
3     './dialog',
4     'wlxml/wlxml',
5     'wlxml/extensions/list/list',
6     'fnpjs/logging/logging',
7 ], function($, Dialog, wlxml, listExtension, logging) {
8
9 'use strict';
10 /* global gettext, alert */
11
12 var logger = logging.getLogger('editor.modules.data'),
13     stubDocument = '<section><div>' + gettext('This is an empty document.') + '</div></section>';
14
15
16 return function(sandbox) {
17
18     var document_id = sandbox.getBootstrappedData().document_id;
19     var document_version = sandbox.getBootstrappedData().version;
20     var history = sandbox.getBootstrappedData().history;
21
22     var wlxmlDocument;
23     try {
24         wlxmlDocument = wlxml.WLXMLDocumentFromXML(sandbox.getBootstrappedData().document);
25     } catch(e) {
26         logger.exception(e);
27         alert(gettext('This document contains errors and can\'t be loaded. :(')); // TODO
28         wlxmlDocument = wlxml.WLXMLDocumentFromXML(stubDocument);
29     }
30
31     wlxmlDocument.registerExtension(listExtension);
32     sandbox.getPlugins().forEach(function(plugin) {
33         if(plugin.documentExtension) {
34             wlxmlDocument.registerExtension(plugin.documentExtension);
35         }
36     });
37      
38     
39     function readCookie(name) {
40         /* global escape, unescape, document */
41         var nameEQ = escape(name) + '=';
42         var ca = document.cookie.split(';');
43         for (var i = 0; i < ca.length; i++) {
44             var c = ca[i];
45             while (c.charAt(0) === ' ') {
46                 c = c.substring(1, c.length);
47             }
48             if (c.indexOf(nameEQ) === 0) {
49                 return unescape(c.substring(nameEQ.length, c.length));
50             }
51         }
52         return null;
53     }
54     
55     $.ajaxSetup({
56         crossDomain: false,
57         beforeSend: function(xhr, settings) {
58             if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
59                 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
60             }
61         }
62     });
63     
64     var reloadHistory = function() {
65         $.ajax({
66             method: 'get',
67             url: sandbox.getConfig().documentHistoryUrl(document_id),
68             success: function(data) {
69                 history = data;
70                 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
71             },
72         });
73     };
74     
75     return {
76         start: function() {
77             sandbox.publish('ready');
78         },
79         getDocument: function() {
80             return wlxmlDocument;
81         },
82         saveDocument: function() {
83             var documentSaveForm = $.extend({
84                         fields: [],
85                         content_field_name: 'text',
86                         version_field_name: 'version'
87                     },
88                     sandbox.getConfig().documentSaveForm
89                 ),
90                 dialog = Dialog.create({
91                     fields: documentSaveForm.fields,
92                     title: gettext('Save Document'),
93                     submitButtonText: gettext('Save')
94                 });
95             
96             dialog.on('save', function(event) {
97                 sandbox.publish('savingStarted');
98
99                 var formData = event.formData;
100                 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
101                 formData[documentSaveForm.version_field_name] = document_version;
102                 if(sandbox.getConfig().jsonifySentData) {
103                     formData = JSON.stringify(formData);
104                 }
105
106                 dialog.toggleButtons(false);
107                 $.ajax({
108                     method: 'post',
109                     url: sandbox.getConfig().documentSaveUrl(document_id),
110                     data: formData,
111                     success: function(data) {
112                         event.success();
113                         sandbox.publish('savingEnded', 'success', data.version);
114                         document_version = data.version;
115                         reloadHistory();
116                     },
117                     error: function() {event.error(); sandbox.publish('savingEnded', 'error');}
118                 });
119             });
120             dialog.on('cancel', function() {
121             });
122             dialog.show();
123             
124
125         },
126         getHistory: function() {
127             return history;
128         },
129         fetchDiff: function(ver1, ver2) {
130             $.ajax({
131                 method: 'get',
132                 url: sandbox.getConfig().documentDiffUrl(document_id),
133                 data: {from: ver1, to: ver2},
134                 success: function(data) {
135                     sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
136                 },
137             });
138         },
139         restoreVersion: function(version) {
140             var documentRestoreForm = $.extend({
141                         fields: [],
142                         version_field_name: 'version'
143                     },
144                     sandbox.getConfig().documentRestoreForm
145                 ),
146                 dialog = Dialog.create({
147                     fields: documentRestoreForm.fields,
148                     title: gettext('Restore Version'),
149                     submitButtonText: gettext('Restore')
150                 });
151
152             dialog.on('save', function(event) {
153                 var formData = event.formData;
154                 formData[documentRestoreForm.version_field_name] = version;
155                 sandbox.publish('restoringStarted', {version: version});
156                 if(sandbox.getConfig().jsonifySentData) {
157                     formData = JSON.stringify(formData);
158                 }
159                 $.ajax({
160                     method: 'post',
161                     dataType: 'json',
162                     url: sandbox.getConfig().documentRestoreUrl(document_id),
163                     data: formData,
164                     success: function(data) {
165                         document_version = data.version;
166                         reloadHistory();
167                         wlxmlDocument.loadXML(data.document);
168                         sandbox.publish('documentReverted', data.version);
169                         event.success();
170                     },
171                 });
172             });
173             dialog.show();
174         },
175         getDocumentId: function() {
176             return document_id;
177         },
178         getDocumentVersion: function() {
179             return document_version;
180         }
181     };
182 };
183
184 });