AdminLTE/build/js/TodoList.js

121 lines
2.3 KiB
JavaScript
Raw Normal View History

/**
* --------------------------------------------
* AdminLTE TodoList.js
* License MIT
* --------------------------------------------
*/
2020-05-30 15:44:20 +02:00
const TodoList = ($ => {
/**
* Constants
* ====================================================
*/
2020-05-30 15:44:20 +02:00
const NAME = 'TodoList'
const DATA_KEY = 'lte.todolist'
const JQUERY_NO_CONFLICT = $.fn[NAME]
const Selector = {
DATA_TOGGLE: '[data-widget="todo-list"]'
}
const ClassName = {
TODO_LIST_DONE: 'done'
}
const Default = {
2020-05-30 15:44:20 +02:00
onCheck(item) {
return item
},
2020-05-30 15:44:20 +02:00
onUnCheck(item) {
return item
}
}
/**
* Class Definition
* ====================================================
*/
class TodoList {
constructor(element, config) {
2020-05-30 15:44:20 +02:00
this._config = config
this._element = element
this._init()
}
// Public
toggle(item) {
2020-05-30 15:44:20 +02:00
item.parents('li').toggleClass(ClassName.TODO_LIST_DONE)
if (!$(item).prop('checked')) {
this.unCheck($(item))
return
}
2020-05-30 15:44:20 +02:00
this.check(item)
}
2020-05-30 15:44:20 +02:00
check(item) {
this._config.onCheck.call(item)
}
2020-05-30 15:44:20 +02:00
unCheck(item) {
this._config.onUnCheck.call(item)
}
// Private
_init() {
$(Selector.DATA_TOGGLE).find('input:checkbox:checked').parents('li').toggleClass(ClassName.TODO_LIST_DONE)
2020-05-30 15:44:20 +02:00
$(Selector.DATA_TOGGLE).on('change', 'input:checkbox', event => {
2020-05-31 12:52:26 +02:00
this.toggle($(event.target))
})
}
// Static
static _jQueryInterface(config) {
return this.each(function () {
2019-11-17 11:53:47 +01:00
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
if (!data) {
2019-11-17 11:53:47 +01:00
data = new TodoList($(this), _options)
$(this).data(DATA_KEY, data)
}
if (config === 'init') {
data[config]()
}
})
}
}
/**
* Data API
* ====================================================
*/
$(window).on('load', () => {
TodoList._jQueryInterface.call($(Selector.DATA_TOGGLE))
})
/**
* jQuery API
* ====================================================
*/
$.fn[NAME] = TodoList._jQueryInterface
$.fn[NAME].Constructor = TodoList
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
return TodoList._jQueryInterface
}
return TodoList
})(jQuery)
export default TodoList