5 'wlxml/extensions/list/list'
7 ], function($, saveDialog, wlxml, listExtension) {
11 return function(sandbox) {
13 var document_id = sandbox.getBootstrappedData().document_id;
14 var document_version = sandbox.getBootstrappedData().version;
15 var history = sandbox.getBootstrappedData().history;
17 var wlxmlDocument = wlxml.WLXMLDocumentFromXML(sandbox.getBootstrappedData().document);
19 wlxmlDocument.registerExtension(listExtension);
20 sandbox.getPlugins().forEach(function(plugin) {
21 if(plugin.documentExtension) {
22 wlxmlDocument.registerExtension(plugin.documentExtension);
27 function readCookie(name) {
28 /* global escape, unescape, document */
29 var nameEQ = escape(name) + '=';
30 var ca = document.cookie.split(';');
31 for (var i = 0; i < ca.length; i++) {
33 while (c.charAt(0) === ' ') {
34 c = c.substring(1, c.length);
36 if (c.indexOf(nameEQ) === 0) {
37 return unescape(c.substring(nameEQ.length, c.length));
45 beforeSend: function(xhr, settings) {
46 if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
47 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
52 var reloadHistory = function() {
55 url: sandbox.getConfig().documentHistoryUrl(document_id),
56 success: function(data) {
58 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
65 sandbox.publish('ready');
67 getDocument: function() {
70 saveDocument: function() {
71 var documentSaveForm = $.extend({
73 content_field_name: 'text',
74 version_field_name: 'version'
76 sandbox.getConfig().documentSaveForm
78 dialog = saveDialog.create({fields: documentSaveForm.fields});
80 dialog.on('save', function(event) {
81 sandbox.publish('savingStarted');
83 var formData = event.formData;
84 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
85 formData[documentSaveForm.version_field_name] = document_version;
86 if(sandbox.getConfig().jsonifySentData) {
87 formData = JSON.stringify(formData);
90 dialog.toggleButtons(false);
93 url: sandbox.getConfig().documentSaveUrl(document_id),
95 success: function(data) {
97 sandbox.publish('savingEnded', 'success', data.version);
98 document_version = data.version;
101 error: function() {event.error(); sandbox.publish('savingEnded', 'error');}
104 dialog.on('cancel', function() {
110 getHistory: function() {
113 fetchDiff: function(ver1, ver2) {
116 url: sandbox.getConfig().documentDiffUrl(document_id),
117 data: {from: ver1, to: ver2},
118 success: function(data) {
119 sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
123 restoreVersion: function(options) {
124 if(options.version && options.description) {
125 sandbox.publish('restoringStarted', {version: options.version});
129 url: sandbox.getConfig().documentRestoreUrl(document_id),
130 data: JSON.stringify(options),
131 success: function(data) {
132 document_version = data.current_version;
134 wlxmlDocument.loadXML(data.document);
135 sandbox.publish('documentReverted', data);
140 getDocumentId: function() {
143 getDocumentVersion: function() {
144 return document_version;