AdminLTE/build/js/CardWidget.js

239 lines
6.4 KiB
JavaScript
Raw Normal View History

/**
* --------------------------------------------
* AdminLTE CardWidget.js
* License MIT
* --------------------------------------------
*/
2020-06-02 13:23:22 +00:00
import $ from 'jquery'
2020-06-02 13:23:22 +00:00
/**
* Constants
* ====================================================
*/
2020-06-02 13:23:22 +00:00
const NAME = 'CardWidget'
const DATA_KEY = 'lte.cardwidget'
const EVENT_KEY = `.${DATA_KEY}`
const JQUERY_NO_CONFLICT = $.fn[NAME]
const EVENT_EXPANDED = `expanded${EVENT_KEY}`
const EVENT_COLLAPSED = `collapsed${EVENT_KEY}`
const EVENT_MAXIMIZED = `maximized${EVENT_KEY}`
const EVENT_MINIMIZED = `minimized${EVENT_KEY}`
const EVENT_REMOVED = `removed${EVENT_KEY}`
const CLASS_NAME_CARD = 'card'
const CLASS_NAME_COLLAPSED = 'collapsed-card'
const CLASS_NAME_COLLAPSING = 'collapsing-card'
const CLASS_NAME_EXPANDING = 'expanding-card'
const CLASS_NAME_WAS_COLLAPSED = 'was-collapsed'
const CLASS_NAME_MAXIMIZED = 'maximized-card'
const SELECTOR_DATA_REMOVE = '[data-card-widget="remove"]'
const SELECTOR_DATA_COLLAPSE = '[data-card-widget="collapse"]'
const SELECTOR_DATA_MAXIMIZE = '[data-card-widget="maximize"]'
const SELECTOR_CARD = `.${CLASS_NAME_CARD}`
const SELECTOR_CARD_HEADER = '.card-header'
const SELECTOR_CARD_BODY = '.card-body'
const SELECTOR_CARD_FOOTER = '.card-footer'
2020-06-02 13:23:22 +00:00
const Default = {
animationSpeed: 'normal',
collapseTrigger: SELECTOR_DATA_COLLAPSE,
removeTrigger: SELECTOR_DATA_REMOVE,
maximizeTrigger: SELECTOR_DATA_MAXIMIZE,
2020-06-02 13:23:22 +00:00
collapseIcon: 'fa-minus',
expandIcon: 'fa-plus',
maximizeIcon: 'fa-expand',
minimizeIcon: 'fa-compress'
}
class CardWidget {
constructor(element, settings) {
this._element = element
this._parent = element.parents(SELECTOR_CARD).first()
2020-06-02 13:23:22 +00:00
if (element.hasClass(CLASS_NAME_CARD)) {
2020-06-02 13:23:22 +00:00
this._parent = element
}
2020-06-02 13:23:22 +00:00
this._settings = $.extend({}, Default, settings)
}
2020-06-02 13:23:22 +00:00
collapse() {
this._parent.addClass(CLASS_NAME_COLLAPSING).children(`${SELECTOR_CARD_BODY}, ${SELECTOR_CARD_FOOTER}`)
2020-06-02 13:23:22 +00:00
.slideUp(this._settings.animationSpeed, () => {
this._parent.addClass(CLASS_NAME_COLLAPSED).removeClass(CLASS_NAME_COLLAPSING)
2020-06-02 13:23:22 +00:00
})
2020-06-13 12:34:10 +00:00
this._parent.find(`> ${SELECTOR_CARD_HEADER} ${this._settings.collapseTrigger} .${this._settings.collapseIcon}`)
2020-06-02 13:23:22 +00:00
.addClass(this._settings.expandIcon)
.removeClass(this._settings.collapseIcon)
this._element.trigger($.Event(EVENT_COLLAPSED), this._parent)
2020-06-02 13:23:22 +00:00
}
2020-06-02 13:23:22 +00:00
expand() {
this._parent.addClass(CLASS_NAME_EXPANDING).children(`${SELECTOR_CARD_BODY}, ${SELECTOR_CARD_FOOTER}`)
2020-06-02 13:23:22 +00:00
.slideDown(this._settings.animationSpeed, () => {
this._parent.removeClass(CLASS_NAME_COLLAPSED).removeClass(CLASS_NAME_EXPANDING)
2020-06-02 13:23:22 +00:00
})
2020-06-13 12:34:10 +00:00
this._parent.find(`> ${SELECTOR_CARD_HEADER} ${this._settings.collapseTrigger} .${this._settings.expandIcon}`)
2020-06-02 13:23:22 +00:00
.addClass(this._settings.collapseIcon)
.removeClass(this._settings.expandIcon)
this._element.trigger($.Event(EVENT_EXPANDED), this._parent)
2020-06-02 13:23:22 +00:00
}
2020-06-02 13:23:22 +00:00
remove() {
this._parent.slideUp()
this._element.trigger($.Event(EVENT_REMOVED), this._parent)
2020-06-02 13:23:22 +00:00
}
2020-06-02 13:23:22 +00:00
toggle() {
if (this._parent.hasClass(CLASS_NAME_COLLAPSED)) {
2020-06-02 13:23:22 +00:00
this.expand()
return
}
2020-06-02 13:23:22 +00:00
this.collapse()
}
2020-06-02 13:23:22 +00:00
maximize() {
2020-06-13 12:34:10 +00:00
this._parent.find(`${this._settings.maximizeTrigger} .${this._settings.maximizeIcon}`)
2020-06-02 13:23:22 +00:00
.addClass(this._settings.minimizeIcon)
.removeClass(this._settings.maximizeIcon)
this._parent.css({
height: this._parent.height(),
width: this._parent.width(),
transition: 'all .15s'
}).delay(150).queue(function () {
2020-06-04 18:06:38 +00:00
const $element = $(this)
$element.addClass(CLASS_NAME_MAXIMIZED)
$('html').addClass(CLASS_NAME_MAXIMIZED)
if ($element.hasClass(CLASS_NAME_COLLAPSED)) {
$element.addClass(CLASS_NAME_WAS_COLLAPSED)
}
2020-06-04 18:06:38 +00:00
$element.dequeue()
2020-06-02 13:23:22 +00:00
})
this._element.trigger($.Event(EVENT_MAXIMIZED), this._parent)
2020-06-02 13:23:22 +00:00
}
2020-06-02 13:23:22 +00:00
minimize() {
2020-06-13 12:34:10 +00:00
this._parent.find(`${this._settings.maximizeTrigger} .${this._settings.minimizeIcon}`)
2020-06-02 13:23:22 +00:00
.addClass(this._settings.maximizeIcon)
.removeClass(this._settings.minimizeIcon)
2020-06-13 12:34:10 +00:00
this._parent.css('cssText', `height: ${this._parent[0].style.height} !important; width: ${this._parent[0].style.width} !important; transition: all .15s;`
2020-06-02 13:23:22 +00:00
).delay(10).queue(function () {
2020-06-04 18:06:38 +00:00
const $element = $(this)
$element.removeClass(CLASS_NAME_MAXIMIZED)
$('html').removeClass(CLASS_NAME_MAXIMIZED)
2020-06-04 18:06:38 +00:00
$element.css({
2020-06-02 13:23:22 +00:00
height: 'inherit',
width: 'inherit'
})
if ($element.hasClass(CLASS_NAME_WAS_COLLAPSED)) {
$element.removeClass(CLASS_NAME_WAS_COLLAPSED)
2020-06-02 13:23:22 +00:00
}
2020-06-04 18:06:38 +00:00
$element.dequeue()
2020-06-02 13:23:22 +00:00
})
this._element.trigger($.Event(EVENT_MINIMIZED), this._parent)
2020-06-02 13:23:22 +00:00
}
2020-06-02 13:23:22 +00:00
toggleMaximize() {
if (this._parent.hasClass(CLASS_NAME_MAXIMIZED)) {
2020-06-02 13:23:22 +00:00
this.minimize()
return
}
2020-06-02 13:23:22 +00:00
this.maximize()
}
2020-06-02 13:23:22 +00:00
// Private
2020-06-02 13:23:22 +00:00
_init(card) {
this._parent = card
2020-06-02 13:23:22 +00:00
$(this).find(this._settings.collapseTrigger).click(() => {
this.toggle()
})
2020-06-02 13:23:22 +00:00
$(this).find(this._settings.maximizeTrigger).click(() => {
this.toggleMaximize()
})
2020-06-02 13:23:22 +00:00
$(this).find(this._settings.removeTrigger).click(() => {
this.remove()
})
}
2020-06-02 13:23:22 +00:00
// Static
2020-06-02 13:23:22 +00:00
static _jQueryInterface(config) {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
2020-06-02 13:23:22 +00:00
if (!data) {
data = new CardWidget($(this), _options)
$(this).data(DATA_KEY, typeof config === 'string' ? data : config)
}
if (typeof config === 'string' && /collapse|expand|remove|toggle|maximize|minimize|toggleMaximize/.test(config)) {
2020-06-02 13:23:22 +00:00
data[config]()
} else if (typeof config === 'object') {
data._init($(this))
}
2020-06-02 13:23:22 +00:00
}
}
2020-06-02 13:23:22 +00:00
/**
* Data API
* ====================================================
*/
$(document).on('click', SELECTOR_DATA_COLLAPSE, function (event) {
2020-06-02 13:23:22 +00:00
if (event) {
event.preventDefault()
}
2020-06-02 13:23:22 +00:00
CardWidget._jQueryInterface.call($(this), 'toggle')
})
$(document).on('click', SELECTOR_DATA_REMOVE, function (event) {
2020-06-02 13:23:22 +00:00
if (event) {
event.preventDefault()
}
2020-06-02 13:23:22 +00:00
CardWidget._jQueryInterface.call($(this), 'remove')
})
$(document).on('click', SELECTOR_DATA_MAXIMIZE, function (event) {
2020-06-02 13:23:22 +00:00
if (event) {
event.preventDefault()
}
2020-06-02 13:23:22 +00:00
CardWidget._jQueryInterface.call($(this), 'toggleMaximize')
})
/**
* jQuery API
* ====================================================
*/
$.fn[NAME] = CardWidget._jQueryInterface
$.fn[NAME].Constructor = CardWidget
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
return CardWidget._jQueryInterface
}
export default CardWidget