@@ -21,6 +21,7 @@ function update(action, model, data) {
21
21
// console.log(arguments)
22
22
// console.log(' - - - - - - - - - - - ');
23
23
var new_model = JSON . parse ( JSON . stringify ( model ) ) // "clone" the model
24
+
24
25
switch ( action ) {
25
26
case 'ADD' :
26
27
var last = ( typeof model . todos !== 'undefined' && model . todos . length > 0 )
@@ -106,14 +107,16 @@ function update(action, model, data) {
106
107
return ! item . done ; // only return items which are item.done = false
107
108
} ) ;
108
109
break ;
110
+ case 'ROUTE' :
111
+ new_model . hash = ( window && window . location && window . location . hash ) ?
112
+ window . location . hash : '#/' ;
113
+ break ;
109
114
default : // if action unrecognised or undefined,
110
115
return model ; // return model unmodified
111
116
} // see: https://softwareengineering.stackexchange.com/a/201786/211301
112
117
return new_model ;
113
118
}
114
119
115
-
116
-
117
120
/**
118
121
* `render_item` creates an DOM "tree" with a single Todo List Item
119
122
* using the "elmish" DOM functions (`li`, `div`, `input`, `label` and `button`)
@@ -181,7 +184,18 @@ function render_main (model, signal) {
181
184
label ( [ "for=toggle-all" ] , [ text ( "Mark all as complete" ) ] ) ,
182
185
ul ( [ "class=todo-list" ] ,
183
186
( model . todos && model . todos . length > 0 ) ?
184
- model . todos . map ( function ( item ) {
187
+ model . todos
188
+ . filter ( function ( item ) {
189
+ switch ( model . hash ) {
190
+ case '#/active' :
191
+ return ! item . done ;
192
+ case '#/completed' :
193
+ return item . done ;
194
+ default :
195
+ return item ;
196
+ }
197
+ } )
198
+ . map ( function ( item ) {
185
199
return render_item ( item , model , signal )
186
200
} ) : null
187
201
) // </ul>
@@ -229,13 +243,25 @@ function render_footer (model, signal) {
229
243
] ) ,
230
244
ul ( [ "class=filters" ] , [
231
245
li ( [ ] , [
232
- a ( [ "href=#/" , "class=selected" ] , [ text ( "All" ) ] )
246
+ a ( [
247
+ "href=#/" , "id=all" , "class=" +
248
+ ( model . hash === '#/' ? "selected" : '' )
249
+ ] ,
250
+ [ text ( "All" ) ] )
233
251
] ) ,
234
252
li ( [ ] , [
235
- a ( [ "href=#/active" ] , [ text ( "Active" ) ] )
253
+ a ( [
254
+ "href=#/active" , "id=active" , "class=" +
255
+ ( model . hash === '#/active' ? "selected" : '' )
256
+ ] ,
257
+ [ text ( "Active" ) ] )
236
258
] ) ,
237
259
li ( [ ] , [
238
- a ( [ "href=#/completed" ] , [ text ( "Completed" ) ] )
260
+ a ( [
261
+ "href=#/completed" , "id=completed" , "class=" +
262
+ ( model . hash === '#/completed' ? "selected" : '' )
263
+ ] ,
264
+ [ text ( "Completed" ) ] )
239
265
] )
240
266
] ) , // </ul>
241
267
button ( [ "class=clear-completed" , "style=display:" + display_clear ,
@@ -265,6 +291,7 @@ function render_footer (model, signal) {
265
291
* var DOM = view(model);
266
292
*/
267
293
function view ( model , signal ) {
294
+
268
295
return (
269
296
section ( [ "class=todoapp" ] , [ // array of "child" elements
270
297
header ( [ "class=header" ] , [
@@ -295,7 +322,7 @@ function subscriptions (signal) {
295
322
var ESCAPE_KEY = 27 ; // used for "escaping" when editing a Todo item
296
323
297
324
document . addEventListener ( 'keyup' , function handler ( e ) {
298
- // console.log('e.keyCode:', e.keyCode, '| key:', e.key);
325
+ console . log ( 'e.keyCode:' , e . keyCode , '| key:' , e . key ) ;
299
326
300
327
switch ( e . keyCode ) {
301
328
case ENTER_KEY :
@@ -316,6 +343,11 @@ function subscriptions (signal) {
316
343
break ;
317
344
}
318
345
} ) ;
346
+
347
+ window . onhashchange = function route ( ) {
348
+ console . log ( "signal('ROUTE')()" ) ;
349
+ signal ( 'ROUTE' ) ( ) ;
350
+ }
319
351
}
320
352
321
353
/* module.exports is needed to run the functions using Node.js for testing! */
0 commit comments