@@ -386,7 +386,8 @@ Double-clicking the <label> activates editing mode, by toggling the .editing cla
386
386
387
387
> _ ** Note** : the sample TodoMVC Browser Tests:
388
388
https://github.com/tastejs/todomvc/tree/master/tests#example-output
389
- does ** not** include a test-case for ** double-clicking** _
389
+ does ** not** include a test-case for ** double-clicking** .
390
+ We are going to add one below for "extra credit"._
390
391
391
392
Since Double-clicking/tapping is the _ only_ way to edit a todo item,
392
393
we feel that it deserves a test.
@@ -398,7 +399,9 @@ https://stackoverflow.com/questions/5497073/how-to-differentiate-single-click-ev
398
399
Reading though all the answers, we determine that the most relevant (_ to us_ )
399
400
is: https://stackoverflow.com/a/16033129/1148249 (_ which uses "vanilla" JS_ )
400
401
401
- ![ stackoverflow-double-click-example] ( https://user-images.githubusercontent.com/194400/45124122-14942f80-b161-11e8-94c0-f54f2352bdd5.png )
402
+ [ ![ stackoverflow-double-click-example] ( https://user-images.githubusercontent.com/194400/45124122-14942f80-b161-11e8-94c0-f54f2352bdd5.png )] ( https://stackoverflow.com/a/16033129/1148249 )
403
+
404
+ > _ ** Note** : when you find a StackOverflow question/answer ** helpful, upvote** !_
402
405
403
406
``` html
404
407
<div onclick =" doubleclick(this, function(){alert('single')}, function(){alert('double')})" >click me</div >
@@ -421,19 +424,85 @@ is: https://stackoverflow.com/a/16033129/1148249 (_which uses "vanilla" JS_)
421
424
```
422
425
Given that we are using the Elm Architecture to manage the DOM,
423
426
we don't want a function that _ alters_ the DOM.
424
- So we are going to _ borrow_ the _ logic_ from this example but simplify it.
427
+ So we are going to _ borrow_ the _ logic_ from this example but _ simplify_ it.
428
+ Since we are not mutating the DOM by setting ` data-dblclick ` attributes,
429
+ we won't need to remove the attribute using a ` setTimeout ` ,
430
+
431
+
432
+
433
+
434
+
435
+ #### 5.2 ` render_item ` view function with "Edit Mode" ` <input class="edit"> `
436
+
437
+ In order to edit an item the ** ` render_item ` ** function
438
+ will require ** 3 modifications** :
439
+
440
+ 1 . Add the ` signal('EDIT', item.id) ` as an ** ` onclick ` attribute** to ` <label> `
441
+ so that when a ` <label> ` is (double-)clicked
442
+ the ` model.editing ` property is set by the ` update ` function (_ see below_ ).
443
+ 2 . Apply the ** ` "class=editing" ` ** to the list item which is being edited.
444
+ 3 . Display the ** ` <input class="edit"> ` **
445
+ with the Todo list item title as it's ** ` value ` ** property.
446
+
447
+ ##### 5.2 ` render_item ` "Edit Mode" _ Test_
448
+
449
+ For the above modifications (_ requirements_ ) we can write a _ single_ test
450
+ with four assertions:
451
+
452
+ ``` js
453
+ test .only (' 5. Editing: > Render an item in "editing mode"' , function (t ) {
454
+ elmish .empty (document .getElementById (id));
455
+ localStorage .removeItem (' elmish_' + id);
456
+ const model = {
457
+ todos: [
458
+ { id: 0 , title: " Make something people want." , done: false },
459
+ { id: 1 , title: " Bootstrap for as long as you can" , done: false },
460
+ { id: 2 , title: " Let's solve our own problem" , done: false }
461
+ ],
462
+ hash: ' #/' , // the "route" to display
463
+ editing: 2 // edit the 3rd todo list item (which has id == 2)
464
+ };
465
+
466
+ // render the ONE todo list item in "editing mode" based on model.editing:
467
+ document .getElementById (id).appendChild (
468
+ app .render_item (model .todos [2 ], model, mock_signal),
469
+ );
470
+ // test that signal (in case of the test mock_signal) is onclick attribute:
471
+ t .equal (document .querySelectorAll (' .view > label' )[0 ].onclick .toString (),
472
+ mock_signal ().toString (), " mock_signal is onclick attribute of label" );
473
+
474
+ // test that the <li class="editing"> and <input class="edit"> was rendered:
475
+ t .equal (document .querySelectorAll (' .editing' ).length , 1 ,
476
+ " <li class='editing'> element is visible" );
477
+ t .equal (document .querySelectorAll (' .edit' ).length , 1 ,
478
+ " <input class='edit'> element is visible" );
479
+ t .equal (document .querySelectorAll (' .edit' )[0 ].value , model .todos [2 ].title ,
480
+ " <input class='edit'> has value: " + model .todos [2 ].title );
481
+ t .end ();
482
+ });
483
+ ```
425
484
485
+ There is quite a lot to "unpack" here, but the main gist is that
486
+ based on the ` model.editing ` key being set to ` 2 ` , our ` render_item ` function,
487
+ will add the ` editing ` CSS class to the ` <li> ` element and render an
488
+ ` <input> ` with CSS class ` edit ` .
489
+ The TodoMVC style sheet (` todomvc-app.css ` ) will take care of displaying
490
+ the input correctly.
426
491
492
+ Setting the ** ` onclick ` ** attribute of the ` <label> ` element
493
+ to whatever is passed in as the third argument of ` redner_item `
494
+ i.e. the ` signal ` will mean that a specific action will be dispatched/triggered
495
+ when the ` <label> ` element is clicked.
427
496
428
497
498
+ > ** SPOILER ALERT** : If you want to _ try_ to make the "Edit Mode" _ Test_
499
+ assertions _ pass_ without reading the "solution",
500
+ do it now before proceeding to the reading the _ implementation_ section.
429
501
502
+ <br />
430
503
431
- #### 5.2 ` render_item ` view function with Edit controls
504
+ ##### 5.2 ` render_item ` " Edit Mode" _ Implementation _
432
505
433
- The ` render_item ` function will require 3 changes:
434
- 1 . Add the ` "class=editing" ` to the list item which is being edited.
435
- 2 . Add the ` signal('EDIT', item.id) ` as an ` onclick ` attribute to ` <label> `
436
- 3 . Display the ** ` <input class="edit"> ` ** with the
437
506
438
507
BEFORE:
439
508
``` js
0 commit comments