896fc5662ebba431b95b245d76d1d78a0d24fc6d
[fnpeditor.git] / src / editor / modules / data / data.js
1 define([
2     './saveDialog',
3     'wlxml/wlxml'
4
5 ], function(saveDialog, wlxml) {
6
7 'use strict';
8
9 return function(sandbox) {
10
11     var doc = sandbox.getBootstrappedData().document;
12     var document_id = sandbox.getBootstrappedData().document_id;
13     var document_version = sandbox.getBootstrappedData().version;
14     var history = sandbox.getBootstrappedData().history;
15
16     var wlxmlDocument = wlxml.WLXMLDocumentFromXML(sandbox.getBootstrappedData().document);
17     
18     if(doc === '') {
19         doc = '<section\n\
20         xmlns="http://nowoczesnapolska.org.pl/sst#"\n\
21         xmlns:xlink="http://www.w3.org/1999/xlink"\n\
22         xmlns:dc="http://purl.org/dc/elements/1.1/"\n\
23         xmlns:dcterms="http://purl.org/dc/terms/"\n\
24     >\n\
25         <metadata>\n\
26         </metadata>\n\
27         <div class="p"></div>\n\
28     </section>';
29     }
30     
31     
32     function readCookie(name) {
33         var nameEQ = escape(name) + "=";
34         var ca = document.cookie.split(';');
35         for (var i = 0; i < ca.length; i++) {
36             var c = ca[i];
37             while (c.charAt(0) == ' ') c = c.substring(1, c.length);
38             if (c.indexOf(nameEQ) === 0) return unescape(c.substring(nameEQ.length, c.length));
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: '/' + gettext('editor') + '/' + document_id + '/history',
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 doc;
69         },
70         getDocument2: function() {
71             return wlxmlDocument;
72         },
73         commitDocument: function(newDocument, reason) {
74             doc = newDocument;
75             sandbox.publish('documentChanged', doc, reason);
76         },
77         saveDocument: function() {
78
79             var dialog = saveDialog.create();
80             dialog.on('save', function(event) {
81                 sandbox.publish('savingStarted');
82                 dialog.toggleButtons(false);
83                 $.ajax({
84                     method: 'post',
85                     url: '/' + gettext('editor') + '/' + document_id,
86                     data: JSON.stringify({document:doc, description: event.data.description}),
87                     success: function() {
88                         event.success();
89                         sandbox.publish('savingEnded', 'success');
90                         reloadHistory();
91                     },
92                     error: function() {event.error(); sandbox.publish('savingEnded', 'error');}
93                 });
94                 console.log('save');
95             });
96             dialog.on('cancel', function() {
97             });
98             dialog.show();
99             
100
101         },
102         getHistory: function() {
103             return history;
104         },
105         fetchDiff: function(ver1, ver2) {
106             $.ajax({
107                 method: 'get',
108                 url: '/' + gettext('editor') + '/' + document_id + '/diff',
109                 data: {from: ver1, to: ver2},
110                 success: function(data) {
111                     sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
112                 },
113             });
114         },
115         restoreVersion: function(options) {
116             if(options.version && options.description) {
117                 sandbox.publish('restoringStarted', {version: options.version});
118                 $.ajax({
119                     method: 'post',
120                     dataType: 'json',
121                     url: '/' + gettext('editor') + '/' + document_id + '/revert',
122                     data: JSON.stringify(options),
123                     success: function(data) {
124                         doc = data.document;
125                         document_version = data.version;
126                         reloadHistory();
127                         sandbox.publish('documentReverted', data);
128                     },
129                 }); 
130             }
131         },
132         getDocumentId: function() {
133             return document_id;
134         },
135         getDocumentVersion: function() {
136             return document_version;
137         }
138     };
139 };
140
141 });