AdminLTE/build/js/TodoList.js

120 lines
2.4 KiB
JavaScript
Raw Normal View History

/**
* --------------------------------------------
* AdminLTE TodoList.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 = 'TodoList'
const DATA_KEY = 'lte.todolist'
const JQUERY_NO_CONFLICT = $.fn[NAME]
const SELECTOR_DATA_TOGGLE = '[data-widget="todo-list"]'
const CLASS_NAME_TODO_LIST_DONE = 'done'
2020-06-02 13:23:22 +00:00
const Default = {
onCheck(item) {
return item
},
onUnCheck(item) {
return item
}
}
2020-06-02 13:23:22 +00:00
/**
* Class Definition
* ====================================================
*/
2020-06-02 13:23:22 +00:00
class TodoList {
constructor(element, config) {
this._config = config
this._element = element
2020-06-02 13:23:22 +00:00
this._init()
}
2020-06-02 13:23:22 +00:00
// Public
2020-06-02 13:23:22 +00:00
toggle(item) {
item.parents('li').toggleClass(CLASS_NAME_TODO_LIST_DONE)
2020-06-02 13:23:22 +00:00
if (!$(item).prop('checked')) {
this.unCheck(item)
2020-06-02 13:23:22 +00:00
return
}
2020-06-02 13:23:22 +00:00
this.check(item)
}
2020-06-02 13:23:22 +00:00
check(item) {
this._config.onCheck(item)
2020-06-02 13:23:22 +00:00
}
2020-06-02 13:23:22 +00:00
unCheck(item) {
this._config.onUnCheck(item)
2020-06-02 13:23:22 +00:00
}
2020-06-02 13:23:22 +00:00
// Private
2020-06-02 13:23:22 +00:00
_init() {
const $toggleSelector = this._element
2020-06-04 18:06:38 +00:00
$toggleSelector.find('input:checkbox:checked').parents('li').toggleClass(CLASS_NAME_TODO_LIST_DONE)
2020-06-04 18:06:38 +00:00
$toggleSelector.on('change', 'input:checkbox', event => {
2020-06-02 13:23:22 +00:00
this.toggle($(event.target))
})
}
2020-06-02 13:23:22 +00:00
// Static
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
2020-06-02 13:23:22 +00:00
if (!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}"`)
}
data[config]()
} else if (typeof config === 'undefined') {
data._init()
2020-06-02 13:23:22 +00:00
}
})
}
2020-06-02 13:23:22 +00:00
}
/**
* Data API
* ====================================================
*/
$(window).on('load', () => {
TodoList._jQueryInterface.call($(SELECTOR_DATA_TOGGLE))
2020-06-02 13:23:22 +00:00
})
/**
* jQuery API
* ====================================================
*/
2020-06-02 13:23:22 +00:00
$.fn[NAME] = TodoList._jQueryInterface
$.fn[NAME].Constructor = TodoList
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
return TodoList._jQueryInterface
}
export default TodoList