PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause
  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. field.data('_changed', false);
  13. field.change(function() {
  14. field.data('_changed', true);
  15. });
  16. var populate = function () {
  17. // Bail if the fields value has changed
  18. if (field.data('_changed') == true) return;
  19. var values = [];
  20. $.each(dependencies, function(i, field) {
  21. if ($(field).val().length > 0) {
  22. values.push($(field).val());
  23. }
  24. })
  25. field.val(URLify(values.join(' '), maxLength));
  26. };
  27. $(dependencies.join(',')).keyup(populate).change(populate).focus(populate);
  28. });
  29. };
  30. })(django.jQuery);