openmct/tutorials/todo/todo.js

95 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-05-27 22:31:54 +00:00
define([
"text!./todo.html",
"text!./todo-task.html",
"zepto"
], function (todoTemplate, taskTemplate, $) {
/**
* @param {mct.MCT} mct
*/
2016-05-27 20:17:16 +00:00
return function todoPlugin(mct) {
var todoType = new mct.Type({
metadata: {
label: "To-Do List",
glyph: "2",
description: "A list of things that need to be done."
},
initialize: function (model) {
2016-05-27 23:46:06 +00:00
model.tasks = [
{ description: "This is a task." }
];
2016-05-27 20:17:16 +00:00
},
creatable: true
});
2016-05-27 22:31:54 +00:00
todoType.view(mct.regions.main, function (domainObject) {
2016-05-27 22:49:16 +00:00
var view = new mct.View({
state: function () {
return {
filter: "all"
};
},
elements: $.bind(null, todoTemplate),
2016-05-27 23:36:55 +00:00
initialize: function (elements, state, render) {
2016-05-27 22:49:16 +00:00
var $els = $(elements);
var $buttons = {
all: $els.find('.example-todo-button-all'),
incomplete: $els.find('.example-todo-button-incomplete'),
complete: $els.find('.example-todo-button-complete')
};
Object.keys($buttons).forEach(function (k) {
$buttons[k].on('click', function () {
state.filter = k;
render();
});
});
},
render: function (elements, domainObject, state) {
var $els = $(elements);
var tasks = domainObject.getModel().tasks;
var $message = $els.find('.example-message');
2016-05-27 23:46:06 +00:00
var $list = $els.find('.example-todo-task-list');
2016-05-27 22:49:16 +00:00
var $buttons = {
all: $els.find('.example-todo-button-all'),
incomplete: $els.find('.example-todo-button-incomplete'),
complete: $els.find('.example-todo-button-complete')
};
var filters = {
all: function (task) {
return true;
},
incomplete: function (task) {
return !task.completed;
},
complete: function (task) {
return task.completed;
}
};
2016-05-27 22:31:54 +00:00
2016-05-27 22:49:16 +00:00
Object.keys($buttons).forEach(function (k) {
$buttons[k].toggleClass('selected', state.filter === k);
});
tasks = tasks.filter(filters[state.filter]);
2016-05-27 22:31:54 +00:00
2016-05-27 22:49:16 +00:00
$list.empty();
tasks.forEach(function (task) {
var $taskEls = $(taskTemplate);
$taskEls.find('.example-task-checked')
.prop('checked', task.completed);
$taskEls.find('.example-task-description')
.text(task.description);
2016-05-27 23:46:06 +00:00
$list.append($taskEls);
2016-05-27 22:49:16 +00:00
});
$message.toggle(tasks.length < 1);
}
});
2016-05-27 22:31:54 +00:00
view.model(domainObject);
return view;
});
2016-05-27 20:17:16 +00:00
mct.type('example.todo', todoType);
return mct;
};
});