PluginProbe
WP Coder – Insert & Manage Code Snippets / 3.3
WP Coder – Insert & Manage Code Snippets v3.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.js

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

436 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 'use strict';
2
3 const settingTabs = function () {
4 const tabs = document.getElementById('settings-tab');
5 if (!tabs) {
6 return false;
7 }
8 const tabsContent = document.querySelectorAll('#settings-content .tab-content');
9
10 const links = tabs.querySelectorAll('a');
11 links.forEach((link) => {
12 link.addEventListener('click', function () {
13 const attr = link.getAttribute('data-tab');
14 links.forEach(el => el.classList.remove('nav-tab-active'));
15 tabsContent.forEach(el => el.classList.remove('tab-content-active'));
16 link.classList.add('nav-tab-active');
17 const tabContent = document.querySelector('[data-content=' + attr + ']');
18 tabContent.classList.add('tab-content-active');
19 setLocalStorage(attr);
20 });
21 });
22
23 setTabs();
24
25 function setTabs() {
26 if (getLocalStorage() === '') {
27 return false;
28 }
29 const tab = getLocalStorage();
30 links.forEach(el => el.classList.remove('nav-tab-active'));
31 tabsContent.forEach(el => el.classList.remove('tab-content-active'));
32 const link = tabs.querySelector('a[data-tab="' + tab + '"]');
33 link.classList.add('nav-tab-active');
34 const cont = document.querySelector('[data-content=' + tab + ']');
35 cont.classList.add('tab-content-active');
36 }
37
38 function setLocalStorage(attr) {
39 sessionStorage.setItem("wowpTab", attr);
40 }
41
42 function getLocalStorage() {
43 const tab = sessionStorage.getItem("wowpTab");
44 if (!tab) {
45 return ''
46 } else {
47 return tab;
48 }
49 }
50 }
51
52 const codeEditor = function () {
53 const editorSettings = wp.codeEditor.defaultSettings ? _.clone(wp.codeEditor.defaultSettings) : {};
54 const codemirror_gen =
55 {
56 "indentUnit": 2,
57 "indentWithTabs": true,
58 "inputStyle": "contenteditable",
59 "lineNumbers": true,
60 "lineWrapping": true,
61 "styleActiveLine": true,
62 "continueComments": true,
63 "extraKeys": {
64 "Ctrl-Space": "autocomplete",
65 "Ctrl-\/": "toggleComment",
66 "Cmd-\/": "toggleComment",
67 "Alt-F": "findPersistent",
68 "Ctrl-F": "findPersistent",
69 "Cmd-F": "findPersistent"
70 },
71 "direction": "ltr",
72 "gutters": ["CodeMirror-lint-markers"],
73 "lint": true,
74 "autoCloseBrackets": true,
75 "autoCloseTags": true,
76 "matchTags": {
77 "bothTags": true
78 },
79 "tabSize": 2,
80
81 };
82
83 const html_code = document.getElementById('html_code');
84 if (html_code) {
85 let codemirror_el =
86 {
87 "tagname-lowercase": true,
88 "attr-lowercase": true,
89 "attr-value-double-quotes": false,
90 "doctype-first": false,
91 "tag-pair": true,
92 "spec-char-escape": true,
93 "id-unique": true,
94 "src-not-empty": true,
95 "attr-no-duplication": true,
96 "alt-require": true,
97 "space-tab-mixed-disabled": "tab",
98 "attr-unsafe-chars": true,
99 "mode": 'htmlmixed',
100
101 };
102
103 editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,);
104
105 const htmleditor = wp.codeEditor.initialize(html_code, editorSettings);
106
107 const htmlNavigationMenu = document.getElementById("htmlNavigationMenu");
108
109 htmleditor.codemirror.eachLine(function (line) {
110 const text = line.text;
111 if (text.startsWith("<!-- NAV:")) {
112 let navItem = document.createElement("li");
113 let navLink = document.createElement("a");
114 navLink.href = "#";
115 // navLink.innerText = text.replace("<!-- NAV:", "").trim();
116 navLink.innerText = text.replace(/<!-- NAV:|-->|<!-- NAV: -->/g, '').trim();
117 navLink.addEventListener("click", function (e) {
118 e.preventDefault();
119
120 // Remove existing highlights from the gutter
121 htmleditor.codemirror.eachLine(function (l) {
122 htmleditor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter');
123 });
124
125 // Set cursor and scroll to line
126 htmleditor.codemirror.setCursor(line.lineNo());
127 let heightOfLine = htmleditor.codemirror.defaultTextHeight();
128 let verticalPos = line.lineNo() * heightOfLine;
129 htmleditor.codemirror.scrollTo(null, verticalPos);
130
131 // Highlight the line number in the gutter
132 htmleditor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter');
133
134 htmleditor.codemirror.focus();
135 });
136 navItem.appendChild(navLink);
137 htmlNavigationMenu.appendChild(navItem);
138 }
139 });
140 }
141
142 const css_code = document.getElementById('css_code');
143 if (css_code) {
144 let codemirror_el = {"mode": 'css',};
145 editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,);
146 // const tab = document.querySelector('[data-content="css-code"]');
147 // tab.classList.add('tab-content-active');
148 const csseditor = wp.codeEditor.initialize(css_code, editorSettings);
149 // tab.classList.remove('tab-content-active');
150
151 const cssNavigationMenu = document.getElementById("cssNavigationMenu");
152
153 csseditor.codemirror.eachLine(function (line) {
154 const text = line.text;
155 if (text.startsWith("/* NAV:")) {
156 let navItem = document.createElement("li");
157 let navLink = document.createElement("a");
158 navLink.href = "#";
159 // navLink.innerText = text.replace("// NAV:", "").trim();
160 navLink.innerText = text.replace(/\/\* NAV:|\*\/|\/\* NAV: \*\//g, '').trim();
161 navLink.addEventListener("click", function (e) {
162 e.preventDefault();
163
164 // Remove existing highlights from the gutter
165 csseditor.codemirror.eachLine(function (l) {
166 csseditor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter');
167 });
168
169 // Set cursor and scroll to line
170 csseditor.codemirror.setCursor(line.lineNo());
171 let heightOfLine = csseditor.codemirror.defaultTextHeight();
172 let verticalPos = line.lineNo() * heightOfLine;
173 csseditor.codemirror.scrollTo(null, verticalPos);
174
175 // Highlight the line number in the gutter
176 csseditor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter');
177
178 csseditor.codemirror.focus();
179 });
180 navItem.appendChild(navLink);
181 cssNavigationMenu.appendChild(navItem);
182 }
183 });
184 }
185
186 const js_code = document.getElementById('js_code');
187 if (js_code) {
188 let codemirror_el = {
189 "boss": true,
190 "curly": true,
191 "eqeqeq": true,
192 "eqnull": true,
193 "es3": true,
194 "expr": true,
195 "immed": true,
196 "noarg": true,
197 "nonbsp": true,
198 "onevar": true,
199 "quotmark": "single",
200 "trailing": true,
201 "undef": true,
202 "unused": true,
203 "browser": true,
204 "globals": {
205 "_": false,
206 "Backbone": false,
207 "jQuery": true,
208 "JSON": false,
209 "wp": false
210 },
211 "mode": 'javascript',
212 };
213 editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,);
214 // const tab = document.querySelector('[data-content="js-code"]');
215 // tab.classList.add('tab-content-active');
216 const jseditor = wp.codeEditor.initialize(js_code, editorSettings);
217 // tab.classList.remove('tab-content-active');
218
219 const jsNavigationMenu = document.getElementById("jsNavigationMenu");
220
221 jseditor.codemirror.eachLine(function (line) {
222 const text = line.text;
223 if (text.startsWith("// NAV:")) {
224 let navItem = document.createElement("li");
225 let navLink = document.createElement("a");
226 navLink.href = "#";
227 navLink.innerText = text.replace("// NAV:", "").trim();
228 navLink.addEventListener("click", function (e) {
229 e.preventDefault();
230
231 // Remove existing highlights from the gutter
232 jseditor.codemirror.eachLine(function (l) {
233 jseditor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter');
234 });
235
236 // Set cursor and scroll to line
237 jseditor.codemirror.setCursor(line.lineNo());
238 let heightOfLine = jseditor.codemirror.defaultTextHeight();
239 let verticalPos = line.lineNo() * heightOfLine;
240 jseditor.codemirror.scrollTo(null, verticalPos);
241
242 // Highlight the line number in the gutter
243 jseditor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter');
244
245 jseditor.codemirror.focus();
246 });
247 navItem.appendChild(navLink);
248 jsNavigationMenu.appendChild(navItem);
249 }
250 });
251 }
252
253 const php_code = document.getElementById('php_code');
254 if (php_code) {
255 let codemirror_el = {
256 firstLineNumber: 4,
257 mode: {
258 name: 'php',
259 startOpen: true
260 }
261 };
262 editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,);
263 // const tab = document.querySelector('[data-content="php-code"]');
264 // tab.classList.add('tab-content-active');
265 const phpeditor = wp.codeEditor.initialize(php_code, editorSettings);
266 // tab.classList.remove('tab-content-active');
267
268 const phpNavigationMenu = document.getElementById("phpNavigationMenu");
269
270 phpeditor.codemirror.eachLine(function (line) {
271 const text = line.text;
272 if (text.startsWith("// NAV:")) {
273 let navItem = document.createElement("li");
274 let navLink = document.createElement("a");
275 navLink.href = "#";
276 navLink.innerText = text.replace("// NAV:", "").trim();
277 navLink.addEventListener("click", function (e) {
278 e.preventDefault();
279
280 // Remove existing highlights from the gutter
281 phpeditor.codemirror.eachLine(function (l) {
282 phpeditor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter');
283 });
284
285 // Set cursor and scroll to line
286 phpeditor.codemirror.setCursor(line.lineNo());
287 let heightOfLine = phpeditor.codemirror.defaultTextHeight();
288 let verticalPos = line.lineNo() * heightOfLine;
289 phpeditor.codemirror.scrollTo(null, verticalPos);
290
291 // Highlight the line number in the gutter
292 phpeditor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter');
293
294 phpeditor.codemirror.focus();
295 });
296 navItem.appendChild(navLink);
297 phpNavigationMenu.appendChild(navItem);
298 }
299 });
300 }
301
302 const global_php = document.getElementById('wpcoder-global-php');
303 if (global_php) {
304 let codemirror_el = {
305 mode: {
306 name: 'php',
307 startOpen: true,
308
309 },
310 firstLineNumber: 4,
311 };
312 editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,);
313 const editor = wp.codeEditor.initialize(global_php, editorSettings);
314
315 const navigationMenu = document.getElementById("phpNavigationMenu");
316
317 editor.codemirror.eachLine(function (line) {
318 const text = line.text;
319 if (text.startsWith("// NAV:")) {
320 let navItem = document.createElement("li");
321 let navLink = document.createElement("a");
322 navLink.href = "#";
323 navLink.innerText = text.replace("// NAV:", "").trim();
324 navLink.addEventListener("click", function (e) {
325 e.preventDefault();
326
327 // Remove existing highlights from the gutter
328 editor.codemirror.eachLine(function (l) {
329 editor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter');
330 });
331
332 // Set cursor and scroll to line
333 editor.codemirror.setCursor(line.lineNo());
334 let heightOfLine = editor.codemirror.defaultTextHeight();
335 let verticalPos = line.lineNo() * heightOfLine;
336 editor.codemirror.scrollTo(null, verticalPos);
337
338 // Highlight the line number in the gutter
339 editor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter');
340
341 editor.codemirror.focus();
342 });
343 navItem.appendChild(navLink);
344 navigationMenu.appendChild(navItem);
345 }
346 });
347
348
349 }
350
351 }
352
353
354 const changeTemplate = function () {
355
356 const template = document.getElementById('template');
357 if (!template) {
358 return false;
359 }
360
361
362 template.addEventListener('change', function (event) {
363
364 const parent = document.getElementById('wpcoder-template');
365 let type = template.value;
366 type = default_custom_post(type);
367 const elements = parent.querySelectorAll('.wowp-field');
368
369 switch (type) {
370 case 'post_all':
371 case 'page_all':
372 elements[1].classList.add('is-hidden');
373 elements[2].classList.add('is-hidden');
374 elements[3].classList.add('is-hidden');
375 break;
376 case 'post_selected':
377 case 'post_in_category':
378 case 'page_selected':
379 case 'archive_category':
380 case 'archive_tag':
381 elements[1].classList.remove('is-hidden');
382 elements[2].classList.remove('is-hidden');
383 elements[3].classList.add('is-hidden');
384 break;
385 case 'page_type':
386 elements[1].classList.remove('is-hidden');
387 elements[2].classList.add('is-hidden');
388 elements[3].classList.remove('is-hidden');
389 break;
390 default:
391 elements[1].classList.add('is-hidden');
392 elements[2].classList.add('is-hidden');
393 elements[3].classList.add('is-hidden');
394 break;
395
396 }
397
398 });
399
400 function default_custom_post(el) {
401 if (el.includes('custom_post_selected')) {
402 return 'post_selected';
403 }
404 if (el.includes('custom_post_tax')) {
405 return 'post_category';
406 }
407 if (el.includes('custom_post_all')) {
408 return 'post_all';
409 }
410 return el;
411 }
412
413 }
414
415
416 const feature = function () {
417 const texts = document.querySelectorAll('.w_card-description');
418
419 if (!texts) {
420 return false;
421 }
422 texts.forEach((el) => {
423 el.addEventListener('click', () => {
424 el.classList.toggle('is-open');
425 })
426 })
427
428 }
429
430 document.addEventListener('DOMContentLoaded', function () {
431
432 new codeEditor;
433 new settingTabs;
434 new changeTemplate;
435 new feature;
436 })