AdminLTE/build/js/TodoList.js

109 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-02-25 19:38:26 +00:00
/* TodoList()
* =========
* Converts a list into a todoList.
*
* @Usage: $('.my-list').todoList(options)
* or add [data-widget="todo-list"] to the ul element
* Pass any option as data-option="value"
*/
+function ($) {
2017-10-26 20:53:11 +00:00
'use strict';
2017-02-25 19:38:26 +00:00
2017-10-26 20:53:11 +00:00
var DataKey = 'lte.todolist';
2017-02-25 19:38:26 +00:00
var Default = {
onCheck : function (item) {
2017-10-26 20:53:11 +00:00
return item;
2017-02-25 19:38:26 +00:00
},
onUnCheck: function (item) {
2017-10-26 20:53:11 +00:00
return item;
2017-02-25 19:38:26 +00:00
}
2017-10-26 20:53:11 +00:00
};
2017-02-25 19:38:26 +00:00
var Selector = {
data: '[data-widget="todo-list"]'
2017-10-26 20:53:11 +00:00
};
2017-02-25 19:38:26 +00:00
var ClassName = {
done: 'done'
2017-10-26 20:53:11 +00:00
};
2017-02-25 19:38:26 +00:00
// TodoList Class Definition
// =========================
var TodoList = function (element, options) {
2017-10-26 20:53:11 +00:00
this.element = element;
this.options = options;
2017-02-25 19:38:26 +00:00
2017-10-26 20:53:11 +00:00
this._setUpListeners();
};
2017-02-25 19:38:26 +00:00
TodoList.prototype.toggle = function (item) {
2017-10-26 20:53:11 +00:00
item.parents(Selector.li).first().toggleClass(ClassName.done);
2017-02-25 19:38:26 +00:00
if (!item.prop('checked')) {
2017-10-26 20:53:11 +00:00
this.unCheck(item);
return;
2017-02-25 19:38:26 +00:00
}
2017-10-26 20:53:11 +00:00
this.check(item);
};
2017-02-25 19:38:26 +00:00
TodoList.prototype.check = function (item) {
2017-10-26 20:53:11 +00:00
this.options.onCheck.call(item);
};
2017-02-25 19:38:26 +00:00
TodoList.prototype.unCheck = function (item) {
2017-10-26 20:53:11 +00:00
this.options.onUnCheck.call(item);
};
2017-02-25 19:38:26 +00:00
// Private
TodoList.prototype._setUpListeners = function () {
2017-10-26 20:53:11 +00:00
var that = this;
2017-02-25 19:38:26 +00:00
$(this.element).on('change ifChanged', 'input:checkbox', function () {
2017-10-26 20:53:11 +00:00
that.toggle($(this));
});
};
2017-02-25 19:38:26 +00:00
// Plugin Definition
// =================
function Plugin(option) {
return this.each(function () {
2017-10-26 20:53:11 +00:00
var $this = $(this);
var data = $this.data(DataKey);
2017-02-25 19:38:26 +00:00
if (!data) {
2017-10-26 20:53:11 +00:00
var options = $.extend({}, Default, $this.data(), typeof option == 'object' && option);
$this.data(DataKey, (data = new TodoList($this, options)));
2017-02-25 19:38:26 +00:00
}
if (typeof data == 'string') {
if (typeof data[option] == 'undefined') {
2017-10-26 20:53:11 +00:00
throw new Error('No method named ' + option);
2017-02-25 19:38:26 +00:00
}
2017-10-26 20:53:11 +00:00
data[option]();
2017-02-25 19:38:26 +00:00
}
2017-10-26 20:53:11 +00:00
});
2017-02-25 19:38:26 +00:00
}
2017-10-26 20:53:11 +00:00
var old = $.fn.todoList;
2017-02-25 19:38:26 +00:00
2017-10-26 20:53:11 +00:00
$.fn.todoList = Plugin;
$.fn.todoList.Constructor = TodoList;
2017-02-25 19:38:26 +00:00
// No Conflict Mode
// ================
$.fn.todoList.noConflict = function () {
2017-10-26 20:53:11 +00:00
$.fn.todoList = old;
return this;
};
2017-02-25 19:38:26 +00:00
// TodoList Data API
// =================
$(window).on('load', function () {
$(Selector.data).each(function () {
2017-10-26 20:53:11 +00:00
Plugin.call($(this));
});
});
2017-02-25 19:38:26 +00:00
2017-10-26 20:53:11 +00:00
}(jQuery);