6 ], function($, saveDialog, wlxml) {
10 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);
20 function readCookie(name) {
21 /* global escape, unescape */
22 var nameEQ = escape(name) + '=';
23 var ca = document.cookie.split(';');
24 for (var i = 0; i < ca.length; i++) {
26 while (c.charAt(0) === ' ') {
27 c = c.substring(1, c.length);
29 if (c.indexOf(nameEQ) === 0) {
30 return unescape(c.substring(nameEQ.length, c.length));
38 beforeSend: function(xhr, settings) {
39 if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))) {
40 xhr.setRequestHeader('X-CSRFToken', readCookie('csrftoken'));
45 var reloadHistory = function() {
48 url: '/' + gettext('editor') + '/' + document_id + '/history',
49 success: function(data) {
51 sandbox.publish('historyItemAdded', data.slice(-1)[0]);
58 sandbox.publish('ready');
60 getDocument: function() {
63 saveDocument: function() {
65 var dialog = saveDialog.create();
66 dialog.on('save', function(event) {
67 sandbox.publish('savingStarted');
68 dialog.toggleButtons(false);
71 url: '/' + gettext('editor') + '/' + document_id,
72 data: JSON.stringify({document:wlxmlDocument.toXML(), description: event.data.description}),
75 sandbox.publish('savingEnded', 'success');
78 error: function() {event.error(); sandbox.publish('savingEnded', 'error');}
81 dialog.on('cancel', function() {
87 getHistory: function() {
90 fetchDiff: function(ver1, ver2) {
93 url: '/' + gettext('editor') + '/' + document_id + '/diff',
94 data: {from: ver1, to: ver2},
95 success: function(data) {
96 sandbox.publish('diffFetched', {table: data, ver1: ver1, ver2: ver2});
100 restoreVersion: function(options) {
101 if(options.version && options.description) {
102 sandbox.publish('restoringStarted', {version: options.version});
106 url: '/' + gettext('editor') + '/' + document_id + '/revert',
107 data: JSON.stringify(options),
108 success: function(data) {
109 document_version = data.current_version;
111 sandbox.publish('documentReverted', data);
116 getDocumentId: function() {
119 getDocumentVersion: function() {
120 return document_version;