ActionSet.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. export default class ActionSet {
  2. arrowDown = '<i class="fa fa-arrow-down" aria-hidden="true"></i>';
  3. constructor(actions, element = 'button', value = null) {
  4. this.actions = actions;
  5. this.element = element;
  6. this.value = value;
  7. }
  8. render() {
  9. if (this.element == 'button') {
  10. return this.renderButton();
  11. }
  12. if (this.element == 'span') {
  13. return this.renderSpan();
  14. }
  15. }
  16. renderButton() {
  17. this.actionButton = document.createElement('a');
  18. this.actionButton.classList.add('btn', 'btn-outline-secondary');
  19. this.actionButton.innerHTML = `<i class="fa fa-${this.actions[0].icon}" aria-hidden="true"></i> ${this.actions[0].label}`;
  20. this.actionButton.href = this.actions[0].route.replace('{{id}}', this.value);
  21. if (this.actions.length == 1) {
  22. return { 'button': this.actionButton, 'toggle': null, 'list': null };
  23. }
  24. this.actionToggle = document.createElement('button');
  25. this.actionToggle.classList.add('btn', 'btn-outline-secondary', 'dropdown-toggle-split');
  26. this.actionToggle.innerHTML = this.arrowDown;
  27. this.actionToggle.setAttribute('data-bs-toggle', 'dropdown');
  28. this.actionList = document.createElement('ul');
  29. this.actionList.classList.add('dropdown-menu');
  30. this.actions.forEach((action) => {
  31. const actionHolder = document.createElement('li');
  32. const actionLink = document.createElement('a');
  33. actionLink.classList.add('dropdown-item');
  34. actionLink.href = action.route.replace('{{id}}', this.value);
  35. actionLink.innerHTML = `<i class="fa fa-${action.icon}" aria-hidden="true"></i> ${action.label}`;
  36. actionHolder.appendChild(actionLink);
  37. this.actionList.appendChild(actionHolder);
  38. });
  39. return {
  40. 'button': this.actionButton,
  41. 'toggle': this.actionToggle,
  42. 'list': this.actionList
  43. };
  44. }
  45. renderSpan() {
  46. this.actionButton = document.createElement('div');
  47. this.actionToggle = document.createElement('span');
  48. this.actionToggle.setAttribute('data-bs-toggle', 'dropdown');
  49. this.actionToggle.innerHTML = `#${this.value} ${this.arrowDown}`;
  50. this.actionList = document.createElement('ul');
  51. this.actionList.classList.add('dropdown-menu');
  52. this.actions.forEach((action) => {
  53. const actionHolder = document.createElement('li');
  54. if(action.type == 'divider'){
  55. const actionDivider = document.createElement('hr');
  56. actionDivider.classList.add('dropdown-divider');
  57. this.actionList.appendChild(actionDivider);
  58. return;
  59. }
  60. const actionLink = document.createElement('a');
  61. actionLink.classList.add('dropdown-item');
  62. actionLink.href = action.route.replace('{{id}}', this.value);
  63. actionLink.innerHTML = `<i class="fa fa-${action.icon}" aria-hidden="true"></i> ${action.label}`;
  64. actionHolder.appendChild(actionLink);
  65. this.actionList.appendChild(actionHolder);
  66. });
  67. this.actionButton.appendChild(this.actionToggle);
  68. this.actionButton.appendChild(this.actionList)
  69. return this.actionButton;
  70. }
  71. renderLabel(label, icon){
  72. if(icon){
  73. return `<i class="fa fa-${icon}" aria-hidden="true"></i> ${label}`
  74. }
  75. return label
  76. }
  77. }