mirror of
https://github.com/Hopiu/wagtail-modeltranslation.git
synced 2026-05-25 15:23:48 +00:00
62 lines
No EOL
2.4 KiB
JavaScript
62 lines
No EOL
2.4 KiB
JavaScript
|
|
/* Creates the copy buttons in the header of each stream field */
|
|
$(document).ready(function(){
|
|
//All the stream fields with all his content
|
|
var allStreamFields = $('li.stream-field');
|
|
|
|
/* Iterate all stream fields, put the copy buttons in each one.*/
|
|
for (var i = 0; i < allStreamFields.length; i++) {
|
|
//Current Field with all content
|
|
var currentStreamField = allStreamFields[i];
|
|
//Current Field header
|
|
var header = $(currentStreamField).children('h2')[0];
|
|
//Search for the input field so that we can get is id to know the field's name.
|
|
var streamFieldDiv = $(currentStreamField).find('div.sequence-container.sequence-type-stream')[0];
|
|
var fieldInfos = $(streamFieldDiv).children('input')[0].id.split('-')[0]
|
|
var fieldName = fieldInfos.split('_')[0];
|
|
var fieldLang = fieldInfos.split('_')[1];
|
|
//The cycle to create the buttons for copy each language field
|
|
for (var j = 0; j < langs.length; j++) {
|
|
if (fieldLang != langs[j]) {
|
|
var currentFieldID = fieldName + '_' + fieldLang;
|
|
var targetFieldID = fieldName + '_' + langs [j];
|
|
header.innerHTML += '<button class="translation-field-copy" current-lang-code="'+ currentFieldID +'" data-lang-code="'+ targetFieldID +'">'+langs[j]+'</button>';
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
/* on click binding */
|
|
$('.translation-field-copy').click(function(event){
|
|
event.preventDefault();
|
|
var lang = $(this).attr('data-lang-code');
|
|
var currentLang = $(this).attr('current-lang-code');
|
|
requestCopyField(lang, currentLang);
|
|
});
|
|
});
|
|
|
|
/* Copy the content of originID field to the targetID field */
|
|
function requestCopyField(originID, targetID) {
|
|
/* Get the originID field and convert him to json string */
|
|
var serializedForm = $("#page-edit-form").serializeArray();
|
|
var serializedOriginField = $.grep(serializedForm, function(obj){return obj.name.indexOf(originID) >= 0;});
|
|
var jsonString = JSON.stringify(serializedOriginField);
|
|
|
|
/* AJAX request that returns the html content of originID field
|
|
with the id's changed to targetID */
|
|
$.ajax({
|
|
url: 'copy_translation_content',
|
|
type: 'GET',
|
|
dataType: 'json',
|
|
data: {'origin_field_name': originID, 'target_field_name': targetID, 'serializedOriginField': jsonString},
|
|
})
|
|
.done(function(data) {
|
|
/* Put the html data in the targetID field */
|
|
var wrapperDiv = $("#"+targetID+"-count").parents('.input')[0];
|
|
$(wrapperDiv).html(data);
|
|
})
|
|
.fail(function() {
|
|
console.log("error");
|
|
})
|
|
|
|
} |