function X13Allegro() { this.ajaxUrl = currentIndex; this.ajaxToken = xallegro_token; this.ajaxRequest = []; this.ajaxForceTerminateLoop = false; this.isModernLayout = false; this.presta17 = false; this.successAuctions = 0; this.errorAuctions = 0; this.successAuctionsMsg = ''; this.errorAuctionsMsg = ''; this.productizationName = ''; this.productizationDescription = ''; this.productizationMode = 'ASSIGN'; this.productSelectMode = 0; this.titleMaxCount = 75; this.shippingRateMarketplaces = []; this._cast(); } X13Allegro.prototype = { constructor: X13Allegro, _cast: function() { cast = function() { var type = $(this).data('cast'); var unsigned = false; var allowEmpty = false; var precision = 2; // workaround for classes if (typeof type === 'undefined') { if ($(this).hasClass('xcast-float')) { type = 'float'; } else if ($(this).hasClass('xcast-int')) { type = 'integer'; } else { return; } } if (typeof $(this).data('cast-unsigned') !== 'undefined' && $(this).data('cast-unsigned')) { unsigned = true; } // workaround for classes else if ($(this).hasClass('xcast-unsigned')) { unsigned = true; } if (typeof $(this).data('cast-allow-empty') !== 'undefined' && $(this).data('cast-allow-empty')) { allowEmpty = true; } // workaround for classes else if ($(this).hasClass('xcast-allow-empty')) { allowEmpty = true; } if (typeof $(this).data('cast-precision') !== 'undefined') { precision = parseInt($(this).data('cast-precision')); } // @todo workaround for classes var val = $(this).val(); if (!val.length) { if (allowEmpty) { return; } val = 0; } val = val.toString().replace(/[^\d,.\-]/g, '').replace(',', '.'); if (val < 0 && !unsigned) { val *= -1; } if (isNaN(val) || val.length == 0) { val = 0; } if (type == 'float') { $(this).val(castFormat(val, precision)); } else if (type == 'integer') { $(this).val(parseInt(val)); } function castFormat(value, precision) { var newValue = parseFloat(value); var pow = Math.pow(10, 2); newValue *= pow; var nextDigit = Math.floor(newValue * 10) - 10 * Math.floor(newValue); newValue = (nextDigit >= 5 ? Math.ceil(newValue) : Math.floor(newValue)); return (newValue / pow).toFixed(precision); } }; if ($('[data-cast]').length) { $('[data-cast]').each(cast).on('focusout change xchange', cast); } }, _shipmentsSwitch: function(element) { shipmentsSwitch = function() { if ($(this).is(':checked')) { $(this).parent().parent().find('input[type="text"]:not(.shipment-disabled)').removeAttr('disabled'); } else { $(this).parent().parent().find('input[type="text"]:not(.shipment-disabled)').prop('disabled', 'disabled'); } }; $(element).on('change', shipmentsSwitch).each(shipmentsSwitch); }, _countryCodeSwitch: function(element) { if (element.val() === 'PL') { $('#province, #post_code').removeAttr('disabled'); } else { $('#province, #post_code').val('').prop('disabled', 'disabled'); } }, _titleCount: function(title) { var customChars = { '<': 4, '>': 4, '&': 5, '"': 6 }; var size = title.length; var count = 0; for (var key in customChars) { count = title.split(key).length - 1; size += (count*(customChars[key]-1)); } return size; }, _modalAlert: function($modalAlert, confirmCallback, cancelCallback) { $modalAlert.addClass('active'); $modalAlert.find('.modal-body').css({ 'max-height': $(window).outerHeight() - 200, 'overflow-y': 'auto' }); $modalAlert.closest('.modal').css('overflow-y', 'hidden'); $modalAlert.on('click', '.modal_alert-confirm', function(e) { e.preventDefault(); $modalAlert.removeClass('active'); $modalAlert.closest('.modal').css('overflow-y', 'auto'); if (typeof confirmCallback === 'function') { confirmCallback(); } }); $modalAlert.on('click', '.modal_alert-cancel', function(e) { e.preventDefault(); $modalAlert.removeClass('active'); $modalAlert.closest('.modal').css('overflow-y', 'auto'); if (typeof cancelCallback === 'function') { cancelCallback(); } }); }, _configurationWiderColsFix: function($form) { $form.find('.form-group').each(function(index, element) { var confId = $(element).find('.control-label').parent(); confId.find('.col-lg-3').removeClass('col-lg-3').addClass('col-lg-4'); confId.find('.col-lg-offset-3').removeClass('col-lg-offset-3').addClass('col-lg-offset-4'); confId.find('.col-lg-9').removeClass('col-lg-9').addClass('col-lg-8'); }); }, _configurationDependencies: function() { handleFieldDependencies(); var $fieldDependencies = getFieldDependencies(); for (var i = 0; i < $fieldDependencies.length; i++) { $(document).off($fieldDependencies[i]).on('change', '[name="'+ $fieldDependencies[i] +'"]', function () { handleFieldDependencies($fieldDependencies[i]); }).bind(i); } function getFieldDependencies() { var fieldDependencies = []; $('.depends-on').each(function (index, node) { var $element = $(node); var $classes = $element.prop('class').split(/\s+/); for (var i = 0; i < $classes.length; i++) { var current = $classes[i]; if (current.includes('depends-field')) { var parts = current.replace('depends-field-', '').split(':'); fieldDependencies.push(parts[0]); } } }); return fieldDependencies; } function handleFieldDependencies(specificFieldName) { var specificField = specificFieldName || false; $('.depends-on').each(function (index, node) { var $element = $(node); var $classes = $element.prop('class').split(/\s+/); var $method = 'match'; var $fieldName = false; var $fieldValue = false; var $fieldType = false; var $currentValue; var $typeOfTheField; if ($element.hasClass('depends-on-multiple')) { $fieldValue = []; $fieldName = []; $fieldType = []; } for (var i = 0; i < $classes.length; i++) { var current = $classes[i]; if (current.includes('depends-where')) { if (current === 'depends-where-is-not') { $method = 'not_match'; } } if (current.includes('depends-field')) { var parts = current.replace('depends-field-', '').split(':'); var $nameOfTheField = parts[0]; var $valueOfTheField = parts[1].split('--'); if ($element.hasClass('depends-on-multiple')) { $fieldName.push($nameOfTheField); $fieldValue.push($valueOfTheField); } else { $fieldName = $nameOfTheField; $fieldValue = $valueOfTheField; } if ($('input[name="'+ $nameOfTheField +'"]').length > 0) { $typeOfTheField = $('input[name="'+ $nameOfTheField +'"]').attr('type'); } else if ($('textarea[name="'+ $nameOfTheField +'"]').length === 1) { $typeOfTheField = 'textarea'; } else if ($('select[name="'+ $nameOfTheField +'"]').length === 1) { $typeOfTheField = 'select'; } if ($element.hasClass('depends-on-multiple')) { $fieldType.push($typeOfTheField); } else { $fieldType = $typeOfTheField; } } } if ($element.hasClass('depends-on-multiple')) { var showBasedOnMultiple = true; for (var i = 0; i < $fieldName.length; i++) { if ($fieldType[i] === 'checkbox' || $fieldType[i] === 'radio') { $currentValue = $('[name="'+ $fieldName[i] +'"]:checked').val(); } else if ($fieldType[i] === 'select') { $currentValue = $('[name="'+ $fieldName[i] +'"] option:selected').val(); } else { $currentValue = $('[name="'+ $fieldName[i] +'"]').val(); } if ($method === 'match') { if (!inArray($currentValue, $fieldValue[i])) { showBasedOnMultiple = false; } } if ($method === 'not_match') { if (inArray($currentValue, $fieldValue[i])) { showBasedOnMultiple = false; } } } if (showBasedOnMultiple) { $element.slideDown(); } else { $element.slideUp(); } } else { if (specificField && specificField !== $fieldName) { return; } if ($fieldType === 'checkbox' || $fieldType === 'radio') { $currentValue = $('[name="'+ $fieldName +'"]:checked').val(); } else if ($fieldType === 'select') { $currentValue = $('[name="'+ $fieldName +'"] option:selected').val(); } else { $currentValue = $('[name="'+ $fieldName +'"]').val(); } if ($method === 'not_match' && $fieldName && $fieldValue) { if ($fieldValue.includes($currentValue)) { $element.slideUp(); } else { $element.slideDown(); } } if ($method === 'match' && $fieldName && $fieldValue) { if ($fieldValue.includes($currentValue)) { $element.slideDown(); } else { $element.slideUp(); } } } }); } function inArray(needle, haystack) { var length = haystack.length; for (var i = 0; i < length; i++) { if (haystack[i] === needle) return true; } return false; } }, accountAuth: function() { var self = this; var authInterval; var authIntervalTime = 5; var $modal = $(document).find('#account_authorization_modal'); $(document).on('click', '#allegroAccountAuth', function (e) { e.preventDefault(); self.ajaxPOST({ action: 'authorizeApplication', id_account: $(this).data('account') }, function () { var cover = $('
'); cover.appendTo('body').hide().fadeIn(400); }, function (json) { $(document).find('body #allegro_cover').remove(); if (!json.success) { showErrorMessage(json.text); return false; } authIntervalTime = json.interval; $modal.find('.modal-header .x13allegro-modal-title-small span').text(json.username); $modal.find('.modal-body').html(json.html); $modal.modal('show'); }); }); $(document).on('click', '#allegroAuthButton', function () { var button = $(this); $modal.find('.modal-header .close').remove(); $modal.find('.modal-body #allegroAuthProcess').show(); $modal.find('.modal-body #allegroAuthButton').prop('disabled', 'disabled').addClass('btn-disabled').hide(); authInterval = setInterval(function () { self.ajaxPOST({ action: 'authorizeApplicationCheck', id_account: button.data('account') }, null , function (json) { if (json.success) { if (!json.process) { clearInterval(authInterval); $modal.find('.modal-body #allegroAuthStart').hide(); $modal.find('.modal-body #allegroAuthProcess').hide(); $modal.find('.modal-body #allegroAuthSuccess #allegroAuthMarketplace').text(json.baseMarketplace); $modal.find('.modal-body #allegroAuthSuccess').show(); if (json.configurationForm) { $modal.find('.modal-body #allegroAuthConfiguration').html(json.configurationForm); $modal.find('.modal-body #allegroAuthConfiguration #configuration_id_language').trigger('change'); } else { $modal.find('.modal-body #allegroAuthSuccess #allegroAuthButtonFinish').show(); } } } else { clearInterval(authInterval); $modal.find('.modal-body #allegroAuthStart').hide(); $modal.find('.modal-body #allegroAuthProcess').hide(); $modal.find('.modal-body #allegroAuthError #allegroAuthErrorMsg').text(json.text); $modal.find('.modal-body #allegroAuthError').show(); } }); }, authIntervalTime *1000); }); $(document).on('change', '#configuration_id_language', function() { var $button = $modal.find('.modal-body #allegroAuthConfiguration #allegroAuthButtonSave'); if ($(this).val() === '') { $button.addClass('disabled').prop('disabled', true); } else { $button.removeClass('disabled').prop('disabled', false); } }); }, auctionList: function() { var self = this; var $modalForm = $('#xallegro_auction_form_modal'); var $modalListSettings = $('#xallegro_auction_list_settings'); $('#xallegroFilterStatus').on('change', function () { $(this).parents('form').submit(); }); $('#xallegroFilterMarketplace').on('change', function () { $(this).parents('form').submit(); }); $('#showProductizationTools').on('click', function (e) { e.preventDefault(); $('#productizationTools').toggle(); }); $('#auctionListSettings').on('click', function (e) { e.preventDefault(); $modalListSettings.modal('show'); }); $modalListSettings.find('.auction-fields-list-table tbody').sortable({ handle: '.auction-fields-list-sort', helper: function(e, ui) { ui.children().each(function() { $(this).width($(this).width()); }); return ui; }, start: function (e, ui) { ui.placeholder.css({ height: ui.item.outerHeight(), width: ui.item.outerWidth() }); } }); $modalListSettings.on('click', 'button[name="saveAuctionListSettings"]', function (e) { e.preventDefault(); var fields = {}; $modalListSettings.find('.auction-fields-list-check').each(function (index, element) { fields[$(element).val()] = +$(element).is(':checked'); }); self.ajaxPOST({ action: 'saveAuctionListSettings', fields: fields }, null, function (json) { window.location.href = json.url; }); }); $(document).on('click', '#bulkUpdateAuctionsTrigger', function(e) { e.preventDefault(); $('.bulk-actions .bulkUpdate').parent().click(); }); $(document).on('change', '.x-auction-list-auto_renew select', function() { var offerId = $(this).data('id'); var autoRenew = $(this).val(); self.ajaxPOST({ action: 'changeAutoRenew', offerId: offerId, autoRenew: autoRenew }, null, function(json) { if (json.success) { showSuccessMessage('Zaktualizowano auto wznawianie'); } else { showErrorMessage('Wystąpił błąd podczas aktualizacji auto wznawiania'); } }); }); $(document).on('focusout', '.x-auction-form-list input', function() { if ($(this).data('max') !== '' && (parseInt($(this).prop('value')) > parseInt($(this).data('max')) && parseInt($(this).data('oos')) == 0)) { $(this).prop('value', $(this).data('max')); } if (parseInt($(this).prop('value')) < 1) { $(this).prop('value', 1); } }); $(document).on('click', '.x-auction-form-list-delete', function(e) { e.preventDefault(); $(this).closest('tr').remove(); if ($modalForm.find('.x-auction-form-list-delete').length <= 0) { $modalForm.modal('hide'); } }); $(document).on('click', '.x-updater-redo-btn', function(e) { e.preventDefault(); $modalForm.modal('hide'); $('.bulk-actions .bulkUpdate').parent().click(); }); $(document).on('click', '.x-updater-logger-all', function (e) { e.preventDefault(); $modalForm.find('.x-updater-logger-content').find('li').show(); $(this).hide(); }); $(document).on('click', '.x-updater-logger-with-errors', function (e) { e.preventDefault(); $modalForm.find('.x-updater-logger-content').find('li').show(); $modalForm.find('.x-updater-logger-content').find('li:not(.as-error)').hide(); $modalForm.find('.x-updater-logger-all').show(); }); $(document).on('click', '.x-updater-logger-with-warnings', function (e) { e.preventDefault(); $modalForm.find('.x-updater-logger-content').find('li').show(); $modalForm.find('.x-updater-logger-content').find('li:not(.as-warning)').hide(); $modalForm.find('.x-updater-logger-all').show(); }); $(document).on('change', '[x-name="update-auction-entity"]', function () { if ($(this).val() != 0) { $modalForm.find('.x-auction-form-submit').show(); $modalForm.find('.x-updater-entity').hide(); $modalForm.find('.x-updater-extra-settings #updater_entity_' + $(this).val()).show(); } else { $modalForm.find('.x-auction-form-submit, .x-updater-entity').hide(); } }); $(document).on('click', '.x-auction-form-submit', function(e) { e.preventDefault(); processFormModal(); }); $(document).on('click', '.x-updater-action-close-popup', function(e) { e.preventDefault(); $(this).attr('disabled', 'disabled'); $modalForm.find('.x-updater-redo-btn').attr('disabled', 'disabled'); window.location.reload(); }); // Update $('[x-name=action_update]').on('click', function(e) { e.preventDefault(); $('input[name="xallegro_auctionBox[]"]').removeAttr('checked'); $(this).parents('tr').find('input[name="xallegro_auctionBox[]"]').prop('checked', true); getFormModal(getSelectedAuctions('action_update'), 'update'); }); if ($('.bulkUpdate').length > 0) { var bulkUpdate = $('.bulkUpdate').parent(); bulkUpdate.removeAttr('onclick'); bulkUpdate.bind('click', function(e) { e.preventDefault(); var items = getSelectedAuctions('action_update'); if (items.length > 0) { getFormModal(items, 'update'); } else { alert('Nie wybrano ofert do aktualizacji.'); } }); } // Finish $('[x-name=action_finish]').on('click', function(e) { e.preventDefault(); $('input[name="xallegro_auctionBox[]"]').removeAttr('checked'); $(this).parents('tr').find('input[name="xallegro_auctionBox[]"]').prop('checked', true); getFormModal(getSelectedAuctions('action_finish'), 'finish'); }); if ($('.bulkFinish').length > 0) { var bulkFinish = $('.bulkFinish').parent(); bulkFinish.removeAttr('onclick'); bulkFinish.bind('click', function(e) { e.preventDefault(); var items = getSelectedAuctions('action_finish'); if (items.length > 0) { getFormModal(items, 'finish'); } else { alert('Nie wybrano ofert do zakończenia.'); } }); } // Resume $('[x-name=action_redo]').on('click', function(e) { e.preventDefault(); $('input[name="xallegro_auctionBox[]"]').removeAttr('checked'); $(this).parents('tr').find('input[name="xallegro_auctionBox[]"]').prop('checked', true); getFormModal(getSelectedAuctions('action_redo'), 'redo'); }); if ($('.bulkRedo').length > 0) { var bulkRedo = $('.bulkRedo').parent(); bulkRedo.removeAttr('onclick'); bulkRedo.bind('click', function(e) { e.preventDefault(); var items = getSelectedAuctions('action_redo'); if (items.length > 0) { getFormModal(items, 'redo'); } else { alert('Nie wybrano ofert do wznowienia.'); } }); } // AutoRenew setting if ($('.bulkAutoRenew').length > 0) { var bulkAutoRenew = $('.bulkAutoRenew').parent(); bulkAutoRenew.removeAttr('onclick'); bulkAutoRenew.bind('click', function(e) { e.preventDefault(); var items = getSelectedAuctions('action_unbind'); // get offers by unbind if (items.length > 0) { getFormModal(items, 'auto_renew'); } else { alert('Nie wybrano ofert do zmiany ustawień wznawiania, lub wybrane oferty nie są powiązane.'); } }); } // Unbind if ($('.bulkUnbind').length > 0) { var bulkUnbind = $('.bulkUnbind').parent(); bulkUnbind.removeAttr('onclick'); bulkUnbind.bind('click', function(e) { e.preventDefault(); var items = getSelectedAuctions('action_unbind'); var formAction = $(this).closest('form').attr('action') + '&submitBulkunbind'; if (items.length > 0) { self._modalAlert($('#offer_unbind_modal_alert'), function() { var formBulkUnbind = $('