var joomunited_url = 'https://www.joomunited.com/'; jQuery(document).ready(function($){ // Define jutranslation_ajax_action if doesn't exist if (typeof jutranslation.ajax_action === 'undefined') { jutranslation.ajax_action=''; } //Load the available version from Joomunited $.each(julanguages, function(){ var julanguage = this; $.ajax({ url : joomunited_url + "index.php?option=com_jutranslation&task=translations.getTranslations", type : 'POST', data : julanguage, success : function(data){ data = JSON.parse(data); $('#jutranslations-languages tbody tr[data-slug="'+ julanguage.extension + '"]').each(function(){ var lang = $(this).data('lang'); var slug = $(this).data('slug'); //Is this language one of Joomunited has translated var found = false; //For each translation found add the version and an install btn $.each(data.data,function(){ if(this.language === lang && this.extension === slug){ found = true; //Add new version availability information var html; switch (versionCompare(julanguage.versions[this.language], this.extension_version, julanguage.revisions[this.language], this.revision)) { case 1: html = 'You already have the newer version
The latest version on Joomunited is ' + this.extension_version + ' rev'+this.revision + 'Install this version'; break; case 0: html = 'You already have the latest version Reinstall'; break; case -1: if (julanguage.versions[this.language]) { install_update = 'Update'; } else { install_update = 'Install'; } html = 'There is a new version of language file available v' + this.extension_version + ' rev'+this.revision + '' + install_update + ''; } $('#jutranslations-languages tr[data-lang="'+ this.language + '"][data-slug="'+ julanguage.extension + '"] td .latest_version').html(html); //Initialize all installation button $('.jutranslation-install').unbind().bind('click',function(e){ e.preventDefault(); var $clicked_button = $(this); var $parent_line = $clicked_button.parents('tr'); var slug = $clicked_button.parents('tr').data('slug'); //Get from Joomunited the translation $.ajax({ url : joomunited_url + "index.php?option=com_jutranslation&task=translations.download", type : 'POST', data : { extension : $clicked_button.data("extension"), translation : $clicked_button.data("id") }, success : function(result){ result = JSON.parse(result); //Do not show language columns if it's en-US var show_reference = true; if(result.datas.language==='en-US'){ show_reference = false; } //auto save after download translation var strings = {}; var modified = 0; $.each(result.datas.strings,function(key,value){ strings[value.key] = value.language; }); var ajax_data = { action : jutranslation.ajax_action, slug: slug, strings : JSON.stringify(strings), language : result.datas.language, extension_version : result.datas.version, revision : result.datas.revision, modified : modified, wp_nonce : jutranslation.token }; ajax_data[jutranslation.token] = 1; $.ajax({ type: 'POST', url: jutranslation.base_url + 'task=jutranslation.saveStrings', data: ajax_data, success: function (result2) { result2 = JSON.parse(result2); if (result2.status === 'success') { toast('Language is installed successfully'); $parent_line.find('.current_version').html(result.datas.version + ' rev' + result.datas.revision); $parent_line.attr('data-version', result.datas.version); if(modified){ $parent_line.find('.jutranslation-share').show().addClass('just-added'); }else{ $parent_line.find('.jutranslation-share').hide(); } $parent_line.find('.latest_version').html('The language file has been updated'); $parent_line.find('.jutranslation-edition').removeClass('disabled'); } else { alert('Error while updating language file'); } } }); //(end) auto save after download translation } }); }); } }); //Check if a translation has been found on joomunited var $line = $('#jutranslations-languages tr[data-lang="'+ lang + '"][data-slug="'+ julanguage.extension + '"]'); if(found===false){ $line.find('td .latest_version').html('No translation found'); } if(!$line.attr('data-installed')){ $line.find('td .jutranslation-edition').addClass('disabled').attr({'data-toggle': 'tooltip','data-placement': 'top', 'data-tippy': 'Please install the latest available version first'}); } else { $line.find('td .jutranslation-edition').attr({'data-toggle': 'tooltip','data-placement': 'top', 'data-tippy': 'Make sure you have the latest version installed before editing the original file'}); } $line.find('td .jutranslation-override').attr({'data-toggle': 'tooltip','data-placement': 'top', 'data-tippy': 'Override the extension translation for your own needs, without modifying the original language file'}); tippy('.jutranslation-edition, .jutranslation-override', { theme: 'wpmf', animation: 'scale', animateFill: false, maxWidth: 300, duration: 0, arrow: true, onShow(instance) { instance.popper.hidden = false; instance.setContent($(instance.reference).data('tippy')); } }); }); } }); }); // Edit an installed version $('.jutranslation-edition').click(function(e){ e.preventDefault(); var $clicked_button = $(this); // Do nothing is disabled if ($clicked_button.hasClass('disabled')) { return; } var language = $clicked_button.parents('tr').data('lang'); var slug = $clicked_button.parents('tr').data('slug'); getStrings(language, slug, function(datas){ var table_option = { language : language, slug : slug, confirm_button_action : function(e, $_content, close){ if(e!==undefined){ e.preventDefault(); } if (typeof getAutoSave !== 'undefined') clearTimeout(getAutoSave); var caller = this; //Get the strings var values = {}; var modified = 0; $.each($_content.find('.do_table_line'),function(){ var value = $(this).find('.do_table_language').val(); if(value !== ''){ values[$(this).find('.do_table_constant').data('value')] = value; } //Check if something has been modified if ($(this).find('.do_table_language').prop('defaultValue') !== $(this).find('.do_table_language').val()) { modified = 1; } }); if (modified === 1) { //Initialize ajax request datas var ajax_data = { action : jutranslation.ajax_action, strings: JSON.stringify(values), language: language, slug: slug, destination: 'edition', modified: '1', wp_nonce : jutranslation.token }; ajax_data[jutranslation.token] = 1; $.ajax({ type: 'POST', url: jutranslation.base_url + 'task=jutranslation.saveStrings', data: ajax_data, success: function (result) { result = JSON.parse(result); if (result.status === 'success') { //Close the edition if(close) { caller.cancel_button_action(e); } toast('Modifications saved'); $clicked_button.parents('tr').find('.jutranslation-share').show().addClass('just-added'); } else { alert('Error while updating language file'); } } }); }else{ //Do nothing and close the edition if(close) { caller.cancel_button_action(e); } toast('Modifications saved'); } }, columns : { language : { editable : true, title : 'Language ' + language, allow_copy : false }, override : { show : false } }, copy_destination : 'do_table_language', after_init : function($_content){ //Add a message for translators before allowing to do anything var message = '

This feature is for translators only, the content may be overwritten during the next language update. ' + 'If you only want to edit language strings for your own purpose please use the override feature.

' + '

OK got it, let me translate and share' + 'Cancel

'; $_content.hide().after('
' + message + '
'); //Click on ok edit button $('#jutranslation_show_edition').click(function(){ $('#jutranslation .control-group.edition-message').remove(); $('#jutranslation .control-group.edition').show(); }); //Click on cancel edition button $('#jutranslation_cancel_edition').click(function(){ $('#jutranslation .control-group.edition-message').remove(); $('#jutranslation .control-group.edition').remove(); $('#jutranslation .control-group').show(); }); } }; //Generate the table doTable(datas, table_option); }); }); $('.jutranslation-override').click(function(e){ e.preventDefault(); var $clicked_button = $(this); var language = $clicked_button.parents('tr').data('lang'); var slug = $clicked_button.parents('tr').data('slug'); //Do not show language columns if it's en-US var show_language = true; if(language==='en-US'){ show_language = false; } getStrings(language, slug, function(datas) { var table_option = { language : language, confirm_button_action : function(e, $_content, close){ if(e!==undefined){ e.preventDefault(); } if (typeof getAutoSave !== 'undefined') clearTimeout(getAutoSave); var caller = this; //Get the strings var overrides = {}; $.each($_content.find('.do_table_line'),function(){ var value = $(this).find('.do_table_override').val(); if(value !== ''){ overrides[$(this).find('.do_table_constant').data('value')] = value; } }); //Initialize ajax request datas var ajax_data = { action : jutranslation.ajax_action, strings : JSON.stringify(overrides), language : this.language, slug: slug, destination : 'override', wp_nonce : jutranslation.token }; ajax_data[jutranslation.token] = 1; $.ajax({ type : 'POST', url : jutranslation.base_url + 'task=jutranslation.saveStrings', data : ajax_data, success : function(result){ result = JSON.parse(result); if(result.status === 'success'){ $clicked_button.parents('tr').find('.jutranslation-override-count').html(Object.keys(overrides).length); if(close) { caller.cancel_button_action(e); } toast('Modifications saved'); }else{ alert('Error while updating language file'); } } }); }, columns : { language : { title : 'Language ' + language, show : show_language } } }; //Generate the table doTable(datas, table_option); }); }); //Share a translation to Joomunited $('.jutranslation-share').click(function(e){ e.preventDefault(); if(typeof SqueezeBox !== 'undefined' ){ //Open Joomla lightbox SqueezeBox.open(jutranslation.base_url + 'action=&task=jutranslation.showViewForm&wp_nonce=' + jutranslation.token + '&language=' + $(this).closest('tr').attr('data-lang'), {handler: 'iframe'}); }else{ //Open Wordpress lightbox tb_show('Share with Joomunited',jutranslation.base_url + 'action=' + jutranslation.ajax_action + '&task=jutranslation.showViewForm&wp_nonce=' + jutranslation.token + '&slug=' + $(this).closest('tr').attr('data-slug') + '&language=' + $(this).closest('tr').attr('data-lang') + '&TB_iframe=true'); } }); var eventMethod = window.addEventListener ? 'addEventListener' : 'attachEvent'; var eventer = window[eventMethod]; var messageEvent = (eventMethod == 'attachEvent') ? 'onmessage' : 'message'; // Listen to message from jutranslation window eventer(messageEvent, function (e) { var res = e.data; if(typeof res != 'undefined' && typeof res.type != 'undefined' && res.type === 'translation_login'){ $.ajax({ url : jutranslation.base_url + 'task=jutranslation.saveJuToken', type: 'POST', data: { action : jutranslation.ajax_action, token: res.token, wp_nonce: jutranslation.token }, success: function () { } }); } }, false); function doTable(datas, options) { // DOM element containing the generated content var $_content; var default_options = { language : null, slug : null, confirm_button_label: 'Save and close', save_button_label: 'Save', cancel_button_label: 'Cancel', cancel_button_action: function (e) { if(e!==undefined){ e.preventDefault(); } $('#jutranslation .control-group.edition').remove(); $('#jutranslation .control-group').show(); }, confirm_button_action : function (e) { if(e!==undefined){ e.preventDefault(); } if (typeof getAutoSave !== 'undefined') clearTimeout(getAutoSave); if (typeof getAutoSave !== "undefined") { clearTimeout(getAutoSave); } options.cancel_button_action(e); }, after_init : function () {}, columns : { constant : { show : true, title : 'Constant', allow_copy : false }, reference : { show : true, editable : false, title : 'Reference en-US', allow_copy : true }, language : { show : true, editable : false, title : 'Language', allow_copy : true }, override : { show : true, editable : true, title : 'Override', allow_copy : false } }, copy_destination : 'do_table_override' }; // Initialize options variable if not already if(options === undefined){ var options = {}; } // Merge the default options to the passed ones options = $.extend(true, {}, default_options, options); //Add a button to save var content = ''; content += '

'; content += ' ' + options.confirm_button_label + ''; content += ' ' + options.save_button_label + ''; content += ' ' + options.cancel_button_label + ''; content += '

'; //Create the table of strings content += ''; //Add columns names content += '' + ' '; //Add filter inputs content += '' + ' '; $.each(datas, function(key,value){ content += '' + '' + ' '; }); content += '
'; $('#jutranslation .control-group').after('
' + content + '
'); $_content = $('#jutranslation .control-group.edition'); // Enable filtering $_content.find('[class^="do_table_filter_"]').on("change keyup paste", function(){ //Texts filters var filter_constant = $_content.find('.do_table_filter_constant').val(); var filter_reference = $_content.find('.do_table_filter_reference').val(); var filter_language = $_content.find('.do_table_filter_language').val(); var filter_override = $_content.find('.do_table_filter_override').val(); // Do not allow to check both filters switch (1==1) { case $(this).hasClass('do_table_filter_empty_reference') && $(this).is(':checked'): $_content.find('.do_table_filter_not_empty_reference').attr('checked',null); break; case $(this).hasClass('do_table_filter_empty_language') && $(this).is(':checked'): $_content.find('.do_table_filter_not_empty_language').attr('checked',null); break; case $(this).hasClass('do_table_filter_empty_override') && $(this).is(':checked'): $_content.find('.do_table_filter_not_empty_override').attr('checked',null); break; case $(this).hasClass('do_table_filter_not_empty_reference') && $(this).is(':checked'): $_content.find('.do_table_filter_empty_reference').attr('checked',null); break; case $(this).hasClass('do_table_filter_not_empty_language') && $(this).is(':checked'): $_content.find('.do_table_filter_empty_language').attr('checked',null); break; case $(this).hasClass('do_table_filter_not_empty_override') && $(this).is(':checked'): $_content.find('.do_table_filter_empty_override').attr('checked',null); break; } if($(this).is('[class^="do_table_filter_empty"]')){ } else if ($(this).is('[class^="do_table_filter_not_empty"]')) { } $_content.find('.do_table_filter_not_empty_reference'); //Empty filters var filter_empty_reference = $_content.find('.do_table_filter_empty_reference').is(":checked"); var filter_empty_language = $_content.find('.do_table_filter_empty_language').is(":checked"); var filter_empty_override = $_content.find('.do_table_filter_empty_override').is(":checked"); var filter_not_empty_reference = $_content.find('.do_table_filter_not_empty_reference').is(":checked"); var filter_not_empty_language = $_content.find('.do_table_filter_not_empty_language').is(":checked"); var filter_not_empty_override = $_content.find('.do_table_filter_not_empty_override').is(":checked"); // Show all rows $_content.find('tr.do_table_line').show(); // Filtering is active if(filter_constant !== '' || filter_reference !== '' || filter_language !== '' || filter_override != '' || filter_empty_reference!==false || filter_empty_language !== false || filter_empty_override !== false || filter_not_empty_reference!==false || filter_not_empty_language !== false || filter_not_empty_override !== false){ // Show only rows matching search $_content.find('.do_table_line').filter(function(){ //Filter by empty reference if(filter_empty_reference && $(this).find('.do_table_reference').val() !== ''){ return true; } //Filter by empty language if(filter_empty_language && $(this).find('.do_table_language').val() !== ''){ return true; } //Filter by empty override if(filter_empty_override && $(this).find('.do_table_override').val() !== ''){ return true; } //Filter by not empty reference if(filter_not_empty_reference && $(this).find('.do_table_reference').val() === ''){ return true; } //Filter by empty language if(filter_not_empty_language && $(this).find('.do_table_language').val() === ''){ return true; } //Filter by empty override if(filter_not_empty_override && $(this).find('.do_table_override').val() === ''){ return true; } //Filter by constant if(filter_constant !== ''){ var regex = new RegExp(escapeRegExp(filter_constant), 'i'); if(!regex.test($(this).find('.do_table_constant').data('value'))){ return true; } } //Filter by reference if(filter_reference !== ''){ var regex = new RegExp(escapeRegExp(filter_reference), 'i'); if(!regex.test($(this).find('.do_table_reference').val())){ return true; } } //Filter by current language if(filter_language !== ''){ var regex = new RegExp(escapeRegExp(filter_language), 'i'); if(!regex.test($(this).find('.do_table_language').val())){ return true; } } //Filter by override if(filter_override !== ''){ var regex = new RegExp(escapeRegExp(filter_override), 'i'); if(!regex.test($(this).find('.do_table_override').val())){ return true; } } return false; }).hide(); return; } }); var getAutoSave; var autoSave = () => { clearTimeout(getAutoSave); getAutoSave = setTimeout(function () { bootbox.confirm(jutranslationCmd.warning_auto_save, function (result) { clearTimeout(getAutoSave); if (result) { options.confirm_button_action(undefined, $_content, false); } }); }, 300000) }; // Enable copy from one input to another $_content.find('[class^="do_table_copy_"]').click(function(e){ e.preventDefault(); //Get the value from clicked element if($(this).hasClass('do_table_copy_reference')){ var value = $(this).parents('tr').find('.do_table_reference').val(); }else if($(this).hasClass('do_table_copy_language')){ var value = $(this).parents('tr').find('.do_table_language').val(); }else if($(this).hasClass('do_table_copy_override')){ var value = $(this).parents('tr').find('.do_table_override').val(); } //Copy value to destination input autoSave(); $(this).parents('tr').find('.'+options.copy_destination).val(value); }); //Save and close $_content.find('.do_table_confirm').click(function(e){ clearTimeout(getAutoSave); options.confirm_button_action(e, $_content, true); }); //Confirm the current action $_content.find('.do_table_save').click(function(e){ clearTimeout(getAutoSave); options.confirm_button_action(e, $_content, false); }); //Cancel the current action $_content.find('.do_table_cancel').click(function(e, $_content){ clearTimeout(getAutoSave); options.cancel_button_action(e); }); $('#jutranslation .control-group').not('.edition').hide(); $_content.find('.do_table_line input').focus(function(){ var $this = $(this); var $this_line = $this.parents('tr'); //Do nothing if already hidden if($(this).is(":hidden")){ return; } //Remove all old textarea $this_line.find('textarea').remove(); $this_line.find('input').each(function(){ // Get the input value var val = $(this).val(); // Add a textarea after the inpu $('').insertAfter(this); // Hide the input $(this).hide(); }); // Remove textarea on focus out $this.siblings('textarea').focus().focusout(function(){ $this.val(($(this).val())); autoSave(); $this_line.find('input').show(); $this_line.find('textarea').hide(); //Hide instead of remove to avoid html5fallback.js error }); }); options.after_init.call(options, $_content); } function getStrings(language, slug, done_function) { var strings = {}; $.ajax({ type: 'POST', url: jutranslation.base_url + 'task=jutranslation.getTranslation', data: { action : jutranslation.ajax_action, language: language, slug: slug, wp_nonce : jutranslation.token }, success: function (result) { result = JSON.parse(result); if (result.status === 'success') { done_function(result.datas.strings); } else { alert('Error while loading strings'); } } }); } /** * Escape a string to allow using it literally in a regex * @param string * @return string escaped */ function escapeRegExp(string) { return string.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); } /** * Secure a string to use it in html content * @param string to espace * @return {string} escaped string */ function htmlEntities(string){ return String(string).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); } /** * Compare two version numbers and their revisions * @param version1 * @param version2 * @param revision_version1 * @param revision_version2 * @return int 1 if version1>version2, -1 if version1 0){ //Latest version is upper return 1; } } //End only with 0 version numbers, it's equal if(revision_version1 > revision_version2){ return 1; } else if(revision_version1 < revision_version2){ return -1; } return 0; } else { if(parseInt(version1[ij]) > parseInt(version2[ij])) { return 1; } else if(parseInt(version1[ij]) < parseInt(version2[ij])) { return -1; } } } //Version 2 has the longest version or is equal for(var ik=ij; ik < version2.length; ik++){ //Check if it has another thing as 0 trailing if(parseInt(version2[ik]) > 0){ //Latest version is upper return -1; } } //End only with 0 version numbers, it's equal if(revision_version1 > revision_version2){ return 1; } else if(revision_version1 < revision_version2){ return -1; } return 0; } function toast(text) { $('#jutranslation_toast').remove(); $('body').append('
' + text + '
'); setTimeout(function() { $('#jutranslation_toast').remove(); }, 2000); } });