forms.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. $(document).ready(function() {
  2. // "Toggle all" checkbox in a table header
  3. $('#toggle_all').click(function (event) {
  4. $('td input:checkbox[name=pk]').prop('checked', $(this).prop('checked'));
  5. if ($(this).is(':checked')) {
  6. $('#select_all_box').removeClass('hidden');
  7. } else {
  8. $('#select_all').prop('checked', false);
  9. }
  10. });
  11. // Uncheck the "toggle all" checkbox if an item is unchecked
  12. $('input:checkbox[name=pk]').click(function (event) {
  13. if (!$(this).attr('checked')) {
  14. $('#select_all, #toggle_all').prop('checked', false);
  15. }
  16. });
  17. // Slugify
  18. function slugify(s, num_chars) {
  19. s = s.replace(/[^\-\.\w\s]/g, ''); // Remove unneeded chars
  20. s = s.replace(/^[\s\.]+|[\s\.]+$/g, ''); // Trim leading/trailing spaces
  21. s = s.replace(/[\-\.\s]+/g, '-'); // Convert spaces and decimals to hyphens
  22. s = s.toLowerCase(); // Convert to lowercase
  23. return s.substring(0, num_chars); // Trim to first num_chars chars
  24. }
  25. var slug_field = $('#id_slug');
  26. slug_field.change(function() {
  27. $(this).attr('_changed', true);
  28. });
  29. if (slug_field) {
  30. var slug_source = $('#id_' + slug_field.attr('slug-source'));
  31. slug_source.on('keyup change', function() {
  32. if (slug_field && !slug_field.attr('_changed')) {
  33. slug_field.val(slugify($(this).val(), 50));
  34. }
  35. })
  36. }
  37. // API select widget
  38. $('select[filter-for]').change(function () {
  39. // Resolve child field by ID specified in parent
  40. var child_name = $(this).attr('filter-for');
  41. var child_field = $('#id_' + child_name);
  42. // Wipe out any existing options within the child field
  43. child_field.empty();
  44. child_field.append($("<option></option>").attr("value", "").text(""));
  45. if ($(this).val()) {
  46. var api_url = child_field.attr('api-url');
  47. var disabled_indicator = child_field.attr('disabled-indicator');
  48. var initial_value = child_field.attr('initial');
  49. var display_field = child_field.attr('display-field') || 'name';
  50. // Gather the values of all other filter fields for this child
  51. $("select[filter-for='" + child_name + "']").each(function() {
  52. var filter_field = $(this);
  53. if (filter_field.val()) {
  54. api_url = api_url.replace('{{' + filter_field.attr('name') + '}}', filter_field.val());
  55. } else {
  56. // Not all filters have been selected yet
  57. return false;
  58. }
  59. });
  60. // If all URL variables have been replaced, make the API call
  61. if (api_url.search('{{') < 0) {
  62. $.ajax({
  63. url: api_url,
  64. dataType: 'json',
  65. success: function (response, status) {
  66. $.each(response, function (index, choice) {
  67. var option = $("<option></option>").attr("value", choice.id).text(choice[display_field]);
  68. if (disabled_indicator && choice[disabled_indicator] && choice.id != initial_value) {
  69. option.attr("disabled", "disabled")
  70. }
  71. child_field.append(option);
  72. });
  73. }
  74. });
  75. }
  76. }
  77. // Trigger change event in case the child field is the parent of another field
  78. child_field.change();
  79. });
  80. });