List.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. export class List {
  2. constructor( ...headers ) {
  3. this.headers = headers;
  4. this.children = [];
  5. this.domElement = document.createElement( 'div' );
  6. this.domElement.className = 'list-container';
  7. this.domElement.style.padding = '10px';
  8. this.id = `list-${Math.random().toString( 36 ).substr( 2, 9 )}`;
  9. this.domElement.dataset.listId = this.id;
  10. this.gridStyleElement = document.createElement( 'style' );
  11. this.domElement.appendChild( this.gridStyleElement );
  12. const headerRow = document.createElement( 'div' );
  13. headerRow.className = 'list-header';
  14. this.headers.forEach( headerText => {
  15. const headerCell = document.createElement( 'div' );
  16. headerCell.className = 'list-header-cell';
  17. headerCell.textContent = headerText;
  18. headerRow.appendChild( headerCell );
  19. } );
  20. this.domElement.appendChild( headerRow );
  21. }
  22. setGridStyle( gridTemplate ) {
  23. this.gridStyleElement.textContent = `
  24. [data-list-id="${this.id}"] > .list-header,
  25. [data-list-id="${this.id}"] .list-item-row {
  26. grid-template-columns: ${gridTemplate};
  27. }
  28. `;
  29. }
  30. add( item ) {
  31. if ( item.parent !== null ) {
  32. item.parent.remove( item );
  33. }
  34. item.domElement.classList.add( 'header-wrapper', 'section-start' );
  35. item.parent = this;
  36. this.children.push( item );
  37. this.domElement.appendChild( item.domElement );
  38. }
  39. remove( item ) {
  40. const index = this.children.indexOf( item );
  41. if ( index !== - 1 ) {
  42. this.children.splice( index, 1 );
  43. this.domElement.removeChild( item.domElement );
  44. item.parent = null;
  45. }
  46. return this;
  47. }
  48. }
粤ICP备19079148号