-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
todomvc.view.ts
134 lines (106 loc) · 3.11 KB
/
todomvc.view.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
interface $hyoo_todomvc_task {
completed? : boolean
title? : string
}
namespace $.$$ {
export class $hyoo_todomvc extends $.$hyoo_todomvc {
task_ids( next? : number[] ) : number[] {
return this.$.$mol_state_local.value( this.state_key( 'mol-todos' ) , next ) || []
}
arg_completed() {
return this.$.$mol_state_arg.value( this.state_key( 'completed' ) )
}
@ $mol_mem
groups_completed() {
var groups : { [ index : string ] : number[] } = { 'true' : [] , 'false' : [] }
for( let id of this.task_ids() ) {
var task = this.task( id )!
groups[ String( task.completed ) ].push( id )
}
return groups
}
@ $mol_mem
task_ids_filtered() {
var completed = this.arg_completed()
if( completed ) {
return this.groups_completed()[ completed ] || []
} else {
return this.task_ids()
}
}
@ $mol_mem
completed_all( next? : boolean ) {
if( next === void 0 ) return this.groups_completed()[ 'false' ].length === 0
for( let id of this.groups_completed()[ String( !next ) ] ) {
var task = this.task( id )!
this.task( id , { title : task.title , completed : next } )
}
return next
}
head_complete_enabled() {
return this.task_ids().length > 0
}
@ $mol_mem
pending_message() {
let count = this.groups_completed()[ 'false' ].length
return ( count === 1 ) ? '1 item left' : `${count} items left`
}
@ $mol_mem
new_id() {
return Math.max( 1 , 1 + Math.max( ... this.task_ids() ) )
}
add( next? : Event ) {
var title = this.task_title_new()
if( !title ) return
var id = this.new_id()
var task = { completed : false , title }
this.task( id , task )
this.task_ids([ ... this.task_ids(), id ])
this.task_title_new( '' )
}
@ $mol_mem
task_rows() {
return this.task_ids_filtered().map( id => this.Task_row( id ) )
}
task( id : number , next? : $hyoo_todomvc_task | null ) {
const key = this.state_key( `mol-todos-${id}` )
if( next === void 0 ) {
return this.$.$mol_state_local.value<$hyoo_todomvc_task>( key ) || { title : '' , completed : false }
}
this.$.$mol_state_local.value( key , next )
return next || null
}
@ $mol_mem_key
task_completed( id : number , next? : boolean ) {
return this.task( id , next === undefined ? undefined : { ... this.task( id ) , completed : next } )!.completed ?? false
}
@ $mol_mem_key
task_title( id : number , next? : string ) {
return this.task( id , next === undefined ? undefined : { ... this.task( id ) , title : next } )!.title ?? ''
}
task_drop( id : number , next? : Event ) {
this.task( id , null )
this.task_ids( this.task_ids().filter( id2 => id !== id2 ) )
}
sweep() {
this.task_ids( this.task_ids().filter( id => {
if( !this.task( id )!.completed ) return true
this.task( id , null )
return false
} ) )
}
panels() {
return [
this.Head() ,
this.List() ,
... this.foot_visible() ? [ this.Foot() ] : [] ,
]
}
foot_visible() {
return this.task_ids().length > 0
}
sweep_enabled() {
return this.groups_completed()[ 'true' ].length > 0
}
}
}