7c9077f67056af337d0c3e268aca95671478870a
[fnpeditor.git] / src / editor / modules / data / data.js
1 define([
2     'libs/jquery',
3     './saveDialog',
4     'wlxml/wlxml'
5
6 ], function($, saveDialog, wlxml) {
7
8 'use strict';
9
10 return function(sandbox) {
11     /* global gettext */
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     
20     function readCookie(name) {
21         /* global escape, unescape */
22         var nameEQ = escape(name) + '=';
23         var ca = document.cookie.split(';');
24         for (var i = 0; i < ca.length; i++) {
25             var c = ca[i];
26             while (c.charAt(0) === ' ') {
27                 c = c.substring(1, c.length);
28             }
29             if (c.indexOf(nameEQ) === 0) {
30                 return unescape(c.substring(nameEQ.length, c.length));
31             }
32         }
33         return null;
34     }
35     
36     $.ajaxSetup({
37         crossDomain: false,
38         beforeSend: function(xhr, settings) {
39             if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
40                 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
41             }
42         }
43     });
44     
45     var reloadHistory = function() {
46         $.ajax({
47             method: 'get',
48             url: '/' + gettext('editor') + '/' + document_id + '/history',
49             success: function(data) {
50                 history = data;
51                 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
52             },
53         });
54     };
55     
56     return {
57         start: function() {
58             sandbox.publish('ready');
59         },
60         getDocument: function() {
61             return wlxmlDocument;
62         },
63         saveDocument: function() {
64
65             var dialog = saveDialog.create();
66             dialog.on('save', function(event) {
67                 sandbox.publish('savingStarted');
68                 dialog.toggleButtons(false);
69                 $.ajax({
70                     method: 'post',
71                     url: '/' + gettext('editor') + '/' + document_id,
72                     data: JSON.stringify({document:wlxmlDocument.toXML(), description: event.data.description}),
73                     success: function() {
74                         event.success();
75                         sandbox.publish('savingEnded', 'success');
76                         reloadHistory();
77                     },
78                     error: function() {event.error(); sandbox.publish('savingEnded', 'error');}
79                 });
80             });
81             dialog.on('cancel', function() {
82             });
83             dialog.show();
84             
85
86         },
87         getHistory: function() {
88             return history;
89         },
90         fetchDiff: function(ver1, ver2) {
91             $.ajax({
92                 method: 'get',
93                 url: '/' + gettext('editor') + '/' + document_id + '/diff',
94                 data: {from: ver1, to: ver2},
95                 success: function(data) {
96                     sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
97                 },
98             });
99         },
100         restoreVersion: function(options) {
101             if(options.version && options.description) {
102                 sandbox.publish('restoringStarted', {version: options.version});
103                 $.ajax({
104                     method: 'post',
105                     dataType: 'json',
106                     url: '/' + gettext('editor') + '/' + document_id + '/revert',
107                     data: JSON.stringify(options),
108                     success: function(data) {
109                         document_version = data.current_version;
110                         reloadHistory();
111                         wlxmlDocument.loadXML(data.document);
112                         sandbox.publish('documentReverted', data);
113                     },
114                 });
115             }
116         },
117         getDocumentId: function() {
118             return document_id;
119         },
120         getDocumentVersion: function() {
121             return document_version;
122         }
123     };
124 };
125
126 });