AdminLTE/build/js/TodoList.js

119 lines
2.2 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))
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.call(item)
}
2020-06-02 13:23:22 +00:00
unCheck(item) {
this._config.onUnCheck.call(item)
}
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)
if (!data) {
data = $(this).data()
2020-06-02 13:23:22 +00:00
}
const _options = $.extend({}, Default, typeof config === 'object' ? config : data)
const plugin = new TodoList($(this), _options)
$(this).data(DATA_KEY, typeof config === 'object' ? config : data)
2020-06-02 13:23:22 +00:00
if (config === 'init') {
plugin[config]()
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