diff options
| author | root <root@lalalizard.com> | 2012-11-26 12:06:35 -0500 |
|---|---|---|
| committer | root <root@lalalizard.com> | 2012-11-26 12:06:35 -0500 |
| commit | e0bc5e67f0be025aa54fbac3d25065b4b8c00f40 (patch) | |
| tree | b6da444ba40190fa2a8aece52daedad82c65aab3 /www/static/admin/js/prepopulate.js | |
| parent | ef51b60c6481254d88c5fc3c34f4127b7f881a58 (diff) | |
www/ folder
Diffstat (limited to 'www/static/admin/js/prepopulate.js')
| -rwxr-xr-x | www/static/admin/js/prepopulate.js | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/www/static/admin/js/prepopulate.js b/www/static/admin/js/prepopulate.js new file mode 100755 index 0000000..24f24f9 --- /dev/null +++ b/www/static/admin/js/prepopulate.js @@ -0,0 +1,34 @@ +(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 id's + maxLength - maximum length of the URLify'd string + */ + return this.each(function() { + var field = $(this); + + field.data('_changed', false); + field.change(function() { + field.data('_changed', true); + }); + + var populate = function () { + // Bail if the fields value has changed + if (field.data('_changed') == true) return; + + var values = []; + $.each(dependencies, function(i, field) { + if ($(field).val().length > 0) { + values.push($(field).val()); + } + }) + field.val(URLify(values.join(' '), maxLength)); + }; + + $(dependencies.join(',')).keyup(populate).change(populate).focus(populate); + }); + }; +})(django.jQuery); |
