1 2jQuery( document ).ready( function() { 3 4 if (typeof acf !== 'undefined') { 5 // console.log('ACF is defined', acf); 6 } 7 8 // lets get the page before firing off 9 const searchParams = new URLSearchParams(window.location.search); 10 var rtPage = searchParams.get('page'); 11 // tab will be defined in the url as lowercase version of tab name; dashes instead of spaces 12 var rtTab = searchParams.get('tab'); 13 // if tab then lets loop through our tab buttons to find the right one & click 14 if(rtTab) { 15 jQuery('.acf-tab-wrap .acf-tab-button').each(function(i, button){ 16 if (rtTab === jQuery(button).text().toLowerCase().replace(' ', '-')){ 17 // if we found a match, click it then exit the each() loop 18 jQuery(button).trigger('click'); 19 return false; 20 } 21 }); 22 } 23 24 // lets define the publish button for later 25 var btn = document.getElementById('publish'); 26 27 // location of our install script 28 var rtInstallSource = rtwpVars.theme_url + '/rts/install/rts-install.php?rtcron=1'; 29 30 // admin notice 31 var adminNotice = document.getElementsByClassName('acf-admin-notice')[0]; 32 33 //maybe an old vendor already? 34 var oldApiVendor = localStorage.getItem("oldVendor"); 35 36 // check if file exists for export button 37 function urlExists(url) 38 { 39 var http = new XMLHttpRequest(); 40 http.open('HEAD', url, false); 41 http.send(); 42 return http.status!== 404; 43 } 44 45 // has a new vendor been selected? If so we will include the install script 46 var vendorChange = searchParams.get('newVendor'); 47 if(vendorChange && vendorChange !== '') { 48 btn.style.display='none'; 49 oldApiVendor = localStorage.getItem("oldVendor"); 50 // createOldVendorExportFile(vendorChange, rtInstallSource, oldApiVendor); 51 // lets make an iframe for the install script 52 rtAddiFrame(vendorChange, rtInstallSource, oldApiVendor); 53 } 54 var lastVendor = searchParams.get('oldVendor'); 55 56 if(rtPage === 'rtc-filter-settings') { 57 acf.addAction('load_field', rtcRemoveEmptyResults); 58 acf.addAction('load_field', rtcRemoveEmptyAvailCal); 59 acf.addAction('load_field', rtcRemoveEmptyDrops); 60 } 61 62 if(rtPage === 'rt-global-config') { 63 // used to disable and enable vendor selection in global config 64 acf.addAction('load_field/key=group_rt_config', rtAdvancedChangeConfirmation); 65 } 66 67 function rtcRemoveEmptyResults() { 68 var fields1 = acf.getFields({ 69 key: 'field_checkbox_choices' 70 }); 71 var fields2 = acf.getFields({ 72 key: 'field_checkbox_choices_pd' 73 }); 74 var fields3 = acf.getFields({ 75 key: 'field_checkbox_label' 76 }); 77 78 // remove empty rows for results checkboxes if PD options 79 for (var i = 0; i < fields1.length; i++) { 80 if (fields2[i]) { 81 if (fields1[i].val() === '' && fields2[i].val() === '') { 82 fields1[i].$el[0].parentNode.remove(); 83 } 84 } else if (fields3[i]) { 85 if (fields1[i].val() === '') { 86 fields1[i].$el[0].parentNode.remove(); 87 } 88 } 89 } 90 } 91 92 function rtcRemoveEmptyAvailCal() { 93 var fields1 = acf.getFields({ 94 key: 'field_avail_cal_checkbox_choices_pd' 95 }); 96 var fields2 = acf.getFields({ 97 key: 'field_avail_cal_checkbox_choices' 98 }); 99 var fields3 = acf.getFields({ 100 key: 'field_avail_cal_checkbox_label' 101 }); 102 // remove empty rows for availcal checkboxes 103 for (var i = 0; i < fields1.length; i++) { 104 if (fields2[i]) { 105 if (fields1[i].val() === '' && fields2[i].val() === '') { 106 fields1[i].$el[0].parentNode.remove(); 107 } 108 } else if (fields3[i]) { 109 if (fields1[i].val() === '') { 110 fields1[i].$el[0].parentNode.remove(); 111 } 112 } 113 } 114 } 115 116 function rtcRemoveEmptyDrops() { 117 var fields1 = acf.getFields({ 118 key: 'field_dropdown_label' 119 }); 120 // remove empty rows from dropdowns 121 if(fields1) { 122 for (var i = 0; i < fields1.length; i++) { 123 if (fields1[i].val() === '') { 124 fields1[i].$el[0].parentNode.remove(); 125 } 126 } 127 } 128 } 129 130 // for advanced product change confirmation
131 function rtAdvancedChangeConfirmation() { 132 var vendor = acf.getField('field_vendor_selection'); 133 var enable = acf.getField('field_enable_product_change'); 134 var confirm = acf.getField('field_confirm_change'); 135 var product = acf.getField('field_product_selection'); 136 137 // get the product val before we disable the field and make it null 138 var productSelection = product.val(); 139 // remove instruction if product is already RTC... n/a - field is disabled 140 var productInstruction = product.$el[0].getElementsByClassName('description')[0]; 141 if(productSelection === 'rtc') { 142 productInstruction.innerText = 'RTC Product cannot be changed'; 143 } 144 // checkbox should always load false & confirm field always load empty & hidden 145 enable.prop('checked', false); 146 enable.$input()[0].removeAttribute('checked'); 147 enable.val('0'); 148 confirm.val(''); 149 confirm.hide(); 150 151 // lets remember the old vendor for later 152 var oldVendor = vendor.val(); 153 154 // product & vendor always load disabled 155 product.disable(); 156 vendor.disable(); 157 158 // create a submit div button for the pw field 159 var button = document.createElement("div"); 160 button.setAttribute('class', 'button rt-button'); 161 button.innerText = 'enable fields'; 162 button.style.order='2'; 163 button.style.marginLeft = '10px'; 164 button.style.alignSelf = 'self-end'; 165 166 var vendorConfirm = button.cloneNode(true); 167 vendorConfirm.innerText = 'confirm'; 168 vendorConfirm.style.pointerEvents = 'none'; 169 vendorConfirm.style.opacity = '25%'; 170 confirm.$el[0].insertAdjacentElement("afterbegin", button); 171 confirm.$el[0].style.display='flex'; 172 var productConfirm = vendorConfirm.cloneNode(true); 173 vendor.$el[0].insertAdjacentElement("beforeend", vendorConfirm); 174 product.$el[0].insertAdjacentElement("beforeend", productConfirm); 175 var productSelect = document.getElementById('acf-group_development-group_rt_config-field_product_selection'); 176 productSelect.parentElement.style.display='inline'; 177 productSelect.style.width='50%'; 178 productSelect.style.display='inline-flex'; 179 var vendorSelect = document.getElementById('acf-group_development-group_rt_config-field_vendor_selection'); 180 vendorSelect.parentElement.style.display='inline'; 181 vendorSelect.style.width='50%'; 182 vendorSelect.style.display='inline-flex'; 183 184 // onchange function to show & hide pw field 185 enable.on('change', function () { 186 if(enable.val() === 1) { 187 confirm.show(); 188 // remove publish button from display... we're changin vendors, lets not confuse people 189 btn.style.display = 'none'; 190 } else { 191 confirm.hide(); 192 btn.style.display = 'inline'; 193 } 194 }); 195 196 // enable the confirm button if the vendor changes 197 vendor.on('change', function () { 198 vendorConfirm.style.pointerEvents = 'initial'; 199 vendorConfirm.style.opacity = '100%'; 200 }); 201 202 // enable the confirm button if the product changes 203 product.on('change', function () { 204 productConfirm.style.pointerEvents = 'initial'; 205 productConfirm.style.opacity = '100%'; 206 }); 207 208 // click function for Vendor change 209 jQuery(vendorConfirm).on('click', function () { 210 localStorage.setItem("oldVendor", oldVendor); 211 // now we have a new val- lets save it 212 const newVendor = vendor.val(); 213 localStorage.setItem("newVendor", newVendor); 214 // lets add a query var to the URL to catch on page reload 215 rtAddQueryVar('newVendor', newVendor); 216 // click the publish button to save acf & install new vendor fields 217 var startNotice = document.createElement("div"); 218 219 var wpBody = document.getElementById('wpwrap'); 220 var wpMenu = document.getElementById('adminmenuwrap'); 221 222 startNotice.innerHTML = '<p style="font-size: 3em;"><strong>Changing vendors...</strong></p>'; 223 startNotice.classList.add('acf-admin-notice', 'notice', 'notice-success'); 224 startNotice.style.position = 'absolute'; 225 226 var blurDiv = startNotice.cloneNode(true); 227 blurDiv.style.top = '-25%'; 228 blurDiv.style.bottom = '-25%'; 229 blurDiv.style.left = '-25%'; 230 blurDiv.style.right = '-25%'; 231 blurDiv.style.width = '200%'; 232 blurDiv.style.height = '200%'; 233 blurDiv.style.background = 'transparent'; 234 blurDiv.style.backdropFilter = 'blur(5px)'; 235 blurDiv.style.zIndex = '100'; 236 237 startNotice.style.top = '30%'; 238 startNotice.style.left = '30%'; 239 startNotice.style.height = '300px'; 240 startNotice.style.display = 'flex'; 241 startNotice.style.alignItems = 'center'; 242 startNotice.style.zIndex = '9999999'; 243 startNotice.style.borderLeft = '10px solid red'; 244 wpMenu.style.transitionDuration = '1s, 1s'; 245 wpMenu.style.left = '-25%'; 246 wpBody.appendChild(blurDiv); 247 wpBody.appendChild(startNotice); 248 btn.click(); 249 }); 250 251 // click function for Vendor change 252 jQuery(productConfirm).on('click', function () { 253 // click the publish button 254 btn.click(); 255 }); 256 257 var vendorChangeAlert = 'WARNING: Changing this field will ' +
258 'save and reload the page, ' + 259 'install new API Vendor fields, ' + 260 'run the vendor install script, ' + 261 ' and open the API vendor tab'; 262 // onchange function to evaluate password and enable vendor & product or show error message 263 confirm.on('change', function () { 264 if (confirm.val()) { 265 if (confirm.val() === 'friday') { 266 vendor.enable(); 267 vendor.showNotice({ 268 text: vendorChangeAlert, 269 type: 'warning', // warning, error, success 270 dismiss: true, // allow notice to be dismissed 271 }); 272 // only enable the product selection for RTD > RTC... this is disabled for RTC clients 273 if(productSelection !== 'rtc') { 274 product.enable(); 275 } else { 276 productInstruction.innerText = ''; 277 product.showNotice({ 278 text: "RTC Product cannot be changed", 279 type: 'warning', // warning, error, success 280 dismiss: true, // allow notice to be dismissed 281 }); 282 } 283 } else { 284 product.disable(); 285 vendor.disable(); 286 confirm.showNotice({ 287 text: "wrong password! No changy!", 288 type: 'warning', // warning, error, success 289 dismiss: true, // allow notice to be dismissed 290 }); 291 } 292 } 293 }); 294 295 } 296 297 // helper function to add query vars to URL without refreshing the page 298 function rtAddQueryVar(key, val) { 299 if (history.pushState) { 300 if(key && val !== '') { 301 var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?page=rt-global-config&' + key + '=' + val; 302 } else { 303 var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?page=rt-global-config'; 304 } 305 window.history.pushState({path:newurl},'',newurl); 306 } 307 } 308 309 // helper function to build an iframe for the install script 310 function rtAddiFrame(vendor, source, oldVendor) { 311 btn.style.display='none'; 312 var newIframe = document.createElement('iframe'); 313 var wpBody = document.getElementById('wpwrap'); 314 var wpMenu = document.getElementById('adminmenuwrap'); 315 wpMenu.style.transitionDuration = '1s, 1s'; 316 wpMenu.style.left = '-25%'; 317 adminNotice.innerHTML = '<p style="font-size: 3em;"><strong>Still changing vendors....</strong></p>'; 318 adminNotice.style.position = 'absolute'; 319 adminNotice.style.top = '30%'; 320 adminNotice.style.left = '30%'; 321 adminNotice.style.height = '300px'; 322 adminNotice.style.display = 'flex'; 323 adminNotice.style.alignItems = 'center'; 324 adminNotice.style.zIndex = '9999999'; 325 adminNotice.style.borderLeft = '10px solid red'; 326 327 var blurDiv = adminNotice.cloneNode(true); 328 blurDiv.innerHTML = ''; 329 blurDiv.style.top = '-25%'; 330 blurDiv.style.bottom = '-25%'; 331 blurDiv.style.left = '-25%'; 332 blurDiv.style.right = '-25%'; 333 blurDiv.style.width = '200%'; 334 blurDiv.style.height = '200%'; 335 blurDiv.style.background = 'transparent'; 336 blurDiv.style.backdropFilter = 'blur(5px)'; 337 blurDiv.style.zIndex = '100'; 338 blurDiv.setAttribute('id', 'blurdiv'); 339 340 newIframe.setAttribute('id', 'rtiframe'); 341 newIframe.src = source; 342 newIframe.style.top = '0'; 343 newIframe.style.right = '0'; 344 newIframe.style.bottom = '0'; 345 newIframe.style.left = '0'; 346 newIframe.style.width = '100%'; 347 newIframe.style.height = '100%';
348 newIframe.style.position = 'absolute'; 349 newIframe.style.margin = 'auto'; 350 newIframe.style.zIndex = '100000'; 351 wpBody.appendChild(blurDiv); 352 wpBody.appendChild(newIframe); 353 354 // set a timeout to remove the frame once complete 355 setTimeout(rtRemoveFrame,8000); 356 function rtRemoveFrame() { 357 var vendorTab = jQuery("[data-key=tab_vendor_api]"); 358 vendorTab.trigger('click'); 359 btn.style.display='inline'; 360 361 // final success message 362 adminNotice.style.backdropFilter = 'blur(5px)'; 363 adminNotice.style.borderLeft = '10px solid green'; 364 adminNotice.innerHTML = '<p style="font-size: 3em;"><strong>Vendor Change Complete.</strong></p>' 365 // set a timeout to remove the frame once complete 366 setTimeout(rtRemoveNotice,4000); 367 function rtRemoveNotice() { 368 document.getElementById('blurdiv').style.display='none'; 369 adminNotice.style.display='none'; 370 document.getElementById('adminmenuwrap').style.left = '0'; 371 } 372 373 wpBody.removeChild(newIframe); 374 // remove query var 375 rtAddQueryVar('oldVendor', ''); 376 } 377 } 378 379 // give a warning about 5 groups for Direct 380 var escapiaSelect = acf.getField('field_custom_category_groups'); 381 escapiaSelect.on('change', 'select', function (e) { 382 if (escapiaSelect.val().length > 5) { 383 escapiaSelect.showNotice({ 384 text: "We can only choose up to 5 groups for Direct sites, <strong style='text-decoration:underline;'>please remove 1</strong>", 385 type: 'warning', // warning, error, success 386 dismiss: false, // allow notice to be dismissed 387 }); 388 } 389 }); 390 391 // google material symbols in the wp dash 392 acf.add_filter('select2_args', function(args) { 393 args.templateSelection = function(selection) { 394 var $selection = jQuery('<span class="acf-selection"></span>'); 395 396 $selection.html(acf.escHtml(selection.text)); 397 $selection.data('element', selection.element); 398 399 return $selection; 400 } 401 402 args.templateResult = function(selection) { 403 var $selection = jQuery('<span class="acf-selection"></span>'); 404 405 $selection.html(acf.escHtml(selection.text)); 406 $selection.data('element', selection.element); 407 408 return $selection; 409 } 410 411 return args; 412 }); 413 414 document.addEventListener("mouseover", function(e) { 415 416 // Selector to preview block where you want to show background image 417 const previewContainer = document.querySelector('.block-editor-inserter__preview-content-missing'); 418 419 if (!previewContainer) { 420 return; 421 } 422 // left sidebar block previews 423 424 if (e.target.closest('.block-editor-block-types-list__item')) { 425 // console.log('left side'); 426 const hoveredBlock = e.target.closest('.block-editor-block-types-list__item'); 427 // to find a name of the block we can extract it from block classes 428 429 // Retrieve classes from the block on which the mouse is hovered 430 const blockClasses = hoveredBlock.className.split(' '); 431 432 // Finding a class that starts with "editor-block-list-item-acf-" 433 const blockClass = blockClasses.find(cls => cls.startsWith("editor-block-list-item-acf-")); 434 435 // If such a class is found, extract the name from it 436 if (blockClass) { 437 const blockName = blockClass.replace("editor-block-list-item-acf-", ""); 438 439 // Get the image URL for this block 440 const imageUrl = wp.data.select('core/blocks').getBlockType("acf/" + blockName)?.attributes?.previewImage?.default; 441 const fullImageUrl = rtwpVars.theme_url + '/' + imageUrl; 442 443 // adding our styles if there is a link to the picture 444 if (imageUrl) { 445 previewContainer.style.background = "url(" + fullImageUrl + ") no-repeat center"; 446 previewContainer.style.backgroundSize = 'contain'; 447 previewContainer.style.fontSize = '0px'; 448 } else { 449 // remove our styles if there is no link 450 previewContainer.style.background = ''; 451 previewContainer.style.backgroundSize = ''; 452 previewContainer.style.fontSize = ''; 453 } 454 } 455 } 456 // block styles & variations previews - right sidebar 457 else if (e.target.closest('.block-editor-block-styles__item')) { 458 459 // what is selected block 460 const blockClass = document.getElementsByClassName("is-selected")[0]; 461 // what style are we hovering over? 462 const hoveredBlock = e.target.closest('.block-editor-block-styles__item'); 463 // what is the style name of the block? 464 const blockStyle = hoveredBlock.getAttribute("aria-label"); 465 // console.log(blockStyle); 466 467 // If such a class is found, extract the block name from it 468 if (blockStyle && blockClass) { 469 const blockName = blockClass.getAttribute("data-type"); 470 471 // Get the image URL for this block from block.json 472 const imageUrl = wp.data.select('core/blocks').getBlockType(blockName)?.attributes?.stylePreviews?.[blockStyle]; 473 474 const fullImageUrl = rtwpVars.theme_url + '/' + imageUrl; 475 476 // adding our styles if there is a link to the picture 477 if (imageUrl) { 478 previewContainer.style.background = "url(" + fullImageUrl + ") no-repeat center"; 479 previewContainer.style.backgroundSize = 'contain'; 480 previewContainer.style.fontSize = '0px'; 481 } else { 482 // remove our styles if there is no link 483 previewContainer.style.background = ''; 484 previewContainer.style.backgroundSize = ''; 485 previewContainer.style.fontSize = ''; 486 } 487 } 488 } 489 }); 490 491 // pattern previews 492 // requires strict naming convention between image and pattern slug / id 493 document.addEventListener("click", function(e) { 494 // if (e.target.closest('#tabs-2-rt-custom-patterns')) { 495 var idText = "-rt-custom-patterns"; 496 if (e.target.closest(`[id*="${idText}"]`)) { 497 // get the previews to replace 498 const patternPreviews = document.querySelectorAll(".block-editor-block-patterns-list__list-item > .block-editor-block-patterns-list__item"); 499 // loop through our preview items and create a new image 500 for (var i = 0; i < patternPreviews.length; i++) { 501 // get the id from the pattern preview 502 var id = patternPreviews[i].getAttribute("id"); 503 const newImage = document.createElement("img"); 504 // add src to the image from id 505 newImage.src = rtwpVars.theme_url + '/images/pattern-previews/' + id + '-preview.jpg'; 506 // add a class to the images to check for later & prevent dups 507 newImage.classList.add('rt-preview'); 508 // Add the image to the div if they dont exist yet 509 var count = document.querySelectorAll('.rt-preview').length; 510 if (count < patternPreviews.length) { 511 patternPreviews[i].appendChild(newImage); 512 } 513 } 514 } 515 }); 516}); // end jQuery 517
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.