web/static/admin/js/prepopulate.js
changeset 0 ecdfc63274bf
equal deleted inserted replaced
-1:000000000000 0:ecdfc63274bf
       
     1 (function($) {
       
     2     $.fn.prepopulate = function(dependencies, maxLength) {
       
     3         /*
       
     4             Depends on urlify.js
       
     5             Populates a selected field with the values of the dependent fields,
       
     6             URLifies and shortens the string. 
       
     7             dependencies - selected jQuery object of dependent fields
       
     8             maxLength - maximum length of the URLify'd string 
       
     9         */
       
    10         return this.each(function() {
       
    11             var field = $(this);
       
    12 
       
    13             field.data('_changed', false);
       
    14             field.change(function() {
       
    15                 field.data('_changed', true);
       
    16             });
       
    17 
       
    18             var populate = function () {
       
    19                 // Bail if the fields value has changed
       
    20                 if (field.data('_changed') == true) return;
       
    21  
       
    22                 var values = [];
       
    23                 dependencies.each(function() {
       
    24                     if ($(this).val().length > 0) {
       
    25                         values.push($(this).val());
       
    26                     }
       
    27                 });
       
    28                 field.val(URLify(values.join(' '), maxLength));
       
    29             };
       
    30 
       
    31             dependencies.keyup(populate).change(populate).focus(populate);
       
    32         });
       
    33     };
       
    34 })(django.jQuery);