forked from sveltejs/svelte-todomvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTodoMVCLogic.ts
112 lines (93 loc) · 2.97 KB
/
TodoMVCLogic.ts
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
//TODO use original @pyoner svelte-types as soon as this issue https://github.com/pyoner/svelte-types/issues/4 gone
import {Writable} from '@pyoner/svelte-types/types/svelte/interfaces';
import {derived, get, writable} from 'svelte/store';
const ENTER_KEY = 'Enter';
const ESCAPE_KEY = 'Escape';
export enum FilterStates {
all = 'all',
active = 'active',
completed = 'completed'
}
export let currentFilter:Writable<FilterStates> = writable(FilterStates.all);
interface TodoRecord {
id:string;
description:string;
completed:boolean;
}
type TodoRecords = TodoRecord[];
export const itemsStore:Writable<TodoRecords> = writable([]);
export let editingStore:Writable<number>=writable(NaN);
export const setCurrentFilter = (value:string)=>{
currentFilter.set( FilterStates.all);
if (value === '#/active') {
currentFilter.set( FilterStates.active);
} else if (value === '#/completed') {
currentFilter.set( FilterStates.completed);
}
}
export const clearCompleted = () => {
itemsStore.update(items => items.filter(item => !item.completed));
};
export function remove(index:number) {
itemsStore.update(items => items.slice(0, index).concat(items.slice(index + 1)));
}
export const setEditingIndex= (index:number) =>{
editingStore.set(index)
}
export function toggleAll(event:Event) {
itemsStore.update(items => items.map(item => ({
id: item.id,
description: item.description,
// TS2339: Property 'checked' does not exist on type 'EventTarget'.
//completed: event.target && (event.target).checked,
// TS7017: Element implicitly has an 'any' type because type 'EventTarget' has no index signature.
//completed: event.target && (event.target)['checked'],
completed: event.target && (event.target as any).checked
})));
}
export function createNew(event:KeyboardEvent) {
if (event.key === ENTER_KEY) {
// EventTarget -> HTMLInputElement
let target = event.target as any as HTMLInputElement;
itemsStore.update(items => items.concat({
id: uuid(),
description: target.value,
completed: false
}));
target.value = '';
console.log('createNew:', get(itemsStore));
}
}
export function handleEdit(event:KeyboardEvent) {
switch (event.key) {
case ENTER_KEY:
(event.target as any).blur();
break;
case ESCAPE_KEY:
editingStore.set(NaN);
break;
}
}
export function submit(event:Event) {
console.log('submit: editingStore', get(editingStore));
itemsStore.update(items =>
items.map((item, index) => {
if (index === get(editingStore)) {
return {
...item,
description:(event.target as any).value
}
} else {
return item;
}
}));
console.log('submit: after', get(itemsStore));
// @ts-ignore
editingStore.set(undefined);
}
function uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}