diff options
author | prashantsinalkar | 2019-10-02 20:32:05 +0530 |
---|---|---|
committer | prashantsinalkar | 2019-10-02 20:32:05 +0530 |
commit | cb37e15ceb336de7ba94d90805ef1531c47d5c96 (patch) | |
tree | 9cba0c741a7cfe3222e2c17401e72b2acc8dedf7 /website/static/admin/js/prepopulate.js | |
parent | 76dcbadbf71febe93fb9d2ffd51b2f3ae54233a2 (diff) | |
parent | 070cb90095df4ab5bc7b6e28fd13bb12df29e1f7 (diff) | |
download | R_on_Cloud_Web_Interface-cb37e15ceb336de7ba94d90805ef1531c47d5c96.tar.gz R_on_Cloud_Web_Interface-cb37e15ceb336de7ba94d90805ef1531c47d5c96.tar.bz2 R_on_Cloud_Web_Interface-cb37e15ceb336de7ba94d90805ef1531c47d5c96.zip |
fixed conflict
Diffstat (limited to 'website/static/admin/js/prepopulate.js')
-rw-r--r-- | website/static/admin/js/prepopulate.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/website/static/admin/js/prepopulate.js b/website/static/admin/js/prepopulate.js new file mode 100644 index 0000000..bef45a3 --- /dev/null +++ b/website/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); |