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;
}
var table_option = {
language : result.datas.language,
confirm_button_action : function(e, $_content, close){
e.preventDefault();
var caller = this;
//Get the strings
var strings = {};
var modified = 0;
$.each($_content.find('.do_table_line'),function(){
//Get only strings if they need to be translated and have actually been translated
switch (1===1){
case $(this).find('.do_table_language').is('[readonly]'):
strings[$(this).find('.do_table_constant').data('value')] = $(this).find('.do_table_language').val();
break;
case $(this).find('.do_table_language').prop('defaultValue') !== $(this).find('.do_table_language').val():
strings[$(this).find('.do_table_constant').data('value')] = $(this).find('.do_table_language').val();
modified = 1;
break;
}
});
//Initialize ajax request datas
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') {
//Close the edition
if(close) {
caller.cancel_button_action(e);
}
toast('Modifications saved');
$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');
}
}
});
},
columns : {
reference : {
show : show_reference
},
language : {
editable : true,
title : 'Language ' + result.datas.language,
allow_copy : false
},
override : {
show : false
}
},
copy_destination : 'do_table_language',
after_init : function($_content){
//Do not allow to edit already translated strings
$_content.find('.do_table_language').filter(function () {
return this.value.length > 0
}).attr('readonly','readonly').parents('tr').find('[class^="do_table_copy_"]').hide();
//Set an introduction message
if($_content.find('.do_table_language').not('[readonly="readonly"]').length){
$_content.prepend('
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(''); //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){ e.preventDefault(); 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&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&slug=' + $(this).closest('tr').attr('data-slug') + '&language=' + $(this).closest('tr').attr('data-lang') + '&TB_iframe=true'); } }); 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(); } 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 += ''; //Create the table of strings content += '