-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomponents.js
140 lines (137 loc) · 3.6 KB
/
components.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const newForm = Vue.component('add-new-form',{
data(){
return {
itemText: '',
placeholderText: [
'Do the thing!',
'Take a nap!',
'Eat some food',
'Burn off that food',
'Make that thing!',
'Write it down!',
'What needs fixing',
'What wouldst thou deau?',
],
placeholderToShow: null,
}
},
mounted(){
this.pickNewPlaceholder();
},
template: `
<div>
<input
class="newItemInput"
:placeholder="placeholderText[placeholderToShow]"
v-model="itemText"
@keydown.enter="addNewTodo" />
<button
class="newItemButton"
type="button"
@click="addNewTodo">
Add
</button>
</div>
`,
methods:{
pickNewPlaceholder(){
this.placeholderToShow = Math.floor(Math.random()*this.placeholderText.length);
},
addNewTodo(){
if(this.itemText.trim() == '') return;
this.$emit("new-todo-item", this.itemText);
this.itemText = '';
this.pickNewPlaceholder();
}
}
});
const todoItem = Vue.component('todo-item', {
props: ['todo'],
data(){
return{
mouseIn: false,
isEditText: false,
deleteStatus: 0,
timer: null,
newText: '',
}
},
mounted(){
this.newText = this.todo.text;
},
computed: {
colorHash(){
//awesome hash function I found!
var hash = 0;
if (this.newText.length == 0) return hash;
for(char in this.newText.split('')){
hash = 1.5*char.charCodeAt(0) + ((hash << 3) - hash);
hash = hash & hash;
}
return `hsl(${hash % 360},${this.todo.completed==null?85:25}%,70%)`;
},
isDone(){
return this.todo.completed != null;
}
},
template: `
<div class="todoItem"
@mouseover="mouseIn = true"
@mouseout="mouseIn=false"
:style="{backgroundColor:colorHash, color: isDone?'grey':'black'}">
<div :class="{todoItemText: true, todoItemDone: isDone}">
<div style="padding: 4px 5px;"
@dblclick="showEdit"
v-show="!isEditText">
{{todo.text}}
</div>
<textarea type="text" class="editTodoTextInput"
style="width:100%; padding: 0px 5px; margin: 4px 0px;"
ref="editTodoText"
v-model="newText"
@keydown.enter="updateValue"
@blur="updateValue"
v-show="isEditText"/>
</div>
<div class="todoItemActions">
<span class="todoItemCheck" v-show="!isDone && mouseIn" @click="completeMe">
<i class="material-icons">check</i>
</span>
<span v-show="isDone && mouseIn" @click="deleteMe">
<i :class="{'material-icons':true, 'deleteWarn':deleteStatus==1}">delete</i>
</span>
</div>
</div>
`,
methods:{
showEdit(){
this.isEditText=true;
this.$nextTick(() => {
this.$refs.editTodoText.focus();
this.$refs.editTodoText.style.height = this.$refs.editTodoText.scrollHeight+"px";
});
},
updateValue(){
if(!this.isEditText) return;
this.isEditText = false;
this.todo.text = this.newText;
this.$emit('update-todo', this.todo);
},
completeMe(){
this.todo.completed = true;
this.$emit('update-todo', this.todo);
},
deleteMe(){
clearTimeout(this.timer);
if(this.deleteStatus == 1){
this.deleteStatus = 0;
this.$emit('delete-todo', this.todo.id);
}else{
this.timer = setTimeout(() => {
this.deleteStatus = 0;
}, 800);
this.deleteStatus = 1;
}
}
},
});