From 0c926397cd40f5a424f5e409742d5c472506bbd8 Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Sat, 7 Sep 2019 23:46:39 +0530 Subject: added static --- website/static/admin/js/prepopulate.js | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 website/static/admin/js/prepopulate.js (limited to 'website/static/admin/js/prepopulate.js') 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); -- cgit