Merge pull request #229 from infoportugal/populate_slug_not_translated

Populate slug not translated
This commit is contained in:
Diogo Marques 2019-03-12 17:18:36 +00:00 committed by GitHub
commit d0034364d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 22 deletions

6
.gitignore vendored
View file

@ -41,3 +41,9 @@ _build
# vim
*~
*.swp
# vscode
.vscode
# pyenv
.python-version

View file

@ -1,23 +1,36 @@
$(document).ready(function () {
/* Only non-live pages should auto-populate the slug from the title */
if (!$('body').hasClass('page-is-live')) {
var slugFollowsTitle = false;
$.each(langs, function (idx, lang_code) {
lang_code = lang_code.replace("-", "_");
$('#id_title_' + lang_code).on('focus', function () {
/* slug should only follow the title field if its value matched the title's value at the time of focus */
var currentSlug = $('#id_slug_' + lang_code).val();
var slugifiedTitle = cleanForSlug(this.value, true);
slugFollowsTitle = (currentSlug == slugifiedTitle);
if(!translate_slugs) {
lang_code = default_lang.replace("-", "_");
title_selector = '#id_title_' + lang_code;
slug_selector = '#id_slug';
slugAutoPopulateTranslation(title_selector, slug_selector);
} else {
$.each(langs, function (idx, lang_code) {
lang_code = lang_code.replace("-", "_");
title_selector = '#id_title_' + lang_code;
slug_selector = '#id_slug_' + lang_code;
slugAutoPopulateTranslation(title_selector, slug_selector);
});
$('#id_title_' + lang_code).on('keyup keydown keypress blur', function () {
if (slugFollowsTitle) {
var slugifiedTitle = cleanForSlug(this.value, true);
$('#id_slug_' + lang_code).val(slugifiedTitle);
}
});
});
}
}
});
function slugAutoPopulateTranslation(title_selector, slug_selector) {
var slugFollowsTitle = false;
$(title_selector).on('focus', function () {
/* slug should only follow the title field if its value matched the title's value at the time of focus */
var currentSlug = $(slug_selector).val();
var slugifiedTitle = cleanForSlug(this.value, true);
slugFollowsTitle = (currentSlug == slugifiedTitle);
});
$(title_selector).on('keyup keydown keypress blur', function () {
if (slugFollowsTitle) {
var slugifiedTitle = cleanForSlug(this.value, true);
$(slug_selector).val(slugifiedTitle);
}
});
}

View file

@ -2,13 +2,16 @@
import json
from six import iteritems
from django.conf import settings
from django.conf.urls import url
from django.http import HttpResponse
from django.http import QueryDict
from django.utils.html import format_html, format_html_join, escape
from django.http import HttpResponse, QueryDict
from django.utils.html import escape, format_html, format_html_join
from django.views.decorators.csrf import csrf_exempt
from six import iteritems
from wagtail_modeltranslation import settings as wmt_settings
from modeltranslation import settings as mt_settings
try:
from wagtail.core import hooks
from wagtail.core.models import Page
@ -33,7 +36,17 @@ def translated_slugs():
for lang in settings.LANGUAGES:
lang_codes.append("'%s'" % lang[0])
js_languages = "<script>var langs=[%s];</script>" % (", ".join(lang_codes))
js_languages = """
<script>
var langs=[{langs}];
var default_lang='{default_lang}';
var translate_slugs={translate_slugs};
</script>
""".format(
langs=", ".join(lang_codes),
default_lang=mt_settings.DEFAULT_LANGUAGE,
translate_slugs='true' if wmt_settings.TRANSLATE_SLUGS else 'false'
)
return format_html(js_languages) + js_includes