-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
435 lines (366 loc) · 16.4 KB
/
script.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
(function ($) {
"use strict";
var Filelisting = function(element, options) {
this.$capiton = $(element).find('.plugin__filelisting_capiton');
this.$collapsible = $(element).find('.plugin__filelisting_collapsible');
this.$content = $(element).find('.plugin__filelisting_content');
this.$headertable = $(element).find('.plugin__filelisting_headertable');
this.$bodytable = $(element).find('.plugin__filelisting_bodytable');
this.$footer = $(element).find('.plugin__filelisting_footer');
this.options = $.extend({}, $.fn.dokuwiki_plugin_filelisting.defaults, options);
this.storageKey = 'plugin_filelisting';
if (this.options.remember_state_per_page) {
this.storageKey += '/' + this.options.pageId;
}
this.initToggleButton();
this.initAjaxDirectoryExpand();
this.initFilter();
this.initSorting();
this.initDelete();
};
Filelisting.prototype.getToggleStatus = function () {
if (!localStorage.getItem(this.storageKey)) {
return this.options.defaultToggle;
}
return localStorage.getItem(this.storageKey);
};
Filelisting.prototype.setToggleStatus = function (status) {
if (status !== 'visible' && status !== 'hidden') {
throw 'status must be "visible" or "hidden"';
}
localStorage.setItem(this.storageKey, status);
};
Filelisting.prototype.initToggleButton = function() {
//toggle button
var $toggleButton = $('<div>').text(this.options.toggleVisible)
.css({
float: 'right',
cursor: 'pointer'
}).addClass('plugin__filelisting_toggle').appendTo(this.$capiton);
//by default filelisting is visible
if (this.getToggleStatus() === 'hidden') {
this.$collapsible.hide();
$toggleButton.text(this.options.toggleHidden);
}
$toggleButton.click($.proxy(function () {
if (this.$collapsible.is(':hidden')) {
this.$collapsible.slideDown();
$toggleButton.text(this.options.toggleVisible);
this.setToggleStatus('visible');
} else {
this.$collapsible.slideUp();
$toggleButton.text(this.options.toggleHidden);
this.setToggleStatus('hidden');
}
}, this));
};
Filelisting.prototype.initAjaxDirectoryExpand = function() {
//allow click on link
this.$content.find('tbody').on('click', 'tr[data-namespace] a', $.proxy(function (event) {
event.preventDefault();
//row and namespace are used in $.post
var $row = $(event.target).closest('tr'),
namespace = $row.data('namespace');
//get all siblings and subsiblings
var $children = $row.nextAll('[data-childOf="' + namespace + '"]'),
$descendants = $row.nextAll('[data-childOf^="' + namespace + '"]');
//namespace is expanded - hide it
if ($row.data('isExpanded')) {
//set icon
$row.children('.plugin__filelisting_cell_icon').html(this.options.dirClosedIcon);
//save the state of all expanded sub namespaces to restore it as it was
$descendants.each(function () {
if ($(this).is(':visible')) {
$(this).data('reopenAs', 'visible');
} else {
$(this).data('reopenAs', 'hidden');
}
}).hide();
$row.data('isExpanded', false);
//namespace is hidden and is loaded
} else if ($row.data('isLoaded')) {
$row.children('.plugin__filelisting_cell_icon').html(this.options.dirOpenedIcon);
//always open children
$children.show();
//check if we should open any descendents
$descendants.each(function() {
if ($(this).data('reopenAs') === 'visible') {
$(this).show();
}
});
$row.data('isExpanded', true);
this.$content.trigger('expand', namespace);
//namespace isn't loaded
} else {
//loading
$row.children('.plugin__filelisting_cell_icon').html(this.options.loadingIcon);
var data = {};
data['call'] = 'plugin_filelisting';
data['namespace'] = namespace;
data['baseNamespace'] = this.options.baseNamespace;
$.post(DOKU_BASE + 'lib/exe/ajax.php', data,
$.proxy(function(html) {
$row.children('.plugin__filelisting_cell_icon').html(this.options.dirOpenedIcon);
$row.after(html);
$row.data('isLoaded', true);
this.$content.trigger('nsload', namespace);
$row.data('isExpanded', true);
this.$content.trigger('expand', namespace);
}, this), 'html')
.fail($.proxy(function () {
$row.children('.plugin__filelisting_cell_icon').html(this.options.dirClosedIcon);
}, this));
}
}, this));
this.$content.on('namespaceFilesChanged', $.proxy(function (event, namespace) {
var $row = $('tr[data-namespace="'+namespace+'"]');
if ($row.length && !$row.data('isLoaded')) {
return;
}
var data = {};
data['call'] = 'plugin_filelisting';
data['namespace'] = namespace;
data['baseNamespace'] = this.options.baseNamespace;
data['filesOnly'] = true;
$.post(DOKU_BASE + 'lib/exe/ajax.php', data,
$.proxy(function(html) {
var fileRows = $(html);
if ($row.length && !$row.data('isExpanded')) {
fileRows.hide();
}
var $filesInNamespace = $('tr[data-childOf="'+namespace+'"]').not('[data-namespace]');
if ($filesInNamespace.length) {
// there are already files in the namespace: replace them
$filesInNamespace.first().replaceWith(fileRows);
$filesInNamespace.remove();
} else if ($('tr[data-childOf="'+namespace+'"]').length) {
// there are no files, but other folders in the namespace: insert after them
$('tr[data-childOf="'+namespace+'"]').last().after(fileRows);
} else {
// tbody is currently empty: append to it
this.$content.find('tbody').append(fileRows);
}
this.$content.trigger('nsload', namespace);
}, this), 'html');
}, this))
};
Filelisting.prototype.initFilter = function() {
this.$filter = $('<label>' + this.options.filterLabel + ': <input></label>').appendTo(this.$footer);
var $input = this.$filter.find('input');
//filter has changed, update content
$input.on('keyup', $.proxy(this.applyFilter, this));
//prevent deleting files on pressing enter
$input.on('keydown', function (event) {
if(event.keyCode === 13) {
event.preventDefault();
return false;
}
});
//bind filtering to content update event
this.$content.on('expand', $.proxy(this.applyFilter, this));
};
Filelisting.prototype.applyFilter = function() {
var filter = this.$filter.find('input').val(),
//escape regex
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Special_Characters
escaped = filter.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), // $& means the whole matched string
globbing = escaped.replace(/\\\*/g, '.*').replace(/\\\?/g, '.'),
regex = new RegExp(globbing),
$rows = this.$content.find('tbody tr'),
$files = $rows.not('[data-namespace]'),
$dirs = $rows.not($files),
filterCallback = function() {
//text in second column
var $row = $(this),
text = $row.find('td.plugin__filelisting_cell_name a').text();
if (text.match(regex)) {
$row.show();
} else {
$row.hide();
}
};
//files in base namespace are always visible
$files.filter('[data-childOf="' + this.options.baseNamespace + '"]').each(filterCallback);
//get namespaces
$dirs.filter(function() {
//only expanded
return $(this).data('isExpanded');
}).each(function () {
var namespace = $(this).data('namespace');
$files.filter('[data-childOf="' + namespace + '"]').each(filterCallback);
});
};
Filelisting.prototype.initSorting = function() {
//global: current sort header
//not defined by default
this.$sortHeader = [];
//create sort links (for styling purposes)
this.$content.find('thead th').wrapInner('<a href="#">');
//sorting indicator
this.$content.find('thead th a').prepend('<span>');
//options for click
this.$content.find('thead th a').on('click', $.proxy(function(event) {
//prevent from scrolling to top
event.preventDefault();
this.$sortHeader = $(event.target).closest('th');
var $order = this.$sortHeader.find('span');
//clear other sorting indicators
this.$content.find('thead th').not(this.$sortHeader).find('span').text('');
//toggle sort ordering
if ($order.text() === '' || $order.text() === this.options.sortDesc) {
$order.text(this.options.sortAsc);
} else {
$order.text(this.options.sortDesc);
}
//perform sorting
this.sortBy();
}, this));
//bind sorting to content update event
this.$content.on('nsload', $.proxy(function(event, namespace) {
this.sortBy(namespace);
}, this));
};
Filelisting.prototype.sortBy = function(namespace) {
//don't sort where sortHeader not defined
if (this.$sortHeader.length === 0) return;
//by default sort starts from the base namespace
if (namespace === undefined) {
namespace = this.options.baseNamespace;
}
var $root = this.$content.find('tbody tr[data-namespace="' + namespace + '"]'),
$rows = this.$content.find('tbody tr[data-childOf="' + namespace + '"]'),
$files = $rows.not('[data-namespace]'),
$dirs = $rows.not($files),
sortCallback = $.proxy(function (a, b) {
//remember about first th colspan
var colspan = this.$headertable.find('th').first().attr('colspan'),
index = this.$sortHeader.index() + (colspan - 1),
order = 1; //1 ascending order, -1 descending order
//check for desc sorting
if (this.$sortHeader.find('span').text() === this.options.sortDesc) {
order = -1;
}
var dataA = $(a).find('td').eq(index).data('sort'),
dataB = $(b).find('td').eq(index).data('sort');
//$.data automatically converts string to integer when possible
if (dataA < dataB) {
return -order;
} else if (dataA > dataB) {
return order;
}
return 0;
}, this);
//sort dirs
$dirs.sort(sortCallback);
//sort files
$files.sort(sortCallback);
//we are on top level
if ($root.length === 0) {
this.$content.find('tbody').append($dirs, $files);
} else {
$root.after($dirs, $files);
}
//attach files to corresponding dirs
$dirs.each($.proxy(function(index, element) {
var namespace = $(element).data('namespace'),
$descendants = $(element).siblings('[data-childOf^="' + namespace + '"]');
$descendants.insertAfter(element);
//sort sub namespaces
this.sortBy(namespace);
}, this));
};
Filelisting.prototype.initDelete = function() {
var $deleteButton = this.$collapsible.find('button[name="do[plugin_filelisting_delete]"]');
$deleteButton.on('click', $.proxy(function (event) {
var deleteFiles = window.confirm(this.options.deleteConfirm);
if (!deleteFiles) {
event.preventDefault();
}
}, this));
this.toggleDeleteButton();
//show/hide delete button
this.$content.on('change', 'input[type=checkbox]', $.proxy(this.toggleDeleteButton, this));
};
Filelisting.prototype.toggleDeleteButton = function() {
var $deleteButton = this.$collapsible.find('button[name="do[plugin_filelisting_delete]"]');
if (this.$content.find('input[type=checkbox]:checked').length === 0) {
$deleteButton.hide();
} else {
$deleteButton.show();
}
};
$.fn.dokuwiki_plugin_filelisting = function (options) {
//return jquery object
return this.each(function() {
new Filelisting(this, options);
});
};
$.fn.dokuwiki_plugin_filelisting.defaults = {
//label for visible list
toggleVisible: '▼',
//label for hidden list
toggleHidden: '▲',
//id of the current wiki page
pageId: '',
defaultToggle: 'visible',
//html used as dir open icon
dirOpenedIcon: '',
//html used as dir close icon
dirClosedIcon: '',
//html used as loading icon for ajax call
loadingIcon: '',
//namespace of the current wiki page
baseNamespace: '',
//label of filter input
filterLabel: 'Filter',
//sort ascending label
sortAsc: '↓',
//sort descending label
sortDesc: '↑',
//confirm file deletion
deleteConfirm: LANG.plugins.filelisting.delete_confirm
};
}(window.jQuery));
jQuery(function() {
//read JSINFO and LANG
if (JSINFO === undefined || LANG === undefined) {
console.log('filelisting: JSINFO or LANG undefined');
return;
}
var options = {};
options.pageId = JSINFO.id;
var defaulttoggle = JSINFO.plugin.filelisting.defaulttoggle;
if (defaulttoggle === '1') {
options.defaultToggle = 'visible';
} else {
options.defaultToggle = 'hidden';
}
options.remember_state_per_page = JSINFO.plugin.filelisting.remember_state_per_page;
options.dirOpenedIcon = JSINFO.plugin.filelisting.dirOpenedIcon;
options.dirClosedIcon = JSINFO.plugin.filelisting.dirClosedIcon;
options.loadingIcon = JSINFO.plugin.filelisting.loadingIcon;
var $plugin__filelisting = jQuery('.plugin__filelisting');
// if base namespace is not properly set, sorting and filtering wont work
var ns = $plugin__filelisting.data("namespace");
if (ns !== undefined) {
options.baseNamespace = ns;
} else {
options.baseNamespace = JSINFO.namespace;
}
options.filterLabel = LANG.plugins.filelisting.filter_label;
options.deleteConfirm = LANG.plugins.filelisting.delete_confirm;
$plugin__filelisting.dokuwiki_plugin_filelisting(options);
/**
* Fixes the tablewidths so that the table columns in header and body are exactly aligned
*
* This is necessary, because browsers have different widths for scrollbars
*/
$plugin__filelisting.each(function adjustTableWidthForScrollbar(index, container) {
var $bodyTable = jQuery(container).find('.plugin__filelisting_bodytable table');
var $headerWrapper = jQuery(container).find('.plugin__filelisting_headertable');
var tablediff = $bodyTable.width() - $headerWrapper.find('table').width();
var originalPaddingRight = parseInt($headerWrapper.css('padding-right'), 10);
var newPaddingRight = originalPaddingRight - tablediff;
$headerWrapper.css('padding-right', newPaddingRight + 'px');
});
});