diff options
| author | ckonstanski <kostcarl@isu.edu> | 2026-07-30 17:56:41 -0600 |
|---|---|---|
| committer | ckonstanski <kostcarl@isu.edu> | 2026-07-30 17:56:41 -0600 |
| commit | cfb4bf6be1b18ec15cddbec1ea37e76176b1050b (patch) | |
| tree | 1bbe88bca7ea6282296a9cfb16b6e52bd16c6f38 /src/static/admin/js/prepopulate.js | |
| parent | aeab73540c306e5f906bccc444c252d8ad2adc34 (diff) | |
initial commit
Diffstat (limited to 'src/static/admin/js/prepopulate.js')
| -rw-r--r-- | src/static/admin/js/prepopulate.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/static/admin/js/prepopulate.js b/src/static/admin/js/prepopulate.js new file mode 100644 index 0000000..bef45a3 --- /dev/null +++ b/src/static/admin/js/prepopulate.js @@ -0,0 +1,42 @@ +/*global URLify*/ +(function($) { + 'use strict'; + $.fn.prepopulate = function(dependencies, maxLength, allowUnicode) { + /* + 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 + allowUnicode - Unicode support 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, allowUnicode)); + }; + + prepopulatedField.data('_changed', false); + prepopulatedField.on('change', function() { + prepopulatedField.data('_changed', true); + }); + + if (!prepopulatedField.val()) { + $(dependencies.join(',')).on('keyup change focus', populate); + } + }); + }; +})(django.jQuery); |
