forked from LaswitchTech/writr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
309 lines (273 loc) · 10.4 KB
/
script.js
File metadata and controls
309 lines (273 loc) · 10.4 KB
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
/* DOKUWIKI:include js/skip-link-focus-fix.js */
jQuery(document).ready(function() {
/*
* Click to toggle sidebar.
*/
function toggleSidebar() {
jQuery( '#writr__sidebar' ).on( 'click', '#writr__sidebar-toggle', function( e ) {
e.preventDefault();
jQuery( 'html, body' ).scrollTop( 0 );
jQuery( this ).toggleClass( 'open' );
jQuery( 'body' ).toggleClass( 'sidebar-closed' );
jQuery( '#writr__secondary' ).resize();
} );
}
/**
* Handles toggling the navigation menu for small screens.
*/
function toggleNavigation() {
var $container = jQuery('#writr__site-navigation');
if (!$container.length) return;
var $button = jQuery('.menu-toggle', $container);
if (!$button.length) return;
var $menu = jQuery('ul', $container);
if (!$menu.length) {
$menu.hide();
return;
}
$button.click(function(){
$container.toggleClass('toggled');
});
}
/*
* A function to enable/disable a dropdown submenu.
*/
function toggleSubmenu() {
jQuery( '.main-navigation .node > div > a' ).append( '<span class="dropdown-icon" />' );
jQuery( '#writr__site-navigation' ).on( 'click', '.dropdown-icon', function( e ) {
e.preventDefault();
jQuery( this ).toggleClass( 'open' );
if ( jQuery( this ).hasClass( 'open' ) ) {
jQuery( this ).parent().parent().next( 'ul' ).show();
} else {
jQuery( this ).parent().parent().next( 'ul' ).hide();
}
} );
}
/*
* Close TOC by default
*/
function closeToc() {
var $toc = jQuery('#dw__toc .toggle');
if($toc.length) {
$toc[0].setState(-1);
}
}
/*
* Change search submit input to submit button to make it easier to style
* @deprecated since Detritus
*/
function changeSearchInput() {
var $searchForm = jQuery('.search-form > form > div');
var $searchButton = jQuery('input[type="submit"]', $searchForm).detach();
var title = $searchButton.attr('title');
var value = $searchButton.val();
$searchForm.append('<button type="submit" title="'+title+'">'+value+'</button>');
}
/*
* Enable add new page dropdown
*/
function enableAddNewPage() {
jQuery('.action.AddNewPage').click(function(event) {
event.preventDefault();
const button = jQuery(this);
jQuery('.addnewpage').toggle(0,function(){
// set aria-expanded attribute based on visibility
button.attr('aria-expanded', jQuery(this).is(':visible'));
});
});
jQuery(document).click(function(event) {
if (!jQuery(event.target).closest('.action.AddNewPage, .addnewpage').length) {
jQuery('.addnewpage').hide();
}
});
}
/*
* Enable translation dropdown
*/
function enableTranslation() {
jQuery('.action.Translation').click(function(event) {
event.preventDefault();
const button = jQuery(this);
jQuery('.plugin_translation').toggle(0,function(){
// set aria-expanded attribute based on visibility
button.attr('aria-expanded', jQuery(this).is(':visible'));
});
});
jQuery(document).click(function(event) {
if (!jQuery(event.target).closest('.action.Translation, .plugin_translation').length) {
jQuery('.plugin_translation').hide();
}
});
}
/*
* Enable Toolbar Dropdowns
*/
function enableToolbarDropdowns() {
jQuery('#writr__toolbar .hook .node').each(function() {
const dropdown = jQuery(this);
dropdown.find('div.li').click(function(event) {
const trigger = jQuery(this);
dropdown.find('> ul').toggle(0,function(){
trigger.attr('aria-expanded', jQuery(this).is(':visible'));
});
// Close dropdown when clicking outside
jQuery(document).on('click.dropdown', function(e) {
if (!dropdown.is(e.target) && dropdown.has(e.target).length === 0) {
dropdown.find('> ul').hide();
trigger.attr('aria-expanded', 'false');
jQuery(document).off('click.dropdown');
}
});
});
});
}
/*
* Enable Collapse
*/
function enableCollapse() {
jQuery('[data-toggle="collapse"]').click(function(event){
event.preventDefault();
const trigger = jQuery(this);
const target = jQuery(trigger.attr('data-target'));
target.slideToggle('fast',function(){
// set aria-expanded attribute based on visibility
trigger.attr('aria-expanded', target.is(':visible'));
});
});
}
/*
* Enable Dropdowns
*/
function enableDropdowns() {
jQuery('.dropdown').each(function() {
const dropdown = jQuery(this);
dropdown.find('[data-toggle="dropdown"]').click(function(event) {
event.preventDefault();
const button = jQuery(this);
dropdown.find('.dropdown-menu').toggle(0,function(){
button.attr('aria-expanded', jQuery(this).is(':visible'));
});
// Close dropdown when clicking outside
jQuery(document).on('click.dropdown', function(e) {
if (!dropdown.is(e.target) && dropdown.has(e.target).length === 0) {
dropdown.find('> ul').hide();
button.attr('aria-expanded', 'false');
jQuery(document).off('click.dropdown');
}
});
});
});
}
/*
* Enable Tooltips
*/
function enableTooltips() {
jQuery('body.enableTooltips [title]:not(.media):not(img):not([title=""]), body.enableTooltips [alt]:not(.media):not(img):not([alt=""])').each(function() {
const element = jQuery(this);
const content = element.attr('alt') ? element.attr('alt') : element.attr('title');
element.attr('data-tooltip-content', content);
element.hover(function() {
// Prevent default browser tooltip from showing
const tooltipType = element.attr('alt') ? 'alt' : 'title';
element.removeAttr(tooltipType).attr('data-tooltip-type', tooltipType);
// Create and append the tooltip
const tooltip = jQuery('<div class="tooltip"><div class="tooltip-text">' + content + '</div></div>');
jQuery('body').append(tooltip);
// Calculate and set the position of the tooltip
const elementOffset = element.offset();
const tooltipWidth = tooltip.outerWidth();
const elementWidth = element.outerWidth();
const topPosition = elementOffset.top + element.outerHeight() + 10; // Adjust +10 for spacing
const leftPosition = elementOffset.left + (elementWidth / 2) - (tooltipWidth / 2);
tooltip.css({
top: topPosition,
left: leftPosition,
display: 'inline-block'
});
}, function() {
// Restore the original attribute and remove the tooltip
element.attr(element.attr('data-tooltip-type'), content);
jQuery('.tooltip').remove();
});
});
}
/*
* Enable Improved File Input
*/
function enableFileInput() {
jQuery('input[type="file"]').on('change', function() {
var input = jQuery(this);
var label = input.prev('span');
var group = input.parent('label');
var fileName = input.val().split('\\').pop() || 'No file chosen';
// Check if a div already exists
if (input.next('div').length > 0) {
input.next('div').remove();
}
// Create a div after the input
var file = jQuery('<div style="text-align:center;width:100%"></div>');
// Update the text of the file-name span
file.prepend(fileName);
// Add a button to clear the field
var button = jQuery('<button type="button" class="tagerror" style="margin-left:4px;padding: 4px 8px;font-size: 80%;"><i class="bi bi-trash"></i></button>').appendTo(file);
// Add Click event on the button
button.click(function(){
// Clear value
input.val('');
// Remove the file object
file.remove();
// Show input and label
label.show();
input.show();
});
// Insert the div after the input
input.after(file);
// Hide label & input
label.hide();
input.hide();
});
}
/*
* Disable Newlines in Textareas
*/
function disableNewlines() {
// Check if "?do=" is present in the URL
if (window.location.search.indexOf("?do=") === -1) {
jQuery("form").on("submit", function(event) {
// Prevent the form from submitting immediately
event.preventDefault();
// Find the textarea and replace newlines with " \\ "
var textarea = jQuery(this).find('textarea');
textarea.val(textarea.val().replace(/\n/gm, ' \\\\\\ '));
// After running the function, manually trigger the form submission
this.submit();
});
}
}
/*
* Run the functions
*/
jQuery(function(){
toggleSidebar();
toggleNavigation();
toggleSubmenu();
closeToc();
changeSearchInput();
enableAddNewPage();
enableTranslation();
enableToolbarDropdowns();
enableDropdowns();
enableTooltips();
enableCollapse();
enableFileInput();
});
/*
* NSPAGES enable multi-line support
*/
jQuery('.nspagesPicturesModeTitle').each(function() {
if (jQuery(this).prop('scrollHeight') > 32) {
jQuery(this).addClass('multi-line');
}
});
});