forms.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. $(document).ready(function() {
  2. // "Toggle all" checkbox (table header)
  3. $('#toggle_all').click(function() {
  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. // Enable hidden buttons when "select all" is checked
  12. $('#select_all').click(function() {
  13. if ($(this).is(':checked')) {
  14. $('#select_all_box').find('button').prop('disabled', '');
  15. } else {
  16. $('#select_all_box').find('button').prop('disabled', 'disabled');
  17. }
  18. });
  19. // Uncheck the "toggle all" checkbox if an item is unchecked
  20. $('input:checkbox[name=pk]').click(function (event) {
  21. if (!$(this).attr('checked')) {
  22. $('#select_all, #toggle_all').prop('checked', false);
  23. }
  24. });
  25. // Simple "Toggle all" button (panel)
  26. $('button.toggle').click(function() {
  27. var selected = $(this).attr('selected');
  28. $(this).closest('form').find('input:checkbox[name=pk]').prop('checked', !selected);
  29. $(this).attr('selected', !selected);
  30. $(this).children('span').toggleClass('glyphicon-unchecked glyphicon-check');
  31. return false;
  32. });
  33. // Slugify
  34. function slugify(s, num_chars) {
  35. s = s.replace(/[^\-\.\w\s]/g, ''); // Remove unneeded chars
  36. s = s.replace(/^[\s\.]+|[\s\.]+$/g, ''); // Trim leading/trailing spaces
  37. s = s.replace(/[\-\.\s]+/g, '-'); // Convert spaces and decimals to hyphens
  38. s = s.toLowerCase(); // Convert to lowercase
  39. return s.substring(0, num_chars); // Trim to first num_chars chars
  40. }
  41. var slug_field = $('#id_slug');
  42. slug_field.change(function() {
  43. $(this).attr('_changed', true);
  44. });
  45. if (slug_field) {
  46. var slug_source = $('#id_' + slug_field.attr('slug-source'));
  47. slug_source.on('keyup change', function() {
  48. if (slug_field && !slug_field.attr('_changed')) {
  49. slug_field.val(slugify($(this).val(), 50));
  50. }
  51. })
  52. }
  53. // Bulk edit nullification
  54. $('input:checkbox[name=_nullify]').click(function() {
  55. $('#id_' + this.value).toggle('disabled');
  56. });
  57. // Set formaction and submit using a link
  58. $('a.formaction').click(function(event) {
  59. event.preventDefault();
  60. var form = $(this).closest('form');
  61. form.attr('action', $(this).attr('href'));
  62. form.submit();
  63. });
  64. // API select widget
  65. $('select[filter-for]').change(function() {
  66. // Resolve child field by ID specified in parent
  67. var child_name = $(this).attr('filter-for');
  68. var child_field = $('#id_' + child_name);
  69. var child_selected = child_field.val();
  70. // Wipe out any existing options within the child field and create a default option
  71. child_field.empty();
  72. child_field.append($("<option></option>").attr("value", "").text("---------"));
  73. if ($(this).val() || $(this).attr('nullable') == 'true') {
  74. var api_url = child_field.attr('api-url');
  75. var disabled_indicator = child_field.attr('disabled-indicator');
  76. var initial_value = child_field.attr('initial');
  77. var display_field = child_field.attr('display-field') || 'name';
  78. // Determine the filter fields needed to make an API call
  79. var filter_regex = /\{\{([a-z_]+)\}\}/g;
  80. var match;
  81. while (match = filter_regex.exec(api_url)) {
  82. var filter_field = $('#id_' + match[1]);
  83. if (filter_field.val()) {
  84. api_url = api_url.replace(match[0], filter_field.val());
  85. } else if ($(this).attr('nullable') == 'true') {
  86. api_url = api_url.replace(match[0], '0');
  87. }
  88. }
  89. // If all URL variables have been replaced, make the API call
  90. if (api_url.search('{{') < 0) {
  91. console.log(child_name + ": Fetching " + api_url);
  92. $.ajax({
  93. url: api_url,
  94. dataType: 'json',
  95. success: function(response, status) {
  96. $.each(response.results, function(index, choice) {
  97. var option = $("<option></option>").attr("value", choice.id).text(choice[display_field]);
  98. if (disabled_indicator && choice[disabled_indicator] && choice.id != initial_value) {
  99. option.attr("disabled", "disabled");
  100. } else if (choice.id == child_selected) {
  101. option.attr("selected", "selected");
  102. }
  103. child_field.append(option);
  104. });
  105. }
  106. });
  107. }
  108. }
  109. // Trigger change event in case the child field is the parent of another field
  110. child_field.change();
  111. });
  112. });