admin.js
3445 lines
| 1 | /** |
| 2 | * plugin admin area javascript |
| 3 | */ |
| 4 | (function($){$(function () { |
| 5 | |
| 6 | if ( ! $('body.wpallimport-plugin').length) return; // do not execute any code if we are not on plugin page |
| 7 | |
| 8 | // Handle wizard type switching for import options (only on relevant pages) |
| 9 | function handleWizardTypeChange() { |
| 10 | // Only run on pages that have wizard_type inputs (Step 4 and import edit settings) |
| 11 | var $wizardTypeInputs = $('input[name="wizard_type"]'); |
| 12 | if ($wizardTypeInputs.length === 0) { |
| 13 | return; // Not on a page with wizard type options |
| 14 | } |
| 15 | |
| 16 | $wizardTypeInputs.on('change', function() { |
| 17 | var wizardType = $(this).val(); |
| 18 | var duplicateMatchingInput = $('input[name="duplicate_matching"][type="hidden"]'); |
| 19 | |
| 20 | if (wizardType === 'new') { |
| 21 | // For new items, set duplicate_matching to auto |
| 22 | duplicateMatchingInput.val('auto'); |
| 23 | } else if (wizardType === 'matching') { |
| 24 | // For existing items, set duplicate_matching to manual |
| 25 | duplicateMatchingInput.val('manual'); |
| 26 | } |
| 27 | |
| 28 | // Update delete missing behavior after duplicate_matching is set |
| 29 | updateDeleteMissingBehavior(true); |
| 30 | }); |
| 31 | |
| 32 | // Handle duplicate matching radio buttons for existing items |
| 33 | $('input[name="duplicate_indicator"]').on('change', function() { |
| 34 | if ($('input[name="wizard_type"]:checked').val() === 'matching') { |
| 35 | // The duplicate_matching stays as 'manual' for existing items |
| 36 | // The specific matching method is handled by duplicate_indicator |
| 37 | } |
| 38 | }); |
| 39 | |
| 40 | // Initialize the correct duplicate_matching value on page load |
| 41 | var $duplicateMatchingInput = $('input[name="duplicate_matching"][type="hidden"]'); |
| 42 | |
| 43 | if ($duplicateMatchingInput.length > 0) { |
| 44 | var initialWizardType = $wizardTypeInputs.filter(':checked').val(); |
| 45 | |
| 46 | if (initialWizardType === 'new') { |
| 47 | $duplicateMatchingInput.val('auto'); |
| 48 | } else if (initialWizardType === 'matching') { |
| 49 | $duplicateMatchingInput.val('manual'); |
| 50 | } else { |
| 51 | // Check if 'new' is the default (first radio button) |
| 52 | var $firstRadio = $wizardTypeInputs.first(); |
| 53 | if ($firstRadio.val() === 'new') { |
| 54 | $firstRadio.prop('checked', true); |
| 55 | $duplicateMatchingInput.val('auto'); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Initialize wizard type handling after a short delay to ensure all elements are loaded |
| 62 | setTimeout(function() { |
| 63 | handleWizardTypeChange(); |
| 64 | }, 100); |
| 65 | |
| 66 | // Find all Select2 elements and make sure change events propagate |
| 67 | $(document).ready(function() { |
| 68 | // For all existing select2 instances |
| 69 | $('.select2-hidden-accessible').each(function() { |
| 70 | $(this).on('select2:select', function(e) { |
| 71 | // Trigger the native change event |
| 72 | $(this).trigger('change'); |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | // For any future select2 instances - use event delegation |
| 77 | $(document).on('select2:select', '.select2-hidden-accessible', function(e) { |
| 78 | // Trigger the native change event if it hasn't been triggered already |
| 79 | if (!e.originalEvent) { |
| 80 | $(this).trigger('change'); |
| 81 | } |
| 82 | }); |
| 83 | }); |
| 84 | |
| 85 | var fix_tag_position = function(){ |
| 86 | if ($('.wpallimport-layout').length && $('.tag').length && $('.wpallimport-content-section').length){ |
| 87 | let offset_top = $('.wpallimport-content-section').eq(0).offset().top; |
| 88 | if ($('.xpath_filtering').length) { |
| 89 | offset_top = $('.wpallimport-content-section').eq(2).offset().top; |
| 90 | } |
| 91 | let wordpress_adminbar_height = $('#wpadminbar').height(); |
| 92 | let position_top = $(document).scrollTop() + wordpress_adminbar_height + 20; |
| 93 | |
| 94 | $('.tag').css('margin-top', '0'); |
| 95 | |
| 96 | if (position_top > offset_top){ |
| 97 | $('.tag').css({'top': position_top - offset_top}); |
| 98 | $('.wpallimport-xml').css({'max-height': ($(window).height() - 220) + 'px' }); |
| 99 | } else { |
| 100 | $('.tag').css({'top': '0' }); |
| 101 | $('.wpallimport-xml').css({'max-height': ($(window).height() - 220) + 'px' }); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Global declaration of $wpAllImportDrag and $wpAllImportOriginalColor so we can use them on dynamic elements. |
| 107 | var $wpAllImportDrag = null; |
| 108 | var $wpAllImportOriginalColor = ''; |
| 109 | |
| 110 | function wpaiMakeDroppable(){ |
| 111 | // Click-to-insert lives on wpaiBindGlobalClickHandler. The per-input |
| 112 | // flag prevents stacked droppables: $.fn.xml('dragable') runs on every |
| 113 | // tag-tree render. |
| 114 | $('input, textarea').each(function () { |
| 115 | var $input = $(this); |
| 116 | if ($input.data('wpai-droppable-applied')) return; |
| 117 | $input.data('wpai-droppable-applied', true); |
| 118 | $input.droppable({ |
| 119 | drop: function (event, ui) { |
| 120 | var oldValue = $(this).val(); |
| 121 | var newValue = ui.draggable.data('xpath') || ''; |
| 122 | $(this).val(oldValue + newValue); |
| 123 | }, |
| 124 | greedy: true, |
| 125 | tolerance: 'pointer', |
| 126 | disabled: false |
| 127 | }); |
| 128 | }); |
| 129 | |
| 130 | } |
| 131 | |
| 132 | function wpaiMakeDroppableTinyMce() { |
| 133 | |
| 134 | // Ensure tinymce is defined before use. |
| 135 | if (typeof tinymce === 'undefined') { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | // Get tinymce instance |
| 140 | var ed = tinymce.get('content'); |
| 141 | |
| 142 | // Function to get current iframe bounds. |
| 143 | function getIframeBounds() { |
| 144 | var tinymceIframe = $('#content_ifr'); |
| 145 | if (tinymceIframe.length === 0) { |
| 146 | return null; |
| 147 | } |
| 148 | var iframeOffset = tinymceIframe.offset(); |
| 149 | return { |
| 150 | top: iframeOffset.top - $(window).scrollTop(), |
| 151 | left: iframeOffset.left - $(window).scrollLeft(), |
| 152 | bottom: iframeOffset.top + tinymceIframe.height() - $(window).scrollTop(), |
| 153 | right: iframeOffset.left + tinymceIframe.width() - $(window).scrollLeft() |
| 154 | }; |
| 155 | } |
| 156 | |
| 157 | // Initial drop area coordinates. |
| 158 | var dropArea = getIframeBounds(); |
| 159 | if (!dropArea) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | var proxy; |
| 164 | |
| 165 | // Replaces the config from the original .ui-draggable bind site on every |
| 166 | // MutationObserver fire, so it must mirror zIndex/appendTo/iframeFix. |
| 167 | $(".ui-draggable").draggable({ |
| 168 | helper: function () { |
| 169 | return $('<div>').text($(this).data('xpath')); |
| 170 | }, |
| 171 | zIndex: 2147483646, |
| 172 | appendTo: 'body', |
| 173 | iframeFix: true, |
| 174 | start: function (event, ui) { |
| 175 | proxy = ui.helper.clone().appendTo('body').css('z-index', 2147483646); |
| 176 | }, |
| 177 | drag: function (event, ui) { |
| 178 | // Recalculate drop area for iframe. |
| 179 | dropArea = getIframeBounds(); |
| 180 | |
| 181 | // Check if draggable is over the tinymce, update proxy to follow cursor and hide the helper |
| 182 | if ( |
| 183 | event.clientX >= dropArea.left && |
| 184 | event.clientX <= dropArea.right && |
| 185 | event.clientY >= dropArea.top && |
| 186 | event.clientY <= dropArea.bottom |
| 187 | ) { |
| 188 | proxy.css({ top: event.pageY, left: event.pageX }); |
| 189 | ui.helper.hide(); |
| 190 | } else { |
| 191 | proxy.css({ top: 'auto', left: 'auto' }); |
| 192 | ui.helper.show(); |
| 193 | } |
| 194 | }, |
| 195 | stop: function (event, ui) { |
| 196 | // Recalculate drop area for iframe. |
| 197 | dropArea = getIframeBounds(); |
| 198 | |
| 199 | // Append to tinymce content if it was dropped on tinymce |
| 200 | if ( |
| 201 | event.clientX >= dropArea.left && |
| 202 | event.clientX <= dropArea.right && |
| 203 | event.clientY >= dropArea.top && |
| 204 | event.clientY <= dropArea.bottom |
| 205 | ) { |
| 206 | ed.setContent( |
| 207 | ed.getContent() + $(this).data('xpath') |
| 208 | ); |
| 209 | } |
| 210 | // Clean up proxy and show the helper again |
| 211 | proxy.remove(); |
| 212 | ui.helper.show(); |
| 213 | }, |
| 214 | }); |
| 215 | |
| 216 | ed.on('click', function (e) { |
| 217 | if (!$wpAllImportDrag) return; |
| 218 | |
| 219 | let oldValue = ed.getContent(); |
| 220 | let newValue = $wpAllImportDrag.data('xpath') || ''; |
| 221 | |
| 222 | ed.setContent(oldValue + newValue); |
| 223 | |
| 224 | $wpAllImportDrag.css('color', $wpAllImportOriginalColor).css('font-weight', 'bold'); |
| 225 | $wpAllImportDrag = null; |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | function wpaiMakeDroppableSingle($parent) { |
| 230 | |
| 231 | if($parent.hasClass('dragging')){ |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | // Per-input flag prevents stacked droppables — the MutationObserver |
| 236 | // fires for every DOM addition inside .wpallimport-layout. |
| 237 | $parent.find('input, textarea').each(function() { |
| 238 | let $input = $(this); |
| 239 | if ($input.data('wpai-droppable-applied')) return; |
| 240 | $input.data('wpai-droppable-applied', true); |
| 241 | |
| 242 | $input.droppable({ |
| 243 | drop: function (event, ui) { |
| 244 | let oldValue = $(this).val(); |
| 245 | let newValue = ui.draggable.data('xpath') || ''; |
| 246 | $(this).val(oldValue + newValue); |
| 247 | }, |
| 248 | greedy: true, |
| 249 | tolerance: 'pointer' |
| 250 | }); |
| 251 | }); |
| 252 | |
| 253 | } |
| 254 | |
| 255 | function wpaiBindGlobalClickHandler() { |
| 256 | // Scoped to import form + wp-pointer popups — a wider scope would |
| 257 | // consume $wpAllImportDrag on unrelated admin inputs. |
| 258 | var $doc = $(document); |
| 259 | if ($doc.data('wpai-click-bound')) return; |
| 260 | $doc.data('wpai-click-bound', true); |
| 261 | $doc.on('click', 'input, textarea', function (e) { |
| 262 | if (!$wpAllImportDrag) return; |
| 263 | if (!$(this).closest('.wpallimport-layout, .wp-pointer').length) return; |
| 264 | |
| 265 | let oldValue = $(this).val(); |
| 266 | let newValue = $wpAllImportDrag.data('xpath') || ''; |
| 267 | $(this).val(oldValue + newValue); |
| 268 | |
| 269 | $wpAllImportDrag.css('color', $wpAllImportOriginalColor).css('font-weight', 'bold'); |
| 270 | $wpAllImportDrag = null; |
| 271 | }); |
| 272 | } |
| 273 | |
| 274 | function wpaiBindPopupInputs($popup) { |
| 275 | // wp-pointer innerHTML-copies its content, dropping jQuery UI droppable |
| 276 | // state — re-apply on the fresh DOM nodes. |
| 277 | $popup.find('input, textarea').each(function () { |
| 278 | var $input = $(this); |
| 279 | if ($input.data('wpai-droppable-applied')) return; |
| 280 | $input.data('wpai-droppable-applied', true); |
| 281 | |
| 282 | $input.droppable({ |
| 283 | drop: function (event, ui) { |
| 284 | var oldValue = $(this).val(); |
| 285 | var newValue = ui.draggable.data('xpath') || ''; |
| 286 | $(this).val(oldValue + newValue); |
| 287 | }, |
| 288 | greedy: true, |
| 289 | tolerance: 'pointer' |
| 290 | }); |
| 291 | }); |
| 292 | } |
| 293 | |
| 294 | function wpaiObserveFieldAddition() |
| 295 | { |
| 296 | |
| 297 | // Make drag and drop work for dynamically added elements. |
| 298 | // Select the node that will be observed for child addition |
| 299 | let wpaiXmlTargetNode = document.querySelector('.wpallimport-layout'); |
| 300 | |
| 301 | if(!wpaiXmlTargetNode){ |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | wpaiBindGlobalClickHandler(); |
| 306 | |
| 307 | // Options for the observer (which mutations to observe) |
| 308 | let wpaiXmlConfig = {childList: true, subtree: true}; |
| 309 | |
| 310 | // Callback function to execute when mutations are observed |
| 311 | let wpaiXmlCallback = function (mutationsList) { |
| 312 | for (let mutation of mutationsList) { |
| 313 | if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { |
| 314 | mutation.addedNodes.forEach((node) => { |
| 315 | if (node.nodeType === Node.ELEMENT_NODE) { |
| 316 | |
| 317 | wpaiMakeDroppableSingle($(node)); |
| 318 | wpaiMakeDroppableTinyMce(); |
| 319 | |
| 320 | } |
| 321 | }); |
| 322 | } |
| 323 | } |
| 324 | }; |
| 325 | |
| 326 | // Create an observer instance linked to the callback function |
| 327 | let wpaiXmlObserver = new MutationObserver(wpaiXmlCallback); |
| 328 | |
| 329 | // Start observing the target node for configured mutations |
| 330 | wpaiXmlObserver.observe(wpaiXmlTargetNode, wpaiXmlConfig); |
| 331 | } |
| 332 | |
| 333 | wpaiObserveFieldAddition(); |
| 334 | |
| 335 | function wpai_set_custom_select_image() { |
| 336 | // Only process if custom_type_selector exists |
| 337 | if (!jQuery('#custom_type_selector').length) { |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | // The class name to add to the element. |
| 342 | var $selectedItem = jQuery('#custom_type_selector').next('.select2-container').find('.select2-selection__rendered .dashicon'); |
| 343 | if ($selectedItem.length) { |
| 344 | var class_name = $selectedItem.closest('.select2-selection__rendered').text().toLowerCase(); |
| 345 | class_name = class_name.replace(/\s+/g, ''); |
| 346 | |
| 347 | // This gets the image URL out of the class. |
| 348 | var class_check = $selectedItem.attr('class'); |
| 349 | if (class_check) { |
| 350 | class_check = class_check.replace("dashicon ", ""); |
| 351 | |
| 352 | // String of allowed images. |
| 353 | var imgs = ['jpg','jpeg','jpe','gif','png','bmp'], length = imgs.length; |
| 354 | while(length--) { |
| 355 | if (class_check.indexOf(imgs[length]) != -1) { |
| 356 | // They have defined an image URL, which means it's a custom image and we need to add the class. |
| 357 | $selectedItem.addClass("wpaiimgfor" + class_name); |
| 358 | $selectedItem.removeClass(class_check); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // The new CSS handles icons automatically for custom_type_selector |
| 365 | // No need for complex JavaScript manipulation |
| 366 | } |
| 367 | |
| 368 | // Rapid Add-On API Images |
| 369 | $(document).ready(function($){ |
| 370 | |
| 371 | |
| 372 | var class_check; |
| 373 | var original_class; |
| 374 | var new_class_name; |
| 375 | var allstyles = "<style type='text/css'>"; |
| 376 | |
| 377 | // Process custom images in the dropdown options |
| 378 | $('#custom_type_selector option').each(function() { |
| 379 | var imagesrc = $(this).attr('data-imagesrc'); |
| 380 | if (imagesrc && imagesrc.includes('dashicon')) { |
| 381 | // Extract the custom image class if present |
| 382 | var dashiconClass = imagesrc.replace('dashicon ', ''); |
| 383 | |
| 384 | // Build the class name based on the option text |
| 385 | new_class_name = $(this).text().toLowerCase(); |
| 386 | new_class_name = new_class_name.replace(/\s+/g, ''); |
| 387 | |
| 388 | var imgs = ['jpg','jpeg','jpe','gif','png','bmp'], |
| 389 | length = imgs.length; |
| 390 | while(length--) { |
| 391 | if (dashiconClass.indexOf(imgs[length]) != -1) { |
| 392 | // They've defined a custom image URL, so we need to append the class to the head |
| 393 | // More specific selectors to ensure styles are applied ONLY to custom_type_selector |
| 394 | allstyles = allstyles + ".wpallimport-plugin #custom_type_selector + .select2-container .wpaiimgfor" + new_class_name + ":before { font-family: 'dashicons'; font-size: 24px; float: left; margin: 2px 5px 2px 2px; background-image: url(" + dashiconClass + "); background-repeat: no-repeat; background-position: center center; content:''; height: 25px; width:24px; }"; |
| 395 | allstyles = allstyles + ".wpallimport-plugin #custom_type_selector + .select2-container .select2-selection__rendered .dashicon.wpaiimgfor" + new_class_name + " { position: relative; top: 2px; }"; |
| 396 | allstyles = allstyles + ".wpallimport-plugin #custom_type_selector + .select2-container ~ .select2-dropdown .select2-results__option .dashicon.wpaiimgfor" + new_class_name + " { position: relative; top: 2px; }"; |
| 397 | allstyles = allstyles + ".wpallimport-plugin #custom_type_selector ~ .wpallimport-select2-dropdown .select2-results__option .dashicon.wpaiimgfor" + new_class_name + " { position: relative; top: 2px; }"; |
| 398 | |
| 399 | // Add more specific rules for Select2 dropdown - ONLY for custom_type_selector |
| 400 | allstyles = allstyles + ".wpallimport-plugin #custom_type_selector + .select2-container ~ .select2-dropdown .select2-results__option .dashicon.wpaiimgfor" + new_class_name + ":before { background-image: url(" + dashiconClass + "); }"; |
| 401 | allstyles = allstyles + ".wpallimport-plugin #custom_type_selector ~ .wpallimport-select2-dropdown .select2-results__option .dashicon.wpaiimgfor" + new_class_name + ":before { background-image: url(" + dashiconClass + "); }"; |
| 402 | allstyles = allstyles + ".wpallimport-plugin #custom_type_selector + .select2-container .select2-selection__rendered .dashicon.wpaiimgfor" + new_class_name + ":before { background-image: url(" + dashiconClass + "); }"; |
| 403 | |
| 404 | // Store the custom class in a data attribute for use by templateResult and templateSelection |
| 405 | $(this).attr('data-custom-image-class', "wpaiimgfor" + new_class_name); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | }); |
| 410 | |
| 411 | // Only append styles if we actually have custom images for custom_type_selector |
| 412 | if (allstyles.length > 25) { // More than just the opening style tag |
| 413 | allstyles = allstyles + "</style>"; |
| 414 | $(allstyles).appendTo("head"); |
| 415 | } |
| 416 | |
| 417 | // Initialize Select2 after processing custom images |
| 418 | setTimeout(function() { |
| 419 | // Trigger a change on the custom_type_selector to ensure proper initialization |
| 420 | if ($('#custom_type_selector').length) { |
| 421 | wpai_set_custom_select_image(); |
| 422 | |
| 423 | // Handle custom_type_selector dropdown opening |
| 424 | $('#custom_type_selector').on('select2:open', function() { |
| 425 | setTimeout(function() { |
| 426 | var $dropdown = $('.select2-dropdown'); |
| 427 | $dropdown.addClass('custom-type-dropdown'); |
| 428 | |
| 429 | // Add dashicon classes to dropdown options based on data-imagesrc for Select2 v4 |
| 430 | $('#custom_type_selector option').each(function(index) { |
| 431 | var $option = $(this); |
| 432 | var imagesrc = $option.attr('data-imagesrc'); |
| 433 | if (imagesrc) { |
| 434 | var $dropdownOption = $('.select2-dropdown .select2-results__option:eq(' + index + ')'); |
| 435 | if ($dropdownOption.length && !$dropdownOption.find('.dashicon').length) { |
| 436 | // Clear any existing dashicon classes |
| 437 | $dropdownOption.find('.dashicon').remove(); |
| 438 | // Add the dashicon span with the appropriate class |
| 439 | var iconClass = imagesrc.replace('dashicon ', ''); |
| 440 | $dropdownOption.prepend('<span class="dashicon ' + iconClass + '"></span>'); |
| 441 | } |
| 442 | } |
| 443 | }); |
| 444 | }, 50); // Small delay to ensure dropdown is rendered |
| 445 | }); |
| 446 | |
| 447 | // Handle custom_type_selector dropdown closing for Select2 v4 |
| 448 | $('#custom_type_selector').on('select2:close', function() { |
| 449 | $('.select2-dropdown').removeClass('custom-type-dropdown'); |
| 450 | }); |
| 451 | } |
| 452 | }, 100); |
| 453 | }); |
| 454 | |
| 455 | // fix wpallimport-layout position |
| 456 | setTimeout(function () { |
| 457 | $('table.wpallimport-layout').length && $('table.wpallimport-layout td.left h2:first-child').css('margin-top', $('.wrap').has('.wpallimport-layout').offset().top - $('table.wpallimport-layout').offset().top); |
| 458 | }, 10); |
| 459 | |
| 460 | // help icons |
| 461 | $(document).tipsy({ |
| 462 | gravity: function() { |
| 463 | var ver = 'n'; |
| 464 | if ($(document).scrollTop() < $(this).offset().top - $('.tipsy').height() - 2) { |
| 465 | ver = 's'; |
| 466 | } |
| 467 | var hor = ''; |
| 468 | if ($(this).offset().left + $('.tipsy').width() < $(window).width() + $(document).scrollLeft()) { |
| 469 | hor = 'w'; |
| 470 | } else if ($(this).offset().left - $('.tipsy').width() > $(document).scrollLeft()) { |
| 471 | hor = 'e'; |
| 472 | } |
| 473 | return ver + hor; |
| 474 | }, |
| 475 | live: 'a.wpallimport-help, .scheduling-help', |
| 476 | html: true, |
| 477 | opacity: 1 |
| 478 | }).each(function () { // fix tipsy title for IE |
| 479 | $(this).attr('original-title', $(this).attr('title')); |
| 480 | $(this).removeAttr('title'); |
| 481 | }); |
| 482 | |
| 483 | // help icons |
| 484 | $('.scheduling-disabled').parent().tipsy({ |
| 485 | gravity: function() { |
| 486 | var ver = 'n'; |
| 487 | if ($(document).scrollTop() < $(this).offset().top - $('.tipsy').height() - 2) { |
| 488 | ver = 's'; |
| 489 | } |
| 490 | var hor = ''; |
| 491 | if ($(this).offset().left + $('.tipsy').width() < $(window).width() + $(document).scrollLeft()) { |
| 492 | hor = 'w'; |
| 493 | } else if ($(this).offset().left - $('.tipsy').width() > $(document).scrollLeft()) { |
| 494 | hor = 'e'; |
| 495 | } |
| 496 | return ver + hor; |
| 497 | }, |
| 498 | live: '.scheduling-disabled', |
| 499 | html: true, |
| 500 | delayOut: 2000, |
| 501 | opacity: 1, |
| 502 | trigger: "click" |
| 503 | }); |
| 504 | |
| 505 | // swither show/hide logic |
| 506 | $('input.switcher').on("change", function (e) { |
| 507 | if ($(this).is(':radio:checked')) { |
| 508 | $(this).parents('form').find('input.switcher:radio[name="' + $(this).attr('name') + '"]').not(this).trigger('change'); |
| 509 | } |
| 510 | let $targets = $('.switcher-target-' + $(this).attr('id')); |
| 511 | let is_show = $(this).is(':checked'); if ($(this).is('.switcher-reversed')) is_show = ! is_show; |
| 512 | if (is_show) { |
| 513 | $targets.slideDown(); |
| 514 | } else { |
| 515 | $targets.slideUp().find('.clear-on-switch').add($targets.filter('.clear-on-switch')).val(''); |
| 516 | } |
| 517 | }).trigger('change'); |
| 518 | |
| 519 | // swither show/hide logic |
| 520 | $('input.switcher-horizontal').on('change', function (e) { |
| 521 | // Prevent recursive animation triggering |
| 522 | if ($(this).data('animating-horizontal')) { |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | if ($(this).is(':checked')) { |
| 527 | // For horizontal switchers, handle all related targets in one go to prevent animation conflicts |
| 528 | let radioName = $(this).attr('name'); |
| 529 | let $allRadios = $(this).parents('form').find('input.switcher-horizontal[name="' + radioName + '"]'); |
| 530 | |
| 531 | // Mark all radios as animating to prevent recursive calls |
| 532 | $allRadios.data('animating-horizontal', true); |
| 533 | |
| 534 | // Hide all targets for this radio group first |
| 535 | $allRadios.not(this).each(function() { |
| 536 | let $targets = $('.switcher-target-' + $(this).attr('id')); |
| 537 | if ($targets.width() > 0) { |
| 538 | $targets.animate({width:'0px'}, 1000).find('.clear-on-switch').add($targets.filter('.clear-on-switch')).val(''); |
| 539 | } |
| 540 | }); |
| 541 | |
| 542 | // Then show the target for the selected radio |
| 543 | let $targets = $('.switcher-target-' + $(this).attr('id')); |
| 544 | let is_show = $(this).is(':checked'); if ($(this).is('.switcher-reversed')) is_show = ! is_show; |
| 545 | if (is_show) { |
| 546 | $targets.animate({width:'205px'}, 350); |
| 547 | } |
| 548 | |
| 549 | // Clear the animating flag after a delay |
| 550 | setTimeout(function() { |
| 551 | $allRadios.removeData('animating-horizontal'); |
| 552 | }, 1200); // Longer delay for horizontal animations |
| 553 | } else { |
| 554 | // Handle non-radio horizontal switchers normally |
| 555 | let $targets = $('.switcher-target-' + $(this).attr('id')); |
| 556 | let is_show = $(this).is(':checked'); if ($(this).is('.switcher-reversed')) is_show = ! is_show; |
| 557 | if (is_show) { |
| 558 | $targets.animate({width:'205px'}, 350); |
| 559 | } else { |
| 560 | $targets.animate({width:'0px'}, 1000).find('.clear-on-switch').add($targets.filter('.clear-on-switch')).val(''); |
| 561 | } |
| 562 | } |
| 563 | }).trigger('change'); |
| 564 | |
| 565 | // autoselect input content on click |
| 566 | $(document).on('click', 'input.selectable', function () { |
| 567 | $(this).select(); |
| 568 | }); |
| 569 | |
| 570 | // input tags with title |
| 571 | $('input[title]').each(function () { |
| 572 | var $this = $(this); |
| 573 | $this.on('focus', function () { |
| 574 | if ('' == $(this).val() || $(this).val() == $(this).attr('title')) { |
| 575 | $(this).removeClass('note').val(''); |
| 576 | } |
| 577 | }).on('blur', function () { |
| 578 | if ('' == $(this).val() || $(this).val() == $(this).attr('title')) { |
| 579 | $(this).addClass('note').val($(this).attr('title')); |
| 580 | } |
| 581 | }).trigger('blur'); |
| 582 | $this.parents('form').on('submit', function () { |
| 583 | if ($this.val() == $this.attr('title')) { |
| 584 | $this.val(''); |
| 585 | } |
| 586 | }); |
| 587 | }); |
| 588 | |
| 589 | // datepicker |
| 590 | $('input.datepicker').datepicker({ |
| 591 | dateFormat: 'yy-mm-dd', |
| 592 | showOn: 'button', |
| 593 | buttonText: '', |
| 594 | constrainInput: false, |
| 595 | showAnim: 'fadeIn', |
| 596 | showOptions: 'fast' |
| 597 | }).on('change', function () { |
| 598 | var selectedDate = $(this).val(); |
| 599 | var instance = $(this).data('datepicker'); |
| 600 | var date = null; |
| 601 | if ('' != selectedDate) { |
| 602 | try { |
| 603 | date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings); |
| 604 | } catch (e) { |
| 605 | date = null; |
| 606 | } |
| 607 | } |
| 608 | if ($(this).hasClass('range-from')) { |
| 609 | $(this).parent().find('.datepicker.range-to').datepicker("option", "minDate", date); |
| 610 | } |
| 611 | if ($(this).hasClass('range-to')) { |
| 612 | $(this).parent().find('.datepicker.range-from').datepicker("option", "maxDate", date); |
| 613 | } |
| 614 | }).trigger('change'); |
| 615 | $('.ui-datepicker').hide(); // fix: make sure datepicker doesn't break wordpress wpallimport-layout upon initialization |
| 616 | |
| 617 | // no-enter-submit forms |
| 618 | $('form.no-enter-submit').find('input,select,textarea').not('*[type="submit"]').on('keydown', function (e) { |
| 619 | if (13 == e.keyCode) e.preventDefault(); |
| 620 | }); |
| 621 | |
| 622 | $('a.collapser').each(function(){ |
| 623 | if ($(this).html() == "+"){ |
| 624 | $(this).parents('div:first').find('.collapser_content:first').hide(); |
| 625 | } else { |
| 626 | $(this).parents('div:first').find('.collapser_content:first').fadeIn(); |
| 627 | } |
| 628 | $(this).next('h3').css({'cursor':'pointer'}); |
| 629 | }); |
| 630 | |
| 631 | $('a.collapser').on('click', function(){ |
| 632 | if ($(this).html() == "+") { |
| 633 | $(this).html("-"); |
| 634 | $(this).parents('div:first').find('.collapser_content:first').fadeIn(); |
| 635 | } else { |
| 636 | $(this).html("+"); |
| 637 | $(this).parents('div:first').find('.collapser_content:first').hide(); |
| 638 | } |
| 639 | }); |
| 640 | |
| 641 | $('a.collapser').each(function(){ |
| 642 | $(this).parents('.fieldset:first').find('h3:first').on('click', function(){ |
| 643 | $(this).prev('a.collapser').trigger('click'); |
| 644 | }); |
| 645 | }); |
| 646 | |
| 647 | // Initialize file selector for main import page (not in .change_file context) |
| 648 | if ($('#file_selector').length && !$('#file_selector').hasClass('select2-hidden-accessible')) { |
| 649 | $('#file_selector').select2({ |
| 650 | width: '600px', |
| 651 | minimumResultsForSearch: 10, |
| 652 | theme: 'default', |
| 653 | disabled: true // Completely disable the dropdown in free version |
| 654 | }); |
| 655 | |
| 656 | // Add click handler to show upgrade notice when trying to interact with disabled dropdown |
| 657 | $('#file_selector').next('.select2-container').on('click', function(e) { |
| 658 | e.preventDefault(); |
| 659 | $('.wpallimport-upload-type-container[rel=file_type] .wpallimport-free-edition-notice').show(); |
| 660 | return false; |
| 661 | }); |
| 662 | } |
| 663 | |
| 664 | $('.change_file').each(function(){ |
| 665 | let $wrap = $('.wrap').has('.wpallimport-layout'); |
| 666 | let formHeight = ($('.wpallimport-layout').height() < 730) ? 730 : $('.wpallimport-layout').height(); |
| 667 | $('#file_selector').select2({ |
| 668 | width: '600px', |
| 669 | minimumResultsForSearch: 10, |
| 670 | theme: 'default', |
| 671 | disabled: true // Completely disable the dropdown in free version |
| 672 | }); |
| 673 | |
| 674 | // Add click handler to show upgrade notice when trying to interact with disabled dropdown |
| 675 | $('#file_selector').next('.select2-container').on('click', function(e) { |
| 676 | e.preventDefault(); |
| 677 | $('.wpallimport-upload-type-container[rel=file_type] .wpallimport-free-edition-notice').show(); |
| 678 | return false; |
| 679 | }); |
| 680 | |
| 681 | let fixWrapHeight = false; |
| 682 | |
| 683 | $('#custom_type_selector').select2({ |
| 684 | width: '590px', |
| 685 | minimumResultsForSearch: 10, |
| 686 | theme: 'default', |
| 687 | templateResult: function(data) { |
| 688 | if (!data.id) return data.text; |
| 689 | var $option = $(data.element); |
| 690 | var $result = $('<div class="select2-option-wrapper"></div>'); |
| 691 | var imagesrc = $option.attr('data-imagesrc'); |
| 692 | var customImageClass = $option.attr('data-custom-image-class'); |
| 693 | |
| 694 | if (imagesrc) { |
| 695 | var spanClass = imagesrc; |
| 696 | if (customImageClass) { |
| 697 | // If there's a custom image class, add it to the span |
| 698 | spanClass = imagesrc.replace(/dashicon\s+[^\s]+/, 'dashicon ' + customImageClass); |
| 699 | } |
| 700 | // Create the icon with more explicit styling to ensure visibility |
| 701 | var $icon = $('<span class="' + spanClass + '" style="display:inline-block !important; vertical-align:middle !important; margin-right:5px !important; visibility:visible !important; opacity:1 !important; width:24px !important; height:24px !important; position:relative !important; z-index:100 !important;"></span>'); |
| 702 | $result.append($icon); |
| 703 | } |
| 704 | $result.append(data.text); |
| 705 | return $result; |
| 706 | }, |
| 707 | templateSelection: function(data) { |
| 708 | if (!data.id) return data.text; |
| 709 | var $option = $(data.element); |
| 710 | var $result = $('<div class="select2-selection-wrapper"></div>'); |
| 711 | var imagesrc = $option.attr('data-imagesrc'); |
| 712 | var customImageClass = $option.attr('data-custom-image-class'); |
| 713 | |
| 714 | if (imagesrc) { |
| 715 | var spanClass = imagesrc; |
| 716 | if (customImageClass) { |
| 717 | // If there's a custom image class, add it to the span |
| 718 | spanClass = imagesrc.replace(/dashicon\s+[^\s]+/, 'dashicon ' + customImageClass); |
| 719 | } |
| 720 | var $icon = $('<span class="' + spanClass + '" style="display:inline-block !important; vertical-align:middle !important; margin-right:5px !important; visibility:visible !important; opacity:1 !important;"></span>'); |
| 721 | $result.append($icon); |
| 722 | } |
| 723 | $result.append(data.text); |
| 724 | |
| 725 | // After rendering, trigger the custom image handler |
| 726 | setTimeout(function() { |
| 727 | wpai_set_custom_select_image(); |
| 728 | }, 10); |
| 729 | |
| 730 | return $result; |
| 731 | } |
| 732 | }).on('select2:opening', function(e){ |
| 733 | formHeight = ($('.wpallimport-layout').height() < 730) ? 730 : $('.wpallimport-layout').height(); |
| 734 | // Store the original height to restore later |
| 735 | $(this).data('originalHeight', $wrap.height()); |
| 736 | }).on('select2:open', function(e){ |
| 737 | // Set timeout to allow the dropdown to render before measuring |
| 738 | setTimeout(function() { |
| 739 | $wrap.css({'height': formHeight + $('.select2-dropdown').height() + 'px'}); |
| 740 | }, 50); |
| 741 | }).on('select2:closing', function(e){ |
| 742 | $wrap.css({'height': 'auto'}); |
| 743 | }).on('select2:select', function(e){ |
| 744 | let selectedData = e.params.data; |
| 745 | |
| 746 | if (fixWrapHeight) { |
| 747 | $wrap.css({'height': 'auto'}); |
| 748 | } else { |
| 749 | fixWrapHeight = true; |
| 750 | } |
| 751 | |
| 752 | $('.wpallimport-upgrade-notice').hide(); |
| 753 | |
| 754 | $('input[name=custom_type]').val(selectedData.id); |
| 755 | $(this).next('.select2-container').find('.select2-selection__rendered').css({'color':'#555'}); |
| 756 | |
| 757 | var is_import_denied = $('.wpallimport-upgrade-notice[rel="'+ selectedData.id +'"]').length; |
| 758 | // Free version - also deny taxonomies |
| 759 | var is_taxonomy_import = (selectedData.id === 'taxonomies'); |
| 760 | |
| 761 | if (is_import_denied){ |
| 762 | $('.wpallimport-upgrade-notice[rel="'+ selectedData.id +'"]').slideDown(); |
| 763 | $('.wpallimport-submit-buttons').hide(); |
| 764 | } else if (is_taxonomy_import) { |
| 765 | // Free version - hide submit buttons for taxonomy imports |
| 766 | $('.wpallimport-submit-buttons').hide(); |
| 767 | } else { |
| 768 | // Check if any upgrade notice is visible before showing buttons |
| 769 | if ($('.wpallimport-upgrade-notice:visible').length === 0) { |
| 770 | $('.wpallimport-submit-buttons').slideDown(); |
| 771 | } else { |
| 772 | $('.wpallimport-submit-buttons').hide(); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | // Handle taxonomy_to_import_wrapper and gravity_form_to_import_wrapper visibility |
| 777 | switch (selectedData.id) { |
| 778 | case 'taxonomies': |
| 779 | $('.taxonomy_to_import_wrapper').slideDown(); |
| 780 | $('.gravity_form_to_import_wrapper').slideUp(); |
| 781 | break; |
| 782 | case 'gf_entries': |
| 783 | $('.taxonomy_to_import_wrapper').slideUp(); |
| 784 | $('.gravity_form_to_import_wrapper').slideDown(); |
| 785 | break; |
| 786 | default: |
| 787 | $('.taxonomy_to_import_wrapper').slideUp(); |
| 788 | $('.gravity_form_to_import_wrapper').slideUp(); |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | // Rapid Add-On API Images |
| 793 | wpai_set_custom_select_image(); |
| 794 | |
| 795 | // Add icon to selected state |
| 796 | var selectedOption = $(this).find('option:selected'); |
| 797 | var imagesrc = selectedOption.attr('data-imagesrc'); |
| 798 | if (imagesrc) { |
| 799 | var $selectedDisplay = $(this).next('.select2-container').find('.select2-selection__rendered'); |
| 800 | // Clear any existing dashicon |
| 801 | $selectedDisplay.find('.dashicon').remove(); |
| 802 | // Add the dashicon span with the appropriate class |
| 803 | var iconClass = imagesrc.replace('dashicon ', ''); |
| 804 | $selectedDisplay.prepend('<span class="dashicon ' + iconClass + '"></span>'); |
| 805 | } |
| 806 | |
| 807 | // Trigger native change event to ensure all listeners are notified |
| 808 | $(this).trigger('change'); |
| 809 | }).on('change', function() { |
| 810 | // This handler will be triggered by the native change event |
| 811 | // Only proceed if we're not already in the select2:select handler |
| 812 | if (!$(this).data('select2-processing')) { |
| 813 | // Set a flag to prevent infinite loop |
| 814 | $(this).data('select2-processing', true); |
| 815 | |
| 816 | var selectedValue = $(this).val(); |
| 817 | |
| 818 | // Handle taxonomy_to_import_wrapper and gravity_form_to_import_wrapper visibility |
| 819 | switch (selectedValue) { |
| 820 | case 'taxonomies': |
| 821 | $('.taxonomy_to_import_wrapper').slideDown(); |
| 822 | $('.gravity_form_to_import_wrapper').slideUp(); |
| 823 | break; |
| 824 | case 'gf_entries': |
| 825 | $('.taxonomy_to_import_wrapper').slideUp(); |
| 826 | $('.gravity_form_to_import_wrapper').slideDown(); |
| 827 | break; |
| 828 | default: |
| 829 | $('.taxonomy_to_import_wrapper').slideUp(); |
| 830 | $('.gravity_form_to_import_wrapper').slideUp(); |
| 831 | break; |
| 832 | } |
| 833 | |
| 834 | // Remove the processing flag |
| 835 | $(this).data('select2-processing', false); |
| 836 | } |
| 837 | }); |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | $('.wpallimport-import-from').on('click', function(){ |
| 844 | |
| 845 | var showImportType = false; |
| 846 | |
| 847 | switch ($(this).attr('rel')){ |
| 848 | case 'upload_type': |
| 849 | if ($('input[name=filepath]').val() != '') { |
| 850 | showImportType = true; |
| 851 | } |
| 852 | $('.wpallimport-download-resource').hide(); |
| 853 | break; |
| 854 | case 'url_type': |
| 855 | if ($('input[name=url]').val() != '') { |
| 856 | showImportType = true; |
| 857 | } |
| 858 | $('.wpallimport-download-from-checked').trigger('click'); |
| 859 | break; |
| 860 | case 'file_type': |
| 861 | // Free version - show interface but don't allow progression |
| 862 | showImportType = false; // This prevents the submit buttons from showing |
| 863 | $('.wpallimport-download-resource').hide(); |
| 864 | break; |
| 865 | } |
| 866 | |
| 867 | $('.wpallimport-import-from').removeClass('selected').addClass('bind'); |
| 868 | $(this).addClass('selected').removeClass('bind'); |
| 869 | $('.wpallimport-import-from').removeClass('selected').addClass('bind'); |
| 870 | $(this).addClass('selected').removeClass('bind'); |
| 871 | $('.change_file').find('.wpallimport-upload-type-container').hide(); |
| 872 | $('.change_file').find('.wpallimport-file-upload-result').attr('rel', $(this).attr('rel')); |
| 873 | $('.change_file').find('.wpallimport-upload-type-container[rel=' + $(this).attr('rel') + ']').show(); |
| 874 | $('.change_file').find('#wpallimport-url-upload-status').html(''); |
| 875 | $('.change_file').find('input[name=new_type]').val( $(this).attr('rel').replace('_type', '') ); |
| 876 | //$('.first-step-errors').hide(); |
| 877 | |
| 878 | |
| 879 | }); |
| 880 | $('.wpallimport-import-from.selected').trigger('click'); |
| 881 | |
| 882 | $('.wpallimport-download-from').on('click', function(){ |
| 883 | if ($(this).attr('rel') === 'url') { |
| 884 | $('.wpallimport-download-resource-step-two-url').show(); |
| 885 | $('.wpallimport-download-resource-step-two-ftp').hide(); |
| 886 | // Show upgrade notice for URL download in Free version |
| 887 | $('.wpallimport-download-resource-step-two-url .wpallimport-free-edition-notice').show(); |
| 888 | } else { |
| 889 | $('.wpallimport-download-resource-step-two-url').hide(); |
| 890 | $('.wpallimport-download-resource-step-two-ftp').show(); |
| 891 | // Show upgrade notice for FTP download in Free version |
| 892 | $('.wpallimport-download-resource-step-two-ftp .wpallimport-free-edition-notice').show(); |
| 893 | } |
| 894 | $('.wpallimport-download-from').removeClass('wpallimport-download-from-checked'); |
| 895 | $(this).addClass('wpallimport-download-from-checked'); |
| 896 | $('.change_file').find('input[name=new_type]').val( $(this).attr('rel') ); |
| 897 | }); |
| 898 | $('.wpallimport-download-from.wpallimport-download-from-checked').trigger('click'); |
| 899 | |
| 900 | }); |
| 901 | |
| 902 | // Drag and drop file upload |
| 903 | $(document).ready(function () { |
| 904 | window.dragCounter = 0; |
| 905 | var $overlay = $('#wpallimport-fullscreen-drag-overlay'); |
| 906 | var $dragArea = $('#wpallimport-drag-drop-area'); |
| 907 | window.isDragDropUpload = false; |
| 908 | |
| 909 | // Global drag events for fullscreen drop zone |
| 910 | $(document).on('dragenter', function (e) { |
| 911 | // Only handle file drags |
| 912 | if (e.originalEvent.dataTransfer && e.originalEvent.dataTransfer.types.indexOf('Files') !== -1) { |
| 913 | window.dragCounter++; |
| 914 | |
| 915 | // Show overlay and extend drag area on first drag enter |
| 916 | if (window.dragCounter === 1) { |
| 917 | $dragArea.addClass('fullscreen-active'); |
| 918 | $overlay.show(); |
| 919 | |
| 920 | // Properly select the upload type and show its container |
| 921 | $('.wpallimport-import-from').removeClass('selected').addClass('bind'); |
| 922 | $('#file_upload_type').addClass('selected').removeClass('bind'); |
| 923 | $('.wpallimport-choose-file').find('.wpallimport-upload-type-container').hide(); |
| 924 | $('.wpallimport-choose-file').find('.wpallimport-upload-type-container[rel=upload_type]').show(); |
| 925 | $('.wpallimport-choose-file').find('input[name=type]').val('upload'); |
| 926 | } |
| 927 | } |
| 928 | }); |
| 929 | |
| 930 | $(document).on('dragleave', function (e) { |
| 931 | // Only handle file drags |
| 932 | if (e.originalEvent.dataTransfer && e.originalEvent.dataTransfer.types.indexOf('Files') !== -1) { |
| 933 | window.dragCounter--; |
| 934 | |
| 935 | // Hide overlay when completely leaving the window (only if not uploading) |
| 936 | if (window.dragCounter === 0 && !window.isDragDropUpload) { |
| 937 | $dragArea.removeClass('fullscreen-active'); |
| 938 | $overlay.hide(); |
| 939 | $('#file_upload_type').removeClass('selected'); |
| 940 | } |
| 941 | } |
| 942 | }); |
| 943 | |
| 944 | // Mark as drag-drop upload when files are dropped, but don't hide overlay yet |
| 945 | $(document).on('drop', function (e) { |
| 946 | if (e.originalEvent.dataTransfer && e.originalEvent.dataTransfer.types.indexOf('Files') !== -1) { |
| 947 | dragCounter = 0; |
| 948 | window.isDragDropUpload = true; |
| 949 | // Don't hide overlay yet - let plupload events handle the UI transitions |
| 950 | } |
| 951 | }); |
| 952 | |
| 953 | // Original drag and drop area events (keep for backward compatibility) |
| 954 | $('#wpallimport-drag-drop-area').on('dragover', function (e) { |
| 955 | e.preventDefault(); |
| 956 | // Add the selected class to the upload a file button. |
| 957 | $('#file_upload_type').addClass('selected'); |
| 958 | }); |
| 959 | |
| 960 | // When the file is dragged out of the drag and drop area |
| 961 | $('#wpallimport-drag-drop-area').on('dragleave', function () { |
| 962 | // Remove the selected class (only if not uploading) |
| 963 | if (!window.isDragDropUpload) { |
| 964 | $('#file_upload_type').removeClass('selected'); |
| 965 | } |
| 966 | }); |
| 967 | |
| 968 | // Handle drop event on drag area to prevent default browser behavior |
| 969 | $('#wpallimport-drag-drop-area').on('drop', function (e) { |
| 970 | e.preventDefault(); |
| 971 | e.stopPropagation(); |
| 972 | // The actual file handling is done by plupload |
| 973 | }); |
| 974 | }); |
| 975 | |
| 976 | $('input[name=url]').on('change', function(){ |
| 977 | |
| 978 | }).on('keyup', function (e) { |
| 979 | if ($(this).val() != ''){ |
| 980 | $('.wpallimport-url-icon').addClass('focus'); |
| 981 | $(this).addClass('focus'); |
| 982 | } else { |
| 983 | $('.wpallimport-url-icon').removeClass('focus'); |
| 984 | $(this).removeClass('focus'); |
| 985 | } |
| 986 | }); |
| 987 | |
| 988 | $('#taxonomy_to_import').select2({ |
| 989 | width: '300px', |
| 990 | minimumResultsForSearch: 10, |
| 991 | theme: 'default' |
| 992 | }).on('select2:select', function(e){ |
| 993 | let selectedData = e.params.data; |
| 994 | if (selectedData.id != ""){ |
| 995 | $(this).next('.select2-container').find('.select2-selection__rendered').css({'color':'#555'}); |
| 996 | // Free version - show upgrade notice and don't allow progression for taxonomy imports |
| 997 | $('.wpallimport-upgrade-notice[rel=taxonomies]').show(); |
| 998 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 999 | // Reset the selection |
| 1000 | $(this).val('').trigger('change'); |
| 1001 | } else { |
| 1002 | $(this).next('.select2-container').find('.select2-selection__rendered').css({'color':'#cfceca'}); |
| 1003 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1004 | } |
| 1005 | $('input[name=taxonomy_type]').val(selectedData.id); |
| 1006 | // Trigger native change event to ensure all listeners are notified |
| 1007 | $(this).trigger('change'); |
| 1008 | }).on('change', function() { |
| 1009 | // This handler will be triggered by the native change event |
| 1010 | // Only proceed if we're not already in the select2:select handler |
| 1011 | if (!$(this).data('select2-processing')) { |
| 1012 | // Set a flag to prevent infinite loop |
| 1013 | $(this).data('select2-processing', true); |
| 1014 | |
| 1015 | var selectedValue = $(this).val(); |
| 1016 | if (selectedValue != "") { |
| 1017 | $(this).next('.select2-container').find('.select2-selection__rendered').css({'color':'#555'}); |
| 1018 | // Free version - don't allow progression for taxonomy imports |
| 1019 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1020 | } else { |
| 1021 | $(this).next('.select2-container').find('.select2-selection__rendered').css({'color':'#cfceca'}); |
| 1022 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1023 | } |
| 1024 | |
| 1025 | // Remove the processing flag |
| 1026 | $(this).data('select2-processing', false); |
| 1027 | } |
| 1028 | }); |
| 1029 | |
| 1030 | // Add tooltips to select2 options |
| 1031 | $('#taxonomy_to_import option').each(function() { |
| 1032 | var toolTipText = $(this).val(); |
| 1033 | $(this).attr('title', toolTipText); |
| 1034 | }); |
| 1035 | |
| 1036 | // Initialize gravity_form_to_import with Select2 |
| 1037 | $('#gravity_form_to_import').select2({ |
| 1038 | width: '300px', |
| 1039 | minimumResultsForSearch: 10, |
| 1040 | theme: 'default' |
| 1041 | }).on('select2:select', function(e){ |
| 1042 | let selectedData = e.params.data; |
| 1043 | |
| 1044 | if (selectedData.id != ""){ |
| 1045 | $(this).next('.select2-container').find('.select2-selection__rendered').css({'color':'#555'}); |
| 1046 | // Only show submit buttons if we're in the gravity forms context and upload step is visible |
| 1047 | if ($('.gravity_form_to_import_wrapper:visible').length && $('.wpallimport-upload-resource-step-two:visible').length) { |
| 1048 | // Check if any upgrade notice is visible before showing buttons |
| 1049 | if ($('.wpallimport-upgrade-notice:visible').length === 0) { |
| 1050 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').slideDown(); |
| 1051 | } |
| 1052 | } |
| 1053 | } else { |
| 1054 | $(this).next('.select2-container').find('.select2-selection__rendered').css({'color':'#cfceca'}); |
| 1055 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1056 | } |
| 1057 | $('input[name=gravity_form_title]').val(selectedData.id); |
| 1058 | }).on('change', function() { |
| 1059 | // Handle native change events |
| 1060 | if (!$(this).data('select2-processing')) { |
| 1061 | $(this).data('select2-processing', true); |
| 1062 | |
| 1063 | var selectedValue = $(this).val(); |
| 1064 | if (selectedValue != ""){ |
| 1065 | $(this).next('.select2-container').find('.select2-selection__rendered').css({'color':'#555'}); |
| 1066 | // Only show submit buttons if we're in the gravity forms context and upload step is visible |
| 1067 | if ($('.gravity_form_to_import_wrapper:visible').length && $('.wpallimport-upload-resource-step-two:visible').length) { |
| 1068 | // Check if any upgrade notice is visible before showing buttons |
| 1069 | if ($('.wpallimport-upgrade-notice:visible').length === 0) { |
| 1070 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').slideDown(); |
| 1071 | } |
| 1072 | } |
| 1073 | } else { |
| 1074 | $(this).next('.select2-container').find('.select2-selection__rendered').css({'color':'#cfceca'}); |
| 1075 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1076 | } |
| 1077 | $('input[name=gravity_form_title]').val(selectedValue); |
| 1078 | |
| 1079 | $(this).data('select2-processing', false); |
| 1080 | } |
| 1081 | }); |
| 1082 | |
| 1083 | // Add tooltips to gravity form select2 options |
| 1084 | $('#gravity_form_to_import option').each(function() { |
| 1085 | var toolTipText = $(this).val(); |
| 1086 | $(this).attr('title', toolTipText); |
| 1087 | }); |
| 1088 | |
| 1089 | // enter-submit form on step 1 |
| 1090 | $('.wpallimport-step-1').each(function(){ |
| 1091 | |
| 1092 | var $wrap = $('.wrap').has('.wpallimport-layout'); |
| 1093 | |
| 1094 | var formHeight = ($('.wpallimport-layout').height() < 730) ? 730 : $('.wpallimport-layout').height(); |
| 1095 | |
| 1096 | $('.wpallimport-import-from').on('click', function(){ |
| 1097 | |
| 1098 | var showImportType = false; |
| 1099 | |
| 1100 | switch ($(this).attr('rel')){ |
| 1101 | case 'upload_type': |
| 1102 | if ($('input[name=filepath]').val() != '') { |
| 1103 | showImportType = true; |
| 1104 | } |
| 1105 | $('.wpallimport-download-resource').hide(); |
| 1106 | break; |
| 1107 | case 'url_type': |
| 1108 | if ($('input[name=url]').val() != '') { |
| 1109 | showImportType = true; |
| 1110 | } |
| 1111 | $('.wpallimport-download-from-checked').trigger('click'); |
| 1112 | break; |
| 1113 | case 'file_type': |
| 1114 | // Free version - show interface but don't allow progression |
| 1115 | showImportType = false; // This prevents the submit buttons from showing |
| 1116 | $('.wpallimport-download-resource').hide(); |
| 1117 | break; |
| 1118 | } |
| 1119 | |
| 1120 | $('.wpallimport-import-from').removeClass('selected').addClass('bind'); |
| 1121 | $(this).addClass('selected').removeClass('bind'); |
| 1122 | $('.wpallimport-choose-file').find('.wpallimport-upload-type-container').hide(); |
| 1123 | $('.wpallimport-choose-file').find('.wpallimport-file-upload-result').attr('rel', $(this).attr('rel')); |
| 1124 | $('.wpallimport-choose-file').find('.wpallimport-upload-type-container[rel=' + $(this).attr('rel') + ']').show(); |
| 1125 | $('.wpallimport-choose-file').find('#wpallimport-url-upload-status').html(''); |
| 1126 | $('.wpallimport-choose-file').find('input[name=type]').val( $(this).attr('rel').replace('_type', '') ); |
| 1127 | |
| 1128 | if ($('.auto-generate-template').attr('rel') == $(this).attr('rel')){ |
| 1129 | $('.auto-generate-template').css({'display':'inline-block'}); |
| 1130 | } else { |
| 1131 | $('.auto-generate-template').hide(); |
| 1132 | } |
| 1133 | |
| 1134 | if ($(this).attr('rel') == 'upload_type'){ |
| 1135 | $('input[type=file]').trigger('click'); |
| 1136 | } |
| 1137 | // Check for denied imports and taxonomies |
| 1138 | var selectedPostType = $('input[name=custom_type]').val(); |
| 1139 | var is_import_denied = $('.wpallimport-upgrade-notice[rel="'+ selectedPostType +'"]').length; |
| 1140 | var is_taxonomy_import = (selectedPostType === 'taxonomies'); |
| 1141 | |
| 1142 | if ( ! showImportType){ |
| 1143 | $('.wpallimport-choose-file').find('.wpallimport-upload-resource-step-two').slideUp(); |
| 1144 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1145 | } else if (is_import_denied || is_taxonomy_import) { |
| 1146 | // Show upload step but hide submit buttons for denied/restricted imports |
| 1147 | $('.wpallimport-choose-file').find('.wpallimport-upload-resource-step-two').slideDown(); |
| 1148 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1149 | } else { |
| 1150 | $('.wpallimport-choose-file').find('.wpallimport-upload-resource-step-two').slideDown(); |
| 1151 | // Check if any upgrade notice is visible before showing buttons |
| 1152 | if ($('.wpallimport-upgrade-notice:visible').length === 0) { |
| 1153 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').show(); |
| 1154 | } else { |
| 1155 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1156 | } |
| 1157 | } |
| 1158 | }); |
| 1159 | |
| 1160 | $('.wpallimport-import-from.selected').trigger('click'); |
| 1161 | |
| 1162 | // Hide the H2 when "Download from URL" or "Use existing file" is clicked |
| 1163 | $('.wpallimport-url-type').on('click', function () { |
| 1164 | $('.wpallimport-import-types').find('h2').slideUp(); |
| 1165 | }); |
| 1166 | $('.wpallimport-file-type').on('click', function () { |
| 1167 | $('.wpallimport-import-types').find('h2').slideUp(); |
| 1168 | }); |
| 1169 | |
| 1170 | $('.wpallimport-download-from-url').on('click', function(){ |
| 1171 | |
| 1172 | // Show upgrade notice for Free version |
| 1173 | let $type = $('input[name=type]').val(); |
| 1174 | if ($type === 'url') { |
| 1175 | $('.wpallimport-download-resource-step-two-url .wpallimport-free-edition-notice').show(); |
| 1176 | return false; // Prevent the download from proceeding |
| 1177 | } else if ($type === 'ftp') { |
| 1178 | $('.wpallimport-download-resource-step-two-ftp .wpallimport-free-edition-notice').show(); |
| 1179 | return false; // Prevent the download from proceeding |
| 1180 | } |
| 1181 | |
| 1182 | }); |
| 1183 | |
| 1184 | var fixWrapHeight = false; |
| 1185 | |
| 1186 | $('#custom_type_selector').select2({ |
| 1187 | width: '590px', |
| 1188 | minimumResultsForSearch: 10, |
| 1189 | theme: 'default', |
| 1190 | templateResult: function(data) { |
| 1191 | if (!data.id) return data.text; |
| 1192 | var $option = $(data.element); |
| 1193 | var $result = $('<div class="select2-option-wrapper"></div>'); |
| 1194 | var imagesrc = $option.attr('data-imagesrc'); |
| 1195 | var customImageClass = $option.attr('data-custom-image-class'); |
| 1196 | |
| 1197 | if (imagesrc) { |
| 1198 | var spanClass = imagesrc; |
| 1199 | if (customImageClass) { |
| 1200 | // If there's a custom image class, add it to the span |
| 1201 | spanClass = imagesrc.replace(/dashicon\s+[^\s]+/, 'dashicon ' + customImageClass); |
| 1202 | } |
| 1203 | var $icon = $('<span class="' + spanClass + '" style="display:inline-block !important; vertical-align:middle !important; margin-right:5px !important; visibility:visible !important; opacity:1 !important;"></span>'); |
| 1204 | $result.append($icon); |
| 1205 | } |
| 1206 | $result.append(data.text); |
| 1207 | return $result; |
| 1208 | }, |
| 1209 | templateSelection: function(data) { |
| 1210 | if (!data.id) return data.text; |
| 1211 | var $option = $(data.element); |
| 1212 | var $result = $('<div class="select2-selection-wrapper"></div>'); |
| 1213 | var imagesrc = $option.attr('data-imagesrc'); |
| 1214 | var customImageClass = $option.attr('data-custom-image-class'); |
| 1215 | |
| 1216 | if (imagesrc) { |
| 1217 | var spanClass = imagesrc; |
| 1218 | if (customImageClass) { |
| 1219 | // If there's a custom image class, add it to the span |
| 1220 | spanClass = imagesrc.replace(/dashicon\s+[^\s]+/, 'dashicon ' + customImageClass); |
| 1221 | } |
| 1222 | var $icon = $('<span class="' + spanClass + '" style="display:inline-block !important; vertical-align:middle !important; margin-right:5px !important; visibility:visible !important; opacity:1 !important;"></span>'); |
| 1223 | $result.append($icon); |
| 1224 | } |
| 1225 | $result.append(data.text); |
| 1226 | |
| 1227 | // After rendering, trigger the custom image handler |
| 1228 | setTimeout(function() { |
| 1229 | wpai_set_custom_select_image(); |
| 1230 | }, 10); |
| 1231 | |
| 1232 | return $result; |
| 1233 | } |
| 1234 | }).on('select2:opening', function(e){ |
| 1235 | formHeight = ($('.wpallimport-layout').height() < 730) ? 730 : $('.wpallimport-layout').height(); |
| 1236 | // Store the original height to restore later |
| 1237 | $(this).data('originalHeight', $wrap.height()); |
| 1238 | }).on('select2:open', function(e){ |
| 1239 | // Set timeout to allow the dropdown to render before measuring |
| 1240 | setTimeout(function() { |
| 1241 | $wrap.css({'height': formHeight + $('.select2-dropdown').height() + 'px'}); |
| 1242 | }, 50); |
| 1243 | }).on('select2:closing', function(e){ |
| 1244 | $wrap.css({'height': 'auto'}); |
| 1245 | }).on('select2:select', function(e){ |
| 1246 | let selectedData = e.params.data; |
| 1247 | |
| 1248 | if (fixWrapHeight){ |
| 1249 | $wrap.css({'height': 'auto'}); |
| 1250 | } else { |
| 1251 | fixWrapHeight = true; |
| 1252 | } |
| 1253 | |
| 1254 | $('.wpallimport-upgrade-notice').hide(); |
| 1255 | |
| 1256 | $('input[name=custom_type]').val(selectedData.id); |
| 1257 | |
| 1258 | var is_import_denied = $('.wpallimport-upgrade-notice[rel="'+ selectedData.id +'"]').length; |
| 1259 | // Free version - also deny taxonomies |
| 1260 | var is_taxonomy_import = (selectedData.id === 'taxonomies'); |
| 1261 | |
| 1262 | if (is_import_denied){ |
| 1263 | $('.wpallimport-upgrade-notice[rel="'+ selectedData.id +'"]').slideDown(); |
| 1264 | } |
| 1265 | |
| 1266 | if ($('.wpallimport-upload-resource-step-two:visible').length && ! is_import_denied && ! is_taxonomy_import) { |
| 1267 | $(this).next('.select2-container').find('.select2-selection__rendered').css({'color':'#555'}); |
| 1268 | // Check if any upgrade notice is visible before showing buttons |
| 1269 | if ($('.wpallimport-upgrade-notice:visible').length === 0) { |
| 1270 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').slideDown(); |
| 1271 | } else { |
| 1272 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1273 | } |
| 1274 | } else { |
| 1275 | $(this).next('.select2-container').find('.select2-selection__rendered').css({'color':'#555'}); |
| 1276 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1277 | } |
| 1278 | |
| 1279 | switch (selectedData.id) { |
| 1280 | case 'taxonomies': |
| 1281 | $('.taxonomy_to_import_wrapper').slideDown(); |
| 1282 | $('.gravity_form_to_import_wrapper').slideUp(); |
| 1283 | // Free version - always hide submit buttons for taxonomy imports |
| 1284 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1285 | break; |
| 1286 | case 'gf_entries': |
| 1287 | $('.taxonomy_to_import_wrapper').slideUp(); |
| 1288 | $('.gravity_form_to_import_wrapper').slideDown(); |
| 1289 | var selectedForm = $('input[name=gravity_form_title]').val(); |
| 1290 | if (selectedForm == ''){ |
| 1291 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1292 | } else { |
| 1293 | // Only show submit buttons if upload step is visible |
| 1294 | if ($('.wpallimport-upload-resource-step-two:visible').length) { |
| 1295 | // Check if any upgrade notice is visible before showing buttons |
| 1296 | if ($('.wpallimport-upgrade-notice:visible').length === 0) { |
| 1297 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').slideDown(); |
| 1298 | } |
| 1299 | } |
| 1300 | } |
| 1301 | break; |
| 1302 | default: |
| 1303 | $('.taxonomy_to_import_wrapper').slideUp(); |
| 1304 | $('.gravity_form_to_import_wrapper').slideUp(); |
| 1305 | break; |
| 1306 | } |
| 1307 | |
| 1308 | // Rapid Add-On API Images |
| 1309 | wpai_set_custom_select_image(); |
| 1310 | |
| 1311 | // Trigger native change event to ensure all listeners are notified |
| 1312 | $(this).trigger('change'); |
| 1313 | }).on('change', function() { |
| 1314 | // This handler will be triggered by the native change event |
| 1315 | // Only proceed if we're not already in the select2:select handler |
| 1316 | if (!$(this).data('select2-processing')) { |
| 1317 | // Set a flag to prevent infinite loop |
| 1318 | $(this).data('select2-processing', true); |
| 1319 | |
| 1320 | var selectedValue = $(this).val(); |
| 1321 | |
| 1322 | switch (selectedValue) { |
| 1323 | case 'taxonomies': |
| 1324 | $('.taxonomy_to_import_wrapper').slideDown(); |
| 1325 | $('.gravity_form_to_import_wrapper').slideUp(); |
| 1326 | // Free version - always hide submit buttons for taxonomy imports |
| 1327 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1328 | break; |
| 1329 | case 'gf_entries': |
| 1330 | $('.taxonomy_to_import_wrapper').slideUp(); |
| 1331 | $('.gravity_form_to_import_wrapper').slideDown(); |
| 1332 | var selectedForm = $('input[name=gravity_form_title]').val(); |
| 1333 | if (selectedForm == ''){ |
| 1334 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').hide(); |
| 1335 | } else { |
| 1336 | // Only show submit buttons if upload step is visible |
| 1337 | if ($('.wpallimport-upload-resource-step-two:visible').length) { |
| 1338 | // Check if any upgrade notice is visible before showing buttons |
| 1339 | if ($('.wpallimport-upgrade-notice:visible').length === 0) { |
| 1340 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').slideDown(); |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | break; |
| 1345 | default: |
| 1346 | $('.taxonomy_to_import_wrapper').slideUp(); |
| 1347 | $('.gravity_form_to_import_wrapper').slideUp(); |
| 1348 | // Show submit buttons for non-taxonomy, non-gravity form post types |
| 1349 | if ($('.wpallimport-upload-resource-step-two:visible').length) { |
| 1350 | // Check if any upgrade notice is visible before showing buttons |
| 1351 | if ($('.wpallimport-upgrade-notice:visible').length === 0) { |
| 1352 | $('.wpallimport-choose-file').find('.wpallimport-submit-buttons').slideDown(); |
| 1353 | } |
| 1354 | } |
| 1355 | break; |
| 1356 | } |
| 1357 | |
| 1358 | // Remove the processing flag |
| 1359 | $(this).data('select2-processing', false); |
| 1360 | } |
| 1361 | }); |
| 1362 | |
| 1363 | |
| 1364 | |
| 1365 | $('.wpallimport-download-from').on('click', function(){ |
| 1366 | if ($(this).attr('rel') === 'url') { |
| 1367 | $('.wpallimport-download-resource-step-two-url').show(); |
| 1368 | $('.wpallimport-download-resource-step-two-ftp').hide(); |
| 1369 | // Show upgrade notice for URL download in Free version |
| 1370 | $('.wpallimport-download-resource-step-two-url .wpallimport-free-edition-notice').show(); |
| 1371 | } else { |
| 1372 | $('.wpallimport-download-resource-step-two-url').hide(); |
| 1373 | $('.wpallimport-download-resource-step-two-ftp').show(); |
| 1374 | // Show upgrade notice for FTP download in Free version |
| 1375 | $('.wpallimport-download-resource-step-two-ftp .wpallimport-free-edition-notice').show(); |
| 1376 | } |
| 1377 | $('.wpallimport-download-from').removeClass('wpallimport-download-from-checked'); |
| 1378 | $(this).addClass('wpallimport-download-from-checked'); |
| 1379 | $('.wpallimport-choose-file').find('input[name=type]').val( $(this).attr('rel') ); |
| 1380 | $('.dd-container').fadeIn(); |
| 1381 | }); |
| 1382 | |
| 1383 | $('#custom_type_selector').hide(); |
| 1384 | |
| 1385 | |
| 1386 | $('.wpallimport-download-from.wpallimport-download-from-checked').trigger('click'); |
| 1387 | |
| 1388 | $('a.auto-generate-template').on('click', function(){ |
| 1389 | $('input[name^=auto_generate]').val('1'); |
| 1390 | $(this).parents('form:first').submit(); |
| 1391 | }); |
| 1392 | |
| 1393 | $('a.create-filters-step').on('click', function(){ |
| 1394 | $('input[name^=go_to_create_filters]').val('1'); |
| 1395 | $(this).parents('form:first').submit(); |
| 1396 | }); |
| 1397 | |
| 1398 | $('#advanced_upload').on('click', function(){ |
| 1399 | $('input[name^=auto_generate]').val('1'); |
| 1400 | }); |
| 1401 | }); |
| 1402 | //[/End Step 1] |
| 1403 | |
| 1404 | // template form: auto submit when `load template` list value is picked |
| 1405 | $('form.wpallimport-template').find('select[name="load_template"]').on('change', function () { |
| 1406 | $(this).parents('form').submit(); |
| 1407 | }); |
| 1408 | |
| 1409 | var serialize_ctx_mapping = function(){ |
| 1410 | $('.custom_type[rel=tax_mapping]').each(function(){ |
| 1411 | var values = new Array(); |
| 1412 | $(this).find('.form-field').each(function(){ |
| 1413 | if ($(this).find('.mapping_to').val() != "") { |
| 1414 | var skey = $(this).find('.mapping_from').val(); |
| 1415 | if ('' != skey){ |
| 1416 | var obj = {}; |
| 1417 | obj[skey] = $(this).find('.mapping_to').val(); |
| 1418 | values.push(obj); |
| 1419 | } |
| 1420 | } |
| 1421 | }); |
| 1422 | $(this).find('input[name^=tax_mapping]').val(window.JSON.stringify(values)); |
| 1423 | }); |
| 1424 | }; |
| 1425 | |
| 1426 | // add expander |
| 1427 | $(document).on('click', '.xml-expander', function () { |
| 1428 | var method; |
| 1429 | if ('-' == $(this).text()) { |
| 1430 | $(this).text('+'); |
| 1431 | method = 'addClass'; |
| 1432 | } else { |
| 1433 | $(this).text('-'); |
| 1434 | method = 'removeClass'; |
| 1435 | } |
| 1436 | // for nested representation based on div |
| 1437 | $(this).parent().find('> .xml-content')[method]('collapsed'); |
| 1438 | // for nested representation based on tr |
| 1439 | var $tr = $(this).parent().parent().filter('tr.xml-element').next()[method]('collapsed'); |
| 1440 | }); |
| 1441 | |
| 1442 | // [xml representation dynamic] |
| 1443 | $.fn.xml = function (opt) { |
| 1444 | if ( ! this.length) return this; |
| 1445 | |
| 1446 | var $self = this; |
| 1447 | var opt = opt || {}; |
| 1448 | var action = {}; |
| 1449 | if ('object' == typeof opt) { |
| 1450 | action = opt; |
| 1451 | } else { |
| 1452 | action[opt] = true; |
| 1453 | } |
| 1454 | action = $.extend({init: ! this.data('initialized')}, action); |
| 1455 | |
| 1456 | if (action.init) { |
| 1457 | this.data('initialized', true); |
| 1458 | } |
| 1459 | if (action.dragable) { |
| 1460 | |
| 1461 | this.find('.xml-tag.opening > .xml-tag-name, .xml-attr-name, .csv-tag.opening > .csv-tag-name, .ui-menu-item').each(function () { |
| 1462 | var $this = $(this); |
| 1463 | var xpath = '.'; |
| 1464 | if ($this.is('.xml-attr-name')) |
| 1465 | xpath = '{' + ($this.parents('.xml-element:first').attr('title').replace(/^\/[^\/]+\/?/, '') || '.') + '/@' + $this.html().trim() + '}'; |
| 1466 | else if($this.is('.ui-menu-item')) |
| 1467 | xpath = '{' + ($this.attr('title').replace(/^\/[^\/]+\/?/, '') || '.') + '}'; |
| 1468 | else |
| 1469 | xpath = '{' + ($this.parent().parent().attr('title').replace(/^\/[^\/]+\/?/, '') || '.') + '}'; |
| 1470 | |
| 1471 | $this.data('xpath', xpath).on('dblclick', function(e) { |
| 1472 | $wpAllImportDrag = $(this); |
| 1473 | $wpAllImportOriginalColor = $wpAllImportDrag.css('color'); |
| 1474 | $wpAllImportDrag.css('color', '#46ba69').css('font-weight','900'); |
| 1475 | }).draggable({ |
| 1476 | helper: function() { |
| 1477 | return $('<div>').text($(this).data('xpath')).css({ |
| 1478 | 'padding': '5px', |
| 1479 | 'border-radius': '5px' |
| 1480 | }); |
| 1481 | }, |
| 1482 | zIndex: 2147483646, |
| 1483 | appendTo: 'body', |
| 1484 | cursor: 'pointer', |
| 1485 | iframeFix:true |
| 1486 | }).css('cursor', 'pointer'); |
| 1487 | }); |
| 1488 | |
| 1489 | wpaiMakeDroppable(); |
| 1490 | } |
| 1491 | |
| 1492 | return this; |
| 1493 | }; |
| 1494 | |
| 1495 | // template form: preview button |
| 1496 | $('form.wpallimport-template').each(function () { |
| 1497 | var $form = $(this); |
| 1498 | |
| 1499 | // The form should not submit when Enter is pressed. |
| 1500 | $form.on('keypress', function (event) { |
| 1501 | var keycode = (event.keyCode ? event.keyCode : event.which); |
| 1502 | |
| 1503 | if (keycode === 13 && event.target.tagName !== 'TEXTAREA') { |
| 1504 | event.preventDefault(); |
| 1505 | } |
| 1506 | }); |
| 1507 | |
| 1508 | var $detected_cf = new Array(); |
| 1509 | |
| 1510 | $form.find('.preview, .preview_images, .preview_taxonomies, .preview_prices').on('click', function () { |
| 1511 | var $preview_type = $(this).attr('rel'); |
| 1512 | var $options_slug = $(this).parent('div').find('.wp_all_import_section_slug').val(); |
| 1513 | |
| 1514 | if ($preview_type == 'preview_taxonomies') serialize_ctx_mapping(); |
| 1515 | |
| 1516 | var $URL = 'admin.php?page=pmxi-admin-import&action=' + $preview_type + ((typeof import_id != "undefined") ? '&id=' + import_id : ''); |
| 1517 | var $tagURL = 'admin.php?page=pmxi-admin-import&action=tag' + ((typeof import_id != "undefined") ? '&id=' + import_id : ''); |
| 1518 | |
| 1519 | if ($options_slug != undefined) $URL += '&slug=' + $options_slug; |
| 1520 | |
| 1521 | $('.wpallimport-overlay').show(); |
| 1522 | |
| 1523 | var $ths = $(this); |
| 1524 | |
| 1525 | $(this).pointer({ |
| 1526 | content: '<div class="wpallimport-preview-preload wpallimport-pointer-' + $preview_type + '"></div>', |
| 1527 | position: { |
| 1528 | edge: 'right', |
| 1529 | align: 'center' |
| 1530 | }, |
| 1531 | pointerWidth: ($preview_type == 'preview_images') ? 800 : 715, |
| 1532 | close: function() { |
| 1533 | $.post( ajaxurl, { |
| 1534 | pointer: 'pksn1', |
| 1535 | action: 'dismiss-wp-pointer' |
| 1536 | }); |
| 1537 | $('.wpallimport-overlay').hide(); |
| 1538 | } |
| 1539 | }).pointer('open'); |
| 1540 | |
| 1541 | var $pointer = $('.wpallimport-pointer-' + $preview_type).parents('.wp-pointer').first(); |
| 1542 | |
| 1543 | var $leftOffset = ($(window).width() - (($preview_type == 'preview_images') ? 800 : 715))/2; |
| 1544 | |
| 1545 | $pointer.css({'position':'fixed', 'top' : '15%', 'left' : $leftOffset + 'px'}); |
| 1546 | |
| 1547 | if (typeof tinyMCE != 'undefined') tinyMCE.triggerSave(false, false); |
| 1548 | |
| 1549 | $.post($URL, $form.serialize(), function (response) { |
| 1550 | |
| 1551 | $ths.pointer({'content' : response.html}); |
| 1552 | |
| 1553 | $pointer.css({'position':'fixed', 'top' : '15%', 'left' : $leftOffset + 'px'}); |
| 1554 | |
| 1555 | var $preview = $('.wpallimport-' + $preview_type); |
| 1556 | |
| 1557 | $preview.parent('.wp-pointer-content').removeClass('wp-pointer-content').addClass('wpallimport-pointer-content'); |
| 1558 | |
| 1559 | var $tag = $('.tag'); |
| 1560 | var tagno = parseInt($tag.find('input[name="tagno"]').val()); |
| 1561 | $preview.find('.navigation a').unbind('click').off('click').on('click', function () { |
| 1562 | tagno += '#prev' == $(this).attr('href') ? -1 : 1; |
| 1563 | $tag.addClass('loading').css('opacity', 0.7); |
| 1564 | $preview.addClass('loading').css('opacity', 0.7); |
| 1565 | $.post($tagURL, {tagno: tagno, import_action: import_action, security: wp_all_import_security}, function (data) { |
| 1566 | var $indicator = $('<span />').insertBefore($tag); |
| 1567 | $tag.replaceWith(data.html); |
| 1568 | fix_tag_position(); |
| 1569 | $indicator.next().tag().prevObject.remove(); |
| 1570 | if ($('#variations_xpath').length){ |
| 1571 | $('#variations_xpath').data('checkedValue', '').trigger('change'); |
| 1572 | } |
| 1573 | $preview.find('input[name="tagno"]').off(); |
| 1574 | $preview.find('.navigation a').off('click'); |
| 1575 | $form.find('.' + $preview_type).trigger('click'); |
| 1576 | |
| 1577 | }, 'json'); |
| 1578 | return false; |
| 1579 | }); |
| 1580 | $preview.find('input[name="tagno"]').unbind('click').off('click').on('change', function () { |
| 1581 | tagno = (parseInt($(this).val()) > parseInt($preview.find('.pmxi_count').html())) ? $preview.find('.pmxi_count').html() : ( (parseInt($(this).val())) ? $(this).val() : 1 ); |
| 1582 | $tag.addClass('loading').css('opacity', 0.7); |
| 1583 | $.post($tagURL, {tagno: tagno, security: wp_all_import_security}, function (data) { |
| 1584 | var $indicator = $('<span />').insertBefore($tag); |
| 1585 | $tag.replaceWith(data.html); |
| 1586 | fix_tag_position(); |
| 1587 | $indicator.next().tag().prevObject.remove(); |
| 1588 | if ($('#variations_xpath').length){ |
| 1589 | $('#variations_xpath').data('checkedValue', '').trigger('change'); |
| 1590 | } |
| 1591 | $preview.find('input[name="tagno"]').off(); |
| 1592 | $preview.find('.navigation a').off('click'); |
| 1593 | $form.find('.' + $preview_type).trigger('click'); |
| 1594 | }, 'json'); |
| 1595 | return false; |
| 1596 | }); |
| 1597 | |
| 1598 | }, 'json'); |
| 1599 | return false; |
| 1600 | }); |
| 1601 | |
| 1602 | $form.find('.set_encoding').on('click', function(e){ |
| 1603 | e.preventDefault(); |
| 1604 | $form.find('a[rel="preview"].preview').trigger('click'); |
| 1605 | }); |
| 1606 | |
| 1607 | $form.find('input[name$=download_images]').each(function(){ |
| 1608 | if ($(this).is(':checked') && $(this).val() == 'gallery' ) { |
| 1609 | $(this).parents('.wpallimport-collapsed-content:first').find('.advanced_options_files').find('p:first').show(); |
| 1610 | $(this).parents('.wpallimport-collapsed-content:first').find('.advanced_options_files').find('input').attr({'disabled':'disabled'}); |
| 1611 | } |
| 1612 | }); |
| 1613 | |
| 1614 | $form.find('input[name$=download_images]').on('click', function(){ |
| 1615 | if ($(this).is(':checked') && $(this).val() == 'gallery' ) { |
| 1616 | $(this).parents('.wpallimport-collapsed-content:first').find('.advanced_options_files').find('p:first').show(); |
| 1617 | $(this).parents('.wpallimport-collapsed-content:first').find('.advanced_options_files').find('input').attr({'disabled':'disabled'}); |
| 1618 | } else { |
| 1619 | $(this).parents('.wpallimport-collapsed-content:first').find('.advanced_options_files').find('p:first').hide(); |
| 1620 | $(this).parents('.wpallimport-collapsed-content:first').find('.advanced_options_files').find('input').removeAttr('disabled'); |
| 1621 | } |
| 1622 | }); |
| 1623 | |
| 1624 | // Auto-detect custom fields |
| 1625 | $form.find('.auto_detect_cf').on('click', function(){ |
| 1626 | |
| 1627 | var parent = $(this).parents('.wpallimport-collapsed-content:first'); |
| 1628 | var request = { |
| 1629 | action:'auto_detect_cf', |
| 1630 | fields: $('#existing_meta_keys').val().split(','), |
| 1631 | post_type: $('input[name=custom_type]').val(), |
| 1632 | security: wp_all_import_security |
| 1633 | }; |
| 1634 | $(this).attr({'disabled':'disabled'}); |
| 1635 | |
| 1636 | var $indicator = $('<span class="img_preloader" style="top:0;"/>').insertBefore($(this)).show(); |
| 1637 | |
| 1638 | var ths = $(this); |
| 1639 | |
| 1640 | $.ajax({ |
| 1641 | type: 'POST', |
| 1642 | url: ajaxurl, |
| 1643 | data: request, |
| 1644 | success: function(response) { |
| 1645 | |
| 1646 | parent.find('input[name^=custom_name]:visible').each(function(){ |
| 1647 | if ("" == $(this).val()) $(this).parents('tr').first().remove(); |
| 1648 | }); |
| 1649 | |
| 1650 | $detected_cf = response.result; |
| 1651 | |
| 1652 | var $added_fields_count = 0; |
| 1653 | if (response.result.length){ |
| 1654 | for (var i = 0; i < response.result.length; i++){ |
| 1655 | var allow_add = true; |
| 1656 | parent.find('input[name^=custom_name]:visible').each(function(){ |
| 1657 | if (response.result[i].key == "" || response.result[i].key == $(this).val()) { |
| 1658 | allow_add = false; |
| 1659 | return false; |
| 1660 | } |
| 1661 | }); |
| 1662 | // if this field doesn't present in custom fields section then put it there |
| 1663 | if ( allow_add ){ |
| 1664 | parent.find('a.add-new-custom').trigger('click'); |
| 1665 | var fieldParent = parent.find('.form-field:visible').last(); |
| 1666 | fieldParent.find('input[name^=custom_name]:visible').last().val(response.result[i].key); |
| 1667 | fieldParent.find('textarea[name^=custom_value]:visible').last().val(response.result[i].val); |
| 1668 | if (response.result[i].is_serialized) fieldParent.find('.set_serialize').last().parent().trigger('click'); |
| 1669 | $added_fields_count++; |
| 1670 | } |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | $indicator.remove(); |
| 1675 | |
| 1676 | $('.cf_detected').html(response.msg); |
| 1677 | $('.cf_welcome').hide(); |
| 1678 | $('.cf_detect_result').fadeIn(); |
| 1679 | |
| 1680 | ths.removeAttr('disabled'); |
| 1681 | }, |
| 1682 | error: function(request) { |
| 1683 | $indicator.remove(); |
| 1684 | ths.removeAttr('disabled'); |
| 1685 | }, |
| 1686 | dataType: "json" |
| 1687 | }); |
| 1688 | }); |
| 1689 | |
| 1690 | // Clear all detected custom fields |
| 1691 | $form.find('.clear_detected_cf').on('click', function(){ |
| 1692 | var parent = $(this).parents('.wpallimport-collapsed-content:first'); |
| 1693 | if ($detected_cf.length){ |
| 1694 | for (var i = 0; i < $detected_cf.length; i++){ |
| 1695 | parent.find('input[name^=custom_name]:visible').each(function(){ |
| 1696 | if ($detected_cf[i].key == $(this).val()) $(this).parents('tr').first().remove(); |
| 1697 | }); |
| 1698 | } |
| 1699 | } |
| 1700 | if ( ! parent.find('input[name^=custom_name]:visible').length){ |
| 1701 | parent.find('a.add-new-custom').trigger('click'); |
| 1702 | } |
| 1703 | $('.cf_detected').html(''); |
| 1704 | $('.cf_detect_result').hide(); |
| 1705 | $('.cf_welcome').fadeIn(); |
| 1706 | $detected_cf = new Array(); |
| 1707 | }); |
| 1708 | |
| 1709 | // toggle custom field as serialized/default |
| 1710 | $form.find('.wpallimport-cf-menu li').on('click', function(){ |
| 1711 | var $triggerEvent = $(this).find('a'); |
| 1712 | if ($triggerEvent.hasClass('set_serialize')){ |
| 1713 | var parent = $triggerEvent.parents('.form-field:first'); |
| 1714 | var parent_custom_format = parent.find('input[name^=custom_format]:first'); |
| 1715 | var parent_custom_value = parent.find('textarea[name^=custom_value]:first'); |
| 1716 | if (parseInt(parent_custom_format.val())){ |
| 1717 | parent_custom_format.val(0); |
| 1718 | parent.find('.specify_cf:first').hide(); |
| 1719 | parent_custom_value.fadeIn(); |
| 1720 | $triggerEvent.parent().removeClass('active'); |
| 1721 | } else { |
| 1722 | parent_custom_format.val(1); |
| 1723 | parent_custom_value.hide(); |
| 1724 | parent.find('.specify_cf:first').fadeIn(); |
| 1725 | $triggerEvent.parent().addClass('active'); |
| 1726 | } |
| 1727 | } |
| 1728 | }); |
| 1729 | |
| 1730 | // [Serialized custom fields] |
| 1731 | |
| 1732 | // Save serialized custom field format |
| 1733 | $(document).on('click', '.save_sf', function(){ |
| 1734 | var $source = $(this).parents('table:first'); |
| 1735 | var $destination = $('div#' + $source.attr('rel')); |
| 1736 | $destination.find('table:first').html(''); |
| 1737 | $source.find('input').each(function(i, e){ |
| 1738 | $(this).attr("value", $(this).val()); |
| 1739 | }); |
| 1740 | $destination.find('table:first').html($source.html()); |
| 1741 | $destination.parents('td:first').find('.pmxi_cf_pointer').pointer('destroy'); |
| 1742 | $('.wpallimport-overlay').hide(); |
| 1743 | }); |
| 1744 | |
| 1745 | // Auto-detect serialized custom fields |
| 1746 | $(document).on('click', '.auto_detect_sf', function() { |
| 1747 | var $source = $(this).parents('table:first'); |
| 1748 | var $destination = $('div#' + $source.attr('rel')); |
| 1749 | var $parentDestination = $destination.parents('tr:first'); |
| 1750 | var $cf_name = $parentDestination.find('input[name^=custom_name]:first').val(); |
| 1751 | |
| 1752 | if ($cf_name != ''){ |
| 1753 | var request = { |
| 1754 | action:'auto_detect_sf', |
| 1755 | security: wp_all_import_security, |
| 1756 | post_type: $('input[name=custom_type]').val(), |
| 1757 | name: $cf_name |
| 1758 | }; |
| 1759 | $(this).attr({'disabled':'disabled'}); |
| 1760 | |
| 1761 | var $indicator = $('<span class="img_preloader" style="position: absolute; top:0;"/>').insertBefore($(this)).show(); |
| 1762 | var ths = $(this); |
| 1763 | |
| 1764 | $.ajax({ |
| 1765 | type: 'POST', |
| 1766 | url: ajaxurl, |
| 1767 | data: request, |
| 1768 | success: function(response) { |
| 1769 | if (response.result.length){ |
| 1770 | $destination.find('tr.form-field').each(function(){ |
| 1771 | if ( ! $(this).hasClass('template') ) $(this).remove(); |
| 1772 | }); |
| 1773 | for (var i = 0; i < response.result.length; i++){ |
| 1774 | $destination.find('a.add-new-key').trigger('click'); |
| 1775 | $destination.find('tr.form-field').not('.template').last().css({"opacity": 1}).find('input.serialized_key').attr("value", response.result[i].key); |
| 1776 | $destination.find('tr.form-field').not('.template').last().css({"opacity": 1}).find('input.serialized_value').attr("value", response.result[i].val); |
| 1777 | } |
| 1778 | $destination.parents('td:first').find('.pmxi_cf_pointer').pointer('destroy'); |
| 1779 | $destination.parents('td:first').find('.pmxi_cf_pointer').trigger('click'); |
| 1780 | } else { |
| 1781 | var $notice = $('<p style="color:red; position: absolute; top: -10px; padding:0; margin:0;">No fields detected.</p>').insertBefore(ths).show(); |
| 1782 | setTimeout(function() { |
| 1783 | $notice.slideUp().remove(); |
| 1784 | }, 2500); |
| 1785 | } |
| 1786 | $indicator.remove(); |
| 1787 | ths.removeAttr('disabled'); |
| 1788 | }, |
| 1789 | error: function(request) { |
| 1790 | $indicator.remove(); |
| 1791 | ths.removeAttr('disabled'); |
| 1792 | }, |
| 1793 | dataType: "json" |
| 1794 | }); |
| 1795 | } |
| 1796 | }); |
| 1797 | |
| 1798 | // [/ Serialized custom fields] |
| 1799 | |
| 1800 | // Save mapping rules for custom field |
| 1801 | $(document).on('click', '.save_mr', function(){ |
| 1802 | let $source = $(this).parents('table:first'); |
| 1803 | let $destination = $('div#' + $source.attr('rel')); |
| 1804 | let $is_active = false; |
| 1805 | $destination.find('table:first').html(''); |
| 1806 | $source.find('input').each(function(i, e){ |
| 1807 | $(this).attr("value", $(this).val()); |
| 1808 | if ($(this).val() != "") { |
| 1809 | $is_active = true; |
| 1810 | } |
| 1811 | }); |
| 1812 | let $box = $destination.parents('td.action:first'); |
| 1813 | if ( $is_active ){ |
| 1814 | $box.find('.set_mapping').parent().addClass('active'); |
| 1815 | } else { |
| 1816 | $box.find('.set_mapping').parent().removeClass('active'); |
| 1817 | } |
| 1818 | $destination.find('table:first').html($source.html()); |
| 1819 | $destination.parents('td:first').find('.pmxi_cf_mapping').pointer('destroy'); |
| 1820 | $('.wpallimport-overlay').hide(); |
| 1821 | }); |
| 1822 | |
| 1823 | // Taxonnomies |
| 1824 | $form.find('#show_hidden_ctx').on('click', function(){ |
| 1825 | $form.find('tr.private_ctx').toggle(); |
| 1826 | }); |
| 1827 | |
| 1828 | // Test & Preview images |
| 1829 | $(document).on('click', '.test_images', function(){ |
| 1830 | |
| 1831 | let ths = $(this); |
| 1832 | |
| 1833 | $(this).attr({'disabled':'disabled'}); |
| 1834 | |
| 1835 | $('.img_preloader').show(); |
| 1836 | $('.img_success').html('').hide(); |
| 1837 | $('.img_failed').remove(); |
| 1838 | |
| 1839 | let imgs = new Array(); |
| 1840 | |
| 1841 | $('.images_list').find('li').each(function(){ |
| 1842 | imgs.push($(this).attr('rel')); |
| 1843 | }); |
| 1844 | |
| 1845 | let request = { |
| 1846 | action: 'test_images', |
| 1847 | security: wp_all_import_security, |
| 1848 | download: ths.attr('rel'), |
| 1849 | imgs:imgs |
| 1850 | }; |
| 1851 | |
| 1852 | $.ajax({ |
| 1853 | type: 'POST', |
| 1854 | url: ajaxurl, |
| 1855 | data: request, |
| 1856 | success: function(response) { |
| 1857 | $('.img_preloader').hide(); |
| 1858 | if ( parseInt(response.success_images)) { |
| 1859 | $('.img_success').html(response.success_msg).show(); |
| 1860 | } |
| 1861 | if (response.failed_msgs.length){ |
| 1862 | for (var i = 0; i < response.failed_msgs.length; i++){ |
| 1863 | $('.test_progress').append('<div class="img_failed">' + response.failed_msgs[i] + '</div>'); |
| 1864 | } |
| 1865 | $('.img_failed').show(); |
| 1866 | } |
| 1867 | ths.removeAttr('disabled'); |
| 1868 | }, |
| 1869 | error: function(request) { |
| 1870 | $('.img_failed').html(request.textStatus).show(); |
| 1871 | ths.removeAttr('disabled'); |
| 1872 | }, |
| 1873 | dataType: "json" |
| 1874 | }); |
| 1875 | |
| 1876 | }); |
| 1877 | |
| 1878 | /* Merge Main XML file with sub file by provided fields */ |
| 1879 | $form.find('.parse').on('click', function(){ |
| 1880 | |
| 1881 | var submit = true; |
| 1882 | |
| 1883 | if ("" == $form.find('input[name=nested_url]').val()){ |
| 1884 | $form.find('input[name=nested_url]').css({'background':'red'}); |
| 1885 | submit = false; |
| 1886 | } |
| 1887 | |
| 1888 | if (submit){ |
| 1889 | |
| 1890 | var ths = $(this); |
| 1891 | var $fileURL = $form.find('input[name=nested_url]').val(); |
| 1892 | |
| 1893 | $(this).attr({'disabled':'disabled'}); |
| 1894 | |
| 1895 | var request = { |
| 1896 | action:'nested_merge', |
| 1897 | security: wp_all_import_security, |
| 1898 | filePath: $fileURL, |
| 1899 | }; |
| 1900 | |
| 1901 | var $indicator = $('<span class="img_preloader" style="top:10px;"/>').insertBefore($(this)).show(); |
| 1902 | |
| 1903 | $form.find('.nested_msgs').html(''); |
| 1904 | |
| 1905 | $.ajax({ |
| 1906 | type: 'POST', |
| 1907 | url: ajaxurl + ((typeof import_id != "undefined") ? '?id=' + import_id : ''), |
| 1908 | data: request, |
| 1909 | success: function(response) { |
| 1910 | $indicator.remove(); |
| 1911 | |
| 1912 | if (response.success) |
| 1913 | { |
| 1914 | $form.find('.nested_files ul').append('<li rel="' + $form.find('.nested_files ul').find('li').length + '">' + $fileURL + ' <a href="javascript:void(0);" class="unmerge">remove</a></li>'); |
| 1915 | $form.find('input[name=nested_files]').val(window.JSON.stringify(response.nested_files)); |
| 1916 | |
| 1917 | var $tag = $('.tag'); |
| 1918 | var $tagno = parseInt($tag.find('input[name="tagno"]').val()); |
| 1919 | var $tagURL = 'admin.php?page=pmxi-admin-import&action=tag' + ((typeof import_id != "undefined") ? '&id=' + import_id : ''); |
| 1920 | |
| 1921 | $tag.addClass('loading').css('opacity', 0.7); |
| 1922 | $.post($tagURL, {tagno: $tagno, import_action: import_action}, function (data) { |
| 1923 | var $indicator = $('<span />').insertBefore($tag); |
| 1924 | $tag.replaceWith(data.html); |
| 1925 | fix_tag_position(); |
| 1926 | $indicator.next().tag().prevObject.remove(); |
| 1927 | if ($('#variations_xpath').length){ |
| 1928 | $('#variations_xpath').data('checkedValue', '').trigger('change'); |
| 1929 | } |
| 1930 | }, 'json'); |
| 1931 | return false; |
| 1932 | } else { |
| 1933 | $form.find('.nested_msgs').html(response.msg); |
| 1934 | } |
| 1935 | ths.removeAttr('disabled'); |
| 1936 | }, |
| 1937 | error: function(request) { |
| 1938 | $indicator.remove(); |
| 1939 | ths.removeAttr('disabled'); |
| 1940 | }, |
| 1941 | dataType: "json" |
| 1942 | }); |
| 1943 | } |
| 1944 | }); |
| 1945 | |
| 1946 | /* Unmerge nested XMl/CSV files */ |
| 1947 | $form.find('.unmerge').on('click', function(){ |
| 1948 | |
| 1949 | var ths = $(this); |
| 1950 | |
| 1951 | $(this).attr({'disabled':'disabled'}); |
| 1952 | |
| 1953 | var $indicator = $('<span class="img_preloader" style="top:5px;"/>').insertBefore($(this)).show(); |
| 1954 | |
| 1955 | var request = { |
| 1956 | action:'unmerge_file', |
| 1957 | source: ths.parents('li:first').attr('rel'), |
| 1958 | security: wp_all_import_security |
| 1959 | }; |
| 1960 | |
| 1961 | $form.find('.nested_msgs').html(''); |
| 1962 | |
| 1963 | $.ajax({ |
| 1964 | type: 'POST', |
| 1965 | url: ajaxurl + ((typeof import_id != "undefined") ? '?id=' + import_id : ''), |
| 1966 | data: request, |
| 1967 | success: function(response) { |
| 1968 | $indicator.remove(); |
| 1969 | if (response.success){ |
| 1970 | |
| 1971 | ths.parents('li:first').remove(); |
| 1972 | $form.find('input[name=nested_files]').val(window.JSON.stringify(response.nested_files)); |
| 1973 | |
| 1974 | var $tag = $('.tag'); |
| 1975 | var $tagno = parseInt($tag.find('input[name="tagno"]').val()); |
| 1976 | var $tagURL = 'admin.php?page=pmxi-admin-import&action=tag' + ((typeof import_id != "undefined") ? '&id=' + import_id : ''); |
| 1977 | |
| 1978 | $tag.addClass('loading').css('opacity', 0.7); |
| 1979 | $.post($tagURL, {tagno: $tagno, import_action: import_action}, function (data) { |
| 1980 | var $indicator = $('<span />').insertBefore($tag); |
| 1981 | $tag.replaceWith(data.html); |
| 1982 | fix_tag_position(); |
| 1983 | $indicator.next().tag().prevObject.remove(); |
| 1984 | if ($('#variations_xpath').length){ |
| 1985 | $('#variations_xpath').data('checkedValue', '').trigger('change'); |
| 1986 | } |
| 1987 | }, 'json'); |
| 1988 | return false; |
| 1989 | } |
| 1990 | else{ |
| 1991 | $form.find('.msgs').html(response.errors); |
| 1992 | $form.find('.pmxi_counter').remove(); |
| 1993 | } |
| 1994 | ths.removeAttr('disabled'); |
| 1995 | }, |
| 1996 | error: function(request) { |
| 1997 | $indicator.remove(); |
| 1998 | ths.removeAttr('disabled'); |
| 1999 | }, |
| 2000 | dataType: "json" |
| 2001 | }); |
| 2002 | }); |
| 2003 | |
| 2004 | $form.find('input[name=nested_url]').on('focus', function(){ |
| 2005 | $(this).css({'background':'#fff'}); |
| 2006 | }); |
| 2007 | |
| 2008 | var is_firefox = navigator.userAgent.indexOf('Firefox') > -1; |
| 2009 | var is_safari = navigator.userAgent.indexOf("Safari") > -1; |
| 2010 | var is_chrome = navigator.userAgent.indexOf('Chrome') > -1; |
| 2011 | |
| 2012 | if ((is_safari && !is_chrome) || is_firefox){ |
| 2013 | $form.find('textarea[name$=download_featured_image]').attr("placeholder", "http://example.com/images/image-1.jpg"); |
| 2014 | $form.find('textarea[name$=featured_image]').attr("placeholder", "image-1.jpg"); |
| 2015 | $form.find('textarea[name$=gallery_featured_image]').attr("placeholder", "image-1.jpg"); |
| 2016 | } else { |
| 2017 | $form.find('textarea[name$=download_featured_image]').attr("placeholder", "http://example.com/images/image-1.jpg\nhttp://example.com/images/image-2.jpg\n..."); |
| 2018 | $form.find('textarea[name$=featured_image]').attr("placeholder", "image-1.jpg\nimage-2.jpg\n..."); |
| 2019 | $form.find('textarea[name$=gallery_featured_image]').attr("placeholder", "image-1.jpg\nimage-2.jpg\n..."); |
| 2020 | } |
| 2021 | |
| 2022 | $form.find('input[name$=download_images]:checked').each(function(){ |
| 2023 | if ($(this).val() == 'gallery') { |
| 2024 | $(this).parents('table:first').find('.search_through_the_media_library').slideUp(); |
| 2025 | } else { |
| 2026 | $(this).parents('table:first').find('.search_through_the_media_library').slideDown(); |
| 2027 | } |
| 2028 | // download images hosted elsewhere |
| 2029 | if ($(this).val() == 'yes'){ |
| 2030 | $('.search_through_the_media_library_logic').show(); |
| 2031 | $('.download_images').show(); |
| 2032 | } else { |
| 2033 | $('.search_through_the_media_library_logic').hide(); |
| 2034 | $('.download_images').hide(); |
| 2035 | } |
| 2036 | }); |
| 2037 | |
| 2038 | $form.find('input[name$=download_images]').on('click', function(){ |
| 2039 | if ($(this).is(':checked') && $(this).val() == 'gallery') { |
| 2040 | $(this).parents('table:first').find('.search_through_the_media_library').slideUp(); |
| 2041 | } else { |
| 2042 | $(this).parents('table:first').find('.search_through_the_media_library').slideDown(); |
| 2043 | } |
| 2044 | // download images hosted elsewhere |
| 2045 | if ($(this).val() == 'yes'){ |
| 2046 | $('.search_through_the_media_library_logic').slideDown(); |
| 2047 | $('.download_images').slideDown(); |
| 2048 | } else { |
| 2049 | $('.search_through_the_media_library_logic').slideUp(); |
| 2050 | $('.download_images').slideUp(); |
| 2051 | } |
| 2052 | }); |
| 2053 | |
| 2054 | $form.find('.wpallimport-dismiss-cf-welcome').on('click', function(){ |
| 2055 | $('.cf_welcome, .cf_detect_result').slideUp(); |
| 2056 | }); |
| 2057 | |
| 2058 | }); |
| 2059 | |
| 2060 | // options form: highlight options of selected post type |
| 2061 | $('form.wpallimport-template input[name="type"]').on('click', function() { |
| 2062 | var $container = $(this).parents('.post-type-container'); |
| 2063 | $('.post-type-container').not($container).removeClass('selected').find('.post-type-options').hide(); |
| 2064 | $container.addClass('selected').find('.post-type-options').show(); |
| 2065 | }).filter(':checked').trigger('click'); |
| 2066 | // options form: add / remove custom params |
| 2067 | $('.form-table a.action[href="#add"]').on('click', function () { |
| 2068 | var $template = $(this).parents('table').first().find('tr.template'); |
| 2069 | $template.clone(true).insertBefore($template).css('display', 'none').removeClass('template').fadeIn(); |
| 2070 | return false; |
| 2071 | }); |
| 2072 | |
| 2073 | // options form: auto submit when `load options` checkbox is checked |
| 2074 | $('input[name="load_options"]').on('click', function () { |
| 2075 | if ($(this).is(':checked')) $(this).parents('form').submit(); |
| 2076 | }); |
| 2077 | // options form: auto submit when `reset options` checkbox is checked |
| 2078 | $('form.wpallimport-template').find('input[name="reset_options"]').on('click', function () { |
| 2079 | if ($(this).is(':checked')) $(this).parents('form').submit(); |
| 2080 | }); |
| 2081 | $(document).on('click', '.form-table .action.remove a, .cf-form-table .action.remove a, .tax-form-table .action.remove a', function () { |
| 2082 | let $box = $(this).parents('tbody').first(); |
| 2083 | $(this).parents('tr').first().remove(); |
| 2084 | if ( ! $box.find('tr.form-field:visible').length ){ |
| 2085 | $box.find('.add-new-entry').trigger('click'); |
| 2086 | } |
| 2087 | return false; |
| 2088 | }); |
| 2089 | |
| 2090 | var dblclickbuf = { |
| 2091 | 'selected':false, |
| 2092 | 'value':'' |
| 2093 | }; |
| 2094 | |
| 2095 | function insertxpath(){ |
| 2096 | if ($(this).hasClass('wpallimport-placeholder')){ |
| 2097 | $(this).val(''); |
| 2098 | $(this).removeClass('wpallimport-placeholder'); |
| 2099 | } |
| 2100 | if (dblclickbuf.selected) { |
| 2101 | $(this).val($(this).val() + dblclickbuf.value); |
| 2102 | $('.xml-element[title*="/'+dblclickbuf.value.replace('{','').replace('}','')+'"]').removeClass('selected'); |
| 2103 | dblclickbuf.value = ''; |
| 2104 | dblclickbuf.selected = false; |
| 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | var go_to_template = false; |
| 2109 | |
| 2110 | // selection logic |
| 2111 | $('form.wpallimport-choose-elements').each(function () { |
| 2112 | var $form = $(this); |
| 2113 | $form.find('.wpallimport-xml').xml(); |
| 2114 | var $input = $form.find('input[name="xpath"]'); |
| 2115 | var $next_element = $form.find('#next_element'); |
| 2116 | var $prev_element = $form.find('#prev_element'); |
| 2117 | var $goto_element = $form.find('#goto_element'); |
| 2118 | var $get_default_xpath = $form.find('#get_default_xpath'); |
| 2119 | var $root_element = $form.find('#root_element'); |
| 2120 | var $submit = $form.find('input[type="submit"]'); |
| 2121 | var $csv_delimiter = $form.find('input[name=delimiter]'); |
| 2122 | var $apply_delimiter = $form.find('input[name=apply_delimiter]'); |
| 2123 | |
| 2124 | var $xml = $('.wpallimport-xml'); |
| 2125 | |
| 2126 | var xpathChanged = function (reset_element) { |
| 2127 | if ($input.val() == $input.data('checkedValue')) return; |
| 2128 | |
| 2129 | $form.addClass('loading'); |
| 2130 | $form.find('.xml-element.selected').removeClass('selected'); // clear current selection |
| 2131 | // request server to return elements which correspond to xpath entered |
| 2132 | $input.attr('readonly', true).unbind('change', xpathChanged).data('checkedValue', $input.val()); |
| 2133 | $xml.css({'visibility':'hidden'}); |
| 2134 | $('.wpallimport-set-csv-delimiter').hide(); |
| 2135 | |
| 2136 | $xml.parents('fieldset:first').addClass('preload'); |
| 2137 | go_to_template = false; |
| 2138 | $submit.hide(); |
| 2139 | var evaluate = function(){ |
| 2140 | $.post('admin.php?page=pmxi-admin-import&action=evaluate', {xpath: $input.val(), show_element: reset_element ? 1 : $goto_element.val(), root_element:$root_element.val(), is_csv: $apply_delimiter.length, delimiter:$csv_delimiter.val(), security: wp_all_import_security}, function (response) { |
| 2141 | if (response.result){ |
| 2142 | $('.wpallimport-elements-preloader').hide(); |
| 2143 | $('.ajax-console').html(response.html); |
| 2144 | $input.attr('readonly', false).on('change', function(){$goto_element.val(1); xpathChanged();}); |
| 2145 | $form.removeClass('loading'); |
| 2146 | |
| 2147 | $xml.parents('fieldset:first').removeClass('preload'); |
| 2148 | $('.wpallimport-set-csv-delimiter').show(); |
| 2149 | go_to_template = true; |
| 2150 | $('#pmxi_xml_element').find('option').each(function(){ |
| 2151 | if ($(this).val() != "") $(this).remove(); |
| 2152 | }); |
| 2153 | $('#pmxi_xml_element').append(response.render_element); |
| 2154 | $('.wpallimport-root-element').html(response.root_element); |
| 2155 | $('.wpallimport-elements-count-info').html(response.count); |
| 2156 | $('.wp_all_import_warning').hide(); |
| 2157 | if (response.count){ |
| 2158 | $submit.show(); |
| 2159 | if ($('.xml-element.lvl-1').length < 1) $('.wp_all_import_warning').css({'display':'inline-block'}); |
| 2160 | } else { |
| 2161 | $submit.hide(); |
| 2162 | } |
| 2163 | } |
| 2164 | }, "json").fail(function() { |
| 2165 | $xml.parents('fieldset:first').removeClass('preload'); |
| 2166 | $form.removeClass('loading'); |
| 2167 | $('.ajax-console').html('<div class="error inline"><p>No matching elements found for XPath expression specified.</p></div>'); |
| 2168 | }); |
| 2169 | } |
| 2170 | evaluate(); |
| 2171 | }; |
| 2172 | $next_element.on('click', function(){ |
| 2173 | var matches_count = ($('.matches_count').length) ? parseInt($('.matches_count').html()) : 0; |
| 2174 | var show_element = Math.min((parseInt($goto_element.val()) + 1), matches_count); |
| 2175 | $goto_element.val(show_element).html( show_element ); $input.data('checkedValue', ''); xpathChanged(); |
| 2176 | }); |
| 2177 | $prev_element.on('click', function(){ |
| 2178 | var show_element = Math.max((parseInt($goto_element.val()) - 1), 1); |
| 2179 | $goto_element.val(show_element).html( show_element ); $input.data('checkedValue', ''); xpathChanged(); |
| 2180 | }); |
| 2181 | $goto_element.on('change', function(){ |
| 2182 | var matches_count = ($('.matches_count').length) ? parseInt($('.matches_count').html()) : 0; |
| 2183 | var show_element = Math.max(Math.min(parseInt($goto_element.val()), matches_count), 1); |
| 2184 | $goto_element.val(show_element); $input.data('checkedValue', ''); xpathChanged(); |
| 2185 | }); |
| 2186 | |
| 2187 | var reset_filters = function(){ |
| 2188 | $('#apply_filters').hide(); |
| 2189 | $('.filtering_rules').empty(); |
| 2190 | $('#filtering_rules').find('p').show(); |
| 2191 | } |
| 2192 | |
| 2193 | $get_default_xpath.on('click', function(){ |
| 2194 | $input.val($(this).attr('rel')); |
| 2195 | if ($input.val() == $input.data('checkedValue')) return; |
| 2196 | reset_filters(); |
| 2197 | $root_element.val($(this).attr('root')); |
| 2198 | $goto_element.val(1); |
| 2199 | xpathChanged(true); |
| 2200 | }); |
| 2201 | $('.wpallimport-change-root-element').on('click', function(){ |
| 2202 | $input.val('/' + $(this).attr('rel')); |
| 2203 | if ($input.val() == $input.data('checkedValue')) return; |
| 2204 | $('.wpallimport-change-root-element').removeClass('selected'); |
| 2205 | $(this).addClass('selected'); |
| 2206 | reset_filters(); |
| 2207 | $('.root_element').html($(this).attr('rel')); |
| 2208 | $root_element.val($(this).attr('rel')); |
| 2209 | $goto_element.val(1); |
| 2210 | xpathChanged(true); |
| 2211 | }); |
| 2212 | $input.on('change', function(){ |
| 2213 | $goto_element.val(1); |
| 2214 | xpathChanged(true); |
| 2215 | }).trigger('change'); |
| 2216 | $input.on('keyup', function (e) { |
| 2217 | if (13 == e.keyCode) $(this).trigger('change'); |
| 2218 | }); |
| 2219 | |
| 2220 | $apply_delimiter.on('click', function(){ |
| 2221 | if ( ! $input.attr('readonly') ){ |
| 2222 | $('input[name="xpath"]').data('checkedValue',''); |
| 2223 | $goto_element.val(1); |
| 2224 | xpathChanged(true); |
| 2225 | } |
| 2226 | }); |
| 2227 | |
| 2228 | $('#apply_filters').on('click', function(){ |
| 2229 | |
| 2230 | var $input = $('input[name="xpath"]'); |
| 2231 | var xpath = $input.val(); |
| 2232 | |
| 2233 | filter = '['; |
| 2234 | xpath_builder($('.filtering_rules'), 0); |
| 2235 | filter += ']'; |
| 2236 | |
| 2237 | $input.val( $input.val().split('[')[0] + filter); |
| 2238 | $input.data('checkedValue', ''); |
| 2239 | $goto_element.val(1); |
| 2240 | xpathChanged(true); |
| 2241 | if ($('.filtering_rules').html().length) { |
| 2242 | $('.filtering-output').val(window.JSON.stringify($('.filtering_rules').html())); |
| 2243 | } else { |
| 2244 | $('.filtering-output').val(''); |
| 2245 | } |
| 2246 | }); |
| 2247 | }); |
| 2248 | |
| 2249 | /* Advanced Filtering */ |
| 2250 | |
| 2251 | $('.filtering_rules').pmxi_nestedSortable({ |
| 2252 | handle: 'div', |
| 2253 | items: 'li', |
| 2254 | toleranceElement: '> div', |
| 2255 | update: function () { |
| 2256 | $('.filtering_rules').find('.condition').show(); |
| 2257 | $('.filtering_rules').find('.condition:last').hide(); |
| 2258 | } |
| 2259 | }); |
| 2260 | |
| 2261 | $('#pmxi_add_rule').on('click', function(){ |
| 2262 | |
| 2263 | var $el = $('#pmxi_xml_element'); |
| 2264 | var $rule = $('#pmxi_rule'); |
| 2265 | var $val = $('#pmxi_value'); |
| 2266 | |
| 2267 | if ($el.val() == "" || $rule.val() == "") return; |
| 2268 | |
| 2269 | if ($rule.val() != 'is_empty' && $rule.val() != "is_not_empty" && $val.val() == "") return; |
| 2270 | |
| 2271 | var relunumber = $('.filtering_rules').find('li').length + "_" + $.now(); |
| 2272 | |
| 2273 | var html = '<li><div class="drag-element">'; |
| 2274 | html += '<input type="hidden" value="'+ $el.val() +'" class="pmxi_xml_element"/>'; |
| 2275 | html += '<input type="hidden" value="'+ $rule.val() +'" class="pmxi_rule"/>'; |
| 2276 | html += '<input type="hidden" value="'+ $val.val() +'" class="pmxi_value"/>'; |
| 2277 | html += '<span class="rule_element">' + $el.val() + '</span><span class="rule_as_is">' + $rule.find('option:selected').html() + '</span><span class="rule_condition_value">"' + $val.val() +'"</span>'; |
| 2278 | html += '<span class="condition"> <label for="rule_and_'+relunumber+'">AND</label><input id="rule_and_'+relunumber+'" type="radio" value="and" name="rule_'+relunumber+'" checked="checked" class="rule_condition"/><label for="rule_or_'+relunumber+'">OR</label><input id="rule_or_'+relunumber+'" type="radio" value="or" name="rule_'+relunumber+'" class="rule_condition"/> </span>'; |
| 2279 | html += '</div><a href="javascript:void(0);" class="icon-item remove-ico"></a></li>'; |
| 2280 | |
| 2281 | $('#wpallimport-filters, #apply_filters').show(); |
| 2282 | $('#filtering_rules').find('p').hide(); |
| 2283 | |
| 2284 | $('.filtering_rules').append(html); |
| 2285 | |
| 2286 | $('.filtering_rules').find('.condition').show(); |
| 2287 | $('.filtering_rules').find('.condition:last').hide(); |
| 2288 | |
| 2289 | $el.prop('selectedIndex',0); |
| 2290 | $rule.prop('selectedIndex',0); |
| 2291 | $val.val(''); |
| 2292 | $('#pmxi_value').show(); |
| 2293 | fix_tag_position(); |
| 2294 | |
| 2295 | }); |
| 2296 | |
| 2297 | $(document).on('change', '.rule_condition', function() { |
| 2298 | $('input[name=' + $(this).attr('name') + ']').removeAttr('checked'); |
| 2299 | $(this).prop('checked', true).attr('checked', 'checked'); |
| 2300 | }); |
| 2301 | |
| 2302 | $(document).on('click', '.filtering_rules li a.remove-ico', function() { |
| 2303 | $(this).parents('li:first').remove(); |
| 2304 | $('.filtering_rules').find('li:last div span.condition').hide(); |
| 2305 | if (!$('.filtering_rules').find('li').length){ |
| 2306 | $('#apply_filters').hide().trigger('click'); |
| 2307 | $('#filtering_rules').find('p').show(); |
| 2308 | } |
| 2309 | fix_tag_position(); |
| 2310 | }); |
| 2311 | |
| 2312 | $('#pmxi_rule').on('change', function(){ |
| 2313 | if ($(this).val() == 'is_empty' || $(this).val() == 'is_not_empty') { |
| 2314 | $('#pmxi_value').hide(); |
| 2315 | } else { |
| 2316 | $('#pmxi_value').show(); |
| 2317 | } |
| 2318 | }); |
| 2319 | |
| 2320 | var filter = '['; |
| 2321 | |
| 2322 | var xpath_builder = function(rules_box, lvl){ |
| 2323 | |
| 2324 | var rules = rules_box.children('li'); |
| 2325 | |
| 2326 | var root_element = $('#root_element').val(); |
| 2327 | |
| 2328 | if (lvl && rules.length > 1) filter += ' ('; |
| 2329 | |
| 2330 | rules.each(function(){ |
| 2331 | |
| 2332 | var node = $(this).children('.drag-element').find('.pmxi_xml_element').val(); |
| 2333 | var condition = $(this).children('.drag-element').find('.pmxi_rule').val(); |
| 2334 | var value = $(this).children('.drag-element').find('.pmxi_value').val(); |
| 2335 | |
| 2336 | var clause = ($(this).children('.drag-element').find('.condition').is(':visible')) ? $(this).children('.drag-element').find('input.rule_condition:checked').val() : false; |
| 2337 | |
| 2338 | var is_attr = false; |
| 2339 | |
| 2340 | if (node.indexOf('@') != -1){ |
| 2341 | is_attr = true; |
| 2342 | node_name = node.split('@')[0]; |
| 2343 | attr_name = node.split('@')[1]; |
| 2344 | } |
| 2345 | |
| 2346 | if (is_attr) { |
| 2347 | filter += (node_name == root_element) ? '' : node_name.replace(/->/g, '/'); |
| 2348 | } else { |
| 2349 | filter += node.replace(/->/g, '/'); |
| 2350 | } |
| 2351 | |
| 2352 | if (is_attr) filter += '@' + attr_name; |
| 2353 | |
| 2354 | switch (condition){ |
| 2355 | case 'equals': |
| 2356 | filter += ' = "%s"'; |
| 2357 | break; |
| 2358 | case 'not_equals': |
| 2359 | filter += ' != "%s"'; |
| 2360 | break; |
| 2361 | case 'greater': |
| 2362 | filter += ' > %s'; |
| 2363 | break; |
| 2364 | case 'equals_or_greater': |
| 2365 | filter += ' >= %s'; |
| 2366 | break; |
| 2367 | case 'less': |
| 2368 | filter += ' < %s'; |
| 2369 | break; |
| 2370 | case 'equals_or_less': |
| 2371 | filter += ' <= %s'; |
| 2372 | break; |
| 2373 | case 'contains': |
| 2374 | filter += '[contains(.,"%s")]'; |
| 2375 | break; |
| 2376 | case 'not_contains': |
| 2377 | filter += '[not(contains(.,"%s"))]'; |
| 2378 | break; |
| 2379 | case 'is_empty': |
| 2380 | filter += '[not(string())]'; |
| 2381 | break; |
| 2382 | case 'is_not_empty': |
| 2383 | filter += '[string()]'; |
| 2384 | break; |
| 2385 | } |
| 2386 | |
| 2387 | filter = filter.replace('%s', value); |
| 2388 | |
| 2389 | if (clause) filter += ' ' + clause + ' '; |
| 2390 | |
| 2391 | if ($(this).children('ol').length){ |
| 2392 | $(this).children('ol').each(function(){ |
| 2393 | if ($(this).children('li').length) xpath_builder($(this), 1); |
| 2394 | }); |
| 2395 | } |
| 2396 | }); |
| 2397 | if (lvl && rules.length > 1) filter += ') '; |
| 2398 | |
| 2399 | } |
| 2400 | |
| 2401 | $('form.wpallimport-choose-elements').find('input[type="submit"]').on('click', function(e){ |
| 2402 | e.preventDefault(); |
| 2403 | if (go_to_template) $(this).parents('form:first').submit(); |
| 2404 | }); |
| 2405 | |
| 2406 | var init_context_menu = function(){ |
| 2407 | if ( $(".tag").length ){ |
| 2408 | $('.xml-element').each(function(){ |
| 2409 | var $ths = $(this); |
| 2410 | if ($(this).children('.xml-element-xpaths').find('li').length){ |
| 2411 | $(this).children('.xml-content').css({'cursor':'context-menu'}).attr({'title' : 'Right click to view alternate XPaths'}); |
| 2412 | $(this).contextmenu({ |
| 2413 | delegate: ".xml-content", |
| 2414 | menu: "#" + $(this).children('.xml-element-xpaths').find('ul').attr('id'), |
| 2415 | select: function(event, ui) { |
| 2416 | //alert("select " + ui.cmd + " on " + ui.target.text()); |
| 2417 | } |
| 2418 | }); |
| 2419 | } |
| 2420 | }); |
| 2421 | } |
| 2422 | } |
| 2423 | |
| 2424 | // tag preview |
| 2425 | $.fn.tag = function () { |
| 2426 | this.each(function () { |
| 2427 | |
| 2428 | init_context_menu(); |
| 2429 | |
| 2430 | var $tag = $(this); |
| 2431 | $tag.xml('dragable'); |
| 2432 | var tagno = parseInt($tag.find('input[name="tagno"]').val()); |
| 2433 | var $tagURL = 'admin.php?page=pmxi-admin-import&action=tag' + ((typeof import_id != "undefined") ? '&id=' + import_id : ''); |
| 2434 | |
| 2435 | $tag.find('.navigation a').on('click', function () { |
| 2436 | tagno += '#prev' == $(this).attr('href') ? -1 : 1; |
| 2437 | $tag.addClass('loading').css('opacity', 0.7); |
| 2438 | $.post($tagURL, {tagno: tagno, import_action: import_action, xpath: $('input[name="xpath"]').val(), security: wp_all_import_security}, function (data) { |
| 2439 | var $indicator = $('<span />').insertBefore($tag); |
| 2440 | $tag.replaceWith(data.html); |
| 2441 | fix_tag_position(); |
| 2442 | $indicator.next().tag().prevObject.remove(); |
| 2443 | if ($('#variations_xpath').length){ |
| 2444 | $('#variations_xpath').data('checkedValue', '').trigger('change'); |
| 2445 | } |
| 2446 | |
| 2447 | }, 'json'); |
| 2448 | return false; |
| 2449 | }); |
| 2450 | $tag.find('input[name="tagno"]').on('change keypress', function (event) { |
| 2451 | |
| 2452 | // Check if the key pressed is enter, if not exit the function unless a |
| 2453 | // 'change' event triggered it. |
| 2454 | if(event.type === "keypress") { |
| 2455 | var keycode = (event.keyCode ? event.keyCode : event.which); |
| 2456 | if(keycode != '13'){ |
| 2457 | return; |
| 2458 | } |
| 2459 | } |
| 2460 | |
| 2461 | tagno = (parseInt($(this).val()) > parseInt($tag.find('.pmxi_count').html())) ? $tag.find('.pmxi_count').html() : ( (parseInt($(this).val())) ? $(this).val() : 1 ); |
| 2462 | $(this).val(tagno); |
| 2463 | $tag.addClass('loading').css('opacity', 0.7); |
| 2464 | $.post($tagURL, {tagno: tagno, import_action: import_action, xpath: $('input[name="xpath"]').val(), security: wp_all_import_security}, function (data) { |
| 2465 | var $indicator = $('<span />').insertBefore($tag); |
| 2466 | $tag.replaceWith(data.html); |
| 2467 | fix_tag_position(); |
| 2468 | $indicator.next().tag().prevObject.remove(); |
| 2469 | if ($('#variations_xpath').length){ |
| 2470 | $('#variations_xpath').data('checkedValue', '').trigger('change'); |
| 2471 | } |
| 2472 | }, 'json'); |
| 2473 | |
| 2474 | }); |
| 2475 | }); |
| 2476 | return this; |
| 2477 | }; |
| 2478 | $('.tag').tag(); |
| 2479 | // [/xml representation dynamic] |
| 2480 | |
| 2481 | $('.wpallimport-custom-fields').each(function(){ |
| 2482 | $(this).find('.wp_all_import_autocomplete').each(function(){ |
| 2483 | if ( ! $(this).parents('tr:first').hasClass('template')){ |
| 2484 | $(this).autocomplete({ |
| 2485 | source: eval('__META_KEYS'), |
| 2486 | minLength: 0 |
| 2487 | }).on('click', function () { |
| 2488 | $(this).autocomplete('search', ''); |
| 2489 | $(this).attr('rel', ''); |
| 2490 | }); |
| 2491 | } |
| 2492 | }); |
| 2493 | |
| 2494 | $(this).find('textarea[name^=custom_value]').on('click', function(){ |
| 2495 | var $ths = $(this); |
| 2496 | var $parent = $ths.parents('tr:first'); |
| 2497 | var $custom_name = $parent.find('input[name^=custom_name]'); |
| 2498 | var $key = $custom_name.val(); |
| 2499 | |
| 2500 | if ($key != "" && $custom_name.attr('rel') != "done"){ |
| 2501 | $ths.addClass('loading'); |
| 2502 | $.post('admin.php?page=pmxi-admin-settings&action=meta_values', {key: $key, security: wp_all_import_security}, function (data) { |
| 2503 | if (data.meta_values.length){ |
| 2504 | $ths.autocomplete({ |
| 2505 | source: eval(data.meta_values), |
| 2506 | minLength: 0 |
| 2507 | }).on('click', function () { |
| 2508 | $(this).autocomplete('search', ''); |
| 2509 | }).trigger('click'); |
| 2510 | } |
| 2511 | $custom_name.attr('rel','done'); |
| 2512 | $ths.removeClass('loading'); |
| 2513 | }, 'json'); |
| 2514 | } |
| 2515 | }); |
| 2516 | |
| 2517 | $('.wpallimport-cf-options').on('click', function(){ |
| 2518 | $(this).next('.wpallimport-cf-menu').slideToggle(); |
| 2519 | }); |
| 2520 | }); |
| 2521 | |
| 2522 | /* Categories hierarchy */ |
| 2523 | |
| 2524 | $('ol.sortable').pmxi_nestedSortable({ |
| 2525 | handle: 'div', |
| 2526 | items: 'li.dragging', |
| 2527 | toleranceElement: '> div', |
| 2528 | update: function () { |
| 2529 | $(this).parents('td:first').find('.hierarhy-output').val(window.JSON.stringify($(this).pmxi_nestedSortable('toArray', {startDepthCount: 0}))); |
| 2530 | if ($(this).parents('td:first').find('input:first').val() == '') $(this).parents('td:first').find('.hierarhy-output').val(''); |
| 2531 | } |
| 2532 | }); |
| 2533 | |
| 2534 | $('.drag-element').find('input').on('blur', function(){ |
| 2535 | $(this).parents('td:first').find('.hierarhy-output').val(window.JSON.stringify($(this).parents('.ui-sortable:first').pmxi_nestedSortable('toArray', {startDepthCount: 0}))); |
| 2536 | if ($(this).parents('td:first').find('input:first').val() == '') $(this).parents('td:first').find('.hierarhy-output').val(''); |
| 2537 | }); |
| 2538 | |
| 2539 | $('.drag-element').find('input').on('change', function(){ |
| 2540 | $(this).parents('td:first').find('.hierarhy-output').val(window.JSON.stringify($(this).parents('.ui-sortable:first').pmxi_nestedSortable('toArray', {startDepthCount: 0}))); |
| 2541 | if ($(this).parents('td:first').find('input:first').val() == '') $(this).parents('td:first').find('.hierarhy-output').val(''); |
| 2542 | }); |
| 2543 | |
| 2544 | $('.drag-element').find('input').on('mouseenter', function(){},function(){ |
| 2545 | $(this).parents('td:first').find('.hierarhy-output').val(window.JSON.stringify($(this).parents('.ui-sortable:first').pmxi_nestedSortable('toArray', {startDepthCount: 0}))); |
| 2546 | if ($(this).parents('td:first').find('input:first').val() == '') $(this).parents('td:first').find('.hierarhy-output').val(''); |
| 2547 | }); |
| 2548 | |
| 2549 | $('.taxonomy_auto_nested').on('click', function(){ |
| 2550 | $(this).parents('td:first').find('.hierarhy-output').val(window.JSON.stringify($(this).parents('td:first').find('.ui-sortable:first').pmxi_nestedSortable('toArray', {startDepthCount: 0}))); |
| 2551 | if ($(this).parents('td:first').find('input:first').val() == '') $(this).parents('td:first').find('.hierarhy-output').val(''); |
| 2552 | }); |
| 2553 | |
| 2554 | $(document).on('click', '.sortable li a.remove-ico, .sortable li div a.remove-ico', function(){ |
| 2555 | let parent_td = $(this).parents('td:first'); |
| 2556 | $(this).parents('li:first').remove(); |
| 2557 | parent_td.find('ol.sortable:first').find('li').each(function(i, e){ |
| 2558 | $(this).attr({'id':'item_'+ (i+1)}); |
| 2559 | }); |
| 2560 | parent_td.find('.hierarhy-output').val(window.JSON.stringify(parent_td.find('.ui-sortable:first').pmxi_nestedSortable('toArray', {startDepthCount: 0}))); |
| 2561 | if (parent_td.find('input:first').val() == '') { |
| 2562 | parent_td.find('.hierarhy-output').val(''); |
| 2563 | } |
| 2564 | }); |
| 2565 | |
| 2566 | $('.tax_hierarchical_logic').find('.remove-ico').on('click', function(){ |
| 2567 | $(this).parents('li:first').remove(); |
| 2568 | }); |
| 2569 | |
| 2570 | $(document).on('click', '.add-new-ico', function() { |
| 2571 | let count = parseInt($(this).parents('tr:first').find('ol.sortable').find('li.dragging').last().attr('id').replace('item_', '')) + 1; |
| 2572 | let $template = $(this).parents('td:first').find('ol').children('li.template'); |
| 2573 | $clone = $template.clone(true); |
| 2574 | $clone.addClass('dragging').attr({'id': $clone.attr('id') + '_' + count}).find('input[type=checkbox][name^=categories_mapping]').each(function(){ |
| 2575 | $(this).attr({'id': $(this).attr('id') + '_' + count}); |
| 2576 | $(this).next('label').attr({'for':$(this).next('label').attr('for') + '_' + count}); |
| 2577 | $(this).next('label').next('div').addClass($(this).next('label').next('div').attr('rel') + '_' + count); |
| 2578 | }); |
| 2579 | $clone.insertBefore($template).css('display', 'none').removeClass('template').droppable({ |
| 2580 | drop: function (event, ui) { |
| 2581 | let input = $(this).find("input.xpath_field:first"); |
| 2582 | let oldValue = input.val(); |
| 2583 | let newValue = ui.draggable.data('xpath') || ''; |
| 2584 | input.val(oldValue + newValue); |
| 2585 | }, |
| 2586 | greedy:true |
| 2587 | }).fadeIn().find('input.switcher').trigger('change'); |
| 2588 | let sortable = $(this).parents('.ui-sortable:first'); |
| 2589 | if (sortable.length){ |
| 2590 | $(this).parents('td:first').find('.hierarhy-output').val(window.JSON.stringify(sortable.pmxi_nestedSortable('toArray', {startDepthCount: 0}))); |
| 2591 | if ($(this).parents('td:first').find('input:first').val() == '') $(this).parents('td:first').find('.hierarhy-output').val(''); |
| 2592 | } |
| 2593 | //$('.widefat').on('focus', insertxpath ); |
| 2594 | }); |
| 2595 | |
| 2596 | $(document).on('click', '.add-new-cat', function(){ |
| 2597 | var $template = $(this).parents('td:first').find('ul.tax_hierarchical_logic').children('li.template'); |
| 2598 | var $number = $(this).parents('td:first').find('ul.tax_hierarchical_logic').children('li').length - 1; |
| 2599 | var $cloneName = $template.find('input.assign_term').attr('name').replace('NUMBER', $number); |
| 2600 | $clone = $template.clone(true); |
| 2601 | $clone.find('input[name^=tax_hierarchical_assing]').attr('name', $cloneName); |
| 2602 | $clone.insertBefore($template).css('display', 'none').removeClass('template').droppable({ |
| 2603 | drop: function (event, ui) { |
| 2604 | let input = $(this).find("input.hierarchical_xpath_field:first"); |
| 2605 | let oldValue = input.val(); |
| 2606 | let newValue = ui.draggable.data('xpath'); |
| 2607 | input.val(oldValue + newValue); |
| 2608 | } |
| 2609 | }).fadeIn().find('input.switcher').trigger('change'); |
| 2610 | }); |
| 2611 | |
| 2612 | $('ol.sortable').each(function(){ |
| 2613 | if ( ! $(this).children('li').not('.template').length ) $(this).next('.add-new-ico').trigger('click'); |
| 2614 | }); |
| 2615 | |
| 2616 | $('.tagno').trigger('change'); |
| 2617 | |
| 2618 | $('form.wpallimport-template.edit').each(function() { |
| 2619 | |
| 2620 | var $input = $('input[name="xpath"]'); |
| 2621 | |
| 2622 | $input.on('change', function(){ |
| 2623 | $('.tagno').trigger('change'); |
| 2624 | }).trigger('change'); |
| 2625 | $input.on('keyup', function (e) { |
| 2626 | if (13 == e.keyCode) $(this).trigger('change'); |
| 2627 | }); |
| 2628 | |
| 2629 | $('#apply_filters').on('click', function(){ |
| 2630 | |
| 2631 | filter = ''; |
| 2632 | if ($('.filtering_rules').children('li').length) { |
| 2633 | filter = '['; |
| 2634 | xpath_builder($('.filtering_rules'), 0); |
| 2635 | filter += ']'; |
| 2636 | } |
| 2637 | |
| 2638 | $input.val( $input.val().split('[')[0] + filter); |
| 2639 | $input.data('checkedValue', ''); |
| 2640 | $input.trigger('change'); |
| 2641 | if ($('.filtering_rules').html().length) { |
| 2642 | $('.filtering-output').val(window.JSON.stringify($('.filtering_rules').html())); |
| 2643 | } else { |
| 2644 | $('.filtering-output').val(''); |
| 2645 | } |
| 2646 | }); |
| 2647 | }); |
| 2648 | |
| 2649 | $('form.wpallimport-template').find('input[type=submit]').on('click', function(e){ |
| 2650 | |
| 2651 | e.preventDefault(); |
| 2652 | |
| 2653 | $('.hierarhy-output').each(function(){ |
| 2654 | var sortable = $(this).parents('td:first').find('.ui-sortable:first'); |
| 2655 | if (sortable.length){ |
| 2656 | $(this).val(window.JSON.stringify(sortable.pmxi_nestedSortable('toArray', {startDepthCount: 0}))); |
| 2657 | if ($(this).parents('td:first').find('input:first').val() == '') $(this).val(''); |
| 2658 | } |
| 2659 | }); |
| 2660 | if ($(this).attr('name') == 'btn_save_only') $('.save_only').val('1'); |
| 2661 | |
| 2662 | $('input[name^=in_variations], input[name^=is_visible], input[name^=is_taxonomy], input[name^=create_taxonomy_in_not_exists], input[name^=variable_create_taxonomy_in_not_exists], input[name^=variable_in_variations], input[name^=variable_is_visible], input[name^=variable_is_taxonomy]').each(function(){ |
| 2663 | if ( ! $(this).is(':checked') && ! $(this).parents('.form-field:first').hasClass('template')){ |
| 2664 | $(this).val('0').prop('checked', true); |
| 2665 | } |
| 2666 | }); |
| 2667 | |
| 2668 | $('.custom_type[rel=serialized]').each(function(){ |
| 2669 | var values = new Array(); |
| 2670 | $(this).find('.form-field').each(function(){ |
| 2671 | var skey = $(this).find('.serialized_key').val(); |
| 2672 | if ('' == skey){ |
| 2673 | values.push($(this).find('.serialized_value').val()); |
| 2674 | } else { |
| 2675 | var obj = {}; |
| 2676 | obj[skey] = $(this).find('.serialized_value').val(); |
| 2677 | values.push(obj); |
| 2678 | } |
| 2679 | }); |
| 2680 | $(this).find('input[name^=serialized_values]').val(window.JSON.stringify(values)); |
| 2681 | }); |
| 2682 | |
| 2683 | $('.custom_type[rel=mapping]').each(function(){ |
| 2684 | var values = new Array(); |
| 2685 | $(this).find('.form-field').each(function(){ |
| 2686 | if ($(this).find('.mapping_to').val() != "") { |
| 2687 | var skey = $(this).find('.mapping_from').val(); |
| 2688 | if ('' != skey){ |
| 2689 | var obj = {}; |
| 2690 | obj[skey] = $(this).find('.mapping_to').val(); |
| 2691 | values.push(obj); |
| 2692 | } |
| 2693 | } |
| 2694 | }); |
| 2695 | $(this).find('input[name^=custom_mapping_rules], .pmre_mapping_rules').val(window.JSON.stringify(values)); |
| 2696 | }); |
| 2697 | |
| 2698 | serialize_ctx_mapping(); |
| 2699 | |
| 2700 | $(this).parents('form:first').submit(); |
| 2701 | |
| 2702 | }); |
| 2703 | |
| 2704 | $('.wpallimport-step-4').each(function(){ |
| 2705 | $(this).find('input[name^=custom_duplicate_name]').autocomplete({ |
| 2706 | source: eval('__META_KEYS'), |
| 2707 | minLength: 0 |
| 2708 | }).on('click', function () { |
| 2709 | $(this).autocomplete('search', ''); |
| 2710 | $(this).attr('rel', ''); |
| 2711 | }); |
| 2712 | }); |
| 2713 | |
| 2714 | $(document).on('click', '.add-new-entry', function(e){ |
| 2715 | // Some "Add Another" links carry both classes; suppress the .add-new-key |
| 2716 | // twin so the row isn't cloned twice. Only stop the dual-class case so |
| 2717 | // pure .add-new-entry clicks still reach any other delegated handlers. |
| 2718 | if ($(this).hasClass('add-new-key')) { |
| 2719 | e.stopImmediatePropagation(); |
| 2720 | } |
| 2721 | var $template = $(this).parents('table').first().children('tbody').children('tr.template'); |
| 2722 | $number = $(this).parents('table').first().children('tbody').children('tr').length - 2; |
| 2723 | $clone = $template.clone(true); |
| 2724 | |
| 2725 | $clone.find('div[rel^=serialized]').attr({'id':'serialized_' + $number}).find('table:first').attr({'rel':'serialized_' + $number}); |
| 2726 | $clone.find('div[rel^=mapping]').attr({'id':'cf_mapping_' + $number}).find('table:first').attr({'rel':'cf_mapping_' + $number}); |
| 2727 | $clone.find('a.specify_cf').attr({'rel':'serialized_' + $number}) |
| 2728 | $clone.find('a.pmxi_cf_mapping').attr({'rel':'cf_mapping_' + $number}) |
| 2729 | $clone.find('.wpallimport-cf-menu').attr({'id':'wpallimport-cf-menu-' + $number}).menu(); |
| 2730 | $clone.find('input[name^=custom_name]').autocomplete({ |
| 2731 | source: eval('__META_KEYS'), |
| 2732 | minLength: 0 |
| 2733 | }).on('click', function () { |
| 2734 | $(this).autocomplete('search', ''); |
| 2735 | $(this).attr('rel', ''); |
| 2736 | }); |
| 2737 | $clone.insertBefore($template).css('display', 'none').removeClass('template').fadeIn(); |
| 2738 | |
| 2739 | return false; |
| 2740 | }); |
| 2741 | |
| 2742 | $(document).on('click', '.add-new-key', function(){ |
| 2743 | var $template = $(this).parents('table').first().find('tr.template'); |
| 2744 | $template.clone(true).insertBefore($template).css('display', 'none').removeClass('template').fadeIn(); |
| 2745 | }); |
| 2746 | |
| 2747 | /* END Categories hierarchy */ |
| 2748 | |
| 2749 | $('form.options').each(function(){ |
| 2750 | var $form = $(this); |
| 2751 | var $uniqueKey = $form.find('input[name=unique_key]'); |
| 2752 | var $tmpUniqueKey = $form.find('input[name=tmp_unique_key]'); |
| 2753 | |
| 2754 | // Auto-detect unique key on page load if unique_key is empty and tmp_unique_key has a value |
| 2755 | if ($uniqueKey.length && $tmpUniqueKey.length && !$uniqueKey.val() && $tmpUniqueKey.val()) { |
| 2756 | $uniqueKey.val($tmpUniqueKey.val()); |
| 2757 | } |
| 2758 | |
| 2759 | $form.find('.wpallimport-auto-detect-unique-key').on('click', function(){ |
| 2760 | $uniqueKey.val($tmpUniqueKey.val()); |
| 2761 | }); |
| 2762 | }); |
| 2763 | |
| 2764 | $('form.edit').each(function(){ |
| 2765 | var $form = $(this); |
| 2766 | $form.find('.wpallimport-change-unique-key').on('click', function(){ |
| 2767 | var $ths = $(this); |
| 2768 | $( "#dialog-confirm" ).dialog({ |
| 2769 | resizable: false, |
| 2770 | height: 290, |
| 2771 | width: 550, |
| 2772 | modal: true, |
| 2773 | draggable: false, |
| 2774 | buttons: { |
| 2775 | "Continue": function() { |
| 2776 | $( this ).dialog( "close" ); |
| 2777 | $ths.hide(); |
| 2778 | $('input[name=unique_key]').removeAttr('disabled').trigger('focus'); |
| 2779 | }, |
| 2780 | Cancel: function() { |
| 2781 | $( this ).dialog( "close" ); |
| 2782 | } |
| 2783 | } |
| 2784 | }); |
| 2785 | }); |
| 2786 | var $uniqueKey = $form.find('input[name=unique_key]'); |
| 2787 | var $tmpUniqueKey = $form.find('input[name=tmp_unique_key]'); |
| 2788 | $form.find('.wpallimport-auto-detect-unique-key').on('click', function(){ |
| 2789 | $uniqueKey.val($tmpUniqueKey.val()); |
| 2790 | }); |
| 2791 | }); |
| 2792 | |
| 2793 | // chunk files upload |
| 2794 | if ($('#plupload-ui').length) { |
| 2795 | $('#plupload-ui').show(); |
| 2796 | $('#html-upload-ui').hide(); |
| 2797 | |
| 2798 | wplupload = $('#select-files').wplupload({ |
| 2799 | runtimes : 'gears,browserplus,html5,flash,silverlight,html4', |
| 2800 | url : 'admin.php?page=pmxi-admin-settings&action=upload&_wpnonce=' + wp_all_import_security + ((typeof import_id != "undefined") ? '&id=' + import_id : ''), |
| 2801 | container: 'plupload-ui', |
| 2802 | browse_button : 'select-files', |
| 2803 | file_data_name : 'async-upload', |
| 2804 | multipart: true, |
| 2805 | max_file_size: '1000mb', |
| 2806 | chunk_size: '1mb', |
| 2807 | drop_element: $('#wpallimport-drag-drop-area').length ? 'wpallimport-drag-drop-area' : 'plupload-ui', |
| 2808 | multipart_params : {} |
| 2809 | }); |
| 2810 | |
| 2811 | // Handle alternative Excel processing checkbox state for uploads |
| 2812 | if (wplupload) { |
| 2813 | wplupload.bind('BeforeUpload', function(up, file) { |
| 2814 | // Check current checkbox state and pass it to upload |
| 2815 | if ($('#use_alternative_excel_processing').is(':checked')) { |
| 2816 | up.settings.multipart_params.use_alternative_excel = '1'; |
| 2817 | } else { |
| 2818 | up.settings.multipart_params.use_alternative_excel = '0'; |
| 2819 | } |
| 2820 | }); |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | /* END plupload scripts */ |
| 2825 | |
| 2826 | $('#view_log').on('click', function(){ |
| 2827 | $('#import_finished').css({'visibility':'hidden'}); |
| 2828 | $('#logwrapper').slideToggle(100, function(){ |
| 2829 | $('#import_finished').css({'visibility':'visible'}); |
| 2830 | }); |
| 2831 | }); |
| 2832 | |
| 2833 | // Select Encoding |
| 2834 | $('#import_encoding').on('change', function(){ |
| 2835 | if ($(this).val() == 'new'){ |
| 2836 | $('#select_encoding').hide(); |
| 2837 | $('#add_encoding').show(); |
| 2838 | } |
| 2839 | }); |
| 2840 | |
| 2841 | $('#cancel_new_encoding').on('click', function(){ |
| 2842 | $('#add_encoding').hide(); |
| 2843 | $('#select_encoding').show(); |
| 2844 | $('#new_encoding').val(''); |
| 2845 | $('#import_encoding').prop('selectedIndex', 0); |
| 2846 | }); |
| 2847 | |
| 2848 | $('#add_new_encoding').on('click', function(){ |
| 2849 | var new_encoding = $('#new_encoding').val(); |
| 2850 | if ("" != new_encoding){ |
| 2851 | $('#import_encoding').prepend('<option value="'+new_encoding+'">' + new_encoding + '</option>'); |
| 2852 | $('#cancel_new_encoding').trigger('click'); |
| 2853 | $('#import_encoding').prop('selectedIndex',0); |
| 2854 | } |
| 2855 | else alert('Please enter encoding.'); |
| 2856 | }); |
| 2857 | |
| 2858 | $('input[name=keep_custom_fields]').on('click', function(){ |
| 2859 | $(this).parents('.input:first').find('.keep_except').slideToggle(); |
| 2860 | }); |
| 2861 | |
| 2862 | $('.pmxi_choosen').each(function(){ |
| 2863 | const $container = $(this); |
| 2864 | const $hiddenInput = $container.find(".choosen_input"); |
| 2865 | const tagsString = $container.find('.choosen_values').html() || ''; |
| 2866 | const tags = tagsString.split(',').filter(tag => tag.trim()); |
| 2867 | |
| 2868 | // Create a select element to use with Select2 |
| 2869 | const $select = $('<select multiple class="select2-hidden-accessible"></select>'); |
| 2870 | |
| 2871 | // Add existing values as options |
| 2872 | if ($hiddenInput.val()) { |
| 2873 | const selectedValues = $hiddenInput.val().split(','); |
| 2874 | selectedValues.forEach(value => { |
| 2875 | if (value.trim()) { |
| 2876 | $select.append(`<option value="${value.trim()}" selected>${value.trim()}</option>`); |
| 2877 | } |
| 2878 | }); |
| 2879 | } |
| 2880 | |
| 2881 | // Add all possible tags as options if not already added |
| 2882 | tags.forEach(tag => { |
| 2883 | if (tag.trim()) { |
| 2884 | let optionExists = false; |
| 2885 | $select.find('option').each(function() { |
| 2886 | if ($(this).val() === tag.trim()) { |
| 2887 | optionExists = true; |
| 2888 | return false; |
| 2889 | } |
| 2890 | }); |
| 2891 | |
| 2892 | if (!optionExists) { |
| 2893 | $select.append(`<option value="${tag.trim()}">${tag.trim()}</option>`); |
| 2894 | } |
| 2895 | } |
| 2896 | }); |
| 2897 | |
| 2898 | // Insert the select element after the hidden input |
| 2899 | $hiddenInput.after($select); |
| 2900 | |
| 2901 | // Initialize Select2 on the select element |
| 2902 | $select.select2({ |
| 2903 | tags: true, |
| 2904 | width: '80%' |
| 2905 | }); |
| 2906 | |
| 2907 | // Update hidden input when Select2 selection changes |
| 2908 | $select.on('change', function() { |
| 2909 | $hiddenInput.val($(this).val().join(',')); |
| 2910 | }); |
| 2911 | }); |
| 2912 | |
| 2913 | if (typeof wpPointerL10n != "undefined") wpPointerL10n.dismiss = 'Close'; |
| 2914 | |
| 2915 | $('.show_hints').on('click', function(){ |
| 2916 | var $ths = $(this); |
| 2917 | $('.wpallimport-overlay').show(); |
| 2918 | |
| 2919 | $(this).pointer({ |
| 2920 | content: $('#' + $ths.attr('rel')).html(), |
| 2921 | position: { |
| 2922 | edge: 'right', |
| 2923 | align: 'center' |
| 2924 | }, |
| 2925 | pointerWidth: 715, |
| 2926 | close: function() { |
| 2927 | $.post( ajaxurl, { |
| 2928 | pointer: 'pksn1', |
| 2929 | action: 'dismiss-wp-pointer' |
| 2930 | }); |
| 2931 | $('.wpallimport-overlay').hide(); |
| 2932 | } |
| 2933 | }).pointer('open'); |
| 2934 | }); |
| 2935 | |
| 2936 | // Serialized Custom Field Dialog |
| 2937 | $('.pmxi_cf_pointer').on('click', function(){ |
| 2938 | var $ths = $(this); |
| 2939 | //$('.wpallimport-overlay').show(); |
| 2940 | |
| 2941 | if ($ths.parents('.form-field:first').find('input[name^=custom_name]').val() == "") { |
| 2942 | $('#' + $ths.attr('rel')).find('.auto_detect_sf').hide(); |
| 2943 | } else { |
| 2944 | $('#' + $ths.attr('rel')).find('.auto_detect_sf').show(); |
| 2945 | } |
| 2946 | |
| 2947 | $(this).pointer({ |
| 2948 | content: $('#' + $ths.attr('rel')).html(), |
| 2949 | position: { |
| 2950 | edge: 'top', |
| 2951 | align: 'center' |
| 2952 | }, |
| 2953 | pointerWidth: 450, |
| 2954 | close: function() { |
| 2955 | $.post( ajaxurl, { |
| 2956 | pointer: 'pksn1', |
| 2957 | action: 'dismiss-wp-pointer' |
| 2958 | }); |
| 2959 | //$('.wpallimport-overlay').hide(); |
| 2960 | } |
| 2961 | }).pointer('open'); |
| 2962 | |
| 2963 | wpaiBindPopupInputs($(this).pointer('widget')); |
| 2964 | }); |
| 2965 | |
| 2966 | // Custom Fields Mapping Dialog |
| 2967 | $('.wpallimport-cf-menu li').on('click', function(){ |
| 2968 | var $triggerEvent = $(this).find('a'); |
| 2969 | if ($triggerEvent.hasClass('pmxi_cf_mapping')){ |
| 2970 | |
| 2971 | //$('.wpallimport-overlay').show(); |
| 2972 | var $ths = $triggerEvent; |
| 2973 | $triggerEvent.pointer({ |
| 2974 | content: $('#' + $ths.attr('rel')).html(), |
| 2975 | position: { |
| 2976 | edge: 'right', |
| 2977 | align: 'center' |
| 2978 | }, |
| 2979 | pointerWidth: 450, |
| 2980 | close: function() { |
| 2981 | $.post( ajaxurl, { |
| 2982 | pointer: 'pksn1', |
| 2983 | action: 'dismiss-wp-pointer' |
| 2984 | }); |
| 2985 | //$('.wpallimport-overlay').hide(); |
| 2986 | } |
| 2987 | }).pointer('open'); |
| 2988 | |
| 2989 | wpaiBindPopupInputs($triggerEvent.pointer('widget')); |
| 2990 | } |
| 2991 | }); |
| 2992 | |
| 2993 | $('.wpallimport-overlay').on('click', function(){ |
| 2994 | $('.wp-pointer').hide(); |
| 2995 | $('fieldset.wp-all-import-scheduling-help').hide(); |
| 2996 | $(this).hide(); |
| 2997 | }); |
| 2998 | |
| 2999 | if ($('#wp_all_import_code').length){ |
| 3000 | var editor = wp.codeEditor.initialize($('#wp_all_import_code'), wpai_cm_settings); |
| 3001 | editor.codemirror.setCursor(1); |
| 3002 | $('.CodeMirror').resizable({ |
| 3003 | resize: function() { |
| 3004 | editor.setSize("100%", $(this).height()); |
| 3005 | } |
| 3006 | }); |
| 3007 | var currentImportFunctions = editor.codemirror.getValue(); |
| 3008 | editor.codemirror.on('change',function(cMirror){ |
| 3009 | if ( currentImportFunctions != cMirror.getValue()){ |
| 3010 | window.onbeforeunload = function () { |
| 3011 | return 'WARNING:\nFunctions are not saved, leaving the page will reset changes in Function editor.'; |
| 3012 | }; |
| 3013 | } else { |
| 3014 | window.onbeforeunload = false; |
| 3015 | } |
| 3016 | }); |
| 3017 | } |
| 3018 | |
| 3019 | function wp_all_import_save_functions(){ |
| 3020 | var request = { |
| 3021 | action: 'save_import_functions', |
| 3022 | data: editor.codemirror.getValue(), |
| 3023 | security: wp_all_import_security |
| 3024 | }; |
| 3025 | $('.wp_all_import_functions_preloader').show(); |
| 3026 | $('.wp_all_import_saving_status').removeClass('error updated').html(''); |
| 3027 | return $.ajax({ |
| 3028 | type: 'POST', |
| 3029 | url: ajaxurl + ((typeof import_id != "undefined") ? '?id=' + import_id : ''), |
| 3030 | data: request, |
| 3031 | success: function(response) { |
| 3032 | $('.wp_all_import_functions_preloader').hide(); |
| 3033 | if (response.result) { |
| 3034 | window.onbeforeunload = false; |
| 3035 | $('.wp_all_import_saving_status').addClass('updated'); |
| 3036 | setTimeout(function() { |
| 3037 | $('.wp_all_import_saving_status').removeClass('error updated').html(''); |
| 3038 | }, 3000); |
| 3039 | } else { |
| 3040 | $('.wp_all_import_saving_status').addClass('error'); |
| 3041 | } |
| 3042 | $('.wp_all_import_saving_status').html(response.msg).show(); |
| 3043 | }, |
| 3044 | error: function( jqXHR, textStatus ) { |
| 3045 | $('.wp_all_import_functions_preloader').hide(); |
| 3046 | }, |
| 3047 | dataType: "json" |
| 3048 | }); |
| 3049 | } |
| 3050 | |
| 3051 | $('.wp_all_import_save_functions').on('click', function(){ |
| 3052 | $('.cross-sale-notice.codebox').slideUp(); |
| 3053 | |
| 3054 | wp_all_import_save_functions(); |
| 3055 | }); |
| 3056 | |
| 3057 | $('.wp_all_import_ajax_deletion').on('click', function(e){ |
| 3058 | e.preventDefault(); |
| 3059 | var $ths = $(this); |
| 3060 | $(this).attr('disabled', 'disabled'); |
| 3061 | var iteration = 1; |
| 3062 | var request = { |
| 3063 | action: 'delete_import', |
| 3064 | data: $(this).parents('form:first').serialize(), |
| 3065 | security: wp_all_import_security, |
| 3066 | iteration: iteration |
| 3067 | }; |
| 3068 | var deleteImport = function(){ |
| 3069 | request.iteration = iteration; |
| 3070 | $.ajax({ |
| 3071 | type: 'POST', |
| 3072 | url: ajaxurl + ((typeof import_id != "undefined") ? '?id=' + import_id : ''), |
| 3073 | data: request, |
| 3074 | success: function(response) { |
| 3075 | iteration++; |
| 3076 | $ths.parents('form:first').find('.wp_all_import_deletion_log').html('<p>' + response.msg + '</p>'); |
| 3077 | if (response.result){ |
| 3078 | $('.wp_all_import_functions_preloader').hide(); |
| 3079 | window.location.href = response.redirect; |
| 3080 | } else { |
| 3081 | deleteImport(); |
| 3082 | } |
| 3083 | }, |
| 3084 | error: function( jqXHR, textStatus ) { |
| 3085 | $ths.removeAttr('disabled'); |
| 3086 | $('.wp_all_import_functions_preloader').hide(); |
| 3087 | }, |
| 3088 | dataType: "json" |
| 3089 | }); |
| 3090 | } |
| 3091 | $('.wp_all_import_functions_preloader').show(); |
| 3092 | deleteImport(); |
| 3093 | }); |
| 3094 | |
| 3095 | $('.wpallimport-collapsed').each(function(){ |
| 3096 | if ( ! $(this).hasClass('closed')) $(this).find('.wpallimport-collapsed-content:first').slideDown(); |
| 3097 | }); |
| 3098 | |
| 3099 | $('.wpallimport-collapsed').find('.wpallimport-collapsed-header').not('.disabled').on('click', function(){ |
| 3100 | var $parent = $(this).parents('.wpallimport-collapsed:first'); |
| 3101 | if ($parent.hasClass('closed')){ |
| 3102 | $parent.removeClass('closed'); |
| 3103 | $parent.find('.wpallimport-collapsed-content:first').slideDown(400, function(){ |
| 3104 | if ($('#wp_all_import_code').length) editor.codemirror.setCursor(1); |
| 3105 | }); |
| 3106 | } else { |
| 3107 | $parent.addClass('closed'); |
| 3108 | $parent.find('.wpallimport-collapsed-content:first').slideUp(400); |
| 3109 | } |
| 3110 | }); |
| 3111 | |
| 3112 | $('#is_delete_posts').on('change', function(){ |
| 3113 | if ($(this).is(':checked')){ |
| 3114 | $('.wpallimport-delete-posts-warning').show(); |
| 3115 | } else { |
| 3116 | $('.wpallimport-delete-posts-warning').hide(); |
| 3117 | } |
| 3118 | }); |
| 3119 | |
| 3120 | $('.wpallimport-dependent-options').each(function(){ |
| 3121 | $(this).prev('div.input').find('input[type=text]:last, textarea:last').addClass('wpallimport-top-radius'); |
| 3122 | }); |
| 3123 | |
| 3124 | $('.wpallimport-delete-and-edit, .download_import_template, .download_import_bundle').on('click', function(e){ |
| 3125 | e.preventDefault(); |
| 3126 | window.location.href = $(this).attr('rel'); |
| 3127 | }); |
| 3128 | |
| 3129 | $('.wpallimport-wpae-notify-read-more').on('click', function(e){ |
| 3130 | e.preventDefault(); |
| 3131 | |
| 3132 | var request = { |
| 3133 | action: 'dismiss_notifications', |
| 3134 | security: wp_all_import_security, |
| 3135 | addon: $(this).parent('div:first').attr('rel') |
| 3136 | }; |
| 3137 | |
| 3138 | var ths = $(this); |
| 3139 | |
| 3140 | $.ajax({ |
| 3141 | type: 'POST', |
| 3142 | url: ajaxurl, |
| 3143 | data: request, |
| 3144 | success: function(response) { |
| 3145 | |
| 3146 | }, |
| 3147 | dataType: "json" |
| 3148 | }); |
| 3149 | |
| 3150 | $(this).parent('div:first').slideUp(); |
| 3151 | |
| 3152 | window.open($(this).attr('href'), '_blank'); |
| 3153 | }); |
| 3154 | |
| 3155 | // [ Delete Import] |
| 3156 | var wpai_are_sure_to_delete_import = function() { |
| 3157 | if ( ! $('.delete-single-import').length ) return; |
| 3158 | |
| 3159 | $('.delete-single-import').removeAttr('disabled'); |
| 3160 | |
| 3161 | if ( $('#is_delete_import').is(':checked') || $('#is_delete_posts').is(':checked')) { |
| 3162 | $('.wp-all-import-sure-to-delete').show(); |
| 3163 | } |
| 3164 | if ( ! $('#is_delete_import').is(':checked') && ! $('#is_delete_posts').is(':checked')) { |
| 3165 | $('.wp-all-import-sure-to-delete').hide(); |
| 3166 | $('.delete-single-import').attr('disabled', 'disabled'); |
| 3167 | } |
| 3168 | if ( $('#is_delete_import').is(':checked') && $('#is_delete_posts').is(':checked')) { |
| 3169 | $('.sure_delete_posts_and_import').show(); |
| 3170 | } |
| 3171 | if ($('#is_delete_import').is(':checked')) { |
| 3172 | $('.sure_delete_import').show(); |
| 3173 | } else { |
| 3174 | $('.sure_delete_import').hide(); |
| 3175 | $('.sure_delete_posts_and_import').hide(); |
| 3176 | } |
| 3177 | if ($('#is_delete_posts').is(':checked')) { |
| 3178 | $('.sure_delete_posts').show(); |
| 3179 | } else { |
| 3180 | $('.sure_delete_posts').hide(); |
| 3181 | $('.sure_delete_posts_and_import').hide(); |
| 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | wpai_are_sure_to_delete_import(); |
| 3186 | |
| 3187 | $('#is_delete_import, #is_delete_posts').on('click', function(){ |
| 3188 | wpai_are_sure_to_delete_import(); |
| 3189 | }); |
| 3190 | // [\ Delete Import] |
| 3191 | |
| 3192 | let get_delete_missing_notice_type = function() { |
| 3193 | let $is_delete_missing = $('input#is_delete_missing'); |
| 3194 | if (!$is_delete_missing.is(':checked') || $is_delete_missing.data('backups-prompt') === 'disabled') { |
| 3195 | return 0; |
| 3196 | } |
| 3197 | if ($('input[name="delete_missing_logic"]:checked').val() == 'import' && $('input[name="delete_missing_action"]:checked').val() == 'keep' && $('input[name="is_send_removed_to_trash"]').is(':checked')) { |
| 3198 | return 1; |
| 3199 | } |
| 3200 | if ($('input[name="delete_missing_logic"]:checked').val() == 'import' && $('input[name="delete_missing_action"]:checked').val() == 'keep' && $('input[name="is_change_post_status_of_removed"]').is(':checked')) { |
| 3201 | return 2; |
| 3202 | } |
| 3203 | if ($('input[name="delete_missing_logic"]:checked').val() == 'import' && $('input[name="delete_missing_action"]:checked').val() == 'remove') { |
| 3204 | return 3; |
| 3205 | } |
| 3206 | if ($('input[name="delete_missing_logic"]:checked').val() == 'all' && $('input[name="delete_missing_action"]:checked').val() == 'keep' && $('input[name="is_send_removed_to_trash"]').is(':checked')) { |
| 3207 | return 4; |
| 3208 | } |
| 3209 | if ($('input[name="delete_missing_logic"]:checked').val() == 'all' && $('input[name="delete_missing_action"]:checked').val() == 'keep' && $('input[name="is_change_post_status_of_removed"]').is(':checked')) { |
| 3210 | return 5; |
| 3211 | } |
| 3212 | if ($('input[name="delete_missing_logic"]:checked').val() == 'all' && $('input[name="delete_missing_action"]:checked').val() == 'remove') { |
| 3213 | return 6; |
| 3214 | } |
| 3215 | return 0; |
| 3216 | } |
| 3217 | |
| 3218 | function is_valid_delete_missing_options() { |
| 3219 | let is_valid = true; |
| 3220 | if ( $('input[name="is_delete_missing"]').is(':checked') && $('input[name="delete_missing_action"]:checked').val() == 'keep' ) { |
| 3221 | if ( ! $('input[name="is_send_removed_to_trash"]').is(':checked') |
| 3222 | && ! $('input[name="is_change_post_status_of_removed"]').is(':checked') |
| 3223 | && ! $('input[name="is_update_missing_cf"]').is(':checked') |
| 3224 | && ! $('input[name="missing_records_stock_status"]').is(':checked') |
| 3225 | ) { |
| 3226 | is_valid = false; |
| 3227 | } |
| 3228 | } |
| 3229 | return is_valid; |
| 3230 | } |
| 3231 | |
| 3232 | let delete_missing_helper_text = function() { |
| 3233 | $('.helper-text').hide(); |
| 3234 | if ( is_valid_delete_missing_options() ) { |
| 3235 | $('.delete-missing-error').addClass('hidden'); |
| 3236 | $('.switcher-target-delete_missing_action_keep').removeClass('delete-missing-error-wrapper'); |
| 3237 | } else { |
| 3238 | $('.delete-missing-error').removeClass('hidden'); |
| 3239 | $('.switcher-target-delete_missing_action_keep').addClass('delete-missing-error-wrapper'); |
| 3240 | } |
| 3241 | let notice_type = get_delete_missing_notice_type(); |
| 3242 | // Show notice if any. |
| 3243 | if (notice_type) { |
| 3244 | $('.helper-text-' + notice_type).find('.status_of_removed').html($('select[name="status_of_removed"]').val()); |
| 3245 | $('.helper-text-' + notice_type).show(); |
| 3246 | } |
| 3247 | }; |
| 3248 | |
| 3249 | delete_missing_helper_text(); |
| 3250 | $('.switcher-target-is_delete_missing').find('input, select').on('change', function() { |
| 3251 | delete_missing_helper_text(); |
| 3252 | }); |
| 3253 | $('#is_delete_missing').on('change', function() { |
| 3254 | delete_missing_helper_text(); |
| 3255 | }); |
| 3256 | |
| 3257 | if ($('.switcher-target-update_choosen_data').length) { |
| 3258 | var $re_import_options = $('.switcher-target-update_choosen_data'); |
| 3259 | var $toggle_re_import_options = $('.wpallimport-trigger-options'); |
| 3260 | |
| 3261 | if ($re_import_options.find('input[type=checkbox]').length == $re_import_options.find('input[type=checkbox]:checked').length) { |
| 3262 | var $newtitle = $toggle_re_import_options.attr('rel'); |
| 3263 | $toggle_re_import_options.attr('rel', $toggle_re_import_options.html()); |
| 3264 | $toggle_re_import_options.html($newtitle); |
| 3265 | $toggle_re_import_options.removeClass('wpallimport-select-all'); |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | $('.wpallimport-trigger-options').on('click', function(){ |
| 3270 | var $parent = $(this).parents('.switcher-target-update_choosen_data:first'); |
| 3271 | var $newtitle = $(this).attr('rel'); |
| 3272 | if ( $(this).hasClass('wpallimport-select-all') ) { |
| 3273 | $parent.find('input[type=checkbox]').not('.exclude-select-all').removeAttr('checked').trigger('click'); |
| 3274 | $(this).removeClass('wpallimport-select-all'); |
| 3275 | } else { |
| 3276 | $parent.find('input[type=checkbox]:checked').not('.exclude-select-all').trigger('click'); |
| 3277 | $(this).addClass('wpallimport-select-all'); |
| 3278 | } |
| 3279 | $(this).attr('rel', $(this).html()); |
| 3280 | $(this).html($newtitle); |
| 3281 | }); |
| 3282 | |
| 3283 | $('table.pmxi-admin-imports').each(function () { |
| 3284 | let manage_table = $(this); |
| 3285 | $(this).find('thead tr th.check-column :checkbox, tfoot tr th.check-column :checkbox').on('click', function () { |
| 3286 | let is_checked = $(this).is(':checked'); |
| 3287 | manage_table.find('tbody tr th.check-column :checkbox').prop('checked', function () { |
| 3288 | if (is_checked) { |
| 3289 | return true; |
| 3290 | } |
| 3291 | return false; |
| 3292 | }); |
| 3293 | }); |
| 3294 | }); |
| 3295 | |
| 3296 | // Function to check if delete missing should be restricted based on duplicate_matching |
| 3297 | function shouldRestrictDeleteMissing() { |
| 3298 | return $('input[name="duplicate_matching"]').val() === 'manual'; |
| 3299 | } |
| 3300 | |
| 3301 | $('input#is_send_removed_to_trash').change(function(e){ |
| 3302 | // "Send to trash" is always restricted in free edition |
| 3303 | $('.wpallimport-free-edition-notice.send-removed-to_trash-notice').slideDown(); |
| 3304 | $('input#is_send_removed_to_trash').addClass('wpai-shake-small'); |
| 3305 | setTimeout(function(){ |
| 3306 | $('input#is_send_removed_to_trash').prop('checked', false); |
| 3307 | $('input#is_send_removed_to_trash').removeClass('wpai-shake-small'); |
| 3308 | return false; |
| 3309 | },600); |
| 3310 | }); |
| 3311 | |
| 3312 | |
| 3313 | |
| 3314 | |
| 3315 | |
| 3316 | // Function to update delete missing behavior based on wizard type |
| 3317 | function updateDeleteMissingBehavior(resetSection = false) { |
| 3318 | let $deleteMissingCheckbox = $('input#is_delete_missing'); |
| 3319 | let $deleteMissingNotice = $('.wpallimport-free-edition-notice.delete-missing-existing-items'); |
| 3320 | let $sendToTrashNotice = $('.wpallimport-free-edition-notice.send-removed-to_trash-notice'); |
| 3321 | |
| 3322 | |
| 3323 | |
| 3324 | // Only reset delete missing section when switching wizard types, not on initial load |
| 3325 | if (resetSection) { |
| 3326 | $deleteMissingCheckbox.prop('checked', false); |
| 3327 | $('.switcher-target-is_delete_missing').hide(); // Close the delete missing section |
| 3328 | } |
| 3329 | |
| 3330 | // Show or hide the delete missing notice based on restriction status |
| 3331 | if (shouldRestrictDeleteMissing()) { |
| 3332 | $deleteMissingNotice.show(); |
| 3333 | } else { |
| 3334 | $deleteMissingNotice.hide(); |
| 3335 | } |
| 3336 | |
| 3337 | // Elements that are disabled only when wizard_type == 'matching' |
| 3338 | let $wizardRestrictedElements = $( |
| 3339 | '#delete_missing_logic_import, #delete_missing_logic_all, ' + |
| 3340 | '#delete_missing_action_keep, #delete_missing_action_remove, ' + |
| 3341 | '#is_change_post_status_of_removed, #is_update_missing_cf, ' + |
| 3342 | '#missing_records_stock_status, #is_delete_attachments, #is_delete_imgs, ' + |
| 3343 | 'select[name="status_of_removed"], select[name="status_of_removed_products"]' |
| 3344 | ); |
| 3345 | |
| 3346 | // Elements that are always restricted in free edition (regardless of wizard type) |
| 3347 | let $alwaysRestrictedElements = $('#is_send_removed_to_trash'); |
| 3348 | |
| 3349 | if (shouldRestrictDeleteMissing()) { |
| 3350 | // For "Update existing" - show main notice, disable wizard-restricted elements |
| 3351 | $deleteMissingNotice.show(); |
| 3352 | $wizardRestrictedElements.prop('disabled', true); |
| 3353 | } else { |
| 3354 | // For "Create new" - hide main notice, enable wizard-restricted elements |
| 3355 | $deleteMissingNotice.hide(); |
| 3356 | $wizardRestrictedElements.prop('disabled', false); |
| 3357 | $deleteMissingCheckbox.removeClass('wpai-shake-small'); |
| 3358 | } |
| 3359 | |
| 3360 | // Always keep free edition restricted elements enabled so shake behavior works |
| 3361 | $alwaysRestrictedElements.prop('disabled', false); // Enable so shake behavior works |
| 3362 | $sendToTrashNotice.hide(); // Hide notice initially (shown on click) |
| 3363 | $('input#is_send_removed_to_trash').removeClass('wpai-shake-small'); |
| 3364 | |
| 3365 | |
| 3366 | } |
| 3367 | |
| 3368 | // Initialize on page load |
| 3369 | updateDeleteMissingBehavior(false); // Don't reset section on initial load |
| 3370 | |
| 3371 | $('input#is_delete_missing').change(function(e){ |
| 3372 | if (shouldRestrictDeleteMissing()) { |
| 3373 | // Show notice if it exists, otherwise just do shake behavior |
| 3374 | let $notice = $('.wpallimport-free-edition-notice.delete-missing-existing-items'); |
| 3375 | if ($notice.length) { |
| 3376 | $notice.slideDown(); |
| 3377 | } |
| 3378 | $('input#is_delete_missing').addClass('wpai-shake-small'); |
| 3379 | setTimeout(function(){ |
| 3380 | $('input#is_delete_missing').prop('checked', false); |
| 3381 | $('input#is_delete_missing').removeClass('wpai-shake-small'); |
| 3382 | return false; |
| 3383 | },600); |
| 3384 | } |
| 3385 | }); |
| 3386 | |
| 3387 | fix_tag_position(); |
| 3388 | |
| 3389 | var toggle_dynamic_notice = function(){ |
| 3390 | |
| 3391 | $('.wpallimport-dynamic-notice').hide(); |
| 3392 | |
| 3393 | var $import_to_custom_type = $('input[name=custom_type]').val(); |
| 3394 | |
| 3395 | var $is_show_cf_notice = false; |
| 3396 | var $is_show_images_notice = false; |
| 3397 | |
| 3398 | $('input[name^=custom_name]').each(function(){ |
| 3399 | if ( $(this).val() != "") |
| 3400 | { |
| 3401 | $is_show_cf_notice = true; |
| 3402 | return false; |
| 3403 | } |
| 3404 | }); |
| 3405 | |
| 3406 | if ( $import_to_custom_type != 'product' && |
| 3407 | ( $('textarea[name=download_featured_image]').length && $('textarea[name=download_featured_image]').val() != "" || |
| 3408 | $('textarea[name=gallery_featured_image]').length && $('textarea[name=gallery_featured_image]').val() != "" || |
| 3409 | $('textarea[name=featured_image]').length && $('textarea[name=featured_image]').val() != "" )) |
| 3410 | { |
| 3411 | $is_show_images_notice = true; |
| 3412 | } |
| 3413 | |
| 3414 | if ( $is_show_cf_notice && $is_show_images_notice ){ |
| 3415 | $('.wpallimport-dynamic-notice').find('a').html($('.wpallimport-dynamic-notice-cf-image-text').val()); |
| 3416 | $('.wpallimport-dynamic-notice').show(); |
| 3417 | } |
| 3418 | else if ( $is_show_cf_notice ){ |
| 3419 | $('.wpallimport-dynamic-notice').find('a').html($('.wpallimport-dynamic-notice-cf-text').val()); |
| 3420 | $('.wpallimport-dynamic-notice').show(); |
| 3421 | } |
| 3422 | else if ( $is_show_images_notice ) { |
| 3423 | $('.wpallimport-dynamic-notice').find('a').html($('.wpallimport-dynamic-notice-images-text').val()); |
| 3424 | $('.wpallimport-dynamic-notice').show(); |
| 3425 | } |
| 3426 | |
| 3427 | } |
| 3428 | |
| 3429 | $(document).on('scroll', function() { |
| 3430 | fix_tag_position(); |
| 3431 | toggle_dynamic_notice(); |
| 3432 | }); |
| 3433 | |
| 3434 | toggle_dynamic_notice(); |
| 3435 | |
| 3436 | $('input[name^=custom_name], input[name^=custom_value], textarea[name=download_featured_image], textarea[name=gallery_featured_image], textarea[name=featured_image]').change(function() { |
| 3437 | toggle_dynamic_notice(); |
| 3438 | }); |
| 3439 | |
| 3440 | $('input[name^=custom_name], input[name^=custom_value], textarea[name=download_featured_image], textarea[name=gallery_featured_image], textarea[name=featured_image]').blur(function() { |
| 3441 | toggle_dynamic_notice(); |
| 3442 | }); |
| 3443 | |
| 3444 | });})(jQuery); |
| 3445 |