forms.js 3.5 KB

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