diff --git a/wagtail_modeltranslation/static/modeltranslation/js/copy_stream_fields.js b/wagtail_modeltranslation/static/modeltranslation/js/copy_stream_fields.js new file mode 100644 index 0000000..74aa323 --- /dev/null +++ b/wagtail_modeltranslation/static/modeltranslation/js/copy_stream_fields.js @@ -0,0 +1,62 @@ + +/* 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 += ''; + }; + }; + }; + + + /* 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"); + }) + +} \ No newline at end of file diff --git a/wagtail_modeltranslation/wagtail_hooks.py b/wagtail_modeltranslation/wagtail_hooks.py index d15c273..8d6cc07 100644 --- a/wagtail_modeltranslation/wagtail_hooks.py +++ b/wagtail_modeltranslation/wagtail_hooks.py @@ -1,7 +1,11 @@ # coding: utf-8 +import json + from django.utils.html import format_html, format_html_join from django.conf import settings +from django.conf.urls import url +from django.http import QueryDict from wagtail.wagtailcore import hooks @@ -23,3 +27,79 @@ def translated_slugs(): js_languages = "" % (", ".join(lang_codes)) return format_html(js_languages) + js_includes + + +############################################################################### +# Copy StreamFields content +############################################################################### +""" +Ajax view that allows to duplicate content +between translated streamfields +""" +from django.http import HttpResponse + +def return_translation_target_field_rendered_html(request, page_id): + from wagtail.wagtailcore.models import Page + + page = Page.objects.get(pk=page_id) + + if request.is_ajax(): + origin_field_name = request.GET.get('origin_field_name') + target_field_name = request.GET.get('target_field_name') + origin_field_serialized = json.loads(request.GET.get('serializedOriginField')) + + # Patch field prefixes from origin field to target field + target_field_patched = [] + for item in origin_field_serialized: + patched_item = None + for att in item.iteritems(): + target_value = att[1] + if att[0] == 'name': + target_value = att[1].replace(origin_field_name, target_field_name) + patched_item = {"name": target_value} + else: + patched_item["value"] = att[1] + + target_field_patched.append(patched_item) + + # convert to QueryDict + q_data = QueryDict('', mutable=True) + for item in target_field_patched: + q_data.update({item['name']: item['value']}) + + # get render html + + target_field = page.specific._meta.get_field(target_field_name) + value_data = target_field.stream_block.value_from_datadict(q_data, {}, target_field_name) + target_field_content_html = target_field.formfield().widget.render(target_field_name, value_data) + + # return html json + return HttpResponse(json.dumps(target_field_content_html), content_type='application/json') + + +@hooks.register('register_admin_urls') +def copy_streamfields_content(): + + return [ + url(r'(?P\d+)/edit/copy_translation_content$', return_translation_target_field_rendered_html, name='' ), + ] + + +@hooks.register('insert_editor_js') +def streamfields_translation_copy(): + """ + Includes script in editor html file that creates + buttons to copy content between translated stream fields + and send a ajax request to copy the content. + """ + + #includes the java script file in the html file + js_files = [ + 'modeltranslation/js/copy_stream_fields.js', + ] + + js_includes = format_html_join('\n', '', + ((settings.STATIC_URL, filename) for filename in js_files) + ) + + return js_includes