/*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('');
$li.find('span').text('[' + this.language + '] ' + this.title);
$li.appendTo ( $ul );
if(this.source_language && !translation_set_has_source_language) {
translation_set_has_source_language = true;
}
});
var alert = $('').append(jQuery('').html(postEdit.$connect_translations_dialog.data('alert-text')));
alert.appendTo($list);
var set_as_source_checkbox = $('');
if(!translation_set_has_source_language) {
set_as_source_checkbox.attr('checked', 'checked');
}
var action = $('