2019-07-28 19:11:54 +00:00
|
|
|
/**
|
|
|
|
* --------------------------------------------
|
|
|
|
* AdminLTE TodoList.js
|
|
|
|
* License MIT
|
|
|
|
* --------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
import $ from 'jquery'
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
* ====================================================
|
|
|
|
*/
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
const NAME = 'TodoList'
|
|
|
|
const DATA_KEY = 'lte.todolist'
|
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-11 12:34:44 +00:00
|
|
|
const SELECTOR_DATA_TOGGLE = '[data-widget="todo-list"]'
|
|
|
|
const CLASS_NAME_TODO_LIST_DONE = 'done'
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
const Default = {
|
|
|
|
onCheck(item) {
|
|
|
|
return item
|
|
|
|
},
|
|
|
|
onUnCheck(item) {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
}
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
/**
|
|
|
|
* Class Definition
|
|
|
|
* ====================================================
|
|
|
|
*/
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
class TodoList {
|
|
|
|
constructor(element, config) {
|
|
|
|
this._config = config
|
|
|
|
this._element = element
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
this._init()
|
|
|
|
}
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
// Public
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
toggle(item) {
|
2020-06-11 12:34:44 +00:00
|
|
|
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
|
2019-07-28 19:11:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
this.check(item)
|
|
|
|
}
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
check(item) {
|
|
|
|
this._config.onCheck.call(item)
|
|
|
|
}
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
unCheck(item) {
|
|
|
|
this._config.onUnCheck.call(item)
|
|
|
|
}
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
// Private
|
2019-07-28 19:11:54 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
_init() {
|
2020-09-11 09:57:33 +00:00
|
|
|
const $toggleSelector = this._element
|
2020-06-04 18:06:38 +00:00
|
|
|
|
2020-06-11 12:34:44 +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))
|
|
|
|
})
|
2019-07-28 19:11:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
// Static
|
|
|
|
|
|
|
|
static _jQueryInterface(config) {
|
|
|
|
return this.each(function () {
|
|
|
|
let data = $(this).data(DATA_KEY)
|
|
|
|
|
|
|
|
if (!data) {
|
2020-09-11 09:57:33 +00:00
|
|
|
data = $(this).data()
|
2020-06-02 13:23:22 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 09:57:33 +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') {
|
2020-09-11 09:57:33 +00:00
|
|
|
plugin[config]()
|
2020-06-02 13:23:22 +00:00
|
|
|
}
|
|
|
|
})
|
2019-07-28 19:11:54 +00:00
|
|
|
}
|
2020-06-02 13:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data API
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
$(window).on('load', () => {
|
2020-06-11 12:34:44 +00:00
|
|
|
TodoList._jQueryInterface.call($(SELECTOR_DATA_TOGGLE))
|
2020-06-02 13:23:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jQuery API
|
|
|
|
* ====================================================
|
|
|
|
*/
|
2019-07-28 19:11:54 +00:00
|
|
|
|
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
|
|
|
|
}
|
2019-07-28 19:11:54 +00:00
|
|
|
|
|
|
|
export default TodoList
|