PageRenderTime 5ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/admin/media/js/prepopulate.js

https://code.google.com/p/mango-py/
JavaScript | 34 lines | 22 code | 4 blank | 8 comment | 3 complexity | 575825d0f554b0975f9d32a84f99f6a4 MD5 | raw file
 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 - array of dependent fields id's 
 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                $.each(dependencies, function(i, field) {
24                  if ($(field).val().length > 0) {
25                      values.push($(field).val());
26                  }
27                })
28                field.val(URLify(values.join(' '), maxLength));
29            };
30
31            $(dependencies.join(',')).keyup(populate).change(populate).focus(populate);
32        });
33    };
34})(django.jQuery);