-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
VersionControl.js
284 lines (269 loc) · 15.1 KB
/
VersionControl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
$(function() {
// configuration: "run-time" settings are defined here, "constant" settings
// (translations, interface URL etc.) in VersionControlForTextFields.module
var settings = { empty: true, render: 'HTML' };
var moduleConfig = config.VersionControl;
// field data is cached to reduce need for redundant AJAX requests
var cache = {};
// fetch revision data for this page as HTML markup
$.get(moduleConfig.processPage+'page', { pages_id: moduleConfig.pageID, settings: settings }, function(data) {
// prepend data (#text-field-history) to body
$('body').prepend(data);
// iterate through field specific revision containers and add their
// contents to that fields header (.ui-widget-header) only if they
// contain at least one revision other than what's currently used
$('#text-field-history > div').each(function() {
if ($(this).data('revision')) {
var $if = $('.Inputfield_'+$(this).data('field'));
$if.find('> label')
.addClass('with-history')
.before($(this));
$(this).find('a:first').addClass('ui-state-active');
if ($if.hasClass('InputfieldTinyMCE') || $if.hasClass('InputfieldCKEditor')) return;
cache[$(this).data('field')+"."+$(this).data('revision')] = $if.find('div.ui-widget-content:first, .InputfieldContent:first').filter(':first').clone(true, true);
}
});
// iterate through history-enabled fields to add a revision toggle
$('.ui-widget-header.with-history, .InputfieldHeader.with-history').each(function() {
var toggle_class = "field-revisions-toggle";
var toggle_title = "";
if ($(this).siblings('.field-revisions').find('li').length < 1) {
toggle_class += " inactive";
toggle_title = " title='"+$(this).siblings('.field-revisions').text()+"'";
}
var revisions_toggle = '<a '+toggle_title+'class="'+toggle_class+'"><i class="fa fa-clock-o"></i></a>';
if ($(this).find('.toggle-icon').length) {
$(this).find('.toggle-icon').after(revisions_toggle);
} else {
$(this).append(revisions_toggle);
}
});
// when a link in revision list is clicked, fetch data for appropriate
// revision from the interface (most of the code here is related to how
// things are presented, loading animation etc.)
$('.field-revisions').on('click', '> ul > li > a', function() {
if ($(this).hasClass('ui-state-active')) return false;
var settings = { render: 'Input' };
var $this = $(this);
var $if = $this.parents('.Inputfield:first');
var field = $this.parents('.field-revisions:first').data('field');
$if.find('.field-revisions .ui-state-active').removeClass('ui-state-active');
$this.addClass('ui-state-active');
$('.compare-revisions').remove();
var $content = $if.find('div.ui-widget-content:first, .InputfieldContent:first').filter(':first');
var $loading = $('<span class="field-revisions-loading"></span>').hide().css({
height: $content.innerHeight()+'px',
backgroundColor: $content.css('background-color')
});
if ($if.hasClass('InputfieldTinyMCE') || $if.hasClass('InputfieldCKEditor')) {
// for some inputfield types we need to get raw data as JSON
// instead of pre-rendered inputfield markup (HTML)
settings = { render: 'JSON' };
}
var revision = $(this).data('revision');
if (cache[field+"."+revision]) {
if (settings.render != "JSON" && revision == $this.parents('.field-revisions:first').data('revision')) {
// current (latest) revision is the only one we've got
// inputfield content cached as a jQuery object
$content.replaceWith(cache[field+"."+revision].clone(true, true));
if ($if.find('.InputfieldFileList').length) {
// for file inputs we need to reload InputfieldFile.js
// in order to reset the HTML5 AJAX file upload
$.getScript(config.urls.modules+"Inputfield/InputfieldFile/InputfieldFile.js");
}
if ($if.hasClass('InputfieldImage')) {
// for image inputs we need to reload InputfieldImage.js
// in order to reset Magnific Popup
$if.find('.InputfieldImageListToggle').remove();
$.getScript(config.urls.modules+"Inputfield/InputfieldImage/InputfieldImage.js");
}
if ($if.find('.InputfieldAsmSelect').length) {
var $select = $if.find('select[multiple=multiple]');
var options = typeof config === 'undefined' ? { sortable: true } : config[$select.attr('id')];
$select.appendTo($if.find('.InputfieldAsmSelect')).show();
$if.find('.asmContainer').remove();
$select.asmSelect(options);
}
} else {
update($if, $content, settings, field, cache[field+"."+revision]);
}
} else {
$content.css('position', 'relative').prepend($loading.fadeIn(250));
$.get(moduleConfig.processPage+'field', { revision: $this.data('revision'), field: field, settings: settings }, function(data) {
cache[field+"."+revision] = data;
update($if, $content, settings, field, cache[field+"."+revision]);
$loading.fadeOut(350, function() {
$(this).remove();
});
});
}
return false;
});
// this function updates inputfield content based on inputfield and
// content objects, settings (render mode etc.) and data (HTML or JSON)
var update = function($if, $content, settings, field, data) {
if (settings.render == "Input") {
// format of returned data is HTML
var before = $content.children('p.description:first');
var after = $content.children('p.notes:first');
$content.html(data).prepend(before).append(after);
if ($if.hasClass('InputfieldImage') || $if.hasClass('InputfieldFile')) {
// make sure that grid mode (for images) is not enabled
$if.removeClass('InputfieldImageGrid');
// image and file data isn't editable until it has been
// restored; we're using an overlay to avoid confusion
$content.prepend('<div class="version-control-overlay"></div>');
$('.version-control-overlay')
.attr('title', moduleConfig.i18n.editDisabled)
.on('click', function() {
alert($(this).attr('title'));
})
.hover(
function() { $(this).parent('.overlay-parent').addClass('hover') },
function() { $(this).parent('.overlay-parent').removeClass('hover') }
)
.parent('.InputfieldContent')
.addClass('overlay-parent');
} else if ($if.hasClass('Inputfield_permissions')) {
$('.Inputfield_permissions .Inputfield_permissions > .InputfieldContent').insertAfter($('.Inputfield_permissions:first > .InputfieldContent:first'));
$('.Inputfield_permissions:first > .InputfieldContent:first').remove();
}
if ($if.find('.InputfieldAsmSelect').length) {
var $select = $if.find('select[multiple=multiple]');
var options = typeof config === 'undefined' ? { sortable: true } : config[$select.attr('id')];
$select.asmSelect(options);
}
} else {
// format of returned data is JSON
$.each(data, function(property, value) {
var language = property.replace('data', '');
if (language) language = "__"+language;
if (typeof tinyMCE != "undefined" && tinyMCE.get('Inputfield_'+field+language)) {
// TinyMCE inputfield
tinyMCE.get('Inputfield_'+field+language).setContent(value);
} else if ($if.find('.InputfieldCKEditorInline').length) {
// CKeditor inputfield in inline mode
$if.find('.InputfieldCKEditorInline').html(value);
} else if (typeof CKEDITOR != "undefined" && CKEDITOR.instances['Inputfield_'+field+language]) {
// CKEditor inputfield
CKEDITOR.instances['Inputfield_'+field+language].setData(value);
}
});
}
}
// when mouse cursor is moved on a revisions toggle (or it is clicked,
// to make it accessible for touch devices etc.) show (or hide if it
// was already visible) revision list
$('.field-revisions-toggle').on('click mouseenter', function() {
if ($(this).hasClass('inactive')) return false;
var $revisions = $(this).parent('label').siblings('.field-revisions');
var show = ($revisions.is(':visible')) ? false : true;
$('.field-revisions').slideUp();
if (show) $revisions.slideDown();
return false;
});
// hide revision list when user moves mouse cursor off it; timeout
// started out as a bugfix, but now it's kept purely for usability
var revision_timeout;
$('.field-revisions')
.on('mouseenter', function() {
$(this).slideDown();
if (revision_timeout) {
clearTimeout(revision_timeout);
revision_timeout = false;
}
})
.on('mouseleave', function() {
var $this = $(this);
revision_timeout = setTimeout(function() {
revision_timeout = false;
$this.slideUp(function() {
$('.compare-revisions').remove();
});
}, 500);
});
// if <ul> element containing revision history is long enough to get
// vertical scrollbar, add some extra padding to compensate for it
$('.field-revisions > ul').each(function() {
// fetch DOM object matching current jQuery object (jQuery object
// doesn't have clientHeight or scrollHeight which we need here)
var dom_ul = $(this)[0];
// to get correct heights parent element needs to be visible (this
// should happen so fast that user never notices anything strange)
$(this).parent().show();
if (dom_ul.clientHeight < dom_ul.scrollHeight) {
$(this).addClass('scroll');
}
$(this).parent().hide();
});
// enable Diff Match Patch if/when required
var enableDiffMatchPatch = function() {
var dmp = new diff_match_patch();
var r1 = document.getElementById('r1').value;
var r2 = document.getElementById('r2').value;
dmp.Diff_Timeout = moduleConfig.diff.timeout;
dmp.Diff_EditCost = moduleConfig.diff.editCost;
var ms_start = (new Date()).getTime();
var d = dmp.diff_main(r1, r2);
var ms_end = (new Date()).getTime();
var s = (ms_end - ms_start) / 1000 + 's';
if (moduleConfig.diff.cleanup) {
dmp['diff_cleanup' + moduleConfig.diff.cleanup](d);
}
var ds = dmp.diff_prettyHtml(d);
document.getElementById('diff').innerHTML = ds;
}
// when mouse cursor is moved on a revision link, show compare/diff
// link, which -- when clicked -- loads a text diff for displaying
// differences between selected revision and current revision.
$('.field-revisions.diff')
.on('hover', '> ul > li > a', function() {
$('.compare-revisions').remove();
if (!$(this).hasClass('ui-state-active')) {
// in this case r1 refers to current revision, r2 to selected
// revision. diff is fetched as HTML from revision interface.
var field = $(this).parents('.field-revisions:first').data('field');
var r1 = $(this).parents('.field-revisions:first').find('.ui-state-active').data('revision');
var r2 = $(this).data('revision');
var href = moduleConfig.processPage+'diff/?revisions='+r1+':'+r2+'&field='+field;
var label = moduleConfig.i18n.compareWithCurrent;
$(this).before('<div class="compare-revisions"><a class="diff-trigger" href="'+href+'">'+label+'</a></div>');
}
})
.on('click', '.compare-revisions > a.diff-trigger', function() {
var $parent = $(this).parent();
var $loading = $('<span class="field-revisions-loading"></span>').hide().css({
height: $parent.innerHeight()+'px',
backgroundColor: $parent.css('background-color')
});
$parent.prepend($loading.fadeIn(250)).load($(this).attr('href'), function() {
$(this).find('a.diff-trigger').remove();
if ($parent.find('ul.page-diff').length) {
if (typeof enableDiffSwitch != 'function') {
$.getScript(moduleConfig.moduleDir+"diff_switch.min.js", function() {
enableDiffSwitch(moduleConfig);
});
} else {
enableDiffSwitch(moduleConfig);
}
} else {
if (typeof diff_match_patch != 'function') {
$.getScript(moduleConfig.moduleDir+"diff_match_patch_20121119/javascript/diff_match_patch.js", function() {
enableDiffMatchPatch();
});
} else {
enableDiffMatchPatch();
}
}
$(this).animate({
width: '400px',
padding: '14px'
});
$loading.fadeOut(350, function() {
$(this).remove();
});
});
return false;
});
});
});