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.

ExpandableTable.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, options) {
  28. this._options = options
  29. this._element = element
  30. }
  31. // Public
  32. static _jQueryInterface(operation) {
  33. return this.each(function () {
  34. let data = $(this).data(DATA_KEY)
  35. if (!data) {
  36. data = new ExpandableTable($(this))
  37. $(this).data(DATA_KEY, data)
  38. }
  39. if (typeof operation === 'string' && /init|toggleRow/.test(operation)) {
  40. data[operation]()
  41. }
  42. })
  43. }
  44. init() {
  45. $(SELECTOR_DATA_TOGGLE).each((_, $header) => {
  46. const $type = $($header).attr(SELECTOR_ARIA_ATTR)
  47. const $body = $($header).next(SELECTOR_EXPANDABLE_BODY).children().first().children()
  48. if ($type === 'true') {
  49. $body.show()
  50. } else if ($type === 'false') {
  51. $body.hide()
  52. $body.parent().parent().addClass('d-none')
  53. }
  54. })
  55. }
  56. // Static
  57. toggleRow() {
  58. const $element = this._element
  59. const time = 500
  60. const $type = $element.attr(SELECTOR_ARIA_ATTR)
  61. const $body = $element.next(SELECTOR_EXPANDABLE_BODY).children().first().children()
  62. $body.stop()
  63. if ($type === 'true') {
  64. $body.slideUp(time, () => {
  65. $element.next(SELECTOR_EXPANDABLE_BODY).addClass('d-none')
  66. })
  67. $element.attr(SELECTOR_ARIA_ATTR, 'false')
  68. $element.trigger($.Event(EVENT_COLLAPSED))
  69. } else if ($type === 'false') {
  70. $element.next(SELECTOR_EXPANDABLE_BODY).removeClass('d-none')
  71. $body.slideDown(time)
  72. $element.attr(SELECTOR_ARIA_ATTR, 'true')
  73. $element.trigger($.Event(EVENT_EXPANDED))
  74. }
  75. }
  76. }
  77. /**
  78. * Data API
  79. * ====================================================
  80. */
  81. $(SELECTOR_TABLE).ready(function () {
  82. ExpandableTable._jQueryInterface.call($(this), 'init')
  83. })
  84. $(document).on('click', SELECTOR_DATA_TOGGLE, function () {
  85. ExpandableTable._jQueryInterface.call($(this), 'toggleRow')
  86. })
  87. /**
  88. * jQuery API
  89. * ====================================================
  90. */
  91. $.fn[NAME] = ExpandableTable._jQueryInterface
  92. $.fn[NAME].Constructor = ExpandableTable
  93. $.fn[NAME].noConflict = function () {
  94. $.fn[NAME] = JQUERY_NO_CONFLICT
  95. return ExpandableTable._jQueryInterface
  96. }
  97. export default ExpandableTable