diff options
author | Philip Sargent <philip.sargent@klebos.com> | 2020-06-18 21:51:19 +0100 |
---|---|---|
committer | Philip Sargent <philip.sargent@klebos.com> | 2020-06-18 21:51:19 +0100 |
commit | ee1d4bb600dd05d2850f13acd1859cd39162512b (patch) | |
tree | f3cc1135fd967e4f10bbca77affb83030a191e94 /media/admin/js/prepopulate.js | |
parent | d9c6986a892c47233a95081af94a1d2ea25a64ce (diff) | |
download | troggle-ee1d4bb600dd05d2850f13acd1859cd39162512b.tar.gz troggle-ee1d4bb600dd05d2850f13acd1859cd39162512b.tar.bz2 troggle-ee1d4bb600dd05d2850f13acd1859cd39162512b.zip |
static files icons into troggle repo
Diffstat (limited to 'media/admin/js/prepopulate.js')
-rw-r--r-- | media/admin/js/prepopulate.js | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/media/admin/js/prepopulate.js b/media/admin/js/prepopulate.js new file mode 100644 index 0000000..db7903a --- /dev/null +++ b/media/admin/js/prepopulate.js @@ -0,0 +1,39 @@ +(function($) { + $.fn.prepopulate = function(dependencies, maxLength) { + /* + Depends on urlify.js + Populates a selected field with the values of the dependent fields, + URLifies and shortens the string. + dependencies - array of dependent fields ids + maxLength - maximum length of the URLify'd string + */ + return this.each(function() { + var prepopulatedField = $(this); + + var populate = function () { + // Bail if the field's value has been changed by the user + if (prepopulatedField.data('_changed')) { + return; + } + + var values = []; + $.each(dependencies, function(i, field) { + field = $(field); + if (field.val().length > 0) { + values.push(field.val()); + } + }); + prepopulatedField.val(URLify(values.join(' '), maxLength)); + }; + + prepopulatedField.data('_changed', false); + prepopulatedField.change(function() { + prepopulatedField.data('_changed', true); + }); + + if (!prepopulatedField.val()) { + $(dependencies.join(',')).keyup(populate).change(populate).focus(populate); + } + }); + }; +})(django.jQuery); |