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