/*jshint browser:true, devel:true */ /*global jQuery, wp, document, wpml_language_switcher_admin, _, iclSaveForm_success_cb, iclSaveForm */ var WPML_core = WPML_core || {}; WPML_core.languageSwitcher = (function( $, wpml_ls ) { "use strict"; /** @namespace wpml_ls.strings */ /** @namespace wpml_ls.strings.confirmation_item_remove */ /** @namespace wpml_ls.color_schemes */ /** @namespace wpml_ls.strings.table_no_item */ /** @namespace wpml_ls.templates */ var form, dialogBox, formAndDialogBox, additionalCssStyleId = 'wpml-ls-inline-styles-additional-css', dialogInlineStyleId = 'wpml-ls-inline-styles-dialog', currentItemSlug, slotInlineStylesBackup; var init = function () { form = $('#wpml-ls-settings-form'); dialogBox = $('#wpml-ls-dialog'); formAndDialogBox = form.add(dialogBox); maybeInitAdditionalCssStyle(); initDialogNode(); initColorPicker(); initLanguageSortable(); attachEvents(); openDialogFromHash(); $(document).trigger('wpml_ls_admin_loaded'); }; var attachEvents = function () { attachAutoSaveEvents(); attachToggleEvents(); attachTooltipEvents(); attachDialogEvents(); attachRowActionEvents(); attachSelectedSlotChangeEvents(); attachPresetColorsEvent(); attachTemplateChangeEvents(); attachMenuHierarchicalEvents(); attachUpdatePreviewEvents(); attachSaveClickEvents(); fixSelectedOption(); forceRefreshOnBrowserBackButton(); setupWizardNextEvent(); preventClickOnPreviewLinks(); }; var maybeInitAdditionalCssStyle = function() { if ($('#' + additionalCssStyleId).length < 1) { var style = $(''); $('#' + styleId).replaceWith(newStyleNode); }); }; var attachTemplateChangeEvents = function() { formAndDialogBox.on('change', '.js-wpml-ls-template-selector', function() { var selected = $(this).val(), subform = $(this).closest('.js-wpml-ls-subform'), force = wpml_ls.templates[ selected ].force_settings; subform.find('.js-wpml-ls-to-include input').prop('disabled', false); jQuery.each(force, function(k, v){ if(v === 1) { subform.find('.js-wpml-ls-setting-' + k).attr('checked', 'checked').prop('disabled', true); } else { subform.find('.js-wpml-ls-setting-' + k).removeAttr('checked').prop('disabled', true); } }); }); }; var attachMenuHierarchicalEvents = function() { formAndDialogBox.on('change', '.js-wpml-ls-menu-is-hierarchical', function() { var isHierarchical = parseInt($(this).val()), subform = $(this).closest('.js-wpml-ls-subform'), includeCurrentLangInput = subform.find('.js-wpml-ls-setting-display_link_for_current_lang'); includeCurrentLangInput.prop('disabled', false); if(isHierarchical === 1) { includeCurrentLangInput.attr('checked', 'checked').prop('disabled', true); } }); }; var updatePreview = _.debounce( function(subform) { $('.js-wpml-ls-dialog-save').prop('disabled', true); var preview = subform.find('.js-wpml-ls-preview'); if(preview.length > 0) { showSpinner(preview); var previewData = getSerializedSettings(subform); wp.ajax.send({ data: { action: 'wpml-ls-update-preview', nonce: wpml_ls.nonce, slot_settings: previewData }, success: function (data) { preview.empty(); updateDialogInlineStyle(data.styles); buildCSSLinks(data.css); buildJSScripts(data.js); showUpdatedContent(preview, data.html); }, error: function (data) { showUpdatedContent(preview, data, 0, 'error'); } }); } }, 500); var attachSaveClickEvents = function() { form.on('click', '.js-wpml-ls-save-settings', function(e){ e.preventDefault(); updateSettings($(this)); }); }; var updateSettings = _.debounce(function (wrapper, successCallback) { var messageWrapper = wrapper.closest('.js-wpml-ls-option').find('.js-wpml-ls-messages'), settings = getSerializedSettings(form); if(messageWrapper.length === 0) { messageWrapper = wrapper.closest('.js-wpml-ls-section').find('.js-wpml-ls-messages'); } messageWrapper.empty(); showSpinner(messageWrapper); wp.ajax.send({ data: { action: 'wpml-ls-save-settings', nonce: wpml_ls.nonce, settings: settings }, success: function (data) { showUpdatedContent(messageWrapper, data, 2500, 'updated'); if(typeof successCallback === 'function') { successCallback(); } }, error: function (data) { showUpdatedContent(messageWrapper, data, 2500, 'error'); } }); }, 500); var getSerializedSettings = function(form) { var disabled = form.find(':input:disabled').removeAttr('disabled'), settings = form.find('input, select, textarea').serialize(); disabled.attr('disabled','disabled'); return settings; }; var showSpinner = function(wrapper) { $('.js-wpml-ls-messages').removeClass('success error').hide().empty(); wrapper.siblings('.spinner').addClass('is-active'); }; var showUpdatedContent = function(wrapper, content, delay, type) { wrapper.siblings('.spinner').removeClass('is-active'); wrapper.removeClass('updated notice error'); if(type) { wrapper.addClass(type); } wrapper.html(content).fadeIn(); if(delay) { wrapper.delay(delay).fadeOut(); } $('.js-wpml-ls-dialog-save').prop('disabled', false); }; var updateDialogInlineStyle = function (styles) { $('#' + dialogInlineStyleId).remove(); if(styles) { var newDialogStyle; if (styles instanceof jQuery) { newDialogStyle = styles; } else { newDialogStyle = $($.parseHTML(styles)).first(); } newDialogStyle.attr('id', dialogInlineStyleId); $('#' + additionalCssStyleId).before(newDialogStyle); } }; var copyDialogInlineStyleToSlot = function (itemType, slug) { var dialogStyleClone = $('#' + dialogInlineStyleId).clone(), targetId = 'wpml-ls-inline-styles-' + itemType + '-' + slug, targetStyle = $('#' + targetId); dialogStyleClone.attr('id', targetId); if (targetStyle.length) { targetStyle.replaceWith(dialogStyleClone); } else { $('#' + additionalCssStyleId).before(dialogStyleClone); } }; var buildCSSLinks = function (css) { var i, linkTag; if (typeof css !== typeof undefined && null !== css) { for (i = 0; i < css.length; i++) { linkTag = $('link[href="' + css[i] + '"]'); if (0 === linkTag.length) { linkTag = $(''); linkTag.attr('rel', 'stylesheet'); linkTag.attr('type', 'text/css'); linkTag.attr('media', 'all'); linkTag.attr('href', css[i]); linkTag.prependTo('head'); } } } }; var buildJSScripts = function (js) { var i, jsTag; if (typeof js !== typeof undefined && null !== js) { for (i = 0; i < js.length; i++) { jsTag = $('script[src="' + js[i] + '"]'); if (0 === jsTag.length) { jsTag = $(''); jsTag.attr('type', 'text/javascript'); jsTag.attr('src', js[i]); jsTag.prependTo('head'); } } } }; return { 'init': init }; })( jQuery, wpml_language_switcher_admin ); jQuery(document).ready(function () { "use strict"; WPML_core.languageSwitcher.init(); });