modeltranslations 10.7 obviates the need for our own l10n shim (#4184)

* modeltranslations 10.7 obviates the need for our own l10n shim

* forget to save

* ugh
This commit is contained in:
Pomax 2020-02-11 11:51:48 -08:00 коммит произвёл GitHub
Родитель 2174450350
Коммит df4b0ef5cf
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 4 добавлений и 257 удалений

Просмотреть файл

@ -29,7 +29,7 @@ wagtail-factories = "*"
wagtail-inventory = "==0.8"
wagtail-metadata = "*"
whitenoise = "*"
wagtail-modeltranslation = "==0.10.6"
wagtail-modeltranslation = "==0.10.7"
"psycopg2-binary" = "==2.8.4"
cloudinary = "*"
wagtail-experiments = "*"

6
Pipfile.lock сгенерированный
Просмотреть файл

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "117f5ee349f9f4cb49697708a339dc0cc280e71f851bc95a2a71f4e1bc5c0c38"
"sha256": "9e18873c6682dd77c95e01d1554592dec4e9463c445794aad685867ba16408c2"
},
"pipfile-spec": 6,
"requires": {
@ -511,10 +511,10 @@
},
"wagtail-modeltranslation": {
"hashes": [
"sha256:d3d8e072f9b1a9b81c7ff9c4ca7aa4572ff97d9f6689f80f552d84454cb49b9b"
"sha256:16ca215c5d35837a6cd5ca7e9e7f9f5f91811d6be3c8a9a928915d55ce4a493e"
],
"index": "pypi",
"version": "==0.10.6"
"version": "==0.10.7"
},
"webencodings": {
"hashes": [

Просмотреть файл

@ -214,7 +214,6 @@ INSTALLED_APPS = list(filter(None, [
'networkapi.milestones',
# wagtail localisation app
'networkapi.wagtail_l10n_customization',
'wagtail_modeltranslation',
'wagtail_modeltranslation.makemigrations',
'wagtail_modeltranslation.migrate',

Просмотреть файл

@ -1,51 +0,0 @@
form .l10n-hidden {
display: none;
}
div.locale-picker {
display: block;
text-align: right;
margin-top: 1.5em;
margin-bottom: 0.5em;
}
form > div.locale-picker {
/* calc, to highlight what we're doing here:
wagtail's header has a 2em margin-bottom,
and so we correct that for the 1.5em
margin-bottom used in the normal definition
for the div.locale-picker, above */
margin-top: calc(1.5em - 2em);
margin-bottom: 1em;
}
div.locale-picker h2 {
display: inline;
}
div.locale-picker ul.locales {
display: inline-block;
margin: auto 1em;
}
div.locale-picker ul.locales li.locale {
display: inline;
margin: auto 0.5em;
}
div.locale-picker ul.locales li.locale button.locale-toggle {
background-color: rgba(57, 151, 150, 0.3);
text-transform: uppercase;
outline: none;
border: none;
font-weight: 700;
font-size: 1.2em;
text-decoration: none;
color: #ffffff;
padding: 0.25em;
max-height: 2em;
}
div.locale-picker ul.locales li.locale button.locale-toggle.showing-locale {
background-color: #43b1b0;
}

Просмотреть файл

@ -1,153 +0,0 @@
///////////////
jQuery(() => {
///////////////
const tabbedContent = $(`form .tab-content`);
const topLevel =
tabbedContent.length > 0 ? tabbedContent : $(`.content form`);
if (topLevel.length === 0) {
// obviously, if we don't have an element to attach
// the picker to, we might as well stop right now.
return;
}
if (topLevel.attr(`class`) && topLevel.attr(`class`).indexOf(`search`) > -1) {
// if the only forms on the page are search forms,
// we're not actually dealing with page/snippets
return;
}
/**
* ...
*/
function filterForLocale(index, element) {
var tc = element.textContent;
var res = tc.match(/ \[\w\w\]/);
if (res === null) return;
var code = res[0],
locale = code.replace(/[ \[\]]/g, "");
// Verify this is a known locale and not a fluke,
// using the global "langs" variable, which is an
// array of all language codes specified in the
// settings.LANGUAGES variable for Django.
if (langs.indexOf(locale) === -1) return;
// We do our show/hiding based on list items,
// otherwise we're just "emptying" a list item
// while leaving its spacing CSS intact.
if (element.nodeName !== "LI") {
element = $(element).closest("li")[0];
}
// Bootstrap an empty bin if we don't have one.
if (!localisedElements[locale]) {
localisedElements[locale] = [];
}
// Add this element to our bin, provided it had
// not already been added.
var bin = localisedElements[locale];
if (bin.indexOf(element) === -1) {
bin.push(element);
element.classList.add(`l10n-hidden`);
// also note that "field-col" elements may now look horribly
// wrong, due to how Wagtail computes which of "col3"..."col12"
// to use. Because wagtail-modeltranslation introduces many more
// elements to show in an "inline" element, things that were
// "col6" before end up being "col1", looking terribly wrong indeed.
element.classList.remove(...columnCSS);
}
}
/**
* Build the set of fields-per-locale. Each set will receive
* a button to toggle visibility for all fields in that set,
* with the note that unlocalised content (such as images)
* will always stay visible.
*/
function buildSets() {
$(`li.object, div.field`, topLevel).each(filterForLocale);
}
/**
* Build a locale picker bar, with buttons that toggle
* visibility for each locale's fields.
*/
function buildLocaleToggler() {
var bar = $(
`<div class="locale-picker"><h2>View/edit fields for:</h2></div>`
);
var ul = $(`<ul class="locales"></ul>`);
bar.append(ul);
var toggles = {};
locales.forEach(locale => {
var li = $(
`<li class="locale"><button class="locale-toggle">${locale}</button></li>`
);
ul.append(li);
$(`button.locale-toggle`, li).each((index, toggle) => {
toggle.addEventListener(`click`, e => {
e.preventDefault();
toggle.classList.toggle(`showing-locale`);
toggleLocale(locale);
});
toggles[locale] = toggle;
});
});
bar.prependTo(topLevel);
return toggles;
}
/**
* This function allows either blind toggling
* of a field's visibility, or explicitly
* making visible/invisible based on the
* value of `state` (a boolean).
*/
function toggleLocale(locale, state) {
var action = `toggle`;
if (state !== undefined) {
action = state ? `remove` : `add`;
}
localisedElements[locale].forEach(element => {
element.classList[action](`l10n-hidden`);
});
}
var default_locale = `en`;
var localisedElements = {};
var columnCSS = [`field-col`];
for (var i = 1; i <= 12; i++) {
columnCSS.push(`col${i}`);
}
// Build the sets that track which fields
// belong to which language code.
buildSets();
var locales = Object.keys(localisedElements).sort();
// If there are no locale sets, then there is
// no locale field picker to build, either.
if (locales.length === 0) return;
// If there are locale sets, make sure to
// enable at least the default locale after
// building and hiding all locale sets.
var localeToggler = buildLocaleToggler();
localeToggler[default_locale].click();
///////////////
});
///////////////

Просмотреть файл

@ -1,48 +0,0 @@
from django.conf import settings
from django.utils.html import format_html_join
from django.utils.translation import gettext
from wagtail.core import hooks
@hooks.register('insert_global_admin_js')
def language_toggles():
"""
On any admin page, try to load the l10n code that aggregates
fieldsets per locale, then gives it a button that you can
click to show/hide all those fields.
"""
js_files = ['js/language_toggles.js']
lang_codes = []
for lang in settings.LANGUAGES:
lang_codes.append("'%s'" % lang[0])
js_languages = """
<script>
wagtailModelTranslations = {{
languages: [{languages}],
defaultLanguage: '{language_code}',
viewEditString: '{view_edit_string}',
}};
</script>
""".format(
languages=", ".join(lang_codes),
language_code=settings.LANGUAGE_CODE,
view_edit_string=gettext('View / edit fields for')
)
js_includes = format_html_join(
'\n', '<script src="{0}{1}"></script>',
((settings.STATIC_URL, filename) for filename in js_files)
)
css_files = ['css/language_toggles.css']
css_includes = format_html_join(
'\n', '<link rel="stylesheet" href="{0}{1}">',
((settings.STATIC_URL, filename) for filename in css_files)
)
return js_languages + js_includes + css_includes