forms.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. $(document).ready(function() {
  2. // "Toggle all" checkbox (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. // Simple "Toggle all" button (panel)
  18. $('button.toggle').click(function (event) {
  19. var selected = $(this).attr('selected');
  20. $(this).closest('form').find('input:checkbox[name=pk]').prop('checked', !selected);
  21. $(this).attr('selected', !selected);
  22. $(this).children('span').toggleClass('glyphicon-unchecked glyphicon-check');
  23. return false;
  24. });
  25. // Slugify
  26. function slugify(s, num_chars) {
  27. s = s.replace(/[^\-\.\w\s]/g, ''); // Remove unneeded chars
  28. s = s.replace(/^[\s\.]+|[\s\.]+$/g, ''); // Trim leading/trailing spaces
  29. s = s.replace(/[\-\.\s]+/g, '-'); // Convert spaces and decimals to hyphens
  30. s = s.toLowerCase(); // Convert to lowercase
  31. return s.substring(0, num_chars); // Trim to first num_chars chars
  32. }
  33. var slug_field = $('#id_slug');
  34. slug_field.change(function() {
  35. $(this).attr('_changed', true);
  36. });
  37. if (slug_field) {
  38. var slug_source = $('#id_' + slug_field.attr('slug-source'));
  39. slug_source.on('keyup change', function() {
  40. if (slug_field && !slug_field.attr('_changed')) {
  41. slug_field.val(slugify($(this).val(), 50));
  42. }
  43. })
  44. }
  45. // Bulk edit nullification
  46. $('input:checkbox[name=_nullify]').click(function (event) {
  47. $('#id_' + this.value).toggle('disabled');
  48. });
  49. // API select widget
  50. $('select[filter-for]').change(function () {
  51. // Resolve child field by ID specified in parent
  52. var child_name = $(this).attr('filter-for');
  53. var child_field = $('#id_' + child_name);
  54. // Wipe out any existing options within the child field
  55. child_field.empty();
  56. child_field.append($("<option></option>").attr("value", "").text(""));
  57. if ($(this).val()) {
  58. var api_url = child_field.attr('api-url');
  59. var disabled_indicator = child_field.attr('disabled-indicator');
  60. var initial_value = child_field.attr('initial');
  61. var display_field = child_field.attr('display-field') || 'name';
  62. // Gather the values of all other filter fields for this child
  63. $("select[filter-for='" + child_name + "']").each(function() {
  64. var filter_field = $(this);
  65. if (filter_field.val()) {
  66. api_url = api_url.replace('{{' + filter_field.attr('name') + '}}', filter_field.val());
  67. } else {
  68. // Not all filters have been selected yet
  69. return false;
  70. }
  71. });
  72. // If all URL variables have been replaced, make the API call
  73. if (api_url.search('{{') < 0) {
  74. $.ajax({
  75. url: api_url,
  76. dataType: 'json',
  77. success: function (response, status) {
  78. $.each(response, function (index, choice) {
  79. var option = $("<option></option>").attr("value", choice.id).text(choice[display_field]);
  80. if (disabled_indicator && choice[disabled_indicator] && choice.id != initial_value) {
  81. option.attr("disabled", "disabled")
  82. }
  83. child_field.append(option);
  84. });
  85. }
  86. });
  87. }
  88. }
  89. // Trigger change event in case the child field is the parent of another field
  90. child_field.change();
  91. });
  92. });