From 240933c531cd41334d4716c8b0549e792b66ae0f Mon Sep 17 00:00:00 2001 From: Alexandre Silva Date: Wed, 8 Mar 2017 12:45:25 +0000 Subject: [PATCH] Auto-population for slug is made using the same behaviour as wagtail (only auto-populated in non-live pages and slug only follows the title field if its value matched the title's value at the time of focus). Fixes #54. --- .../js/wagtail_translated_slugs.js | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/wagtail_modeltranslation/static/wagtail_modeltranslation/js/wagtail_translated_slugs.js b/wagtail_modeltranslation/static/wagtail_modeltranslation/js/wagtail_translated_slugs.js index 5626714..ea14a69 100644 --- a/wagtail_modeltranslation/static/wagtail_modeltranslation/js/wagtail_translated_slugs.js +++ b/wagtail_modeltranslation/static/wagtail_modeltranslation/js/wagtail_translated_slugs.js @@ -1,15 +1,22 @@ -$(document).ready(function() { - $.each(langs, function(idx, lang_code){ - $('#id_title_'+lang_code).on('focus', function() { - $('#id_slug_'+lang_code).data('previous-val', $('#id_slug_'+lang_code).val()); - $(this).data('previous-val', $(this).val()); - }); +$(document).ready(function () { + /* Only non-live pages should auto-populate the slug from the title */ + if (!$('body').hasClass('page-is-live')) { + var slugFollowsTitle = false; - $('#id_title_'+lang_code).on('keyup keydown keypress blur', function() { - if ($('body').hasClass('create') || (!$('#id_slug_'+lang_code).data('previous-val').length || cleanForSlug($('#id_title_'+lang_code).data('previous-val')) === $('#id_slug_'+lang_code).data('previous-val'))) { - // only update slug if the page is being created from scratch, if slug is completely blank, or if title and slug prior to typing were identical - $('#id_slug_'+lang_code).val(cleanForSlug($('#id_title_'+lang_code).val())); - } + $.each(langs, function (idx, lang_code) { + $('#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); + slugFollowsTitle = (currentSlug == slugifiedTitle); + }); + + $('#id_title_' + lang_code).on('keyup keydown keypress blur', function () { + if (slugFollowsTitle) { + var slugifiedTitle = cleanForSlug(this.value); + $('#id_slug_' + lang_code).val(slugifiedTitle); + } + }); }); - }); + } }); \ No newline at end of file