term_reference_tree.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. (function($) {
  2. /**
  3. * Attaches the tree behavior to the term widget form.
  4. */
  5. Drupal.behaviors.termReferenceTree = {
  6. attach: function(context, settings) {
  7. // Bind the term expand/contract button to slide toggle the list underneath.
  8. $('.term-reference-tree-button', context).once('term-reference-tree-button').click(function() {
  9. $(this).toggleClass('term-reference-tree-collapsed');
  10. $(this).siblings('ul').slideToggle('fast');
  11. });
  12. // An expand all button (unimplemented)
  13. /*
  14. $('.expandbutton').click(function() {
  15. $(this).siblings('.term-reference-tree-button').trigger('click');
  16. });
  17. */
  18. $('.term-reference-tree', context).once('term-reference-tree').each(function() {
  19. // On page load, check whether the maximum number of choices is already selected.
  20. // If so, disable the other options.
  21. var tree = $(this);
  22. checkMaxChoices(tree, false);
  23. $(this).find('input[type=checkbox]').change(function() {
  24. checkMaxChoices(tree, $(this));
  25. });
  26. //On page load, check if the user wants a cascading selection.
  27. if($(this).hasClass('term-reference-tree-select-parents')) {
  28. $(this).find('.form-checkbox').parent().addClass('select-parents');
  29. }
  30. //On page load, check if the user wants a cascading selection.
  31. if($(this).hasClass('term-reference-tree-cascading-selection')) {
  32. var mode_select = $(this).hasClass('term-reference-tree-cascading-selection-mode-select');
  33. var mode_deselect = $(this).hasClass('term-reference-tree-cascading-selection-mode-deselect');
  34. //Check children when checkboxes are clicked.
  35. $(this).find('.form-checkbox').change(function(event) {
  36. var event_target = $(event.target);
  37. var event_target_checked = event_target.is(':checked');
  38. var control_id = event_target.attr('id');
  39. var children = event_target.parent().next().children().find('> :not(ul) > input[id^="' + control_id + '-children"]');
  40. if (!mode_select && !mode_deselect) {
  41. if(event_target_checked) {
  42. $(children).filter(':not(:checked)').click().trigger('change');
  43. }
  44. else {
  45. $(children).filter(':checked').click().trigger('change');
  46. }
  47. } else if (mode_select && event_target_checked) {
  48. $(children).filter(':not(:checked)').click().trigger('change');
  49. } else if (mode_deselect && !event_target_checked) {
  50. $(children).filter(':checked').click().trigger('change');
  51. }
  52. });
  53. //End process checkbox changes.
  54. } //End Want a cascading checking.
  55. });
  56. // track_list
  57. // just unselect items in tree on track_list button remove click
  58. // let builtin ajax form do the heavy work
  59. $('.term-reference-tree-track-list input[type="submit"][value="remove"]', context).click(function(e){
  60. e.preventDefault();
  61. var key = $(this).attr('term-reference-tree-key');
  62. $('input[type="checkbox"][value="'+key+'"]', $(this).parents('.term-reference-tree'))
  63. .prop('checked', false)
  64. .trigger('change');
  65. return false;
  66. });
  67. }
  68. };
  69. // This helper function checks if the maximum number of choices is already selected.
  70. // If so, it disables all the other options. If not, it enables them.
  71. function checkMaxChoices(item, checkbox) {
  72. var maxChoices = -1;
  73. try {
  74. maxChoices = parseInt(Drupal.settings.term_reference_tree.trees[item.attr('id')]['max_choices']);
  75. }
  76. catch (e){}
  77. var count = item.find(':checked').length;
  78. if(maxChoices > 0 && count >= maxChoices) {
  79. item.find('input[type=checkbox]:not(:checked)').attr('disabled', 'disabled').parent().addClass('disabled');
  80. } else {
  81. item.find('input[type=checkbox]').removeAttr('disabled').parent().removeClass('disabled');
  82. }
  83. if(checkbox) {
  84. if(item.hasClass('term-reference-tree-select-parents')) {
  85. if(checkbox.prop('checked')) {
  86. checkbox.parents('ul.term-reference-tree-level li').children('div.form-item').find('input[type=checkbox]').each(function() {
  87. $(this).prop('checked', true);
  88. });
  89. }
  90. }
  91. }
  92. }
  93. })(jQuery);