5 'wlxml/extensions/list/list'
7 ], function($, saveDialog, wlxml, listExtension) {
11 return function(sandbox) {
14 var document_id = sandbox.getBootstrappedData().document_id;
15 var document_version = sandbox.getBootstrappedData().version;
16 var history = sandbox.getBootstrappedData().history;
18 var wlxmlDocument = wlxml.WLXMLDocumentFromXML(sandbox.getBootstrappedData().document);
20 wlxmlDocument.registerExtension(listExtension);
21 sandbox.getPlugins().forEach(function(plugin) {
22 if(plugin.documentExtension) {
23 wlxmlDocument.registerExtension(plugin.documentExtension);
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++) {
34 while (c.charAt(0) === ' ') {
35 c = c.substring(1, c.length);
37 if (c.indexOf(nameEQ) === 0) {
38 return unescape(c.substring(nameEQ.length, c.length));
46 beforeSend: function(xhr, settings) {
47 if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
48 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
53 var reloadHistory = function() {
56 url: sandbox.getConfig().documentHistoryUrl(document_id),
57 success: function(data) {
59 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
66 sandbox.publish('ready');
68 getDocument: function() {
71 saveDocument: function() {
72 var documentSaveForm = $.extend({
74 content_field_name: 'text',
75 version_field_name: 'version'
77 sandbox.getConfig().documentSaveForm
79 dialog = saveDialog.create({fields: documentSaveForm.fields});
81 dialog.on('save', function(event) {
82 sandbox.publish('savingStarted');
84 var formData = event.formData;
85 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
86 formData[documentSaveForm.version_field_name] = document_version;
87 if(sandbox.getConfig().jsonifySentData) {
88 formData = JSON.stringify(formData);
91 dialog.toggleButtons(false);
94 url: sandbox.getConfig().documentSaveUrl(document_id),
96 success: function(data) {
98 sandbox.publish('savingEnded', 'success', data.version);
99 document_version = data.version;
102 error: function() {event.error(); sandbox.publish('savingEnded', 'error');}
105 dialog.on('cancel', function() {
111 getHistory: function() {
114 fetchDiff: function(ver1, ver2) {
117 url: '/' + gettext('editor') + '/diff/' + document_id,
118 data: {from: ver1, to: ver2},
119 success: function(data) {
120 sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
124 restoreVersion: function(options) {
125 if(options.version && options.description) {
126 sandbox.publish('restoringStarted', {version: options.version});
130 url: '/' + gettext('editor') + '/' + document_id + '/revert',
131 data: JSON.stringify(options),
132 success: function(data) {
133 document_version = data.current_version;
135 wlxmlDocument.loadXML(data.document);
136 sandbox.publish('documentReverted', data);
141 getDocumentId: function() {
144 getDocumentVersion: function() {
145 return document_version;