2019-08-20 10:33:52 +00:00
|
|
|
/**
|
|
|
|
* --------------------------------------------
|
|
|
|
* AdminLTE CardWidget.js
|
|
|
|
* License MIT
|
|
|
|
* --------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2020-05-30 13:44:20 +00:00
|
|
|
const CardWidget = ($ => {
|
2019-08-20 10:33:52 +00:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
2020-05-30 13:44:20 +00:00
|
|
|
const NAME = 'CardWidget'
|
|
|
|
const DATA_KEY = 'lte.cardwidget'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
2019-08-20 10:33:52 +00:00
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
|
|
|
|
|
|
|
const Event = {
|
|
|
|
EXPANDED: `expanded${EVENT_KEY}`,
|
|
|
|
COLLAPSED: `collapsed${EVENT_KEY}`,
|
|
|
|
MAXIMIZED: `maximized${EVENT_KEY}`,
|
|
|
|
MINIMIZED: `minimized${EVENT_KEY}`,
|
|
|
|
REMOVED: `removed${EVENT_KEY}`
|
|
|
|
}
|
|
|
|
|
|
|
|
const ClassName = {
|
|
|
|
CARD: 'card',
|
|
|
|
COLLAPSED: 'collapsed-card',
|
2020-01-28 10:25:39 +00:00
|
|
|
COLLAPSING: 'collapsing-card',
|
|
|
|
EXPANDING: 'expanding-card',
|
2019-08-20 10:33:52 +00:00
|
|
|
WAS_COLLAPSED: 'was-collapsed',
|
2020-05-30 13:44:20 +00:00
|
|
|
MAXIMIZED: 'maximized-card'
|
2019-08-20 10:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Selector = {
|
|
|
|
DATA_REMOVE: '[data-card-widget="remove"]',
|
|
|
|
DATA_COLLAPSE: '[data-card-widget="collapse"]',
|
|
|
|
DATA_MAXIMIZE: '[data-card-widget="maximize"]',
|
|
|
|
CARD: `.${ClassName.CARD}`,
|
|
|
|
CARD_HEADER: '.card-header',
|
|
|
|
CARD_BODY: '.card-body',
|
2020-05-30 13:44:20 +00:00
|
|
|
CARD_FOOTER: '.card-footer'
|
2019-08-20 10:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Default = {
|
|
|
|
animationSpeed: 'normal',
|
|
|
|
collapseTrigger: Selector.DATA_COLLAPSE,
|
|
|
|
removeTrigger: Selector.DATA_REMOVE,
|
|
|
|
maximizeTrigger: Selector.DATA_MAXIMIZE,
|
|
|
|
collapseIcon: 'fa-minus',
|
|
|
|
expandIcon: 'fa-plus',
|
|
|
|
maximizeIcon: 'fa-expand',
|
2020-05-30 13:44:20 +00:00
|
|
|
minimizeIcon: 'fa-compress'
|
2019-08-20 10:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class CardWidget {
|
|
|
|
constructor(element, settings) {
|
2020-05-30 13:44:20 +00:00
|
|
|
this._element = element
|
2019-08-20 10:33:52 +00:00
|
|
|
this._parent = element.parents(Selector.CARD).first()
|
|
|
|
|
|
|
|
if (element.hasClass(ClassName.CARD)) {
|
|
|
|
this._parent = element
|
|
|
|
}
|
|
|
|
|
|
|
|
this._settings = $.extend({}, Default, settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
collapse() {
|
2020-01-28 10:25:39 +00:00
|
|
|
this._parent.addClass(ClassName.COLLAPSING).children(`${Selector.CARD_BODY}, ${Selector.CARD_FOOTER}`)
|
2019-08-20 10:33:52 +00:00
|
|
|
.slideUp(this._settings.animationSpeed, () => {
|
2020-01-28 10:25:39 +00:00
|
|
|
this._parent.addClass(ClassName.COLLAPSED).removeClass(ClassName.COLLAPSING)
|
2019-08-20 10:33:52 +00:00
|
|
|
})
|
2020-01-17 06:37:39 +00:00
|
|
|
|
|
|
|
this._parent.find('> ' + Selector.CARD_HEADER + ' ' + this._settings.collapseTrigger + ' .' + this._settings.collapseIcon)
|
2019-08-20 10:33:52 +00:00
|
|
|
.addClass(this._settings.expandIcon)
|
|
|
|
.removeClass(this._settings.collapseIcon)
|
|
|
|
|
2020-05-31 17:58:24 +00:00
|
|
|
this._element.trigger($.Event(Event.COLLAPSED), this._parent)
|
2019-08-20 10:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expand() {
|
2020-01-28 10:25:39 +00:00
|
|
|
this._parent.addClass(ClassName.EXPANDING).children(`${Selector.CARD_BODY}, ${Selector.CARD_FOOTER}`)
|
2019-08-20 10:33:52 +00:00
|
|
|
.slideDown(this._settings.animationSpeed, () => {
|
2020-01-28 10:25:39 +00:00
|
|
|
this._parent.removeClass(ClassName.COLLAPSED).removeClass(ClassName.EXPANDING)
|
2019-08-20 10:33:52 +00:00
|
|
|
})
|
|
|
|
|
2020-01-17 06:37:39 +00:00
|
|
|
this._parent.find('> ' + Selector.CARD_HEADER + ' ' + this._settings.collapseTrigger + ' .' + this._settings.expandIcon)
|
2019-08-20 10:33:52 +00:00
|
|
|
.addClass(this._settings.collapseIcon)
|
|
|
|
.removeClass(this._settings.expandIcon)
|
|
|
|
|
2020-05-31 17:58:24 +00:00
|
|
|
this._element.trigger($.Event(Event.EXPANDED), this._parent)
|
2019-08-20 10:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
remove() {
|
|
|
|
this._parent.slideUp()
|
2020-05-31 17:58:24 +00:00
|
|
|
this._element.trigger($.Event(Event.REMOVED), this._parent)
|
2019-08-20 10:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toggle() {
|
|
|
|
if (this._parent.hasClass(ClassName.COLLAPSED)) {
|
|
|
|
this.expand()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.collapse()
|
|
|
|
}
|
2020-05-30 13:44:20 +00:00
|
|
|
|
2019-08-20 10:33:52 +00:00
|
|
|
maximize() {
|
2019-08-29 09:55:25 +00:00
|
|
|
this._parent.find(this._settings.maximizeTrigger + ' .' + this._settings.maximizeIcon)
|
2019-08-20 10:33:52 +00:00
|
|
|
.addClass(this._settings.minimizeIcon)
|
|
|
|
.removeClass(this._settings.maximizeIcon)
|
|
|
|
this._parent.css({
|
2020-05-30 13:44:20 +00:00
|
|
|
height: this._parent.height(),
|
|
|
|
width: this._parent.width(),
|
|
|
|
transition: 'all .15s'
|
|
|
|
}).delay(150).queue(function () {
|
2019-08-20 10:33:52 +00:00
|
|
|
$(this).addClass(ClassName.MAXIMIZED)
|
|
|
|
$('html').addClass(ClassName.MAXIMIZED)
|
|
|
|
if ($(this).hasClass(ClassName.COLLAPSED)) {
|
|
|
|
$(this).addClass(ClassName.WAS_COLLAPSED)
|
|
|
|
}
|
2020-05-30 13:44:20 +00:00
|
|
|
|
2019-08-20 10:33:52 +00:00
|
|
|
$(this).dequeue()
|
|
|
|
})
|
|
|
|
|
2020-05-31 17:58:24 +00:00
|
|
|
this._element.trigger($.Event(Event.MAXIMIZED), this._parent)
|
2019-08-20 10:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
minimize() {
|
2019-08-29 09:55:25 +00:00
|
|
|
this._parent.find(this._settings.maximizeTrigger + ' .' + this._settings.minimizeIcon)
|
2019-08-20 10:33:52 +00:00
|
|
|
.addClass(this._settings.maximizeIcon)
|
|
|
|
.removeClass(this._settings.minimizeIcon)
|
|
|
|
this._parent.css('cssText', 'height:' + this._parent[0].style.height + ' !important;' +
|
|
|
|
'width:' + this._parent[0].style.width + ' !important; transition: all .15s;'
|
2020-05-30 13:44:20 +00:00
|
|
|
).delay(10).queue(function () {
|
2019-08-20 10:33:52 +00:00
|
|
|
$(this).removeClass(ClassName.MAXIMIZED)
|
|
|
|
$('html').removeClass(ClassName.MAXIMIZED)
|
|
|
|
$(this).css({
|
2020-05-30 13:44:20 +00:00
|
|
|
height: 'inherit',
|
|
|
|
width: 'inherit'
|
2019-08-20 10:33:52 +00:00
|
|
|
})
|
|
|
|
if ($(this).hasClass(ClassName.WAS_COLLAPSED)) {
|
|
|
|
$(this).removeClass(ClassName.WAS_COLLAPSED)
|
|
|
|
}
|
2020-05-30 13:44:20 +00:00
|
|
|
|
2019-08-20 10:33:52 +00:00
|
|
|
$(this).dequeue()
|
|
|
|
})
|
|
|
|
|
2020-05-31 17:58:24 +00:00
|
|
|
this._element.trigger($.Event(Event.MINIMIZED), this._parent)
|
2019-08-20 10:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleMaximize() {
|
|
|
|
if (this._parent.hasClass(ClassName.MAXIMIZED)) {
|
|
|
|
this.minimize()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.maximize()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
|
|
|
_init(card) {
|
|
|
|
this._parent = card
|
|
|
|
|
|
|
|
$(this).find(this._settings.collapseTrigger).click(() => {
|
|
|
|
this.toggle()
|
|
|
|
})
|
|
|
|
|
|
|
|
$(this).find(this._settings.maximizeTrigger).click(() => {
|
|
|
|
this.toggleMaximize()
|
|
|
|
})
|
|
|
|
|
|
|
|
$(this).find(this._settings.removeTrigger).click(() => {
|
|
|
|
this.remove()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Static
|
|
|
|
|
|
|
|
static _jQueryInterface(config) {
|
|
|
|
let data = $(this).data(DATA_KEY)
|
2019-11-17 10:53:47 +00:00
|
|
|
const _options = $.extend({}, Default, $(this).data())
|
2019-08-20 10:33:52 +00:00
|
|
|
|
|
|
|
if (!data) {
|
2019-11-17 10:53:47 +00:00
|
|
|
data = new CardWidget($(this), _options)
|
2020-05-30 13:44:20 +00:00
|
|
|
$(this).data(DATA_KEY, typeof config === 'string' ? data : config)
|
2019-08-20 10:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof config === 'string' && config.match(/collapse|expand|remove|toggle|maximize|minimize|toggleMaximize/)) {
|
|
|
|
data[config]()
|
|
|
|
} else if (typeof config === 'object') {
|
|
|
|
data._init($(this))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data API
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
$(document).on('click', Selector.DATA_COLLAPSE, function (event) {
|
|
|
|
if (event) {
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
CardWidget._jQueryInterface.call($(this), 'toggle')
|
|
|
|
})
|
|
|
|
|
|
|
|
$(document).on('click', Selector.DATA_REMOVE, function (event) {
|
|
|
|
if (event) {
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
CardWidget._jQueryInterface.call($(this), 'remove')
|
|
|
|
})
|
|
|
|
|
|
|
|
$(document).on('click', Selector.DATA_MAXIMIZE, function (event) {
|
|
|
|
if (event) {
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
CardWidget._jQueryInterface.call($(this), 'toggleMaximize')
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jQuery API
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
$.fn[NAME] = CardWidget._jQueryInterface
|
|
|
|
$.fn[NAME].Constructor = CardWidget
|
2020-05-30 13:44:20 +00:00
|
|
|
$.fn[NAME].noConflict = function () {
|
2019-08-20 10:33:52 +00:00
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
|
|
return CardWidget._jQueryInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
return CardWidget
|
|
|
|
})(jQuery)
|
|
|
|
|
|
|
|
export default CardWidget
|