@@ -8,7 +8,6 @@ import HTMLOperation from "../HTMLOperation.mjs";
8
8
import Sortable from "sortablejs" ;
9
9
import { fuzzyMatch , calcMatchRanges } from "../../core/lib/FuzzyMatch.mjs" ;
10
10
11
-
12
11
/**
13
12
* Waiter to handle events related to the operations.
14
13
*/
@@ -41,7 +40,7 @@ class OperationsWaiter {
41
40
if ( e . type === "keyup" ) {
42
41
const searchResults = document . getElementById ( "search-results" ) ;
43
42
44
- this . openOperationsDropdown ( ) ;
43
+ this . openOpsDropdown ( ) ;
45
44
46
45
if ( e . target . value . length !== 0 ) {
47
46
this . app . setElementVisibility ( searchResults , true ) ;
@@ -60,9 +59,9 @@ class OperationsWaiter {
60
59
}
61
60
62
61
if ( e . type === "click" && ! e . target . value . length ) {
63
- this . openOperationsDropdown ( ) ;
62
+ this . openOpsDropdown ( ) ;
64
63
} else if ( e . keyCode === 27 ) { // Escape
65
- this . closeOperationsDropdown ( ) ;
64
+ this . closeOpsDropdown ( ) ;
66
65
} else if ( e . keyCode === 40 ) { // Down
67
66
e . preventDefault ( ) ;
68
67
ops = document . querySelectorAll ( "#search-results li" ) ;
@@ -107,7 +106,7 @@ class OperationsWaiter {
107
106
searchResultsEl . innerHTML = matchedOpsHtml ;
108
107
searchResultsEl . dispatchEvent ( this . manager . oplistcreate ) ;
109
108
}
110
- this . manager . recipe . updateSelectedOperations ( ) ;
109
+ this . manager . ops . updateListItemsClasses ( "#rec-list" , "selected" ) ;
111
110
}
112
111
}
113
112
@@ -116,7 +115,7 @@ class OperationsWaiter {
116
115
* Filters operations based on the search string and returns the matching ones.
117
116
*
118
117
* @param {string } searchStr
119
- * @param {boolean } highlight - Whether or not to highlight the matching string in the operation
118
+ * @param {boolean } highlight - Whether to highlight the matching string in the operation
120
119
* name and description
121
120
* @returns {string[] }
122
121
*/
@@ -323,21 +322,21 @@ class OperationsWaiter {
323
322
/**
324
323
* Open operations dropdown
325
324
*/
326
- openOperationsDropdown ( ) {
325
+ openOpsDropdown ( ) {
327
326
// the 'close' ( dropdown ) icon in Operations component mobile UI
328
- const closeOperationsDropdown = document . getElementById ( "close-operations -dropdown" ) ;
327
+ const closeOpsDropdownIcon = document . getElementById ( "close-ops -dropdown-icon " ) ;
329
328
const categories = document . getElementById ( "categories" ) ;
330
329
331
330
this . app . setElementVisibility ( categories , true ) ;
332
- this . app . setElementVisibility ( closeOperationsDropdown , true ) ;
331
+ this . app . setElementVisibility ( closeOpsDropdownIcon , true ) ;
333
332
}
334
333
335
334
336
335
/**
337
336
* Hide any operation lists ( #categories or #search-results ) and the close-operations-dropdown
338
337
* icon itself, clear any search input
339
338
*/
340
- closeOperationsDropdown ( ) {
339
+ closeOpsDropdown ( ) {
341
340
const search = document . getElementById ( "search" ) ;
342
341
343
342
// if any input remains in #search, clear it
@@ -347,7 +346,7 @@ class OperationsWaiter {
347
346
348
347
this . app . setElementVisibility ( document . getElementById ( "categories" ) , false ) ;
349
348
this . app . setElementVisibility ( document . getElementById ( "search-results" ) , false ) ;
350
- this . app . setElementVisibility ( document . getElementById ( "close-operations -dropdown" ) , false ) ;
349
+ this . app . setElementVisibility ( document . getElementById ( "close-ops -dropdown-icon " ) , false ) ;
351
350
}
352
351
353
352
/**
@@ -358,11 +357,7 @@ class OperationsWaiter {
358
357
const favs = document . querySelectorAll ( "#edit-favourites-list li" ) ;
359
358
const favouritesList = Array . from ( favs , e => e . childNodes [ 0 ] . textContent ) ;
360
359
361
- this . app . saveFavourites ( favouritesList ) ;
362
- this . app . loadFavourites ( ) ;
363
- this . app . populateOperationsList ( ) ;
364
- this . manager . recipe . updateSelectedOperations ( ) ;
365
- this . manager . recipe . initialiseOperationDragNDrop ( ) ;
360
+ this . app . updateFavourites ( favouritesList ) ;
366
361
}
367
362
368
363
@@ -374,13 +369,113 @@ class OperationsWaiter {
374
369
this . app . resetFavourites ( ) ;
375
370
}
376
371
372
+
377
373
/**
378
- * Add a favourite op, for mobile UI only
374
+ * Add op to Favourites and add the 'favourite' class to the list item,
375
+ * set the star icon to a filled star
379
376
*
380
377
* @param {Event } e
381
378
*/
382
379
onIconFavouriteClick ( e ) {
383
380
this . app . addFavourite ( e . target . getAttribute ( "title" ) ) ;
381
+ document . querySelectorAll ( `li[data-name="${ e . target . getAttribute ( "title" ) } "]` ) . forEach ( listItem => {
382
+ listItem . querySelector ( "i.star-icon" ) . innerText = "star" ;
383
+ listItem . classList . add ( "favourite" ) ;
384
+ } ) ;
385
+ }
386
+
387
+
388
+ /**
389
+ * Update classes in the #dropdown-operations op-lists based on the
390
+ * list items of a srcListSelector.
391
+ *
392
+ * e.g: the operations currently listed in the recipe-list and the appropriate
393
+ * list items in operations-dropdown that need to have the 'selected' class added
394
+ * or removed. Another use case is using the current 'Favourite' category op-list
395
+ * as a source and handle the 'favourite' class on operations-dropdown op-lists
396
+ * accordingly
397
+ *
398
+ * @param {string } srcListSelector - the UL element list to compare to
399
+ * @param {string } className - the className to update
400
+ */
401
+ updateListItemsClasses ( srcListSelector , className ) {
402
+ const listItems = document . querySelectorAll ( `${ srcListSelector } > li` ) ;
403
+ const ops = document . querySelectorAll ( ".op-list > li.operation" ) ;
404
+
405
+ this . removeClassFromOps ( className ) ;
406
+
407
+ if ( listItems . length !== 0 ) {
408
+ listItems . forEach ( ( item => {
409
+ const targetDataName = item . getAttribute ( "data-name" ) ;
410
+
411
+ ops . forEach ( ( op ) => {
412
+ if ( targetDataName === op . getAttribute ( "data-name" ) ) {
413
+ this . addClassToOp ( targetDataName , className ) ;
414
+ }
415
+ } ) ;
416
+ } ) ) ;
417
+ }
418
+ }
419
+
420
+
421
+ /**
422
+ * Set 'favourite' classes to all ops currently listed in the Favourites
423
+ * category, and update the ops-list operation favourite icons
424
+ */
425
+ updateOpsFavouriteIcons ( ) {
426
+ this . updateListItemsClasses ( "#catFavourites > .op-list" , "favourite" ) ;
427
+ document . querySelectorAll ( "li.operation.favourite > i.star-icon" ) . forEach ( ( icon ) => {
428
+ icon . innerText = "star" ;
429
+ } ) ;
430
+ document . querySelectorAll ( "li.operation:not(.favourite) > i.star-icon" ) . forEach ( ( icon ) => {
431
+ icon . innerText = "star_outline" ;
432
+ } ) ;
433
+ }
434
+
435
+
436
+ /**
437
+ * Generic function to remove a class from > ALL < operation list items
438
+ *
439
+ * @param {string } className - the class to remove
440
+ */
441
+ removeClassFromOps ( className ) {
442
+ const ops = document . querySelectorAll ( ".op-list > li.operation" ) ;
443
+
444
+ ops . forEach ( ( op => {
445
+ this . removeClassFromOp ( op . getAttribute ( "data-name" ) , className ) ;
446
+ } ) ) ;
447
+ }
448
+
449
+
450
+ /**
451
+ * Generic function to remove a class from target operation list item
452
+ *
453
+ * @param {string } opDataName - data-name attribute of the target operation
454
+ * @param {string } className - the class to remove
455
+ */
456
+ removeClassFromOp ( opDataName , className ) {
457
+ const ops = document . querySelectorAll ( `.op-list > li.operation[data-name="${ opDataName } "].${ className } ` ) ;
458
+
459
+ // the same operation may occur twice if it is also in #catFavourites
460
+ ops . forEach ( ( op ) => {
461
+ op . classList . remove ( `${ className } ` ) ;
462
+ } ) ;
463
+ }
464
+
465
+
466
+ /**
467
+ * Generic function to add a class to an operation list item
468
+ *
469
+ * @param {string } opDataName - data-name attribute of the target operation
470
+ * @param {string } className - the class to add to the operation list item
471
+ */
472
+ addClassToOp ( opDataName , className ) {
473
+ const ops = document . querySelectorAll ( `.op-list > li.operation[data-name="${ opDataName } "]` ) ;
474
+
475
+ // the same operation may occur twice if it is also in #catFavourites
476
+ ops . forEach ( ( op => {
477
+ op . classList . add ( `${ className } ` ) ;
478
+ } ) ) ;
384
479
}
385
480
}
386
481
0 commit comments