-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
287 lines (253 loc) · 7.28 KB
/
Copy pathmain.js
File metadata and controls
287 lines (253 loc) · 7.28 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// ★STEP2
// https://jp.vuejs.org/v2/examples/todomvc.html
var STORAGE_KEY = 'todos-vuejs-demo'
var STORAGE_KEY_BUTTONS = 'buttons-vuejs-demo'
var todoStorage = {
fetch: function () {
var todos = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]')
todos.forEach(function (todo, index) {
todo.id = index
})
todoStorage.uid = todos.length
return todos
},
save: function (todos, buttons) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos))
localStorage.setItem(STORAGE_KEY_BUTTONS, JSON.stringify(buttons))
},
/*
fetchButtons: function () {
var buttons = JSON.parse(localStorage.getItem(STORAGE_KEY_BUTTONS) || '[]')
return buttons
},
save: function (todos, buttons) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos))
localStorage.setItem(STORAGE_KEY_BUTTONS, JSON.stringify(buttons))
}
*/
}
var registerDate = {
getDate: function(){
var d = new Date();
var formatted = `
${d.getFullYear()}/
${d.getMonth()+1}/${d.getDate()}
${d.getHours()}:${d.getMinutes()}
`.replace(/\n|\r/g, '');
return formatted
}
}
// 消費期限を設定
// 現在時刻に保存期間を追加
var Deadline = {
getDate: function(date){
var d = new Date();
var formatted = `
${d.getFullYear()}/
${d.getMonth()+1}/${d.getDate()}
${d.getHours()}:${d.getMinutes()+1}
`.replace(/\n|\r/g, '');
var lastd = new Date(d.getFullYear(), d.getMonth(), 0);
if ((d.getDate()+date) > lastd.getDate()){
var formatted = `
${d.getFullYear()}/${d.getMonth()+2}/${(d.getDate()+date) - lastd.getDate()}
${d.getHours()}:${d.getMinutes()}
`.replace(/\n|\r/g, '');
} else {
var formatted = `
${d.getFullYear()}/${d.getMonth()+1}/${d.getDate()+date}
${d.getHours()}:${d.getMinutes()}
`.replace(/\n|\r/g, '');
}
return formatted
}
}
//デモ用Deadline関数
var DeadlineDemo = {
getDate: function(date){
var d = new Date();
var formatted = `
${d.getFullYear()}/${d.getMonth()+1}/${d.getDate()}
${d.getHours()}:${d.getMinutes()+date}
`.replace(/\n|\r/g, '');
return formatted
}
}
// 現在時刻と消費期限を比較
function comTime(deadline){
var d = new Date();
var dead = new Date(deadline)
console.log(dead);
console.log(d);
console.log(dead <= d);
return dead <= d
}
//自動状態変更のためにタイマーを起動
function autoChange() {
for (var i in vm._data.todos){
console.log(vm._data.todos[i]);
vm.doTimer(vm._data.todos[i])
}
}
//リロードされてもタイマーを起動
window.onload = function(){
autoChange();
}
// ★STEP1
let vm = new Vue({
el: '#app',
data: {
// ★STEP5 localStorage から 取得した ToDo のリスト
todos: [],
buttons: [],
// ★STEP11 抽出しているToDoの状態
current: -1,
// ★STEP11&STEP13 各状態のラベル
options: [
{ value: -1, label: 'すべて' },
{ value: 0, label: '新鮮' },
{ value: 1, label: '腐敗' }
]
},
computed: {
// ★STEP12
computedTodos: function () {
return this.todos.filter(function (el) {
return this.current < 0 ? true : this.current === el.state
}, this)
},
// ★STEP13 新鮮・腐敗のラベルを表示する
labels() {
return this.options.reduce(function (a, b) {
return Object.assign(a, { [b.value]: b.label })
}, {})
// キーから見つけやすいように、次のように加工したデータを作成
// {0: '新鮮', 1: '腐敗', -1: 'すべて'}
}
},
// ★STEP8
watch: {
// オプションを使う場合はオブジェクト形式にする
todos: {
// 引数はウォッチしているプロパティの変更後の値
handler: function () {
todoStorage.save(this.todos, this.buttons)
},
// deep オプションでネストしているデータも監視できる
deep: true
},
buttons: {
handler: function () {
todoStorage.save(this.todos, this.buttons)
},
deep: true
}
},
// ★STEP9
created() {
// インスタンス作成時に自動的に fetch() する
this.todos = todoStorage.fetch()
this.buttons = todoStorage.fetchButtons()
},
methods: {
// ★STEP7 ToDo 追加の処理
doAdd: function(event, value) {
// ref で名前を付けておいた要素を参照
//コメントの内容
var comment = this.$refs.comment
// 入力がなければ何もしないで return
if (!comment.value.length) {
return
}
// { 新しいID, コメント, 作業状態 }
// というオブジェクトを現在の todos リストへ push
// 作業状態「state」はデフォルト「新鮮=0」で作成
this.todos.push({
id: todoStorage.uid++,
comment: comment.value,
date: registerDate.getDate(),
//ここで任意の消費期限の設定
deadline: Deadline.getDate(i),
state: 0
})
// フォーム要素を空にする
comment.value = ''
},
addItem: function(itemname, i) {
// { 新しいID, コメント, 作業状態 }
// というオブジェクトを現在の todos リストへ push
// 作業状態「state」はデフォルト「新鮮=0」で作成
this.todos.push({
id: todoStorage.uid++,
comment: itemname,
date: registerDate.getDate(),
//ここで任意の消費期限の設定
deadline: Deadline.getDate(i),
state: 0
})
/*
console.log(this.todos)
// フォーム要素を空にする
comment.value = ''
*/
autoChange();
},
//デモ用addItem関数
addItemDemo: function(itemname, i) {
this.todos.push({
id: todoStorage.uid++,
comment: itemname,
date: registerDate.getDate(),
//ここで任意の消費期限の設定
deadline: DeadlineDemo.getDate(i),
state: 0
})
autoChange();
},
// 食材ボタンの追加
addButton: function() {
var buttonName = this.$refs.buttonName
var buttonDate = this.$refs.buttonDate
if (!buttonName.value.length) {
return
}
if (!buttonDate.value.length) {
return
}
this.buttons.push({
name: buttonName.value,
date: Number(buttonDate.value),
})
buttonName.value = ''
buttonDate.value = ''
},
// ボタン削除
// ★STEP10 状態変更の処理
doChangeState: function (item) {
if (item.state==0){
item.state = 1;
}
},
// ★STEP10 削除の処理
doRemove: function (item) {
var index = this.todos.indexOf(item)
this.todos.splice(index, 1)
},
// 消費期限になったら通知
doTimer: function (item) {
const intervalTime = setInterval(() =>{
if(comTime(item.deadline)){
if(item.state == 0)
{
this.doChangeState(item);
alert(item.date + 'に追加した' + item.comment + 'が腐りました');
}
clearInterval(intervalTime);
}
},1000);
},
// doPrint: function(item) {
// console.log(item.deadline);
// this.doTimer(item);
// }
} })