Linting
[fnpeditor.git] / src / editor / modules / data / data.js
1 define(['./saveDialog'], function(saveDialog) {
2
3 'use strict';
4
5 return function(sandbox) {
6
7     var doc = sandbox.getBootstrappedData().document;
8     var document_id = sandbox.getBootstrappedData().document_id;
9     var document_version = sandbox.getBootstrappedData().version;
10     var history = sandbox.getBootstrappedData().history;
11
12     
13     if(doc === '') {
14         doc = '<section\n\
15         xmlns="http://nowoczesnapolska.org.pl/sst#"\n\
16         xmlns:xlink="http://www.w3.org/1999/xlink"\n\
17         xmlns:dc="http://purl.org/dc/elements/1.1/"\n\
18         xmlns:dcterms="http://purl.org/dc/terms/"\n\
19     >\n\
20         <metadata>\n\
21         </metadata>\n\
22         <div class="p"></div>\n\
23     </section>';
24     }
25     
26     
27     function readCookie(name) {
28         var nameEQ = escape(name) + "=";
29         var ca = document.cookie.split(';');
30         for (var i = 0; i < ca.length; i++) {
31             var c = ca[i];
32             while (c.charAt(0) == ' ') c = c.substring(1, c.length);
33             if (c.indexOf(nameEQ) === 0) return unescape(c.substring(nameEQ.length, c.length));
34         }
35         return null;
36     }
37     
38     $.ajaxSetup({
39         crossDomain: false,
40         beforeSend: function(xhr, settings) {
41             if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
42                 xhr.setRequestHeader("X-CSRFToken", readCookie('csrftoken'));
43             }
44         }
45     });
46     
47     var reloadHistory = function() {
48         $.ajax({
49             method: 'get',
50             url: '/' + gettext('editor') + '/' + document_id + '/history',
51             success: function(data) {
52                 history = data; 
53                 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
54             },
55         });
56     };
57     
58     return {
59         start: function() {
60             sandbox.publish('ready');
61         },
62         getDocument: function() {
63             return doc;
64         },
65         commitDocument: function(newDocument, reason) {
66             doc = newDocument;
67             sandbox.publish('documentChanged', doc, reason);
68         },
69         saveDocument: function() {
70
71             var dialog = saveDialog.create();
72             dialog.on('save', function(event) {
73                 sandbox.publish('savingStarted');
74                 dialog.toggleButtons(false);
75                 $.ajax({
76                     method: 'post',
77                     url: '/' + gettext('editor') + '/' + document_id,
78                     data: JSON.stringify({document:doc, description: event.data.description}),
79                     success: function() {
80                         event.success();
81                         sandbox.publish('savingEnded', 'success');
82                         reloadHistory();
83                     },
84                     error: function() {event.error(); sandbox.publish('savingEnded', 'error');}
85                 });
86                 console.log('save');
87             });
88             dialog.on('cancel', function() {
89             });
90             dialog.show();
91             
92
93         },
94         getHistory: function() {
95             return history;
96         },
97         fetchDiff: function(ver1, ver2) {
98             $.ajax({
99                 method: 'get',
100                 url: '/' + gettext('editor') + '/' + document_id + '/diff',
101                 data: {from: ver1, to: ver2},
102                 success: function(data) {
103                     sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
104                 },
105             });
106         },
107         restoreVersion: function(options) {
108             if(options.version && options.description) {
109                 sandbox.publish('restoringStarted', {version: options.version});
110                 $.ajax({
111                     method: 'post',
112                     dataType: 'json',
113                     url: '/' + gettext('editor') + '/' + document_id + '/revert',
114                     data: JSON.stringify(options),
115                     success: function(data) {
116                         doc = data.document;
117                         document_version = data.version;
118                         reloadHistory();
119                         sandbox.publish('documentReverted', data);
120                     },
121                 }); 
122             }
123         },
124         getDocumentId: function() {
125             return document_id;
126         },
127         getDocumentVersion: function() {
128             return document_version;
129         }
130     };
131 };
132
133 });