5 'wlxml/extensions/list/list',
6 'fnpjs/logging/logging',
7 ], function($, Dialog, wlxml, listExtension, logging) {
10 /* global gettext, alert */
12 var logger = logging.getLogger('editor.modules.data'),
13 stubDocument = '<section><div>' + gettext('This is an empty document.') + '</div></section>';
16 return function(sandbox) {
18 var document_id = sandbox.getBootstrappedData().document_id;
19 var document_version = sandbox.getBootstrappedData().version;
20 var history = sandbox.getBootstrappedData().history;
24 wlxmlDocument = wlxml.WLXMLDocumentFromXML(sandbox.getBootstrappedData().document);
27 alert(gettext('This document contains errors and can\'t be loaded. :(')); // TODO
28 wlxmlDocument = wlxml.WLXMLDocumentFromXML(stubDocument);
31 wlxmlDocument.registerExtension(listExtension);
32 sandbox.getPlugins().forEach(function(plugin) {
33 if(plugin.documentExtension) {
34 wlxmlDocument.registerExtension(plugin.documentExtension);
39 function readCookie(name) {
40 /* global escape, unescape, document */
41 var nameEQ = escape(name) + '=';
42 var ca = document.cookie.split(';');
43 for (var i = 0; i < ca.length; i++) {
45 while (c.charAt(0) === ' ') {
46 c = c.substring(1, c.length);
48 if (c.indexOf(nameEQ) === 0) {
49 return unescape(c.substring(nameEQ.length, c.length));
57 beforeSend: function(xhr, settings) {
58 if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
59 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
64 var reloadHistory = function() {
67 url: sandbox.getConfig().documentHistoryUrl(document_id),
68 success: function(data) {
70 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
77 sandbox.publish('ready');
79 getDocument: function() {
82 saveDocument: function() {
83 var documentSaveForm = $.extend({
85 content_field_name: 'text',
86 version_field_name: 'version'
88 sandbox.getConfig().documentSaveForm
90 dialog = Dialog.create({
91 fields: documentSaveForm.fields,
92 title: gettext('Save Document'),
93 submitButtonText: gettext('Save')
96 dialog.on('save', function(event) {
97 sandbox.publish('savingStarted');
99 var formData = event.formData;
100 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
101 formData[documentSaveForm.version_field_name] = document_version;
102 if(sandbox.getConfig().jsonifySentData) {
103 formData = JSON.stringify(formData);
106 dialog.toggleButtons(false);
109 url: sandbox.getConfig().documentSaveUrl(document_id),
111 success: function(data) {
113 sandbox.publish('savingEnded', 'success', data.version);
114 document_version = data.version;
117 error: function() {event.error(); sandbox.publish('savingEnded', 'error');}
120 dialog.on('cancel', function() {
126 getHistory: function() {
129 fetchDiff: function(ver1, ver2) {
132 url: sandbox.getConfig().documentDiffUrl(document_id),
133 data: {from: ver1, to: ver2},
134 success: function(data) {
135 sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
139 restoreVersion: function(version) {
140 var documentRestoreForm = $.extend({
142 version_field_name: 'version'
144 sandbox.getConfig().documentRestoreForm
146 dialog = Dialog.create({
147 fields: documentRestoreForm.fields,
148 title: gettext('Restore Version'),
149 submitButtonText: gettext('Restore')
152 dialog.on('save', function(event) {
153 var formData = event.formData;
154 formData[documentRestoreForm.version_field_name] = version;
155 sandbox.publish('restoringStarted', {version: version});
156 if(sandbox.getConfig().jsonifySentData) {
157 formData = JSON.stringify(formData);
162 url: sandbox.getConfig().documentRestoreUrl(document_id),
164 success: function(data) {
165 document_version = data.version;
167 wlxmlDocument.loadXML(data.document);
168 sandbox.publish('documentReverted', data.version);
175 getDocumentId: function() {
178 getDocumentVersion: function() {
179 return document_version;