-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
203 lines (202 loc) · 5.72 KB
/
main.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
;(() => {
class IndexedDBHelper {
constructor (options) {
this._options = Object.assign({
name: 'todolist',
version: 1,
tables: [
{
name: 'demo',
primaryKey: 'id',
autoIncrement: true
}
]
}, options)
this.db = null
this.request = null
}
open () {
return new Promise((resolve, reject) => {
if (this.request) {
resolve()
return
}
this.request = window.indexedDB.open(this._options.name, this._options.version)
this.request.onerror = e => {
console.log('open error')
reject()
}
this.request.onsuccess = e => {
this.db = e.target.result
resolve()
}
this.request.onupgradeneeded = e => {
this.db = e.target.result
console.log(`DB version changed to ${this._options.version}`)
console.log(this)
this._options.tables.map(table => {
console.log(!this.db.objectStoreNames.contains(table.name))
if (!this.db.objectStoreNames.contains(table.name)) {
this.db.createObjectStore(table.name, {
autoIncrement: !!table.autoIncrement,
keyPath: table.primaryKey || 'id'
})
}
})
resolve()
}
})
}
close () {
this.request = null
this.db.close()
this.db = null
}
delete () {
this.request = null
this.db = null
window.indexedDB.deleteDatabase(this._options.name)
}
createTable (name, primaryKey = 'id') {
if (this.db.objectStoreNames.contains(name)) {
// 该表已存在
return
}
this.db.createObjectStore(name, {
keyPath: primaryKey
})
}
put (table, data) {
let transaction = this.db.transaction(table, 'readwrite')
let store = transaction.objectStore(table)
let req = store.put(data)
req.onerror = () => {
console.log('更新成功')
}
req.onsuccess = () => {
console.log('添加成功')
}
}
query (table, key = '') {
return new Promise((resolve, reject) => {
if (key === '') {
reject()
}
let transaction = this.db.transaction(table, 'readwrite')
let store = transaction.objectStore(table)
let req = store.get(key)
req.onerror = e => {
reject(e)
}
req.onsuccess = e => {
resolve(e.target.result)
}
})
}
}
class Todolist {
constructor () {
this.input = null
this.listContainer = null
this.listItems = []
this.items = []
this.itemTemplate = `
<li class="{{done}}">
{{text}}
<div class="options">
<i class="icon icon-delete" data-index="{{index}}"></i>
</div>
</li>
`
this.currDate = ''
this.dbHelper = null
this._init()
}
async _init () {
let date = new Date()
this.currDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
this.dbHelper = new IndexedDBHelper({
tables: [
{
name: 'tododata',
primaryKey: 'date'
}
]
})
await this.dbHelper.open()
let res = await this.dbHelper.query('tododata', this.currDate)
if (res) {
this.items = res.items
}
this.listContainer = document.querySelector('#todolist')
this.listContainer.addEventListener('click', e => {
let { target } = e
if (target.tagName.toLowerCase() === 'li') {
// 点击 li item
let index = target.querySelector('.icon-delete').getAttribute('data-index')
this._toggleItemStatus(index)
} else if (target.tagName.toLowerCase() === 'i' && /icon-delete/.test(target.className)) {
// 按下删除
let index = target.getAttribute('data-index')
this._handleDelete(index)
}
}, false)
try {
this.listItems = Array.from(this.listContainer.children)
} catch (err) {}
this.input = document.querySelector('input')
this.input.addEventListener('keyup', e => {
if (e.keyCode === 13) {
// 按下 enter 键
this._handleInput()
}
}, false)
this._renderItems()
}
_handleInput () {
let value = this.input.value.trim()
let repeat = this.items.some(_ => _.text === value)
if (repeat || value === '') {
// 重复事件 或 为空
return
}
this.items.unshift({
text: value,
done: false
})
this.input.value = ''
this._renderItems()
this._saveData()
}
_handleDelete (index) {
this.items.splice(index, 1)
this._renderItems()
this._saveData()
}
_toggleItemStatus (index) {
this.items[index].done = !this.items[index].done
this._renderItems()
this._saveData()
}
_renderItems () {
this.listContainer.innerHTML = ''
this.items.map((item, index) => {
this.listContainer.appendChild(this._convertNode(this.itemTemplate.replace('{{done}}', item.done ? 'done' : '')
.replace('{{text}}', `${index + 1}. ${item.text}`)
.replace('{{index}}', index)))
})
}
_convertNode (htmlText) {
let div = document.createElement('div')
div.innerHTML = htmlText
return div.children[0]
}
_saveData () {
this.dbHelper.put('tododata', {
date: this.currDate,
items: this.items
})
}
}
new Todolist()
})()