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",
|
2016-07-07 21:30:45 +00:00
|
|
|
"../../src/api/objects/object-utils",
|
2016-05-27 22:31:54 +00:00
|
|
|
"zepto"
|
2016-07-07 21:30:45 +00:00
|
|
|
], 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;
|
2016-07-07 21:30:45 +00:00
|
|
|
this.mutableObject = undefined;
|
|
|
|
|
2016-05-28 00:08:04 +00:00
|
|
|
this.filterValue = "all";
|
2016-07-07 21:30:45 +00:00
|
|
|
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
|
|
|
|
2016-07-07 21:30:45 +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) {
|
2016-07-07 21:30:45 +00:00
|
|
|
var self = this;
|
2016-06-17 21:21:37 +00:00
|
|
|
this.destroy();
|
|
|
|
|
2016-08-11 22:29:46 +00:00
|
|
|
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
|
|
|
|
2016-08-11 22:29:46 +00:00
|
|
|
$(container).empty().append(self.$els);
|
2016-06-17 21:21:37 +00:00
|
|
|
|
2016-07-22 20:56:45 +00:00
|
|
|
|
2016-08-11 22:29:46 +00:00
|
|
|
self.initialize();
|
|
|
|
self.objectChanged(this.domainObject);
|
2016-07-22 20:56:45 +00:00
|
|
|
|
2016-08-11 22:29:46 +00:00
|
|
|
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 () {
|
2016-07-07 21:30:45 +00:00
|
|
|
if (this.mutableObject) {
|
|
|
|
this.mutableObject.stopListening();
|
2016-06-17 21:21:37 +00:00
|
|
|
}
|
2016-07-22 20:56:45 +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;
|
2016-07-07 21:30:45 +00:00
|
|
|
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;
|
2016-07-07 21:30:45 +00:00
|
|
|
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');
|
2016-07-07 21:30:45 +00:00
|
|
|
var $desc = $taskEls.find('.example-task-description');
|
2016-05-28 00:08:04 +00:00
|
|
|
$checkbox.prop('checked', task.completed);
|
2016-07-07 21:30:45 +00:00
|
|
|
$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');
|
2016-07-07 21:30:45 +00:00
|
|
|
mutableObject.set("tasks." + index + ".completed", checked);
|
2016-05-28 00:08:04 +00:00
|
|
|
});
|
2016-05-27 22:49:16 +00:00
|
|
|
|
2016-07-07 21:30:45 +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;
|
2016-07-07 21:30:45 +00:00
|
|
|
this.mutableObject = undefined;
|
|
|
|
this.handleSelectionChange = this.handleSelectionChange.bind(this);
|
2016-06-17 18:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TodoToolbarView.prototype.show = function (container) {
|
2016-07-07 21:30:45 +00:00
|
|
|
var self = this;
|
|
|
|
this.destroy();
|
|
|
|
|
2016-08-11 22:29:46 +00:00
|
|
|
self.mutableObject = mct.Objects.getMutable(this.domainObject);
|
2016-07-07 21:30:45 +00:00
|
|
|
|
2016-08-11 22:29:46 +00:00
|
|
|
var $els = $(toolbarTemplate);
|
|
|
|
var $add = $els.find('a.example-add');
|
|
|
|
var $remove = $els.find('a.example-remove');
|
2016-07-07 21:30:45 +00:00
|
|
|
|
2016-08-11 22:29:46 +00:00
|
|
|
$(container).append($els);
|
2016-07-07 21:30:45 +00:00
|
|
|
|
2016-08-11 22:29:46 +00:00
|
|
|
$add.on('click', function () {
|
|
|
|
var $dialog = $(dialogTemplate),
|
|
|
|
view = {
|
|
|
|
show: function (container) {
|
|
|
|
$(container).append($dialog);
|
|
|
|
},
|
|
|
|
destroy: function () {}
|
|
|
|
};
|
2016-07-07 21:30:45 +00:00
|
|
|
|
2016-08-11 22:29:46 +00:00
|
|
|
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-07-07 21:30:45 +00:00
|
|
|
});
|
2016-06-17 20:51:15 +00:00
|
|
|
});
|
2016-08-11 22:29:46 +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
|
|
|
};
|
|
|
|
|
2016-07-07 21:30:45 +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
|
|
|
|
2016-07-07 21:30:45 +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);
|
2016-07-20 20:46:03 +00:00
|
|
|
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
|
|
|
});
|
2016-07-20 20:46:03 +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;
|
|
|
|
};
|
|
|
|
});
|