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