5 'wlxml/extensions/list/list'
6 ], function($, Dialog, 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 = Dialog.create({
79 fields: documentSaveForm.fields,
80 title: gettext('Save Document'),
81 submitButtonText: gettext('Save')
84 dialog.on('save', function(event) {
85 sandbox.publish('savingStarted');
87 var formData = event.formData;
88 formData[documentSaveForm.content_field_name] = wlxmlDocument.toXML();
89 formData[documentSaveForm.version_field_name] = document_version;
90 if(sandbox.getConfig().jsonifySentData) {
91 formData = JSON.stringify(formData);
94 dialog.toggleButtons(false);
97 url: sandbox.getConfig().documentSaveUrl(document_id),
99 success: function(data) {
101 sandbox.publish('savingEnded', 'success', data.version);
102 document_version = data.version;
105 error: function() {event.error(); sandbox.publish('savingEnded', 'error');}
108 dialog.on('cancel', function() {
114 getHistory: function() {
117 fetchDiff: function(ver1, ver2) {
120 url: sandbox.getConfig().documentDiffUrl(document_id),
121 data: {from: ver1, to: ver2},
122 success: function(data) {
123 sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
127 restoreVersion: function(version) {
128 var documentRestoreForm = $.extend({
130 version_field_name: 'version'
132 sandbox.getConfig().documentRestoreForm
134 dialog = Dialog.create({
135 fields: documentRestoreForm.fields,
136 title: gettext('Restore Version'),
137 submitButtonText: gettext('Restore')
140 dialog.on('save', function(event) {
141 var formData = event.formData;
142 formData[documentRestoreForm.version_field_name] = version;
143 sandbox.publish('restoringStarted', {version: version});
144 if(sandbox.getConfig().jsonifySentData) {
145 formData = JSON.stringify(formData);
150 url: sandbox.getConfig().documentRestoreUrl(document_id),
152 success: function(data) {
153 document_version = data.version;
155 wlxmlDocument.loadXML(data.document);
156 sandbox.publish('documentReverted', data.version);
163 getDocumentId: function() {
166 getDocumentVersion: function() {
167 return document_version;