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