common_config_form.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. Drupal.HierarchicalSelectConfigForm = {};
  2. (function ($, cfg) {
  3. cfg.context = function(configId) {
  4. if (configId === undefined) {
  5. return $('.hierarchical-select-config-form > *').not('.live-preview');
  6. }
  7. else {
  8. return $('#hierarchical-select-config-form-'+ configId + ' > *').not('.live-preview');
  9. }
  10. };
  11. cfg.levelLabels = function(configId) {
  12. var $status = $('.level-labels-status', cfg.context(configId));
  13. var $enforceDeepest = $('.enforce-deepest input', cfg.context(configId));
  14. var showHide = function(speed) {
  15. $affected = $('.level-labels-settings', cfg.context(configId));
  16. if (!$status.is(':checked')) {
  17. $affected.hide(speed);
  18. }
  19. else {
  20. // For showing/hiding rows, I'm relying on setting the style
  21. // "display: none" and removing it again. jQuery's show()/hide() leave
  22. // "display: block" behind and are thereby messing up the table layout.
  23. if ($enforceDeepest.slice(1, 2).is(':checked')) {
  24. $affected.find('tr').removeAttr('style');
  25. }
  26. else {
  27. // We need to take special measures if sticky headers are enabled, so
  28. // handle the show/hide separately when it's enabled.
  29. if ($affected.find('table.sticky-header').length == 0) {
  30. $affected.find('tr').slice(0, 2).removeAttr('style'); // Show header tr and root level tr.
  31. $affected.find('tr').slice(2).attr('style', 'display: none'); // Hide all other tr's.
  32. }
  33. else {
  34. $affected.find('table').show(speed); // Show both tables (the one with the sticky headers and the one with the actual content).
  35. $affected.find('table').slice(1).find('tr').slice(2).attr('style', 'display: none'); // Show all tr's after the header tr and root level tr of the 2nd table (the one with the actual content).
  36. }
  37. }
  38. // If $status was unchecked previously, the entire div would have been
  39. // hidden!
  40. if ($affected.css('display') == 'none') {
  41. $affected.show(speed);
  42. }
  43. }
  44. };
  45. $status.click(function() { showHide(200); });
  46. $enforceDeepest.click(function() { showHide(200); });
  47. showHide(0);
  48. };
  49. cfg.dropbox = function(configId) {
  50. var $status = $('.dropbox-status', cfg.context(configId));
  51. var showHide = function(speed) {
  52. var $affected = $('.dropbox-title, .dropbox-limit, .dropbox-reset-hs', cfg.context(configId)).parent();
  53. if ($status.is(':checked')) {
  54. $affected.show(speed);
  55. }
  56. else {
  57. $affected.hide(speed);
  58. }
  59. };
  60. $status.click(function() { showHide(200); });
  61. showHide(0);
  62. };
  63. cfg.editability = function(configId) {
  64. var $status = $('.editability-status', cfg.context(configId));
  65. var $allowNewLevels = $('.editability-allow-new-levels', cfg.context(configId));
  66. var showHide = function(speed) {
  67. var $affected = $('.editability-per-level-settings, .form-item:has(.editability-allow-new-levels)', cfg.context(configId));
  68. var $maxLevels = $('.form-item:has(.editability-max-levels)', cfg.context(configId));
  69. if ($status.is(':checked')) {
  70. if ($allowNewLevels.is(':checked')) {
  71. $affected.add($maxLevels).show(speed);
  72. }
  73. else {
  74. $affected.show(speed);
  75. }
  76. }
  77. else {
  78. $affected.add($maxLevels).hide(speed);
  79. }
  80. };
  81. var showHideMaxLevels = function(speed) {
  82. $affected = $('.editability-max-levels', cfg.context(configId)).parent();
  83. if ($allowNewLevels.is(':checked')) {
  84. $affected.show(speed);
  85. }
  86. else {
  87. $affected.hide(speed);
  88. }
  89. };
  90. $status.click(function() { showHide(200); });
  91. $allowNewLevels.click(function() { showHideMaxLevels(200); });
  92. showHideMaxLevels(0);
  93. showHide(0);
  94. };
  95. cfg.entityCount = function(configId) {
  96. var $status = $('.entity-count-enabled', cfg.context(configId));
  97. var showHide = function(speed) {
  98. var $affected = $('.entity-count-settings', cfg.context(configId));
  99. if ($status.is(':checked')) {
  100. $affected.show(speed);
  101. }
  102. else {
  103. $affected.hide(speed);
  104. }
  105. };
  106. $status.click(function() { showHide(200); });
  107. showHide(0);
  108. };
  109. cfg.livePreview = function(configId) {
  110. // React on changes to any input, except the ones in the live preview.
  111. $updateLivePreview = $('input', cfg.context(configId))
  112. .filter(':not(.create-new-item-input):not(.create-new-item-create):not(.create-new-item-cancel)')
  113. .change(function() {
  114. // TODO: Do an AJAX submit of the entire form.
  115. });
  116. };
  117. $(document).ready(function() {
  118. for (var id in Drupal.settings.HierarchicalSelect.configForm) {
  119. var configId = Drupal.settings.HierarchicalSelect.configForm.id;
  120. cfg.levelLabels(configId);
  121. cfg.dropbox(configId);
  122. cfg.editability(configId);
  123. cfg.entityCount(configId);
  124. //cfg.livePreview(configId);
  125. }
  126. });
  127. })(jQuery, Drupal.HierarchicalSelectConfigForm);