ExpandableTable.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE ExpandableTable.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import $ from 'jquery'
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'ExpandableTable'
  13. const DATA_KEY = 'lte.expandableTable'
  14. const EVENT_KEY = `.${DATA_KEY}`
  15. const JQUERY_NO_CONFLICT = $.fn[NAME]
  16. const EVENT_EXPANDED = `expanded${EVENT_KEY}`
  17. const EVENT_COLLAPSED = `collapsed${EVENT_KEY}`
  18. const SELECTOR_TABLE = '.expandable-table'
  19. const SELECTOR_EXPANDABLE_BODY = '.expandable-body'
  20. const SELECTOR_DATA_TOGGLE = '[data-widget="expandable-table"]'
  21. const SELECTOR_ARIA_ATTR = 'aria-expanded'
  22. /**
  23. * Class Definition
  24. * ====================================================
  25. */
  26. class ExpandableTable {
  27. constructor(element) {
  28. this._element = element
  29. }
  30. // Public
  31. _init() {
  32. $(SELECTOR_DATA_TOGGLE).each((_, $header) => {
  33. const $type = $($header).attr(SELECTOR_ARIA_ATTR)
  34. const $body = $($header).next(SELECTOR_EXPANDABLE_BODY).children().first().children()
  35. if ($type === 'true') {
  36. $body.show()
  37. } else if ($type === 'false') {
  38. $body.hide()
  39. $body.parent().parent().addClass('d-none')
  40. }
  41. })
  42. }
  43. toggleRow() {
  44. let $element = this._element
  45. if ($element[0].nodeName !== 'TR') {
  46. $element = $element.parent()
  47. if ($element[0].nodeName !== 'TR') {
  48. $element = $element.parent()
  49. }
  50. }
  51. const time = 500
  52. const $type = $element.attr(SELECTOR_ARIA_ATTR)
  53. const $body = $element.next(SELECTOR_EXPANDABLE_BODY).children().first().children()
  54. $body.stop()
  55. if ($type === 'true') {
  56. $body.slideUp(time, () => {
  57. $element.next(SELECTOR_EXPANDABLE_BODY).addClass('d-none')
  58. })
  59. $element.attr(SELECTOR_ARIA_ATTR, 'false')
  60. $element.trigger($.Event(EVENT_COLLAPSED))
  61. } else if ($type === 'false') {
  62. $element.next(SELECTOR_EXPANDABLE_BODY).removeClass('d-none')
  63. $body.slideDown(time)
  64. $element.attr(SELECTOR_ARIA_ATTR, 'true')
  65. $element.trigger($.Event(EVENT_EXPANDED))
  66. }
  67. }
  68. // Static
  69. static _jQueryInterface(config) {
  70. return this.each(function () {
  71. let data = $(this).data(DATA_KEY)
  72. if (!data) {
  73. data = new ExpandableTable($(this))
  74. $(this).data(DATA_KEY, data)
  75. }
  76. if (typeof config === 'string' && /init|toggleRow/.test(config)) {
  77. data[config]()
  78. }
  79. })
  80. }
  81. }
  82. /**
  83. * Data API
  84. * ====================================================
  85. */
  86. $(SELECTOR_TABLE).ready(function () {
  87. ExpandableTable._jQueryInterface.call($(this), '_init')
  88. })
  89. $(document).on('click', SELECTOR_DATA_TOGGLE, function () {
  90. ExpandableTable._jQueryInterface.call($(this), 'toggleRow')
  91. })
  92. /**
  93. * jQuery API
  94. * ====================================================
  95. */
  96. $.fn[NAME] = ExpandableTable._jQueryInterface
  97. $.fn[NAME].Constructor = ExpandableTable
  98. $.fn[NAME].noConflict = function () {
  99. $.fn[NAME] = JQUERY_NO_CONFLICT
  100. return ExpandableTable._jQueryInterface
  101. }
  102. export default ExpandableTable