bce9582d9651474e0a4d4437c57813bef998c600
[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
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 = saveDialog.create({fields: documentSaveForm.fields});
79             
80             dialog.on('save', function(event) {
81                 sandbox.publish('savingStarted');
82
83                 var formData = event.formData;
84                 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
85                 formData[documentSaveForm.version_field_name] = document_version;
86                 if(sandbox.getConfig().jsonifySentData) {
87                     formData = JSON.stringify(formData);
88                 }
89
90                 dialog.toggleButtons(false);
91                 $.ajax({
92                     method: 'post',
93                     url: sandbox.getConfig().documentSaveUrl(document_id),
94                     data: formData,
95                     success: function(data) {
96                         event.success();
97                         sandbox.publish('savingEnded', 'success', data.version);
98                         document_version = data.version;
99                         reloadHistory();
100                     },
101                     error: function() {event.error(); sandbox.publish('savingEnded', 'error');}
102                 });
103             });
104             dialog.on('cancel', function() {
105             });
106             dialog.show();
107             
108
109         },
110         getHistory: function() {
111             return history;
112         },
113         fetchDiff: function(ver1, ver2) {
114             $.ajax({
115                 method: 'get',
116                 url: sandbox.getConfig().documentDiffUrl(document_id),
117                 data: {from: ver1, to: ver2},
118                 success: function(data) {
119                     sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
120                 },
121             });
122         },
123         restoreVersion: function(options) {
124             if(options.version && options.description) {
125                 sandbox.publish('restoringStarted', {version: options.version});
126                 $.ajax({
127                     method: 'post',
128                     dataType: 'json',
129                     url: sandbox.getConfig().documentRestoreUrl(document_id),
130                     data: JSON.stringify(options),
131                     success: function(data) {
132                         document_version = data.current_version;
133                         reloadHistory();
134                         wlxmlDocument.loadXML(data.document);
135                         sandbox.publish('documentReverted', data);
136                     },
137                 });
138             }
139         },
140         getDocumentId: function() {
141             return document_id;
142         },
143         getDocumentVersion: function() {
144             return document_version;
145         }
146     };
147 };
148
149 });