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