This repository was archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathtodo.dart
107 lines (88 loc) · 3.06 KB
/
todo.dart
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
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the COPYING file.
// This is a port of "A Simple ToDo List Using HTML5 IndexedDB" to Dart.
// See: http://www.html5rocks.com/en/tutorials/indexeddb/todo/
import 'dart:html';
import 'dart:indexed_db' as idb;
import 'dart:async';
class TodoList {
static final String _TODOS_DB = "todo";
static final String _TODOS_STORE = "todos";
idb.Database _db;
int _version = 2;
InputElement _input;
Element _todoItems;
TodoList() {
_todoItems = querySelector('#todo-items');
_input = querySelector('#todo');
querySelector('input#submit').onClick.listen((e) => _onAddTodo());
}
Future open() {
return window.indexedDB.open(_TODOS_DB, version: _version,
onUpgradeNeeded: _onUpgradeNeeded)
.then(_onDbOpened)
.catchError(_onError);
}
void _onError(e) {
// Get the user's attention for the sake of this tutorial. (Of course we
// would *never* use window.alert() in real life.)
window.alert('Oh no! Something went wrong. See the console for details.');
window.console.log('An error occurred: {$e}');
}
void _onDbOpened(idb.Database db) {
_db = db;
_getAllTodoItems();
}
void _onUpgradeNeeded(idb.VersionChangeEvent e) {
idb.Database db = (e.target as idb.OpenDBRequest).result;
if (!db.objectStoreNames.contains(_TODOS_STORE)) {
db.createObjectStore(_TODOS_STORE, keyPath: 'timeStamp');
}
}
void _onAddTodo() {
var value = _input.value.trim();
if (value.length > 0) {
_addTodo(value);
}
_input.value = '';
}
Future _addTodo(String text) {
var trans = _db.transaction(_TODOS_STORE, 'readwrite');
var store = trans.objectStore(_TODOS_STORE);
return store.put({
'text': text,
'timeStamp': new DateTime.now().millisecondsSinceEpoch.toString()
}).then((_) => _getAllTodoItems())
.catchError((e) => _onError);
}
void _deleteTodo(String id) {
var trans = _db.transaction(_TODOS_STORE, 'readwrite');
var store = trans.objectStore(_TODOS_STORE);
var request = store.delete(id);
request.then((e) => _getAllTodoItems(), onError: _onError);
}
void _getAllTodoItems() {
_todoItems.nodes.clear();
var trans = _db.transaction(_TODOS_STORE, 'readwrite');
var store = trans.objectStore(_TODOS_STORE);
// Get everything in the store.
var request = store.openCursor(autoAdvance:true).listen((cursor) {
_renderTodo(cursor.value);
}, onError: _onError);
}
void _renderTodo(Map todoItem) {
var textDisplay = new Element.tag('span');
textDisplay.text = todoItem['text'];
var deleteControl = new Element.tag('a');
deleteControl.text = '[Delete]';
deleteControl.onClick.listen((e) => _deleteTodo(todoItem['timeStamp']));
var item = new Element.tag('li');
item.nodes.add(textDisplay);
item.nodes.add(deleteControl);
_todoItems.nodes.add(item);
}
}
void main() {
new TodoList().open();
}