Item.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. export class Item {
  2. constructor( ...data ) {
  3. this.children = [];
  4. this.isOpen = true;
  5. this.isCollapsible = false;
  6. this.childrenContainer = null;
  7. this.parent = null;
  8. this.domElement = document.createElement( 'div' );
  9. this.domElement.className = 'list-item-wrapper';
  10. this.itemRow = document.createElement( 'div' );
  11. this.itemRow.className = 'list-item-row';
  12. this.userData = {};
  13. this.data = [];
  14. data.forEach( ( cellData, index ) => {
  15. const cell = document.createElement( 'div' );
  16. cell.className = 'list-item-cell';
  17. this.itemRow.appendChild( cell );
  18. this.setValue( index, cellData );
  19. } );
  20. this.domElement.appendChild( this.itemRow );
  21. // Bindings
  22. this.onItemClick = this.onItemClick.bind( this );
  23. }
  24. onItemClick( e ) {
  25. if ( e.target.closest( 'button, a, input, label' ) ) return;
  26. this.toggle();
  27. }
  28. add( item, index = this.children.length ) {
  29. if ( item.parent !== null ) {
  30. item.parent.remove( item );
  31. }
  32. item.parent = this;
  33. this.children.splice( index, 0, item );
  34. this.itemRow.classList.add( 'collapsible' );
  35. if ( ! this.childrenContainer ) {
  36. this.childrenContainer = document.createElement( 'div' );
  37. this.childrenContainer.className = 'list-children-container';
  38. this.childrenContainer.classList.toggle( 'closed', ! this.isOpen );
  39. this.domElement.appendChild( this.childrenContainer );
  40. this.itemRow.addEventListener( 'click', this.onItemClick );
  41. }
  42. this.childrenContainer.insertBefore(
  43. item.domElement,
  44. this.childrenContainer.children[ index ] || null
  45. );
  46. this.updateToggler();
  47. return this;
  48. }
  49. remove( item ) {
  50. const index = this.children.indexOf( item );
  51. if ( index !== - 1 ) {
  52. this.children.splice( index, 1 );
  53. this.childrenContainer.removeChild( item.domElement );
  54. item.parent = null;
  55. if ( this.children.length === 0 ) {
  56. this.itemRow.classList.remove( 'collapsible' );
  57. this.itemRow.removeEventListener( 'click', this.onItemClick );
  58. this.childrenContainer.remove();
  59. this.childrenContainer = null;
  60. }
  61. this.updateToggler();
  62. }
  63. return this;
  64. }
  65. updateToggler() {
  66. const firstCell = this.itemRow.querySelector( '.list-item-cell:first-child' );
  67. let toggler = this.itemRow.querySelector( '.item-toggler' );
  68. if ( this.children.length > 0 || this.isCollapsible ) {
  69. if ( ! toggler ) {
  70. toggler = document.createElement( 'span' );
  71. toggler.className = 'item-toggler';
  72. firstCell.prepend( toggler );
  73. }
  74. if ( this.isOpen ) {
  75. this.itemRow.classList.add( 'open' );
  76. }
  77. } else if ( toggler ) {
  78. toggler.remove();
  79. }
  80. }
  81. setCollapsible( collapsible ) {
  82. this.isCollapsible = collapsible;
  83. if ( collapsible ) {
  84. this.itemRow.classList.add( 'collapsible' );
  85. if ( ! this.childrenContainer ) {
  86. this.childrenContainer = document.createElement( 'div' );
  87. this.childrenContainer.className = 'list-children-container';
  88. this.childrenContainer.classList.toggle( 'closed', ! this.isOpen );
  89. this.domElement.appendChild( this.childrenContainer );
  90. this.itemRow.addEventListener( 'click', this.onItemClick );
  91. }
  92. } else {
  93. this.itemRow.classList.remove( 'collapsible' );
  94. }
  95. this.updateToggler();
  96. return this;
  97. }
  98. toggle() {
  99. this.isOpen = ! this.isOpen;
  100. this.itemRow.classList.toggle( 'open', this.isOpen );
  101. if ( this.childrenContainer ) {
  102. this.childrenContainer.classList.toggle( 'closed', ! this.isOpen );
  103. }
  104. return this;
  105. }
  106. close() {
  107. if ( this.isOpen ) {
  108. this.toggle();
  109. }
  110. return this;
  111. }
  112. show() {
  113. this.domElement.style.display = '';
  114. return this;
  115. }
  116. hide() {
  117. this.domElement.style.display = 'none';
  118. return this;
  119. }
  120. setValue( index, value ) {
  121. this.data[ index ] = value;
  122. const cell = this.itemRow.children[ index ];
  123. if ( cell ) {
  124. const toggler = cell.querySelector( '.item-toggler' );
  125. cell.innerHTML = '';
  126. if ( toggler ) {
  127. cell.appendChild( toggler );
  128. }
  129. if ( value instanceof HTMLElement ) {
  130. cell.appendChild( value );
  131. } else {
  132. cell.append( String( value ) );
  133. }
  134. }
  135. return this;
  136. }
  137. getValue( index ) {
  138. return this.data[ index ];
  139. }
  140. }
粤ICP备19079148号