-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
108 lines (106 loc) · 3.44 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var app = new Vue({
el: '#app',
components: {
'task': {
props: ['task'],
template:`
<div class="ui segment task"
v-bind:class="task.completed? 'done' : 'todo'" >
<div class="ui grid">
<div class="left floated twelve wide column">
<div class="ui checkbox">
<input type="checkbox" name="task" v-on:click="app.toggleDone($event, task.id)" :checked="task.completed" >
<label>{{ task.name }} <span class="description">{{ task.description }}</span> </label>
</div>
</div>
<div class="right floated three wide column">
<i class="icon pencil blue" alt="Edit" v-on:click="app.editTask($event, task.id)"></i>
<i class="icon trash red" alt="Delete" v-on:click="app.deleteTask($event, task.id)"></i>
</div>
</div>
</div>
`
}
},
data: {
tasks: [
{ id: 1, name: 'Todo 1', description: 'This is a todo', completed: false },
{ id: 2, name: 'Todo 2', description: 'This is a todo', completed: false },
{ id: 3, name: 'Todo 3', description: 'This is a completed todo', completed: true },
{ id: 4, name: 'Todo 4', description: 'This is a completed todo', completed: true },
],
task: {},
message: '',
action: 'create'
},
computed: {
completedTasks: function(){
return this.tasks.filter(item => item.completed == true)
},
todoTasks: function(){
return this.tasks.filter(item => item.completed == false )
},
nextId: function(){
return (this.tasks.sort(function(a,b){ return a.id - b.id; } ))[this.tasks.length - 1].id + 1;
}
},
methods: {
clear: function(){
this.task = {};
this.action = 'create';
this.message = ''
},
toggleDone: function(event, id){
event.stopImmediatePropagation();
let task = this.tasks.find(item => item.id == id);
if (task) {
task.completed = !task.completed;
console.log('task toggled')
this.message = `Task ${id} has been completed`
}
},
createTask: function(event){
if (!this.task.completed) {
this.task.completed = false;
}else {
this.task.complete = true;
}
let taskId = this.nextId;
this.task.id = taskId;
let newTask = Object.assign({}, this.task);
this.tasks.push(newTask)
this.clear();
this.message = `Task has been created`
},
editTask: function(event, id){
this.action = 'edit';
let task = this.tasks.find(item => item.id == id);
if (task) {
this.task = { id: id,
name: task.name,
description: task.description,
completed: task.completed
};
}
},
deleteTask: function(event, id){
event.stopImmediatePropagation();
let taskIndex = this.tasks.findIndex(item => item.id == id);
if(taskIndex > -1){
this.$delete(this.tasks, taskIndex);
}
this.message = `Task ${id} has been deleted`
console.log("task deleted")
},
updateTask: function(event, id){
event.stopImmediatePropagation();
let task = this.tasks.find(item => item.id == id);
if (task) {
task.name = this.task.name;
task.description = this.task.description;
task.completed = this.task.completed;
this.message = `Task ${id} has been updated`
}
}
}
})