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