/*globals icl_ajx_url */ /** * Created by andrea. * Date: 23/01/14 * Time: 17:28 */ jQuery(document).ready(function ($) { setupCopyButtons(); var postEdit = postEdit || {}; postEdit.$connect_translations_dialog = $('#connect_translations_dialog'); postEdit.$no_posts_found_message = postEdit.$connect_translations_dialog.find('.js-no-posts-found'); postEdit.$posts_found_container = postEdit.$connect_translations_dialog.find('.js-posts-found'); postEdit.$ajax_loader = postEdit.$connect_translations_dialog.find('.js-ajax-loader'); postEdit.$connect_translations_dialog_confirm = $("#connect_translations_dialog_confirm"); postEdit.connect_element_translations_open = function(event) { if (typeof(event.preventDefault) !== 'undefined' ) { event.preventDefault(); } else { event.returnValue = false; } postEdit.$connect_translations_dialog.find('#post_search').val(''); postEdit.$connect_translations_dialog.find('#assign_to_trid').val(''); postEdit.$connect_translations_dialog.dialog('open'); postEdit.connect_element_translations_data(); }; postEdit.connect_element_translations_data = function() { var $connect_translations_dialog_selector = $('#post_search', postEdit.$connect_translations_dialog ); var trid = $('#icl_connect_translations_trid').val(); var post_type = $('#icl_connect_translations_post_type').val(); var source_language = $('#icl_connect_translations_language').val(); var nonce = $('#_icl_nonce_get_orphan_posts').val(); var data = 'icl_ajx_action=get_orphan_posts&source_language=' + source_language + '&trid=' + trid + '&post_type=' + post_type + '&_icl_nonce=' + nonce; postEdit.$ajax_loader.show(); var request = $.ajax({ type: "POST", url: icl_ajx_url, dataType: 'json', data: data }); request.done(function( posts ) { var $assignPostButton = $('.js-assign-button'); if ( posts.length > 0 ) { postEdit.$posts_found_container.show(); postEdit.$no_posts_found_message.hide(); $assignPostButton.prop('disabled', false); $connect_translations_dialog_selector.autocomplete({ minLength: 0, source: posts, focus: function (event, ui) { $connect_translations_dialog_selector.val(ui.item.label); return false; }, select: function (event, ui) { $connect_translations_dialog_selector.val(ui.item.label); $("#assign_to_trid").val(ui.item.value); return false; } }) .focus() .data("ui-autocomplete")._renderItem = function (ul, item) { return $("
  • ") .append(jQuery("").text(item.label)) .appendTo(ul); }; } else { postEdit.$posts_found_container.hide(); postEdit.$no_posts_found_message.show(); $assignPostButton.prop('disabled', true); } }); request.always(function() { postEdit.$ajax_loader.hide(); // Hide ajax loader always, no matter if ajax succeed or not. }); }; postEdit.connect_element_translations_init = function () { postEdit.$connect_translations_dialog.dialog( { dialogClass : 'wpml-dialog otgs-ui-dialog', width : 'auto', modal : true, autoOpen : false, closeOnEscape: true, buttons : [ { text : postEdit.$connect_translations_dialog.data('cancel-label'), 'class': 'button button-secondary alignleft', click : function () { $(this).dialog("close"); } }, { text : postEdit.$connect_translations_dialog.data('ok-label'), 'class': 'button button-primary js-assign-button', click : function () { $(this).dialog("close"); postEdit.connect_element_translations_do(); } } ] } ); }(); // Auto executable function postEdit.connect_element_translations_do = function() { var trid = $("#assign_to_trid").val(); var post_type = $('#icl_connect_translations_post_type').val(); var post_id = $('#icl_connect_translations_post_id').val(); var nonce = $('#_icl_nonce_get_posts_from_trid').val(); var data = 'icl_ajx_action=get_posts_from_trid&trid=' + trid + '&post_type=' + post_type + '&_icl_nonce=' + nonce; var request = $.ajax({ type: "POST", url: icl_ajx_url, dataType: 'json', data: data }); request.done(function ( posts ) { if ( posts.length > 0 ) { var $list = $('#connect_translations_dialog_confirm_list'); $list.empty(); var $ul = $('