PluginProbe
Add Custom Codes – Insert Header, Footer, Custom PHP Snippets, CSS, Javascript / trunk
Add Custom Codes – Insert Header, Footer, Custom PHP Snippets, CSS, Javascript vtrunk
trunk 1.0 2.0 3.0 3.5 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.71 4.80 5.0
add-custom-codes / assets / js / scripts.js

scripts.js in Add Custom Codes – Insert Header, Footer, Custom PHP Snippets, CSS, Javascript trunk, at assets/js/scripts.js

236 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function ($) {
2 $(document).ready(function () {
3
4
5 const $editor = $('#accodes_code_editor');
6 const lang = $editor.data('lang') || 'php';
7 let cmInstance;
8
9 if ($editor.length) {
10 let cm_settings = wp.codeEditor.defaultSettings ? _.clone(wp.codeEditor.defaultSettings) : {};
11
12 cm_settings.codemirror = _.extend({}, cm_settings.codemirror, {
13 mode: lang,
14 lineNumbers: true,
15 indentUnit: 4,
16 theme: 'default' // or 'dark' if saved preference
17 });
18
19 // Store instance so we can change it later
20 cmInstance = wp.codeEditor.initialize($editor[0], cm_settings).codemirror;
21
22 function getDefaultTemplate(mode) {
23 switch (mode) {
24 case 'javascript':
25 return '/* Add your scripts here. You can remove example code */\nconsole.log("code loaded.");\n';
26 case 'css':
27 return "/* Your CSS here */\n.selector {\n\t\n}\n";
28 case 'htmlmixed':
29 return '<!-- Add your HTML here. You can remove example code-->\n<div></div>\n';
30 case 'php':
31 default:
32 return "// Your PHP here\n";
33 }
34 }
35
36 // Insert default template on load if empty
37 if (cmInstance && !cmInstance.getValue().trim()) {
38 cmInstance.setValue(getDefaultTemplate(lang));
39 }
40
41 // Handle theme toggle
42 $('#accodes_dark_mode_toggle').on('change', function () {
43 const isDark = $(this).is(':checked');
44 cmInstance.setOption('theme', isDark ? 'material' : 'default');
45
46 // Add/remove "darkmode" class to tab div
47 const $tabs = $('.accodes-radio-group.accodes-tab-style');
48 if (isDark) {
49 $tabs.addClass('darkmode');
50 } else {
51 $tabs.removeClass('darkmode');
52 }
53
54 });
55 }
56
57 // Auto switch mode on code type change
58 $('input[name="accodes_snippet_language"]').on('change', function () {
59 const selectedMode = $(this).val();
60 if (cmInstance) {
61 cmInstance.setOption('mode', selectedMode);
62
63 // If the editor is empty, or contains one of the default templates, replace it.
64 const current = cmInstance.getValue().trim();
65 const isDefaultTemplate = (function (val) {
66 if (!val) return true;
67 const templates = [
68 getDefaultTemplate('php').trim(),
69 getDefaultTemplate('javascript').trim(),
70 getDefaultTemplate('css').trim(),
71 getDefaultTemplate('htmlmixed').trim()
72 ];
73 return templates.indexOf(val) !== -1;
74 })(current);
75
76 if (isDefaultTemplate || !current) {
77 cmInstance.setValue(getDefaultTemplate(selectedMode));
78 }
79 }
80 });
81
82 // For any CSS-specific codemirror blocks
83 $('.codemirror-accodes-css').each(function (index, elem) {
84 let cm_settings = wp.codeEditor.defaultSettings ? _.clone(wp.codeEditor.defaultSettings) : {};
85 cm_settings.codemirror = _.extend({}, cm_settings.codemirror, {
86 mode: 'css'
87 });
88 wp.codeEditor.initialize($(elem), cm_settings);
89 });
90
91 // For any htmlmixed codemirror blocks
92 $('.accodes_cols .codemirror').each(function (index, elem) {
93 let cm_settings = wp.codeEditor.defaultSettings ? _.clone(wp.codeEditor.defaultSettings) : {};
94 cm_settings.codemirror = _.extend({}, cm_settings.codemirror, {
95 mode: 'htmlmixed'
96 });
97 wp.codeEditor.initialize($(elem), cm_settings);
98 });
99
100
101 //change status of snippet on clicking Status slider on listing page
102 $('.accodes-toggle-snippet').on('change', function () {
103 const postId = $(this).data('id');
104 const active = $(this).is(':checked') ? 1 : 0;
105
106 $.post(ajaxurl, {
107 action: 'accodes_toggle_snippet_status',
108 post_id: postId,
109 active: active,
110 _ajax_nonce: accodes_toggle_snippet_nonce
111 }, function (response) {
112 if (response.success) {
113 console.log('Snippet status updated.');
114 } else {
115 alert('Failed to update snippet status.');
116 }
117 });
118 });
119
120 function toggleLocationOptions(type) {
121 $('.location-options').hide();
122
123 const $target = $(`.location-options[data-type="${type}"]`);
124 $target.show();
125
126 const currentSelected = $('input[name="accodes_snippet_location"]:checked').val();
127 const $existingRadioInGroup = $target.find(`input[value="${currentSelected}"]`);
128
129 if ($existingRadioInGroup.length > 0) {
130 // Make sure it's visibly selected
131 $existingRadioInGroup.prop('checked', true);
132 } else {
133 // If not present, select the first
134 $target.find('input[type="radio"]').first().prop('checked', true);
135 }
136
137 toggleShortcodeBox();
138 }
139
140
141 function toggleShortcodeBox() {
142 const selectedLoc = $('input[name="accodes_snippet_location"]:checked').val();
143 const selectedType = $('input[name="accodes_snippet_language"]:checked').val();
144
145 if (selectedLoc === 'shortcode' && selectedType === 'htmlmixed') {
146 $('#accodes_shortcode_box').slideDown();
147 } else {
148 $('#accodes_shortcode_box').slideUp();
149 }
150 }
151
152 // Init
153 toggleShortcodeBox();
154
155
156 // Get selected on load
157 setTimeout(function () {
158 const initialType = $('input[name="accodes_snippet_language"]:checked').val();
159 toggleLocationOptions(initialType);
160 }, 50);
161
162 // On radio change — use event delegation
163 $(document).on('change', 'input[name="accodes_snippet_language"]', function () {
164 toggleLocationOptions($(this).val());
165 });
166
167 //show or hide shortcodebox
168 $(document).on('change', 'input[name="accodes_snippet_location"]', function () {
169 toggleShortcodeBox();
170 });
171
172 // Copy shortcode on click
173 $('#accodes_shortcode').on('click', function () {
174 const shortcode = $(this).text();
175
176 // Create temp input and copy
177 const $temp = $('<input>');
178 $('body').append($temp);
179 $temp.val(shortcode).select();
180 document.execCommand('copy');
181 $temp.remove();
182
183 // Show message
184 $('#accodes_copy_msg').fadeIn(200).delay(1500).fadeOut(300);
185 });
186
187 // tags suggestions while typing
188 if (typeof accodes_data.accodes_tag_suggestions !== 'undefined') {
189 $('#accodes_tag_input').autocomplete({
190 source: accodes_data.accodes_tag_suggestions,
191 minLength: 1,
192 select: function (event, ui) {
193 const val = ui.item.value;
194 const exists = $(`#accodes_tags_list input[value="${val}"]`).length > 0;
195
196 if (!exists) {
197 const chip = `
198 <span class="accodes-tag-chip">${val}
199 <input type="hidden" name="accodes_tags[]" value="${val}">
200 <span class="remove-tag">&times;</span>
201 </span>`;
202 $('#accodes_tags_list').append(chip);
203 }
204 $(this).val('');
205 return false;
206 }
207 });
208 }
209
210 // Enter key as fallback
211 $(document).on('keypress', '#accodes_tag_input', function (e) {
212 if (e.which === 13) {
213 e.preventDefault();
214 const val = $(this).val().trim();
215 if (val !== '') {
216 const chip = `
217 <span class="accodes-tag-chip">${val}
218 <input type="hidden" name="accodes_tags[]" value="${val}">
219 <span class="remove-tag">&times;</span>
220 </span>`;
221 $('#accodes_tags_list').append(chip);
222 $(this).val('');
223 }
224 }
225 });
226
227 // Remove tag on x click
228 $(document).on('click', '.remove-tag', function () {
229 $(this).closest('.accodes-tag-chip').remove();
230 });
231
232
233
234 });
235 })(jQuery);
236