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.

This commit is contained in:
Alexandre Silva 2017-03-08 12:45:25 +00:00
parent bb6b266fcf
commit 240933c531

View file

@ -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);
}
});
});
});
}
});