PluginProbe
WP Coder – Insert & Manage Code Snippets / 4.0.3
WP Coder – Insert & Manage Code Snippets v4.0.3
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.0.3, at assets/js/admin-jquery.js

464 lines 15.1 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 $(parent).find('.js-attr').addClass('is-hidden');
178 switch (type) {
179 case 'css':
180 $(parent).find('.js-attr').addClass('is-hidden');
181 break;
182 case 'js':
183 $(parent).find('.js-attr').removeClass('is-hidden');
184 break;
185 }
186
187 }
188
189 // Dequeue Assets files
190 $('#add-dequeue').on('click', function (e) {
191 e.preventDefault();
192
193 const temlate = $('#clone-dequeue').clone().html();
194
195 $(temlate).insertBefore('#dequeue .btn-add-display');
196 });
197
198 $('#dequeue').on('click', '.dashicons-trash', function () {
199 const parent = $(this).closest('.wowp-fields__group');
200 $(parent).remove();
201 });
202
203 // Add shortcode attributes
204 $('#add-attribute').on('click', function (e) {
205 e.preventDefault();
206 const temlate = $('#clone-attributes').clone().html();
207 $(temlate).insertBefore('#code-attributes .btn-add-display');
208 });
209
210 $('#code-attributes').on('click', '.dashicons-trash', function () {
211 const parent = $(this).closest('.wowp-fields__group');
212 $(parent).remove();
213 });
214
215 $('.button-add-img').on('click', function (event) {
216 event.preventDefault();
217 var upload_button = $(this);
218 var img = $(this).data('img');
219 var frame;
220 event.preventDefault();
221 if (frame) {
222 frame.open();
223 return;
224 }
225 frame = wp.media();
226 frame.on('select', function () {
227 // Grab the selected attachment.
228 const attachment = frame.state().get('selection').first();
229 let url = attachment.attributes.url;
230 url = url.replace('-scaled.', '.');
231 let alt = attachment.attributes.alt;
232 let title = attachment.attributes.title;
233 let height = attachment.attributes.height;
234 let width = attachment.attributes.width;
235 let img = `<img src="${url}" alt="${alt}" loading="lazy" width="${width}" height="${height}">`;
236 frame.close();
237 const editorElement = $('[data-content="wowp-tab-html-code"]').find('.CodeMirror')[0];
238 const editor = editorElement.CodeMirror;
239 const doc = editor.getDoc();
240 const cursor = doc.getCursor();
241 doc.replaceRange(img, cursor);
242 editor.focus();
243
244 });
245 frame.open();
246
247 });
248
249 $('.button-editor').not('#phpglobalnav').on('click', function (event) {
250 event.preventDefault();
251 const parent = $(this).closest('.wowp-settins__tabs-content');
252 const id = $(this).attr('id');
253 let comment;
254 switch (id) {
255 case 'jsnav':
256 case 'phpnav':
257 comment = '// NAV: ';
258 break;
259 case 'htmlnav':
260 comment = '<!-- NAV: -->';
261 break;
262 case 'cssnav':
263 comment = '/* NAV: */';
264 break;
265 }
266
267 const editorElement = $(parent).find('.CodeMirror')[0];
268 const editor = editorElement.CodeMirror;
269 const doc = editor.getDoc();
270 const cursor = doc.getCursor();
271 doc.replaceRange(comment, cursor);
272 editor.focus();
273 });
274
275 $('#phpglobalnav').on('click', function (event) {
276 event.preventDefault();
277 const parent = $(this).closest('.wowp-settings__page');
278 let comment = '// NAV: ';
279 const editorElement = $(parent).find('.CodeMirror')[0];
280 const editor = editorElement.CodeMirror;
281 const doc = editor.getDoc();
282 const cursor = doc.getCursor();
283 doc.replaceRange(comment, cursor);
284 editor.focus();
285 });
286
287 $('.wowp-copy').on('click', function () {
288 const copyText = $(this).closest('.wowp-input-group').find('input')[0];
289
290 copyText.select();
291 copyText.setSelectionRange(0, 99999); // For mobile devices
292
293 // Copy the text inside the text field
294 navigator.clipboard.writeText(copyText.value);
295
296 // Alert the copied text
297 alert("Copied the shortcode: " + copyText.value);
298 });
299
300 $('.wowp-shortcode-copy').on('click', function () {
301 const copyText = $(this).closest('.wowp-field').find('input')[0];
302
303 copyText.select();
304 copyText.setSelectionRange(0, 99999); // For mobile devices
305
306 // Copy the text inside the text field
307 navigator.clipboard.writeText(copyText.value);
308
309 // Alert the copied text
310 alert("Copied the shortcode: " + copyText.value);
311 });
312
313 $('.quickcode').on('click', function () {
314 const editor = $('[data-content="wowp-tab-html-code"] .CodeMirror')[0].CodeMirror;
315 if (editor) {
316 const text = $(this).data('code');
317 const doc = editor.getDoc();
318 const cursor = doc.getCursor();
319 doc.replaceRange(text, cursor);
320 editor.focus();
321 }
322 });
323
324
325 // Copy
326 $('.can-copy').on('click', function () {
327 const parent = $(this).closest('.wowp-field');
328 const input = $(parent).find('input');
329 const originalTooltip = $(this).attr("data-tooltip");
330 const currentElement = $(this);
331
332 navigator.clipboard.writeText(input.val()).then(() => {
333 currentElement.attr("data-tooltip", "Copied");
334 setTimeout(function () {
335 currentElement.attr("data-tooltip", originalTooltip);
336 }, 1000);
337 });
338 });
339
340 // Quick Code
341
342 const button_quickcodes = document.getElementById('open-quickcodes');
343
344 if (button_quickcodes) {
345 button_quickcodes.addEventListener('click', (e) => {
346 e.preventDefault();
347 const dialog = document.getElementById('quickcodes-dialog');
348 const search = document.getElementById('qc-search');
349 if (dialog && typeof dialog.showModal === 'function') {
350 dialog.showModal();
351 if (search) search.focus();
352 }
353 });
354 }
355
356
357 // Open on Cmd+K
358 document.addEventListener('keydown', (e) => {
359 if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
360 e.preventDefault();
361 document.getElementById('quickcodes-dialog').showModal();
362 document.getElementById('qc-search').focus();
363 }
364 });
365
366 // Filter list
367
368 const searchInput = document.getElementById('qc-search');
369
370 if (searchInput) {
371 searchInput.addEventListener('input', (e) => {
372 const term = e.target.value.toLowerCase();
373 document.querySelectorAll('#qc-list li').forEach(li => {
374 li.style.display = li.textContent.toLowerCase().includes(term) ? 'flex' : 'none';
375 });
376 });
377 }
378
379 // Insert at cursor position
380 document.querySelectorAll('#qc-list li').forEach(li => {
381 li.addEventListener('click', () => {
382 const code = li.getAttribute('data-code');
383 insertAtCursor(document.querySelector('textarea'), code);
384 document.getElementById('quickcodes-dialog').close();
385 });
386 });
387
388 // Helper function
389 function insertAtCursor(el, text) {
390 const start = el.selectionStart;
391 const end = el.selectionEnd;
392 el.value = el.value.slice(0, start) + text + el.value.slice(end);
393 el.selectionStart = el.selectionEnd = start + text.length;
394 el.focus();
395 }
396
397 const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
398 const shortcut = isMac ? 'Cmd + K' : 'Ctrl + K';
399 const quickcode_hotkey = document.querySelector('#quickcode-hotkey');
400 if (quickcode_hotkey) {
401 quickcode_hotkey.textContent = `(${shortcut})`;
402 }
403
404
405 // Stippets control-checkbox
406 $('.wowp-snippet__item > .has-checkbox input[type="checkbox"] ').each(snippet_checkbox).on('click', snippet_checkbox);
407
408 function snippet_checkbox() {
409 const expand = $(this).closest('.wowp-snippet__item').find('.wowp-snippet__item-expand');
410 if ($(this).is(':checked') && expand) {
411 $(expand).removeClass('is-hidden');
412 } else {
413 $(expand).addClass('is-hidden');
414 }
415 }
416
417 // Popover
418
419 const toggleBtn = document.getElementById('popover-toggle');
420 const popover = document.getElementById('popover-box');
421
422 if (toggleBtn) {
423
424 toggleBtn.addEventListener('click', (e) => {
425 e.preventDefault();
426 popover.style.display = popover.style.display === 'block' ? 'none' : 'block';
427 });
428
429 document.addEventListener('click', (e) => {
430 if (!toggleBtn.contains(e.target) && !popover.contains(e.target)) {
431 popover.style.display = 'none';
432 }
433 });
434
435 }
436
437 // Hide Tabs
438
439 $('#popover-box input[type="checkbox"]').each(hide_tab).on('click', hide_tab);
440
441 function hide_tab() {
442 const types = {
443 checkbox_hide_html: 'wowp-tab-html-code',
444 checkbox_hide_css: 'wowp-tab-css-code',
445 checkbox_hide_js: 'wowp-tab-js-code',
446 checkbox_hide_php: 'wowp-tab-php-code',
447 checkbox_hide_attributes: 'wowp-tab-attributes',
448 checkbox_hide_settings: 'wowp-tab-settings',
449 checkbox_hide_include: 'wowp-tab-include',
450 };
451
452 const id = $(this).attr('id');
453 const tab = types[id];
454 if ($(this).is(':checked')) {
455 $('#'+ tab).prop('checked', false);
456 $(`[for="${tab}"]`).addClass('is-hidden');
457 } else {
458 $(`[for="${tab}"]`).removeClass('is-hidden');
459 }
460 }
461
462
463 });
464