PluginProbe
WP Coder – Insert & Manage Code Snippets / 4.2
WP Coder – Insert & Manage Code Snippets v4.2
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / assets / js / admin-jquery.js

admin-jquery.js in WP Coder – Insert & Manage Code Snippets 4.2, at assets/js/admin-jquery.js

466 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 'use strict';
2
3 jQuery(document).ready(function ($) {
4
5
6 // All checkboxes
7 $('#wowp-settings').on('click', 'input:checkbox', function () {
8 checkboxchecked(this);
9 });
10
11 function checkboxchecked(el) {
12 if ($(el).prop('checked')) {
13 $(el).next('input[type="hidden"]').val('1');
14 } else {
15 $(el).next('input[type="hidden"]').val('');
16 }
17 }
18
19 // Preview
20 $('[data-option="preview"] input[type="checkbox"]').each(show_preview).on('click', show_preview);
21
22 function show_preview() {
23 const preview = $('.wowp-preview');
24 if ($(this).is(':checked')) {
25 $(preview).removeClass('is-hidden');
26 } else {
27 $(preview).addClass('is-hidden');
28 }
29 }
30
31 // Browsers options
32 $('[data-option="hide_browsers"] input[type="checkbox"]').each(hide_browsers);
33 $('[data-option="hide_browsers"] input[type="checkbox"]').on('click', hide_browsers);
34
35 function hide_browsers() {
36 const browsers = $(this).parents('.wowp-field').siblings('.wowp-field');
37 if ($(this).is(':checked')) {
38 $(browsers).removeClass('is-hidden');
39 } else {
40 $(browsers).addClass('is-hidden');
41 }
42 }
43
44 // Languages options
45 $('[data-option="depending_language"] input[type="checkbox"]').each(languageOn);
46 $('[data-option="depending_language"] input[type="checkbox"]').on('click', languageOn);
47
48 function languageOn() {
49 const languages = $(this).parents('.wowp-field').siblings('.wowp-field');
50 if ($(this).is(':checked')) {
51 $(languages).removeClass('is-hidden');
52 } else {
53 $(languages).addClass('is-hidden');
54 }
55 }
56
57 // Users
58 $('[name="param[item_user]"]').each(usersRule);
59 $('[name="param[item_user]"]').on('change', usersRule);
60
61 function usersRule() {
62 const user = $(this).val();
63 const parent = $(this).closest('fieldset');
64 const boxRoles = $(parent).find('.wowp-users-roles');
65 $(boxRoles).addClass('is-hidden');
66 if (user === '2') {
67 $(boxRoles).removeClass('is-hidden');
68 }
69 }
70
71 $('.wowp-users-roles .wowp-field:first input:first').each(checkAllRoles);
72 $('.wowp-users-roles .wowp-field:first input:first').on('change', checkAllRoles);
73
74 function checkAllRoles() {
75 const checkboxes = $('.wowp-users-roles input[type="checkbox"]');
76 if ($(this).is(':checked')) {
77 $(checkboxes).prop('checked', true);
78 }
79 }
80
81 // Schedule options
82 $('.wowp-dates input[type="checkbox"]').each(datesSchedule);
83 $('#schedule').on('click', '.wowp-dates input', datesSchedule);
84
85 function datesSchedule() {
86 const parent = $(this).closest('.wowp-fields__group');
87 if ($(this).is(':checked')) {
88 $(parent).find('.wowp-date-input').removeClass('is-hidden');
89 } else {
90 $(parent).find('.wowp-date-input').addClass('is-hidden');
91 }
92 }
93
94 $('#add-schedule').on('click', function (e) {
95 e.preventDefault();
96
97 const temlate = $('#clone-schedule').clone().html();
98
99 $(temlate).insertBefore('#schedule .wowp-btn-actions');
100 $('.wowp-dates input[type="checkbox"]').each(datesSchedule);
101 });
102
103 $('#schedule').on('click', '.dashicons-trash', function () {
104 const parent = $(this).closest('.wowp-fields__group');
105 $(parent).remove();
106 });
107
108 // Display rules
109 $('#add_display').on('click', function (e) {
110 e.preventDefault();
111
112 const temlate = $('#template-display').clone().html();
113
114 $(temlate).insertBefore('#display-rules .btn-add-display');
115 $('#display-rules .display-option select').each(displayRules);
116 });
117
118 $('#display-rules').on('click', '.dashicons-trash', function () {
119 const userConfirmed = confirm("Are you sure you want to remove?");
120 if (userConfirmed) {
121 const parent = $(this).closest('.wowp-fields__group');
122 $(parent).remove();
123 }
124
125 });
126
127 $('#display-rules .display-option select').each(displayRules);
128 $('#display-rules').on('change', '.display-option select', displayRules);
129
130 function displayRules() {
131 let type = $(this).val();
132 const parent = $(this).closest('.wowp-fields__group');
133 $(parent).find('.display-operator, .display-ids, .display-pages').addClass('is-hidden');
134 if (type.indexOf('custom_post_selected') !== -1) {
135 type = 'post_selected';
136 }
137 if (type.indexOf('custom_post_tax') !== -1) {
138 type = 'post_category';
139 }
140 switch (type) {
141 case 'post_selected':
142 case 'post_category':
143 case 'page_selected':
144 $(parent).find('.display-operator, .display-ids').removeClass('is-hidden');
145 break;
146 case 'page_type':
147 $(parent).find('.display-operator, .display-pages').removeClass('is-hidden');
148 break;
149 }
150
151 }
152
153 // Includes Assets files
154 $('#add-include').on('click', function (e) {
155 e.preventDefault();
156
157 const temlate = $('#clone-includes').clone().html();
158
159 $(temlate).insertBefore('#includes-files .btn-add-display');
160 $('#includes-files .display-option select').each(includeFiles);
161 });
162
163 $('#includes-files').on('click', '.dashicons-trash', function () {
164 const userConfirmed = confirm("Are you sure you want to remove?");
165 if (userConfirmed) {
166 const parent = $(this).closest('.wowp-fields__group');
167 $(parent).remove();
168 }
169 });
170
171 $('#includes-files .display-option select').each(includeFiles);
172 $('#includes-files').on('change', '.display-option select', includeFiles);
173
174 function includeFiles() {
175 let type = $(this).val();
176 const parent = $(this).closest('.wowp-fields__group');
177 const js = $(parent).find('.js-attr');
178 const css = $(parent).find('.css-only-preview');
179 $(js).add(css).addClass('is-hidden');
180 switch (type) {
181 case 'css':
182 $(css).removeClass('is-hidden');
183 break;
184 case 'js':
185 $(js).removeClass('is-hidden');
186 break;
187 }
188
189 }
190
191 // Dequeue Assets files
192 $('#add-dequeue').on('click', function (e) {
193 e.preventDefault();
194
195 const temlate = $('#clone-dequeue').clone().html();
196
197 $(temlate).insertBefore('#dequeue .btn-add-display');
198 });
199
200 $('#dequeue').on('click', '.dashicons-trash', function () {
201 const parent = $(this).closest('.wowp-fields__group');
202 $(parent).remove();
203 });
204
205 // Add shortcode attributes
206 $('#add-attribute').on('click', function (e) {
207 e.preventDefault();
208 const temlate = $('#clone-attributes').clone().html();
209 $(temlate).insertBefore('#code-attributes .btn-add-display');
210 });
211
212 $('#code-attributes').on('click', '.dashicons-trash', function () {
213 const parent = $(this).closest('.wowp-fields__group');
214 $(parent).remove();
215 });
216
217 $('.button-add-img').on('click', function (event) {
218 event.preventDefault();
219 var upload_button = $(this);
220 var img = $(this).data('img');
221 var frame;
222 event.preventDefault();
223 if (frame) {
224 frame.open();
225 return;
226 }
227 frame = wp.media();
228 frame.on('select', function () {
229 // Grab the selected attachment.
230 const attachment = frame.state().get('selection').first();
231 let url = attachment.attributes.url;
232 url = url.replace('-scaled.', '.');
233 let alt = attachment.attributes.alt;
234 let title = attachment.attributes.title;
235 let height = attachment.attributes.height;
236 let width = attachment.attributes.width;
237 let img = `<img src="${url}" alt="${alt}" loading="lazy" width="${width}" height="${height}">`;
238 frame.close();
239 const editorElement = $('[data-content="wowp-tab-html-code"]').find('.CodeMirror')[0];
240 const editor = editorElement.CodeMirror;
241 const doc = editor.getDoc();
242 const cursor = doc.getCursor();
243 doc.replaceRange(img, cursor);
244 editor.focus();
245
246 });
247 frame.open();
248
249 });
250
251 $('.button-editor').not('#phpglobalnav').on('click', function (event) {
252 event.preventDefault();
253 const parent = $(this).closest('.wowp-settins__tabs-content');
254 const id = $(this).attr('id');
255 let comment;
256 switch (id) {
257 case 'jsnav':
258 case 'phpnav':
259 comment = '// NAV: ';
260 break;
261 case 'htmlnav':
262 comment = '<!-- NAV: -->';
263 break;
264 case 'cssnav':
265 comment = '/* NAV: */';
266 break;
267 }
268
269 const editorElement = $(parent).find('.CodeMirror')[0];
270 const editor = editorElement.CodeMirror;
271 const doc = editor.getDoc();
272 const cursor = doc.getCursor();
273 doc.replaceRange(comment, cursor);
274 editor.focus();
275 });
276
277 $('#phpglobalnav').on('click', function (event) {
278 event.preventDefault();
279 const parent = $(this).closest('.wowp-settings__page');
280 let comment = '// NAV: ';
281 const editorElement = $(parent).find('.CodeMirror')[0];
282 const editor = editorElement.CodeMirror;
283 const doc = editor.getDoc();
284 const cursor = doc.getCursor();
285 doc.replaceRange(comment, cursor);
286 editor.focus();
287 });
288
289 $('.wowp-copy').on('click', function () {
290 const copyText = $(this).closest('.wowp-input-group').find('input')[0];
291
292 copyText.select();
293 copyText.setSelectionRange(0, 99999); // For mobile devices
294
295 // Copy the text inside the text field
296 navigator.clipboard.writeText(copyText.value);
297
298 // Alert the copied text
299 alert("Copied the shortcode: " + copyText.value);
300 });
301
302 $('.wowp-shortcode-copy').on('click', function () {
303 const copyText = $(this).closest('.wowp-field').find('input')[0];
304
305 copyText.select();
306 copyText.setSelectionRange(0, 99999); // For mobile devices
307
308 // Copy the text inside the text field
309 navigator.clipboard.writeText(copyText.value);
310
311 // Alert the copied text
312 alert("Copied the shortcode: " + copyText.value);
313 });
314
315 $('.quickcode').on('click', function () {
316 const editor = $('[data-content="wowp-tab-html-code"] .CodeMirror')[0].CodeMirror;
317 if (editor) {
318 const text = $(this).data('code');
319 const doc = editor.getDoc();
320 const cursor = doc.getCursor();
321 doc.replaceRange(text, cursor);
322 editor.focus();
323 }
324 });
325
326
327 // Copy
328 $('.can-copy').on('click', function () {
329 const parent = $(this).closest('.wowp-field');
330 const input = $(parent).find('input');
331 const originalTooltip = $(this).attr("data-tooltip");
332 const currentElement = $(this);
333
334 navigator.clipboard.writeText(input.val()).then(() => {
335 currentElement.attr("data-tooltip", "Copied");
336 setTimeout(function () {
337 currentElement.attr("data-tooltip", originalTooltip);
338 }, 1000);
339 });
340 });
341
342 // Quick Code
343
344 const button_quickcodes = document.getElementById('open-quickcodes');
345
346 if (button_quickcodes) {
347 button_quickcodes.addEventListener('click', (e) => {
348 e.preventDefault();
349 const dialog = document.getElementById('quickcodes-dialog');
350 const search = document.getElementById('qc-search');
351 if (dialog && typeof dialog.showModal === 'function') {
352 dialog.showModal();
353 if (search) search.focus();
354 }
355 });
356 }
357
358
359 // Open on Cmd+K
360 document.addEventListener('keydown', (e) => {
361 if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
362 e.preventDefault();
363 document.getElementById('quickcodes-dialog').showModal();
364 document.getElementById('qc-search').focus();
365 }
366 });
367
368 // Filter list
369
370 const searchInput = document.getElementById('qc-search');
371
372 if (searchInput) {
373 searchInput.addEventListener('input', (e) => {
374 const term = e.target.value.toLowerCase();
375 document.querySelectorAll('#qc-list li').forEach(li => {
376 li.style.display = li.textContent.toLowerCase().includes(term) ? 'flex' : 'none';
377 });
378 });
379 }
380
381 // Insert at cursor position
382 document.querySelectorAll('#qc-list li').forEach(li => {
383 li.addEventListener('click', () => {
384 const code = li.getAttribute('data-code');
385 insertAtCursor(document.querySelector('textarea'), code);
386 document.getElementById('quickcodes-dialog').close();
387 });
388 });
389
390 // Helper function
391 function insertAtCursor(el, text) {
392 const start = el.selectionStart;
393 const end = el.selectionEnd;
394 el.value = el.value.slice(0, start) + text + el.value.slice(end);
395 el.selectionStart = el.selectionEnd = start + text.length;
396 el.focus();
397 }
398
399 const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
400 const shortcut = isMac ? 'Cmd + K' : 'Ctrl + K';
401 const quickcode_hotkey = document.querySelector('#quickcode-hotkey');
402 if (quickcode_hotkey) {
403 quickcode_hotkey.textContent = `(${shortcut})`;
404 }
405
406
407 // Stippets control-checkbox
408 $('.wowp-snippet__item > .has-checkbox input[type="checkbox"] ').each(snippet_checkbox).on('click', snippet_checkbox);
409
410 function snippet_checkbox() {
411 const expand = $(this).closest('.wowp-snippet__item').find('.wowp-snippet__item-expand');
412 if ($(this).is(':checked') && expand) {
413 $(expand).removeClass('is-hidden');
414 } else {
415 $(expand).addClass('is-hidden');
416 }
417 }
418
419 // Popover
420
421 const toggleBtn = document.getElementById('popover-toggle');
422 const popover = document.getElementById('popover-box');
423
424 if (toggleBtn) {
425
426 toggleBtn.addEventListener('click', (e) => {
427 e.preventDefault();
428 popover.style.display = popover.style.display === 'block' ? 'none' : 'block';
429 });
430
431 document.addEventListener('click', (e) => {
432 if (!toggleBtn.contains(e.target) && !popover.contains(e.target)) {
433 popover.style.display = 'none';
434 }
435 });
436
437 }
438
439 // Hide Tabs
440
441 $('#popover-box input[type="checkbox"]').each(hide_tab).on('click', hide_tab);
442
443 function hide_tab() {
444 const types = {
445 checkbox_hide_html: 'wowp-tab-html-code',
446 checkbox_hide_css: 'wowp-tab-css-code',
447 checkbox_hide_js: 'wowp-tab-js-code',
448 checkbox_hide_php: 'wowp-tab-php-code',
449 checkbox_hide_attributes: 'wowp-tab-attributes',
450 checkbox_hide_settings: 'wowp-tab-settings',
451 checkbox_hide_include: 'wowp-tab-include',
452 };
453
454 const id = $(this).attr('id');
455 const tab = types[id];
456 if ($(this).is(':checked')) {
457 $('#'+ tab).prop('checked', false);
458 $(`[for="${tab}"]`).addClass('is-hidden');
459 } else {
460 $(`[for="${tab}"]`).removeClass('is-hidden');
461 }
462 }
463
464
465 });
466