You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

59 lines
1.6 KiB

  1. import $ from "jquery";
  2. class IHKCheckboxGroup {
  3. constructor(wrapper) {
  4. this.wrapper = wrapper.addClass('initiated');
  5. this.label = wrapper.find('label.acc');
  6. this.toggle = this.label.find('a');
  7. this.checkbox = this.label.prev('input[type="checkbox"]');
  8. this.subs = wrapper.find('.checkbox-wrapper');
  9. this.initToggle();
  10. this.initChangeListener();
  11. }
  12. initChangeListener() {
  13. this.checkbox.on('change', (e) => {
  14. const isChecked = $(e.currentTarget).removeClass('half-checked').prop('checked');
  15. this.subs.find('input[type="checkbox"]').prop('checked', isChecked);
  16. })
  17. this.subs.find('input[type="checkbox"]').on('change', () => {
  18. this.checkSelection();
  19. })
  20. this.checkSelection();
  21. }
  22. initToggle() {
  23. this.toggle.on('click', (e) => {
  24. e.preventDefault();
  25. e.stopPropagation();
  26. this.label.toggleClass('open');
  27. this.subs.stop().slideToggle(300, 'swing');
  28. })
  29. }
  30. checkSelection() {
  31. const checked = this.subs.find('input[type="checkbox"]:checked').length;
  32. const unchecked = this.subs.find('input[type="checkbox"]:not(:checked)').length;
  33. if (checked === 0) {
  34. this.checkbox.prop('checked', false).removeClass('half-checked');
  35. }
  36. else if (unchecked === 0) {
  37. this.checkbox.prop('checked', true).removeClass('half-checked');
  38. }
  39. else {
  40. this.checkbox.prop('checked', false).addClass('half-checked');
  41. }
  42. }
  43. }
  44. export default IHKCheckboxGroup;
  45. $('body').on('ihk-init dynamic-component-loaded gfi-dynamic-init', function () {
  46. $('.checkbox-group:not(.initiated)').each(() => {
  47. new IHKCheckboxGroup($(this));
  48. });
  49. });