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(''); //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 += ''; //Create the table of strings content += '