openmct/tutorials/todo/todo.js

222 lines
7.5 KiB
JavaScript
Raw Normal View History

2016-05-27 22:31:54 +00:00
define([
"text!./todo.html",
"text!./todo-task.html",
2016-06-17 18:43:18 +00:00
"text!./todo-toolbar.html",
2016-06-17 21:18:51 +00:00
"text!./todo-dialog.html",
"../../src/api/objects/object-utils",
2016-05-27 22:31:54 +00:00
"zepto"
], function (todoTemplate, taskTemplate, toolbarTemplate, dialogTemplate, utils, $) {
2016-05-27 22:31:54 +00:00
/**
* @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-28 00:08:04 +00:00
function TodoView(domainObject) {
2016-06-17 18:16:08 +00:00
this.domainObject = domainObject;
this.mutableObject = undefined;
2016-05-28 00:08:04 +00:00
this.filterValue = "all";
this.render = this.render.bind(this);
this.objectChanged = this.objectChanged.bind(this);
2016-06-17 18:16:08 +00:00
}
2016-05-28 00:08:04 +00:00
TodoView.prototype.objectChanged = function (object) {
if (this.mutableObject) {
this.mutableObject.stopListening();
}
this.mutableObject = mct.Objects.getMutable(object);
this.render();
//If anything on object changes, re-render view
this.mutableObject.on("*", this.objectChanged);
};
2016-06-17 18:16:08 +00:00
TodoView.prototype.show = function (container) {
var self = this;
2016-06-17 21:21:37 +00:00
this.destroy();
self.$els = $(todoTemplate);
self.$buttons = {
all: self.$els.find('.example-todo-button-all'),
incomplete: self.$els.find('.example-todo-button-incomplete'),
complete: self.$els.find('.example-todo-button-complete')
};
2016-06-17 18:16:08 +00:00
$(container).empty().append(self.$els);
2016-06-17 21:21:37 +00:00
self.initialize();
self.objectChanged(this.domainObject);
mct.selection.on('change', self.render);
2016-06-17 18:16:08 +00:00
};
2016-05-28 00:08:04 +00:00
2016-06-17 18:16:08 +00:00
TodoView.prototype.destroy = function () {
if (this.mutableObject) {
this.mutableObject.stopListening();
2016-06-17 21:21:37 +00:00
}
mct.selection.off('change', this.render);
2016-06-17 18:16:08 +00:00
};
2016-05-28 00:08:04 +00:00
TodoView.prototype.setFilter = function (value) {
this.filterValue = value;
this.render();
};
TodoView.prototype.initialize = function () {
Object.keys(this.$buttons).forEach(function (k) {
this.$buttons[k].on('click', this.setFilter.bind(this, k));
}, this);
};
TodoView.prototype.render = function () {
2016-06-17 18:16:08 +00:00
var $els = this.$els;
var mutableObject = this.mutableObject;
var tasks = mutableObject.get('tasks');
2016-05-28 00:08:04 +00:00
var $message = $els.find('.example-message');
var $list = $els.find('.example-todo-task-list');
var $buttons = this.$buttons;
var filters = {
all: function () {
return true;
2016-05-27 22:49:16 +00:00
},
2016-05-28 00:08:04 +00:00
incomplete: function (task) {
return !task.completed;
2016-05-27 22:49:16 +00:00
},
2016-05-28 00:08:04 +00:00
complete: function (task) {
return task.completed;
}
};
var filterValue = this.filterValue;
var selected = mct.selection.selected();
2016-05-27 22:31:54 +00:00
2016-05-28 00:08:04 +00:00
Object.keys($buttons).forEach(function (k) {
$buttons[k].toggleClass('selected', filterValue === k);
});
tasks = tasks.filter(filters[filterValue]);
2016-05-27 23:55:10 +00:00
2016-05-28 00:08:04 +00:00
$list.empty();
tasks.forEach(function (task, index) {
var $taskEls = $(taskTemplate);
var $checkbox = $taskEls.find('.example-task-checked');
var $desc = $taskEls.find('.example-task-description');
2016-05-28 00:08:04 +00:00
$checkbox.prop('checked', task.completed);
$desc.text(task.description);
2016-05-27 23:55:10 +00:00
2016-05-28 00:08:04 +00:00
$checkbox.on('change', function () {
var checked = !!$checkbox.prop('checked');
mutableObject.set("tasks." + index + ".completed", checked);
2016-05-28 00:08:04 +00:00
});
2016-05-27 22:49:16 +00:00
$desc.on('click', function () {
mct.selection.clear();
mct.selection.select({ index: index });
});
if (selected.length > 0 && selected[0].index === index) {
$desc.addClass('selected');
}
2016-05-28 00:08:04 +00:00
$list.append($taskEls);
2016-05-27 22:49:16 +00:00
});
2016-05-28 00:08:04 +00:00
$message.toggle(tasks.length < 1);
};
2016-06-17 18:43:18 +00:00
function TodoToolbarView(domainObject) {
this.domainObject = domainObject;
this.mutableObject = undefined;
this.handleSelectionChange = this.handleSelectionChange.bind(this);
2016-06-17 18:43:18 +00:00
}
TodoToolbarView.prototype.show = function (container) {
var self = this;
this.destroy();
self.mutableObject = mct.Objects.getMutable(this.domainObject);
var $els = $(toolbarTemplate);
var $add = $els.find('a.example-add');
var $remove = $els.find('a.example-remove');
$(container).append($els);
$add.on('click', function () {
var $dialog = $(dialogTemplate),
view = {
show: function (container) {
$(container).append($dialog);
},
destroy: function () {}
};
mct.dialog(view, "Add a Task").then(function () {
var description = $dialog.find('input').val();
var tasks = self.mutableObject.get('tasks');
tasks.push({ description: description });
self.mutableObject.set('tasks', tasks);
});
2016-06-17 20:51:15 +00:00
});
$remove.on('click', function () {
var index = mct.selection.selected()[0].index;
if (index !== undefined) {
var tasks = self.mutableObject.get('tasks').filter(function (t, i) {
return i !== index;
});
self.mutableObject.set("tasks", tasks);
self.mutableObject.set("selected", undefined);
mct.selection.clear();
}
});
self.$remove = $remove;
self.handleSelectionChange();
mct.selection.on('change', self.handleSelectionChange);
2016-06-17 18:43:18 +00:00
};
TodoToolbarView.prototype.handleSelectionChange = function () {
var selected = mct.selection.selected();
if (this.$remove) {
this.$remove.toggle(selected.length > 0);
}
};
2016-06-17 18:43:18 +00:00
TodoToolbarView.prototype.destroy = function () {
mct.selection.off('change', this.handleSelectionChange);
this.$remove = undefined;
if (this.mutableObject) {
this.mutableObject.stopListening();
}
2016-06-17 18:43:18 +00:00
};
2016-05-27 20:17:16 +00:00
mct.type('example.todo', todoType);
mct.view(mct.regions.main, {
view: function (domainObject) {
return new TodoView(domainObject);
},
canView: todoType.check.bind(todoType)
2016-06-17 17:53:56 +00:00
});
mct.view(mct.regions.toolbar, {
view: function (domainObject) {
return new TodoToolbarView(domainObject);
},
canView: todoType.check.bind(todoType)
2016-06-17 18:43:18 +00:00
});
2016-05-27 20:17:16 +00:00
return mct;
};
});