1+ import Mikado from "../node_modules/mikado/dist/mikado.bundle.module.min.js" ;
2+ import { todos } from "./app.js" ;
3+ import { updateApp } from "./controller.js" ;
4+ import { $ } from "./helper.js" ;
5+
6+ /**@enum {Number} */
7+ const KEYS = { ENTER :13 , ESC :27 } ;
8+
9+ Mikado . route ( "new-todo" , function ( target , event ) {
10+
11+ switch ( event . which || event . keyCode ) {
12+
13+ case KEYS . ENTER :
14+
15+ const value = target . value . trim ( ) ;
16+
17+ if ( value ) {
18+
19+ todos . push ( {
20+ "id" :todos . length ,
21+ "title" :value ,
22+ "completed" :false
23+ } ) ;
24+
25+ updateApp ( ) ;
26+ }
27+
28+ //fallthrough:
29+
30+ case KEYS . ESC :
31+
32+ target . value = "" ;
33+ }
34+ } )
35+
36+ . route ( "enter-edit-mode" , function ( target , event ) {
37+
38+ const input = $ ( "input.edit" , target ) ;
39+ const self = event . target ;
40+
41+ Mikado . setClass ( target , "editing" ) ;
42+ input . value = Mikado . getText ( self ) ;
43+ input . focus ( ) ;
44+ } )
45+
46+ . route ( "edit" , function ( target , event ) {
47+
48+ switch ( event . which || event . keyCode || KEYS . ESC ) {
49+
50+ case KEYS . ENTER :
51+
52+ const index = Mikado . getAttribute ( target , "data-index" ) ;
53+ const self = event . target ;
54+ const value = self . value . trim ( ) ;
55+
56+ // when empty or changed
57+ if ( ! value || todos [ index ] . title !== value ) {
58+
59+ if ( value ) {
60+
61+ todos [ index ] . title = value ;
62+ }
63+ else {
64+
65+ todos . splice ( index , 1 ) ;
66+ }
67+
68+ updateApp ( ) ;
69+ }
70+
71+ //fallthrough:
72+
73+ case KEYS . ESC :
74+
75+ Mikado . setClass ( target , "" ) ;
76+ }
77+ } )
78+
79+ . route ( "update-state" , function ( target , event ) {
80+
81+ const index = Mikado . getAttribute ( target , "data-index" ) ;
82+ todos [ index ] . completed = event . target . checked ;
83+ updateApp ( ) ;
84+ } )
85+
86+ . route ( "clear-completed" , function ( ) {
87+
88+ for ( let i = 0 ; i < todos . length ; i ++ ) {
89+
90+ todos [ i ] . completed &&
91+ todos . splice ( i -- , 1 ) ;
92+ }
93+
94+ updateApp ( ) ;
95+ } )
96+
97+ . route ( "toggle-all" , function ( target ) {
98+
99+ todos . forEach ( todo => todo . completed = target . checked ) ;
100+ updateApp ( ) ;
101+ } )
102+
103+ . route ( "delete" , function ( target ) {
104+
105+ const index = Mikado . getAttribute ( target , "data-index" ) ;
106+ todos . splice ( index , 1 ) ;
107+ updateApp ( ) ;
108+ } ) ;