2020-06-04 22:35:50 +00:00
|
|
|
/**
|
|
|
|
* --------------------------------------------
|
|
|
|
* AdminLTE ExpandableTable.js
|
|
|
|
* License MIT
|
|
|
|
* --------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
import $ from 'jquery'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
const NAME = 'ExpandableTable'
|
|
|
|
const DATA_KEY = 'lte.expandableTable'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
|
|
|
|
2020-06-11 12:34:44 +00:00
|
|
|
const EVENT_EXPANDED = `expanded${EVENT_KEY}`
|
|
|
|
const EVENT_COLLAPSED = `collapsed${EVENT_KEY}`
|
2020-06-04 22:35:50 +00:00
|
|
|
|
2020-06-11 12:34:44 +00:00
|
|
|
const CLASS_NAME_HEADER = 'expandable-header'
|
2020-06-04 22:35:50 +00:00
|
|
|
|
2020-06-13 07:17:22 +00:00
|
|
|
const SELECTOR_TABLE = '.expandable-table'
|
2020-06-11 12:34:44 +00:00
|
|
|
const SELECTOR_HEADER = `.${CLASS_NAME_HEADER}`
|
|
|
|
const SELECTOR_DATA_SELECTOR = 'data-expandable-table'
|
|
|
|
const SELECTOR_EXPANDED = 'expanded'
|
|
|
|
const SELECTOR_COLLAPSED = 'collapsed'
|
2020-06-04 22:35:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Definition
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
class ExpandableTable {
|
|
|
|
constructor(element, config) {
|
|
|
|
this._config = config
|
|
|
|
this._element = element
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public
|
|
|
|
|
|
|
|
init() {
|
2020-06-11 12:34:44 +00:00
|
|
|
$(SELECTOR_HEADER).each((_, $header) => {
|
2020-06-04 22:35:50 +00:00
|
|
|
// Next Child to the header will have the same column span as header
|
2020-06-07 15:08:03 +00:00
|
|
|
$($header).next().children().first().attr('colspan', $($header).children().length)
|
2020-06-04 22:35:50 +00:00
|
|
|
|
|
|
|
// Setting up table design for the first time
|
2020-06-11 12:34:44 +00:00
|
|
|
const $type = $($header).next().attr(SELECTOR_DATA_SELECTOR)
|
2020-06-04 22:35:50 +00:00
|
|
|
const $body = $($header).next().children().first().children()
|
2020-06-11 12:34:44 +00:00
|
|
|
if ($type === SELECTOR_EXPANDED) {
|
2020-06-04 22:35:50 +00:00
|
|
|
$body.show()
|
2020-06-11 12:34:44 +00:00
|
|
|
} else if ($type === SELECTOR_COLLAPSED) {
|
2020-06-04 22:35:50 +00:00
|
|
|
$body.hide()
|
|
|
|
$body.parent().parent().addClass('d-none')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleRow() {
|
|
|
|
const $element = this._element
|
|
|
|
const time = 500
|
2020-06-11 12:34:44 +00:00
|
|
|
const $type = $element.next().attr(SELECTOR_DATA_SELECTOR)
|
2020-06-04 22:35:50 +00:00
|
|
|
const $body = $element.next().children().first().children()
|
|
|
|
$body.stop()
|
2020-06-11 12:34:44 +00:00
|
|
|
if ($type === SELECTOR_EXPANDED) {
|
2020-06-04 22:35:50 +00:00
|
|
|
$body.slideUp(time, () => {
|
|
|
|
$element.next().addClass('d-none')
|
|
|
|
})
|
2020-06-11 12:34:44 +00:00
|
|
|
$element.next().attr(SELECTOR_DATA_SELECTOR, SELECTOR_COLLAPSED)
|
|
|
|
$element.trigger($.Event(EVENT_COLLAPSED))
|
|
|
|
} else if ($type === SELECTOR_COLLAPSED) {
|
2020-06-04 22:35:50 +00:00
|
|
|
$element.next().removeClass('d-none')
|
|
|
|
$body.slideDown(time)
|
2020-06-11 12:34:44 +00:00
|
|
|
$element.next().attr(SELECTOR_DATA_SELECTOR, SELECTOR_EXPANDED)
|
|
|
|
$element.trigger($.Event(EVENT_EXPANDED))
|
2020-06-04 22:35:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Static
|
|
|
|
|
|
|
|
static _jQueryInterface(config) {
|
|
|
|
return this.each(function () {
|
|
|
|
let data = $(this).data(DATA_KEY)
|
|
|
|
if (!data) {
|
|
|
|
data = new ExpandableTable($(this))
|
|
|
|
$(this).data(DATA_KEY, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config === 'init' || config === 'toggleRow') {
|
|
|
|
data[config]()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data API
|
|
|
|
* ====================================================
|
|
|
|
*/
|
2020-06-13 07:17:22 +00:00
|
|
|
$(SELECTOR_TABLE).ready(function () {
|
2020-06-04 22:35:50 +00:00
|
|
|
ExpandableTable._jQueryInterface.call($(this), 'init')
|
|
|
|
})
|
|
|
|
|
2020-06-11 12:34:44 +00:00
|
|
|
$(document).on('click', SELECTOR_HEADER, function () {
|
2020-06-04 22:35:50 +00:00
|
|
|
ExpandableTable._jQueryInterface.call($(this), 'toggleRow')
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jQuery API
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
$.fn[NAME] = ExpandableTable._jQueryInterface
|
|
|
|
$.fn[NAME].Constructor = ExpandableTable
|
|
|
|
$.fn[NAME].noConflict = function () {
|
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
|
|
return ExpandableTable._jQueryInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ExpandableTable
|