-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue.js
212 lines (200 loc) · 6.61 KB
/
vue.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var list = [{
priority:1,
title:"Fix bootstrap issues",
date:"2021-06-21",
checked:false
},{
priority:1,
title:"Make Mobile Friendly",
date:"2021-06-21",
checked:true
},{
priority:3,
title:"Add Readme.md",
date:"2021-06-19",
checked:true
}];
Vue.component("todo-item",{
props:['item', 'index'],
template:`
<div class="todo-item my-1 py-2 px-1">
<div class="row">
<div class="col-2 my-auto">
<div class="check-box float-end">
<input type="checkbox" class="checkbox-round" @change="$emit('toggle-check', index)" :checked="item.checked">
</div>
</div>
<div class="col-8">
<div class="todo-title">
<strike @click="$emit('toggle-check', index)" v-if="item.checked">{{item.title}}</strike>
<span @click="$emit('toggle-check', index)" v-if="!item.checked">{{item.title}}</span>
</div>
<span class="todo-date">{{item.date}}</span>
<span class="delete-button" @click="$emit('del', index)">
<i class="fa fa-times"></i>
</span>
</div>
<div class="col-2 priority my-auto">
{{item.priority}}
</div>
</div>
</div>
`
});
var app = new Vue({
el:"#app",
data:{
todos:[],
newTitle:"",
newDate:"",
newPriority:4,
sortByPriorityFlag:true,
openForm: false,
urlInput:"",
urlError: ""
},
mounted() {
if (localStorage.getItem('todoList')) {
try {
this.todos = JSON.parse(localStorage.getItem('todoList'));
} catch(e) {
localStorage.removeItem('todoList');
this.todos = list;
}
}else{
this.todos = list;
}
if (localStorage.getItem('themeURL')){
this.setUrlTheme(localStorage.getItem('themeURL'));
}
},
methods:{
addTodo: function(){
var newTodo = {
priority:this.newPriority,
title:this.newTitle,
date:this.newDate,
checked:false
}
this.todos.push(newTodo);
this.saveTodos();
this.newTitle = "";
this.newDate = "";
this.newPriority = 4;
this.sortList();
this.openForm = false;
},
saveTodos: function(){
var parsed = JSON.stringify(this.todos);
localStorage.setItem('todoList', parsed);
},
toggleCheck: function(index){
this.todos[index].checked = !this.todos[index].checked;
this.sortList();
this.saveTodos();
},
del: function(index){
this.todos.splice(index, 1);
this.saveTodos();
},
sortList: function(){
this.sortByPriorityFlag ? this.sortByPriority() : this.sortByDate();
this.saveTodos();
},
sortByPriority: function(){
this.todos.sort(function(a, b){
if(a.checked == b.checked){
return a.priority == b.priority ? compareDates(a.date, b.date) : a.priority - b.priority;
}else{
return !a.checked?-1:1;
}
});
},
sortByDate: function(){
this.sortByPriorityFlag = false;
this.todos.sort(function(a, b){
if(a.checked == b.checked){
return compareDates(a.date, b.date) == 0 ? a.priority - b.priority : compareDates(a.date, b.date);
}else{
return !a.checked?-1:1;
}
});
},
clearTodos: function(){
this.todos = [];
this.saveTodos();
},
urlInputFunc: function(){
if(this.urlInput.length != 0){
if(!verifyUrl(this.urlInput)){
this.urlError = "Invalid URL";
} else {
this.urlError = "";
}
} else {
this.urlError = "";
}
},
enterPath: function(){
if(this.urlInput){
localStorage.setItem("themeURL", this.urlInput);
localStorage.removeItem("themeB64");
this.setUrlTheme(this.urlInput);
} else {
var file = document.querySelector("#fileEnter").files[0];
reader = new FileReader();
reader.onloadend = function() {
var b64 = reader.result;
localStorage.setItem("themeB64", b64);
localStorage.removeItem("themeURL");
setB64Theme(b64);
}
reader.readAsDataURL(file);
}
},
setUrlTheme: function(URL){
var v = new Vibrant(URL);
v.getPalette().then((palette) => {
document.documentElement.style.setProperty('--accent', palette["DarkVibrant"].hex);
document.documentElement.style.setProperty('--todoItem', palette["DarkVibrant"].hex+"c0");
document.documentElement.style.setProperty('--bg', URL);
var bodyS = document.getElementsByTagName('body')[0].style;
bodyS.background = 'url('+v._src+') no-repeat center center fixed';
bodyS["-webkit-background-size"] = "cover";
});
},
setB64Theme: function(b64){
var decodedByte = Base64.decode(b64);
}
}
});
function compareDates(a, b){
var year_a = a.substring(0,4);
var year_b = b.substring(0,4);
if(year_a === year_b){
var month_a = a.substring(5,7);
var month_b = b.substring(5,7);
if(month_a === month_b){
var date_a = a.substring(8);
var date_b = b.substring(8);
if(date_a === date_b){
return 0;
}else{
return date_a - date_b;
}
}else{
return month_a - month_b;
}
}else{
return year_a - year_b;
}
}
function verifyUrl(path){
var pattern = new RegExp('^(https?:\\/\\/)?'+
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|'+
'((\\d{1,3}\\.){3}\\d{1,3}))'+
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+
'(\\?[;&a-z\\d%_.~+=-]*)?'+
'(\\#[-a-z\\d_]*)?$','i');
return pattern.test(path);
}