fix(plugins): reworked jQueryInterface's for all plugins

pull/4558/head
REJack 2022-06-29 21:10:20 +02:00
parent fe9461033d
commit 75deb497b3
15 changed files with 155 additions and 133 deletions

View File

@ -114,21 +114,25 @@ class CardRefresh {
}
// Static
static _jQueryInterface(config) {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
if (!data) {
data = new CardRefresh($(this), _options)
$(this).data(DATA_KEY, typeof config === 'string' ? data : config)
}
if (!data) {
data = new CardRefresh($(this), _config)
$(this).data(DATA_KEY, data)
data._init()
} else if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
if (typeof config === 'string' && /load/.test(config)) {
data[config]()
} else {
data._init($(this))
}
data[config]()
} else if (typeof config === 'undefined') {
data._init()
}
})
}
}

View File

@ -108,6 +108,7 @@ class CardWidget {
this._parent.css({
height: this._parent.height(),
width: this._parent.width(),
position: 'fixed',
transition: 'all .15s'
}).delay(150).queue(function () {
const $element = $(this)
@ -176,13 +177,12 @@ class CardWidget {
}
// Static
static _jQueryInterface(config) {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
const _config = $.extend({}, Default, $(this).data())
if (!data) {
data = new CardWidget($(this), _options)
data = new CardWidget($(this), _config)
$(this).data(DATA_KEY, typeof config === 'string' ? data : config)
}

View File

@ -286,22 +286,24 @@ class ControlSidebar {
}
// Static
static _jQueryInterface(operation) {
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
if (!data) {
data = new ControlSidebar(this, _options)
data = new ControlSidebar($(this), _config)
$(this).data(DATA_KEY, data)
}
data._init()
} else if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
if (data[operation] === 'undefined') {
throw new Error(`${operation} is not a function`)
data[config]()
} else if (typeof config === 'undefined') {
data._init()
}
data[operation]()
})
}
}

View File

@ -40,7 +40,6 @@ class DirectChat {
}
// Static
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
@ -48,9 +47,15 @@ class DirectChat {
if (!data) {
data = new DirectChat($(this))
$(this).data(DATA_KEY, data)
}
} else if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
data[config]()
data[config]()
} else if (typeof config === 'undefined') {
data._init()
}
})
}
}

View File

@ -89,18 +89,19 @@ class Dropdown {
}
// Static
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _config = $.extend({}, Default, $(this).data())
const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
if (!data) {
data = new Dropdown($(this), _config)
$(this).data(DATA_KEY, data)
}
} else if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
if (config === 'toggleSubmenu' || config === 'fixPosition') {
data[config]()
}
})

View File

@ -30,14 +30,13 @@ const SELECTOR_ARIA_ATTR = 'aria-expanded'
* ====================================================
*/
class ExpandableTable {
constructor(element, options) {
this._options = options
constructor(element) {
this._element = element
}
// Public
init() {
_init() {
$(SELECTOR_DATA_TOGGLE).each((_, $header) => {
const $type = $($header).attr(SELECTOR_ARIA_ATTR)
const $body = $($header).next(SELECTOR_EXPANDABLE_BODY).children().first().children()
@ -80,8 +79,7 @@ class ExpandableTable {
}
// Static
static _jQueryInterface(operation) {
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
@ -90,8 +88,8 @@ class ExpandableTable {
$(this).data(DATA_KEY, data)
}
if (typeof operation === 'string' && /init|toggleRow/.test(operation)) {
data[operation]()
if (typeof config === 'string' && /init|toggleRow/.test(config)) {
data[config]()
}
})
}
@ -102,7 +100,7 @@ class ExpandableTable {
* ====================================================
*/
$(SELECTOR_TABLE).ready(function () {
ExpandableTable._jQueryInterface.call($(this), 'init')
ExpandableTable._jQueryInterface.call($(this), '_init')
})
$(document).on('click', SELECTOR_DATA_TOGGLE, function () {

View File

@ -34,7 +34,7 @@ const Default = {
class Fullscreen {
constructor(_element, _options) {
this.element = _element
this.options = $.extend({}, Default, _options)
this.options = _options
}
// Public
@ -82,24 +82,22 @@ class Fullscreen {
}
// Static
static _jQueryInterface(config) {
let data = $(this).data(DATA_KEY)
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
if (!data) {
data = $(this).data()
}
if (!data) {
data = new Fullscreen($(this), _config)
$(this).data(DATA_KEY, data)
} else if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
const _options = $.extend({}, Default, typeof config === 'object' ? config : data)
const plugin = new Fullscreen($(this), _options)
$(this).data(DATA_KEY, typeof config === 'object' ? config : data)
if (typeof config === 'string' && /toggle|toggleIcon|fullscreen|windowed/.test(config)) {
plugin[config]()
} else {
plugin.init()
}
data[config]()
}
})
}
}

View File

@ -1,3 +1,5 @@
// noinspection EqualityComparisonWithCoercionJS
/**
* --------------------------------------------
* AdminLTE IFrame.js
@ -106,7 +108,11 @@ class IFrame {
if (autoOpen) {
if (this._config.loadingScreen) {
const $loadingScreen = $(SELECTOR_TAB_LOADING)
$loadingScreen.fadeIn()
if (!$loadingScreen.is(':animated')) {
$loadingScreen.fadeIn()
}
$(`${tabId} iframe`).ready(() => {
if (typeof this._config.loadingScreen === 'number') {
this.switchTab(`#${navId}`)
@ -258,8 +264,6 @@ class IFrame {
if (usingDefTab) {
const $el = $(`${SELECTOR_TAB_PANE}`).first()
// eslint-disable-next-line no-console
console.log($el)
const uniqueName = $el.attr('id').replace('panel-', '')
const navId = `#tab-${uniqueName}`
@ -316,7 +320,7 @@ class IFrame {
e.preventDefault()
let { target } = e
if (target.nodeName == 'I') {
if (target.nodeName === 'I') {
target = e.target.offsetParent
}
@ -411,8 +415,8 @@ class IFrame {
}
// Static
static _jQueryInterface(config) {
// eslint-disable-next-line max-params
static _jQueryInterface(config, name, link, id, reload) {
if ($(SELECTOR_DATA_TOGGLE).length > 0) {
let data = $(this).data(DATA_KEY)
@ -422,16 +426,14 @@ class IFrame {
const _options = $.extend({}, Default, typeof config === 'object' ? config : data)
localStorage.setItem('AdminLTE:IFrame:Options', JSON.stringify(_options))
const plugin = new IFrame($(this), _options)
$(this).data(DATA_KEY, typeof config === 'object' ? config : data)
window.iFrameInstance = plugin
$(this).data(DATA_KEY, typeof config === 'object' ? config : { link, name, id, reload, ...data })
if (typeof config === 'string' && /createTab|openTabSidebar|switchTab|removeActiveTab/.test(config)) {
plugin[config]()
plugin[config](name, link, id, reload)
}
} else {
new IFrame($(this), JSON.parse(localStorage.getItem('AdminLTE:IFrame:Options')))._initFrameElement()
window.iFrameInstance = new IFrame($(this), JSON.parse(localStorage.getItem('AdminLTE:IFrame:Options')))._initFrameElement()
}
}
}

View File

@ -210,21 +210,23 @@ class Layout {
}
// Static
static _jQueryInterface(config = '') {
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
if (!data) {
data = new Layout($(this), _options)
data = new Layout($(this), _config)
$(this).data(DATA_KEY, data)
}
if (config === 'init' || config === '') {
data._init()
} else if (config === 'fixLayoutHeight' || config === 'fixLoginRegisterHeight') {
} else if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
data[config]()
} else if (typeof config === 'undefined') {
data._init()
}
})
}

View File

@ -35,7 +35,7 @@ const Default = {
class NavbarSearch {
constructor(_element, _options) {
this._element = _element
this._config = $.extend({}, Default, _options)
this._config = _options
}
// Public
@ -62,22 +62,21 @@ class NavbarSearch {
}
// Static
static _jQueryInterface(options) {
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
if (!data) {
data = new NavbarSearch(this, _options)
data = new NavbarSearch($(this), _config)
$(this).data(DATA_KEY, data)
}
} else if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
if (!/toggle|close|open/.test(options)) {
throw new Error(`Undefined method ${options}`)
data[config]()
}
data[options]()
})
}
}

View File

@ -46,7 +46,7 @@ const Default = {
class PushMenu {
constructor(element, options) {
this._element = element
this._options = $.extend({}, Default, options)
this._options = options
if ($(SELECTOR_OVERLAY).length === 0) {
this._addOverlay()
@ -175,19 +175,23 @@ class PushMenu {
}
// Static
static _jQueryInterface(operation) {
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
if (!data) {
data = new PushMenu(this, _options)
data = new PushMenu($(this), _config)
$(this).data(DATA_KEY, data)
}
data._init()
} else if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
if (typeof operation === 'string' && /collapse|expand|toggle/.test(operation)) {
data[operation]()
data[config]()
} else if (typeof config === 'undefined') {
data._init()
}
})
}

View File

@ -60,7 +60,7 @@ class SidebarSearch {
// Public
init() {
_init() {
if ($(SELECTOR_DATA_WIDGET).length === 0) {
return
}
@ -207,24 +207,25 @@ class SidebarSearch {
}
// Static
static _jQueryInterface(config) {
let data = $(this).data(DATA_KEY)
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
if (!data) {
data = $(this).data()
}
if (!data) {
data = new SidebarSearch($(this), _config)
$(this).data(DATA_KEY, data)
data._init()
} else if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
const _options = $.extend({}, Default, typeof config === 'object' ? config : data)
const plugin = new SidebarSearch($(this), _options)
$(this).data(DATA_KEY, typeof config === 'object' ? config : data)
if (typeof config === 'string' && /init|toggle|close|open|search/.test(config)) {
plugin[config]()
} else {
plugin.init()
}
data[config]()
} else if (typeof config === 'undefined') {
data._init()
}
})
}
}

View File

@ -1,3 +1,5 @@
// noinspection EqualityComparisonWithCoercionJS
/**
* --------------------------------------------
* AdminLTE Toasts.js
@ -140,19 +142,19 @@ class Toasts {
// Static
_getContainerId() {
if (this._config.position == POSITION_TOP_RIGHT) {
if (this._config.position === POSITION_TOP_RIGHT) {
return SELECTOR_CONTAINER_TOP_RIGHT
}
if (this._config.position == POSITION_TOP_LEFT) {
if (this._config.position === POSITION_TOP_LEFT) {
return SELECTOR_CONTAINER_TOP_LEFT
}
if (this._config.position == POSITION_BOTTOM_RIGHT) {
if (this._config.position === POSITION_BOTTOM_RIGHT) {
return SELECTOR_CONTAINER_BOTTOM_RIGHT
}
if (this._config.position == POSITION_BOTTOM_LEFT) {
if (this._config.position === POSITION_BOTTOM_LEFT) {
return SELECTOR_CONTAINER_BOTTOM_LEFT
}
}
@ -181,7 +183,6 @@ class Toasts {
}
// Static
static _jQueryInterface(option, config) {
return this.each(function () {
const _options = $.extend({}, Default, config)

View File

@ -46,7 +46,7 @@ class TodoList {
toggle(item) {
item.parents('li').toggleClass(CLASS_NAME_TODO_LIST_DONE)
if (!$(item).prop('checked')) {
this.unCheck($(item))
this.unCheck(item)
return
}
@ -54,11 +54,11 @@ class TodoList {
}
check(item) {
this._config.onCheck.call(item)
this._config.onCheck(item)
}
unCheck(item) {
this._config.onUnCheck.call(item)
this._config.onUnCheck(item)
}
// Private
@ -73,22 +73,23 @@ class TodoList {
}
// Static
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
if (!data) {
data = $(this).data()
}
data = new TodoList($(this), _config)
$(this).data(DATA_KEY, data)
data._init()
} else if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
const _options = $.extend({}, Default, typeof config === 'object' ? config : data)
const plugin = new TodoList($(this), _options)
$(this).data(DATA_KEY, typeof config === 'object' ? config : data)
if (config === 'init') {
plugin[config]()
data[config]()
} else if (typeof config === 'undefined') {
data._init()
}
})
}

View File

@ -51,7 +51,7 @@ class Treeview {
// Public
init() {
_init() {
$(`${SELECTOR_LI}${SELECTOR_OPEN} ${SELECTOR_TREEVIEW_MENU}${SELECTOR_OPEN}`).css('display', 'block')
this._setupListeners()
}
@ -131,19 +131,23 @@ class Treeview {
}
// Static
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
if (!data) {
data = new Treeview($(this), _options)
data = new Treeview($(this), _config)
$(this).data(DATA_KEY, data)
}
data._init()
} else if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
if (config === 'init') {
data[config]()
} else if (typeof config === 'undefined') {
data._init()
}
})
}