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