| @@ -1,55 +1,7 @@ | ||
| 1 | 1 | 'use strict'; |
| 2 | 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 | 3 | |
| 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 | 4 | const codeEditor = function () { |
| 53 | 5 | const editorSettings = wp.codeEditor.defaultSettings ? _.clone(wp.codeEditor.defaultSettings) : {}; |
| 54 | 6 | const codemirror_gen = |
| 55 | 7 | { |
| @@ -57,8 +9,9 @@ | ||
| 57 | 9 | "indentWithTabs": true, |
| 58 | 10 | "inputStyle": "contenteditable", |
| 59 | 11 | "lineNumbers": true, |
| 60 | 12 | "lineWrapping": true, |
| 13 | + "matchBrackets": true, | |
| 61 | 14 | "styleActiveLine": true, |
| 62 | 15 | "continueComments": true, |
| 63 | 16 | "extraKeys": { |
| 64 | 17 | "Ctrl-Space": "autocomplete", |
| @@ -80,8 +33,9 @@ | ||
| 80 | 33 | |
| 81 | 34 | }; |
| 82 | 35 | |
| 83 | 36 | const html_code = document.getElementById('html_code'); |
| 37 | + let htmleditor; | |
| 84 | 38 | if (html_code) { |
| 85 | 39 | let codemirror_el = |
| 86 | 40 | { |
| 87 | 41 | "tagname-lowercase": true, |
| @@ -101,22 +55,93 @@ | ||
| 101 | 55 | }; |
| 102 | 56 | |
| 103 | 57 | editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,); |
| 104 | 58 | |
| 105 | - wp.codeEditor.initialize(html_code, editorSettings); | |
| 59 | + htmleditor = wp.codeEditor.initialize(html_code, editorSettings); | |
| 60 | + | |
| 61 | + | |
| 62 | + const htmlNavigationMenu = document.getElementById("htmlNavigationMenu"); | |
| 63 | + | |
| 64 | + htmleditor.codemirror.eachLine(function (line) { | |
| 65 | + const text = line.text; | |
| 66 | + if (text.startsWith("<!-- NAV:")) { | |
| 67 | + let navItem = document.createElement("li"); | |
| 68 | + let navLink = document.createElement("a"); | |
| 69 | + navLink.href = "#"; | |
| 70 | + // navLink.innerText = text.replace("<!-- NAV:", "").trim(); | |
| 71 | + navLink.innerText = text.replace(/<!-- NAV:|-->|<!-- NAV: -->/g, '').trim(); | |
| 72 | + navLink.addEventListener("click", function (e) { | |
| 73 | + e.preventDefault(); | |
| 74 | + | |
| 75 | + // Remove existing highlights from the gutter | |
| 76 | + htmleditor.codemirror.eachLine(function (l) { | |
| 77 | + htmleditor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter'); | |
| 78 | + }); | |
| 79 | + | |
| 80 | + // Set cursor and scroll to line | |
| 81 | + htmleditor.codemirror.setCursor(line.lineNo()); | |
| 82 | + let heightOfLine = htmleditor.codemirror.defaultTextHeight(); | |
| 83 | + let verticalPos = line.lineNo() * heightOfLine; | |
| 84 | + htmleditor.codemirror.scrollTo(null, verticalPos); | |
| 85 | + | |
| 86 | + // Highlight the line number in the gutter | |
| 87 | + htmleditor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter'); | |
| 88 | + | |
| 89 | + htmleditor.codemirror.focus(); | |
| 90 | + }); | |
| 91 | + navItem.appendChild(navLink); | |
| 92 | + htmlNavigationMenu.appendChild(navItem); | |
| 93 | + } | |
| 94 | + }); | |
| 106 | 95 | } |
| 107 | 96 | |
| 108 | 97 | const css_code = document.getElementById('css_code'); |
| 98 | + let csseditor; | |
| 109 | 99 | if (css_code) { |
| 110 | 100 | let codemirror_el = {"mode": 'css',}; |
| 111 | 101 | editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,); |
| 112 | - const tab = document.querySelector('[data-content="css-code"]'); | |
| 113 | - tab.classList.add('tab-content-active'); | |
| 114 | - wp.codeEditor.initialize(css_code, editorSettings); | |
| 115 | - tab.classList.remove('tab-content-active'); | |
| 102 | + // const tab = document.querySelector('[data-content="css-code"]'); | |
| 103 | + // tab.classList.add('tab-content-active'); | |
| 104 | + csseditor = wp.codeEditor.initialize(css_code, editorSettings); | |
| 105 | + // tab.classList.remove('tab-content-active'); | |
| 106 | + | |
| 107 | + const cssNavigationMenu = document.getElementById("cssNavigationMenu"); | |
| 108 | + | |
| 109 | + csseditor.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 | + csseditor.codemirror.eachLine(function (l) { | |
| 122 | + csseditor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter'); | |
| 123 | + }); | |
| 124 | + | |
| 125 | + // Set cursor and scroll to line | |
| 126 | + csseditor.codemirror.setCursor(line.lineNo()); | |
| 127 | + let heightOfLine = csseditor.codemirror.defaultTextHeight(); | |
| 128 | + let verticalPos = line.lineNo() * heightOfLine; | |
| 129 | + csseditor.codemirror.scrollTo(null, verticalPos); | |
| 130 | + | |
| 131 | + // Highlight the line number in the gutter | |
| 132 | + csseditor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter'); | |
| 133 | + | |
| 134 | + csseditor.codemirror.focus(); | |
| 135 | + }); | |
| 136 | + navItem.appendChild(navLink); | |
| 137 | + cssNavigationMenu.appendChild(navItem); | |
| 138 | + } | |
| 139 | + }); | |
| 116 | 140 | } |
| 117 | 141 | |
| 118 | 142 | const js_code = document.getElementById('js_code'); |
| 143 | + let jseditor; | |
| 119 | 144 | if (js_code) { |
| 120 | 145 | let codemirror_el = { |
| 121 | 146 | "boss": true, |
| 122 | 147 | "curly": true, |
| @@ -142,262 +167,300 @@ | ||
| 142 | 167 | }, |
| 143 | 168 | "mode": 'javascript', |
| 144 | 169 | }; |
| 145 | 170 | editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,); |
| 146 | - const tab = document.querySelector('[data-content="js-code"]'); | |
| 147 | - tab.classList.add('tab-content-active'); | |
| 148 | - wp.codeEditor.initialize(js_code, editorSettings); | |
| 149 | - tab.classList.remove('tab-content-active'); | |
| 171 | + // const tab = document.querySelector('[data-content="js-code"]'); | |
| 172 | + // tab.classList.add('tab-content-active'); | |
| 173 | + jseditor = wp.codeEditor.initialize(js_code, editorSettings); | |
| 174 | + // tab.classList.remove('tab-content-active'); | |
| 175 | + | |
| 176 | + const jsNavigationMenu = document.getElementById("jsNavigationMenu"); | |
| 177 | + | |
| 178 | + jseditor.codemirror.eachLine(function (line) { | |
| 179 | + const text = line.text; | |
| 180 | + if (text.startsWith("// NAV:")) { | |
| 181 | + let navItem = document.createElement("li"); | |
| 182 | + let navLink = document.createElement("a"); | |
| 183 | + navLink.href = "#"; | |
| 184 | + navLink.innerText = text.replace("// NAV:", "").trim(); | |
| 185 | + navLink.addEventListener("click", function (e) { | |
| 186 | + e.preventDefault(); | |
| 187 | + | |
| 188 | + // Remove existing highlights from the gutter | |
| 189 | + jseditor.codemirror.eachLine(function (l) { | |
| 190 | + jseditor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter'); | |
| 191 | + }); | |
| 192 | + | |
| 193 | + // Set cursor and scroll to line | |
| 194 | + jseditor.codemirror.setCursor(line.lineNo()); | |
| 195 | + let heightOfLine = jseditor.codemirror.defaultTextHeight(); | |
| 196 | + let verticalPos = line.lineNo() * heightOfLine; | |
| 197 | + jseditor.codemirror.scrollTo(null, verticalPos); | |
| 198 | + | |
| 199 | + // Highlight the line number in the gutter | |
| 200 | + jseditor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter'); | |
| 201 | + | |
| 202 | + jseditor.codemirror.focus(); | |
| 203 | + }); | |
| 204 | + navItem.appendChild(navLink); | |
| 205 | + jsNavigationMenu.appendChild(navItem); | |
| 206 | + } | |
| 207 | + }); | |
| 150 | 208 | } |
| 151 | 209 | |
| 152 | -} | |
| 210 | + const php_code = document.getElementById('php_code'); | |
| 211 | + if (php_code) { | |
| 212 | + let codemirror_el = { | |
| 213 | + firstLineNumber: 4, | |
| 214 | + mode: { | |
| 215 | + name: 'php', | |
| 216 | + startOpen: true | |
| 217 | + } | |
| 218 | + }; | |
| 219 | + editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,); | |
| 220 | + // const tab = document.querySelector('[data-content="php-code"]'); | |
| 221 | + // tab.classList.add('tab-content-active'); | |
| 222 | + const phpeditor = wp.codeEditor.initialize(php_code, editorSettings); | |
| 223 | + // tab.classList.remove('tab-content-active'); | |
| 153 | 224 | |
| 154 | -const cloneIncludes = function () { | |
| 225 | + const phpNavigationMenu = document.getElementById("phpNavigationMenu"); | |
| 155 | 226 | |
| 156 | - const template = document.getElementById('clone-includes'); | |
| 157 | - if (!template) { | |
| 158 | - return false; | |
| 159 | - } | |
| 227 | + phpeditor.codemirror.eachLine(function (line) { | |
| 228 | + const text = line.text; | |
| 229 | + if (text.startsWith("// NAV:")) { | |
| 230 | + let navItem = document.createElement("li"); | |
| 231 | + let navLink = document.createElement("a"); | |
| 232 | + navLink.href = "#"; | |
| 233 | + navLink.innerText = text.replace("// NAV:", "").trim(); | |
| 234 | + navLink.addEventListener("click", function (e) { | |
| 235 | + e.preventDefault(); | |
| 160 | 236 | |
| 161 | - const addBtn = document.getElementById('add-include'); | |
| 162 | - if (!addBtn) { | |
| 163 | - return false; | |
| 164 | - } | |
| 237 | + // Remove existing highlights from the gutter | |
| 238 | + phpeditor.codemirror.eachLine(function (l) { | |
| 239 | + phpeditor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter'); | |
| 240 | + }); | |
| 165 | 241 | |
| 166 | - addBtn.addEventListener('click', (event) => { | |
| 167 | - event.preventDefault(); | |
| 168 | - const clone = template.content.cloneNode(true); | |
| 169 | - const targetElement = document.getElementById('includes-files'); | |
| 170 | - targetElement.appendChild(clone); | |
| 171 | - }); | |
| 242 | + // Set cursor and scroll to line | |
| 243 | + phpeditor.codemirror.setCursor(line.lineNo()); | |
| 244 | + let heightOfLine = phpeditor.codemirror.defaultTextHeight(); | |
| 245 | + let verticalPos = line.lineNo() * heightOfLine; | |
| 246 | + phpeditor.codemirror.scrollTo(null, verticalPos); | |
| 172 | 247 | |
| 173 | -} | |
| 248 | + // Highlight the line number in the gutter | |
| 249 | + phpeditor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter'); | |
| 174 | 250 | |
| 175 | -const removeLast = function () { | |
| 176 | - const btn = document.getElementById('remove-include'); | |
| 177 | - if (!btn) { | |
| 178 | - return false; | |
| 251 | + phpeditor.codemirror.focus(); | |
| 252 | + }); | |
| 253 | + navItem.appendChild(navLink); | |
| 254 | + phpNavigationMenu.appendChild(navItem); | |
| 255 | + } | |
| 256 | + }); | |
| 179 | 257 | } |
| 180 | 258 | |
| 181 | - btn.addEventListener('click', (event) => { | |
| 182 | - event.preventDefault(); | |
| 183 | - const parentElement = document.getElementById('includes-files'); | |
| 184 | - const lastChild = parentElement.querySelector('.wowp-fields-group:last-child'); | |
| 185 | - if (lastChild) { | |
| 186 | - lastChild.remove(); | |
| 187 | - } | |
| 188 | - }); | |
| 259 | + const global_php = document.getElementById('wpcoder-global-php'); | |
| 260 | + if (global_php) { | |
| 261 | + let codemirror_el = { | |
| 262 | + mode: { | |
| 263 | + name: 'php', | |
| 264 | + startOpen: true, | |
| 189 | 265 | |
| 266 | + }, | |
| 267 | + firstLineNumber: 4, | |
| 268 | + }; | |
| 269 | + editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,); | |
| 270 | + const editor = wp.codeEditor.initialize(global_php, editorSettings); | |
| 190 | 271 | |
| 191 | -} | |
| 272 | + const navigationMenu = document.getElementById("phpNavigationMenu"); | |
| 192 | 273 | |
| 193 | -const changeIncludes = function () { | |
| 194 | - const parentContainer = document.getElementById('includes-files'); | |
| 195 | - if (!parentContainer) { | |
| 196 | - return false; | |
| 197 | - } | |
| 274 | + editor.codemirror.eachLine(function (line) { | |
| 275 | + const text = line.text; | |
| 276 | + if (text.startsWith("// NAV:")) { | |
| 277 | + let navItem = document.createElement("li"); | |
| 278 | + let navLink = document.createElement("a"); | |
| 279 | + navLink.href = "#"; | |
| 280 | + navLink.innerText = text.replace("// NAV:", "").trim(); | |
| 281 | + navLink.addEventListener("click", function (e) { | |
| 282 | + e.preventDefault(); | |
| 198 | 283 | |
| 199 | - parentContainer.addEventListener('change', function (event) { | |
| 284 | + // Remove existing highlights from the gutter | |
| 285 | + editor.codemirror.eachLine(function (l) { | |
| 286 | + editor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter'); | |
| 287 | + }); | |
| 200 | 288 | |
| 201 | - // Check if the clicked element matches the desired selector | |
| 202 | - if (event.target.matches('[name^="param[include]"]')) { | |
| 203 | - const el = event.target.value; | |
| 204 | - const parent = event.target.closest('.wowp-fields-group'); | |
| 205 | - const el_js = parent.querySelector('.wowp-include-js'); | |
| 206 | - el_js.classList.toggle('is-hidden'); | |
| 207 | - } | |
| 208 | - }); | |
| 289 | + // Set cursor and scroll to line | |
| 290 | + editor.codemirror.setCursor(line.lineNo()); | |
| 291 | + let heightOfLine = editor.codemirror.defaultTextHeight(); | |
| 292 | + let verticalPos = line.lineNo() * heightOfLine; | |
| 293 | + editor.codemirror.scrollTo(null, verticalPos); | |
| 209 | 294 | |
| 210 | -} | |
| 295 | + // Highlight the line number in the gutter | |
| 296 | + editor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter'); | |
| 211 | 297 | |
| 212 | -const cloneDisplayRules = function () { | |
| 298 | + editor.codemirror.focus(); | |
| 299 | + }); | |
| 300 | + navItem.appendChild(navLink); | |
| 301 | + navigationMenu.appendChild(navItem); | |
| 302 | + } | |
| 303 | + }); | |
| 213 | 304 | |
| 214 | - const template = document.getElementById('clone-display-rules'); | |
| 215 | - if (!template) { | |
| 216 | - return false; | |
| 217 | - } | |
| 218 | 305 | |
| 219 | - const addBtn = document.getElementById('add-display'); | |
| 220 | - if (!addBtn) { | |
| 221 | - return false; | |
| 222 | 306 | } |
| 223 | 307 | |
| 224 | - addBtn.addEventListener('click', (event) => { | |
| 225 | - event.preventDefault(); | |
| 226 | - const clone = template.content.cloneNode(true); | |
| 227 | - const container = document.getElementById('display-rules'); | |
| 228 | - const targetElement = document.querySelector('#display-rules .wowp-btn-actions'); | |
| 229 | - container.insertBefore(clone, targetElement); | |
| 230 | - }); | |
| 231 | 308 | |
| 232 | -} | |
| 309 | + // Live Preview | |
| 310 | + let timeout; | |
| 311 | + const previewCheckbox = document.getElementById('checkbox_preview'); | |
| 233 | 312 | |
| 234 | -const removeDisplayRules = function () { | |
| 235 | - const btn = document.getElementById('remove-display'); | |
| 236 | - if (!btn) { | |
| 237 | - return false; | |
| 313 | + function initPreviewListener() { | |
| 314 | + document.querySelectorAll('.wowp-settings__page-content').forEach(editor => { | |
| 315 | + editor.addEventListener('keydown', () => { | |
| 316 | + clearTimeout(timeout); | |
| 317 | + if (previewCheckbox.checked) { | |
| 318 | + timeout = setTimeout(updatePreview, 500); | |
| 319 | + } | |
| 320 | + }); | |
| 321 | + }); | |
| 238 | 322 | } |
| 239 | 323 | |
| 240 | - btn.addEventListener('click', (event) => { | |
| 241 | - event.preventDefault(); | |
| 242 | - const parentElement = document.getElementById('display-rules'); | |
| 243 | - const lastChild = parentElement.querySelector('.wowp-fields-group:not(:first-child):nth-last-child(2)'); | |
| 244 | - if (lastChild) { | |
| 245 | - lastChild.remove(); | |
| 246 | - } | |
| 247 | - }); | |
| 248 | 324 | |
| 249 | -} | |
| 325 | + if (previewCheckbox) { | |
| 326 | + updatePreview(); | |
| 250 | 327 | |
| 251 | -const changeDisplayRules = function () { | |
| 252 | - const parentContainer = document.getElementById('display-rules'); | |
| 253 | - if (!parentContainer) { | |
| 254 | - return false; | |
| 328 | + previewCheckbox.addEventListener('change', () => { | |
| 329 | + if (previewCheckbox.checked) { | |
| 330 | + updatePreview(); | |
| 331 | + } | |
| 332 | + }); | |
| 255 | 333 | } |
| 256 | 334 | |
| 257 | - parentContainer.addEventListener('change', function (event) { | |
| 335 | + initPreviewListener(); | |
| 258 | 336 | |
| 259 | - // Check if the clicked element matches the desired selector | |
| 260 | - if (event.target.matches('[name^="param[show]"]')) { | |
| 261 | - let el = event.target.value; | |
| 262 | - el = default_custom_post(el); | |
| 263 | - const parent = event.target.closest('.wowp-fields-group'); | |
| 264 | - const elements = parent.querySelectorAll('.wowp-field'); | |
| 265 | - switch (el) { | |
| 266 | - case 'shortcode': | |
| 267 | - case 'everywhere': | |
| 268 | - case 'post_all': | |
| 269 | - case 'page_all': | |
| 270 | - elements[1].classList.add('is-hidden'); | |
| 271 | - elements[2].classList.add('is-hidden'); | |
| 272 | - elements[3].classList.add('is-hidden'); | |
| 273 | - break; | |
| 274 | - case 'post_selected': | |
| 275 | - case 'post_category': | |
| 276 | - case 'page_selected': | |
| 277 | - elements[1].classList.remove('is-hidden'); | |
| 278 | - elements[2].classList.remove('is-hidden'); | |
| 279 | - elements[3].classList.add('is-hidden'); | |
| 280 | - break; | |
| 281 | - case 'page_type': | |
| 282 | - elements[1].classList.remove('is-hidden'); | |
| 283 | - elements[2].classList.add('is-hidden'); | |
| 284 | - elements[3].classList.remove('is-hidden'); | |
| 285 | - break; | |
| 337 | + function updatePreview() { | |
| 338 | + const html = htmleditor.codemirror.getValue(); | |
| 339 | + const css = csseditor.codemirror.getValue(); | |
| 286 | 340 | |
| 341 | + let styleLinks = []; | |
| 342 | + | |
| 343 | + document.querySelectorAll('input[id*="include_file_"]').forEach(input => { | |
| 344 | + const url = input.value.trim(); | |
| 345 | + | |
| 346 | + if (url.endsWith('.css')) { | |
| 347 | + styleLinks.push(`<link rel="stylesheet" href="${url}">`); | |
| 287 | 348 | } |
| 349 | + }); | |
| 288 | 350 | |
| 289 | - } | |
| 351 | + const iframe = document.getElementById('wowp-preview'); | |
| 352 | + iframe.setAttribute("srcdoc", ` | |
| 353 | + <!DOCTYPE html> | |
| 354 | + <html> | |
| 355 | + <head> | |
| 356 | + <style>body{margin:0;padding:0;box-sizing:border-box;}</style> | |
| 357 | + ${styleLinks.join('\n')} | |
| 358 | + <style>${css}</style> | |
| 359 | + </head> | |
| 360 | + <body>${html}</body> | |
| 361 | + </html> | |
| 362 | + `); | |
| 363 | + } | |
| 364 | + | |
| 365 | + jQuery('.wowp-preview__dot.is-toggle').on('click', function () { | |
| 366 | + jQuery(this).toggleClass('is-active'); | |
| 367 | + jQuery(this).find('.dashicons').toggleClass('is-hidden'); | |
| 368 | + jQuery('.wowp-preview__iframe #wowp-preview').toggleClass('is-hidden'); | |
| 369 | + jQuery('.wowp-preview__iframe').toggleClass('is-toggled'); | |
| 290 | 370 | }); |
| 291 | 371 | |
| 292 | - function default_custom_post(el) { | |
| 293 | - if (el.includes('custom_post_selected')) { | |
| 294 | - return 'post_selected'; | |
| 295 | - } | |
| 296 | - if (el.includes('custom_post_tax')) { | |
| 297 | - return 'post_category'; | |
| 298 | - } | |
| 299 | - if (el.includes('custom_post_all')) { | |
| 300 | - return 'post_all'; | |
| 301 | - } | |
| 302 | - return el; | |
| 303 | - } | |
| 372 | + jQuery('.wowp-preview__dot.is-reset').on('click', function () { | |
| 373 | + jQuery('.wowp-preview__iframe').removeAttr('style'); | |
| 374 | + updatePreview(); | |
| 375 | + }); | |
| 304 | 376 | |
| 305 | -} | |
| 377 | + const iframe = jQuery('.wowp-preview__iframe'); | |
| 378 | + const $size = iframe.find('.wowp-preview__size'); | |
| 379 | + // let timeout; | |
| 306 | 380 | |
| 307 | -const userRoles = function () { | |
| 308 | - const user = document.getElementById('item_user'); | |
| 309 | - if (!user) { | |
| 310 | - return false; | |
| 311 | - } | |
| 312 | - const parent = user.closest('fieldset'); | |
| 381 | + if (iframe.length && $size.length) { | |
| 382 | + const observer = new ResizeObserver(entries => { | |
| 383 | + for (const entry of entries) { | |
| 384 | + const width = Math.round(entry.contentRect.width); | |
| 385 | + const height = Math.round(entry.contentRect.height) - 50; | |
| 313 | 386 | |
| 314 | - user.addEventListener('change', (el) => { | |
| 315 | - const role = user.value; | |
| 316 | - const roles = parent.querySelector('.wowp-users-roles'); | |
| 317 | - if (parseInt(role) === 2) { | |
| 318 | - roles.classList.remove('is-hidden'); | |
| 319 | - } else { | |
| 320 | - roles.classList.add('is-hidden'); | |
| 321 | - } | |
| 322 | - }) | |
| 387 | + $size.text(`${width}px × ${height}px`).fadeIn(150); | |
| 323 | 388 | |
| 324 | - const checkboxes = parent.querySelectorAll('input[type="checkbox"]'); | |
| 325 | - const role_all = document.getElementById('user_role_all'); | |
| 389 | + clearTimeout(timeout); | |
| 390 | + // timeout = setTimeout(() => $size.fadeOut(300), 3000); | |
| 391 | + } | |
| 392 | + }); | |
| 326 | 393 | |
| 327 | - role_all.addEventListener('change', () => { | |
| 328 | - if (role_all.checked) { | |
| 329 | - checkboxes.forEach(el => el.checked = true); | |
| 330 | - } else { | |
| 331 | - checkboxes.forEach(el => el.checked = false); | |
| 332 | - } | |
| 333 | - }); | |
| 334 | -} | |
| 335 | - | |
| 336 | -const languages = function () { | |
| 337 | - const depending = document.getElementById('depending_language'); | |
| 338 | - if (!depending) { | |
| 339 | - return false; | |
| 394 | + observer.observe(iframe[0]); | |
| 340 | 395 | } |
| 341 | - const parent = depending.closest('fieldset'); | |
| 342 | 396 | |
| 343 | - depending.addEventListener('change', () => { | |
| 344 | - if (depending.checked) { | |
| 345 | - parent.querySelector('.wowp-languages').classList.remove('is-hidden'); | |
| 346 | - } else { | |
| 347 | - parent.querySelector('.wowp-languages').classList.add('is-hidden'); | |
| 348 | - } | |
| 349 | - }); | |
| 350 | 397 | } |
| 351 | 398 | |
| 352 | -const schedule = function () { | |
| 353 | - const schedule = document.getElementById('schedule'); | |
| 354 | 399 | |
| 355 | - if (!schedule) { | |
| 400 | +const changeTemplate = function () { | |
| 401 | + | |
| 402 | + const template = document.getElementById('template'); | |
| 403 | + if (!template) { | |
| 356 | 404 | return false; |
| 357 | 405 | } |
| 358 | 406 | |
| 359 | - const template = document.getElementById('clone-schedule'); | |
| 360 | - const addBtn = document.getElementById('add-schedule'); | |
| 361 | - const removeBtn = document.getElementById('remove-schedule'); | |
| 362 | 407 | |
| 363 | - addBtn.addEventListener('click', (event) => { | |
| 364 | - event.preventDefault(); | |
| 365 | - const clone = template.content.cloneNode(true); | |
| 366 | - const container = document.getElementById('schedule'); | |
| 367 | - const targetElement = document.querySelector('#schedule .wowp-btn-actions'); | |
| 368 | - container.insertBefore(clone, targetElement); | |
| 369 | - }); | |
| 408 | + template.addEventListener('change', function (event) { | |
| 370 | 409 | |
| 371 | - removeBtn.addEventListener('click', (event) => { | |
| 372 | - event.preventDefault(); | |
| 373 | - const parentElement = document.getElementById('schedule'); | |
| 374 | - const lastChild = parentElement.querySelector('.wowp-fields-group:nth-last-child(2)'); | |
| 375 | - if (lastChild) { | |
| 376 | - lastChild.remove(); | |
| 410 | + const parent = document.getElementById('wpcoder-template'); | |
| 411 | + let type = template.value; | |
| 412 | + type = default_custom_post(type); | |
| 413 | + const elements = parent.querySelectorAll('.wowp-field'); | |
| 414 | + | |
| 415 | + switch (type) { | |
| 416 | + case 'post_all': | |
| 417 | + case 'page_all': | |
| 418 | + elements[1].classList.add('is-hidden'); | |
| 419 | + elements[2].classList.add('is-hidden'); | |
| 420 | + elements[3].classList.add('is-hidden'); | |
| 421 | + break; | |
| 422 | + case 'post_selected': | |
| 423 | + case 'post_in_category': | |
| 424 | + case 'page_selected': | |
| 425 | + case 'archive_category': | |
| 426 | + case 'archive_tag': | |
| 427 | + case 'archive_author': | |
| 428 | + elements[1].classList.remove('is-hidden'); | |
| 429 | + elements[2].classList.remove('is-hidden'); | |
| 430 | + elements[3].classList.add('is-hidden'); | |
| 431 | + break; | |
| 432 | + case 'page_type': | |
| 433 | + elements[1].classList.remove('is-hidden'); | |
| 434 | + elements[2].classList.add('is-hidden'); | |
| 435 | + elements[3].classList.remove('is-hidden'); | |
| 436 | + break; | |
| 437 | + default: | |
| 438 | + elements[1].classList.add('is-hidden'); | |
| 439 | + elements[2].classList.add('is-hidden'); | |
| 440 | + elements[3].classList.add('is-hidden'); | |
| 441 | + break; | |
| 442 | + | |
| 377 | 443 | } |
| 444 | + | |
| 378 | 445 | }); |
| 379 | 446 | |
| 380 | - schedule.addEventListener('change', function (event) { | |
| 381 | - if (event.target.matches('[name^="param[dates]"]')) { | |
| 382 | - const check_date = event.target; | |
| 383 | - const parent = check_date.closest('.wowp-fields-group'); | |
| 384 | - const date_start = parent.querySelector('.wowp-field:nth-last-child(2)'); | |
| 385 | - const date_end = parent.querySelector('.wowp-field:nth-last-child(1)'); | |
| 386 | - if (check_date.checked) { | |
| 387 | - date_start.classList.remove('is-hidden'); | |
| 388 | - date_end.classList.remove('is-hidden'); | |
| 389 | - } else { | |
| 390 | - date_start.classList.add('is-hidden'); | |
| 391 | - date_end.classList.add('is-hidden'); | |
| 392 | - } | |
| 393 | - | |
| 394 | - | |
| 447 | + function default_custom_post(el) { | |
| 448 | + if (el.includes('custom_post_selected')) { | |
| 449 | + return 'post_selected'; | |
| 395 | 450 | } |
| 396 | - }); | |
| 451 | + if (el.includes('custom_post_tax')) { | |
| 452 | + return 'post_category'; | |
| 453 | + } | |
| 454 | + if (el.includes('custom_post_all')) { | |
| 455 | + return 'post_all'; | |
| 456 | + } | |
| 457 | + return el; | |
| 458 | + } | |
| 397 | 459 | |
| 398 | 460 | } |
| 399 | 461 | |
| 462 | + | |
| 400 | 463 | const feature = function () { |
| 401 | 464 | const texts = document.querySelectorAll('.w_card-description'); |
| 402 | 465 | |
| 403 | 466 | if (!texts) { |
| @@ -413,20 +476,8 @@ | ||
| 413 | 476 | |
| 414 | 477 | document.addEventListener('DOMContentLoaded', function () { |
| 415 | 478 | |
| 416 | 479 | new codeEditor; |
| 417 | - new cloneIncludes; | |
| 418 | - new removeLast; | |
| 419 | - new settingTabs; | |
| 420 | - new changeIncludes; | |
| 480 | + new changeTemplate; | |
| 481 | + new feature; | |
| 421 | 482 | |
| 422 | - new cloneDisplayRules; | |
| 423 | - new removeDisplayRules; | |
| 424 | - new changeDisplayRules; | |
| 425 | - | |
| 426 | - new userRoles; | |
| 427 | - new languages; | |
| 428 | - | |
| 429 | - new schedule; | |
| 430 | - | |
| 431 | - new feature; | |
| 432 | 483 | }) |