Skip to content

Commit bff22d4

Browse files
committed
[gchq#181] refactor favourites updating: rather than building all of the ops-lists in Operations every time a new operation is added or removed, only rerender favCat and handle the updating of the individual operations 'favourite' classes and icons
1 parent 51aa931 commit bff22d4

File tree

1 file changed

+112
-17
lines changed

1 file changed

+112
-17
lines changed

src/web/waiters/OperationsWaiter.mjs

Lines changed: 112 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import HTMLOperation from "../HTMLOperation.mjs";
88
import Sortable from "sortablejs";
99
import {fuzzyMatch, calcMatchRanges} from "../../core/lib/FuzzyMatch.mjs";
1010

11-
1211
/**
1312
* Waiter to handle events related to the operations.
1413
*/
@@ -41,7 +40,7 @@ class OperationsWaiter {
4140
if (e.type === "keyup") {
4241
const searchResults = document.getElementById("search-results");
4342

44-
this.openOperationsDropdown();
43+
this.openOpsDropdown();
4544

4645
if (e.target.value.length !== 0) {
4746
this.app.setElementVisibility(searchResults, true);
@@ -60,9 +59,9 @@ class OperationsWaiter {
6059
}
6160

6261
if (e.type === "click" && !e.target.value.length) {
63-
this.openOperationsDropdown();
62+
this.openOpsDropdown();
6463
} else if (e.keyCode === 27) { // Escape
65-
this.closeOperationsDropdown();
64+
this.closeOpsDropdown();
6665
} else if (e.keyCode === 40) { // Down
6766
e.preventDefault();
6867
ops = document.querySelectorAll("#search-results li");
@@ -107,7 +106,7 @@ class OperationsWaiter {
107106
searchResultsEl.innerHTML = matchedOpsHtml;
108107
searchResultsEl.dispatchEvent(this.manager.oplistcreate);
109108
}
110-
this.manager.recipe.updateSelectedOperations();
109+
this.manager.ops.updateListItemsClasses("#rec-list", "selected");
111110
}
112111
}
113112

@@ -116,7 +115,7 @@ class OperationsWaiter {
116115
* Filters operations based on the search string and returns the matching ones.
117116
*
118117
* @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
120119
* name and description
121120
* @returns {string[]}
122121
*/
@@ -323,21 +322,21 @@ class OperationsWaiter {
323322
/**
324323
* Open operations dropdown
325324
*/
326-
openOperationsDropdown() {
325+
openOpsDropdown() {
327326
// 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");
329328
const categories = document.getElementById("categories");
330329

331330
this.app.setElementVisibility(categories, true);
332-
this.app.setElementVisibility(closeOperationsDropdown, true);
331+
this.app.setElementVisibility(closeOpsDropdownIcon, true);
333332
}
334333

335334

336335
/**
337336
* Hide any operation lists ( #categories or #search-results ) and the close-operations-dropdown
338337
* icon itself, clear any search input
339338
*/
340-
closeOperationsDropdown() {
339+
closeOpsDropdown() {
341340
const search = document.getElementById("search");
342341

343342
// if any input remains in #search, clear it
@@ -347,7 +346,7 @@ class OperationsWaiter {
347346

348347
this.app.setElementVisibility(document.getElementById("categories"), false);
349348
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);
351350
}
352351

353352
/**
@@ -358,11 +357,7 @@ class OperationsWaiter {
358357
const favs = document.querySelectorAll("#edit-favourites-list li");
359358
const favouritesList = Array.from(favs, e => e.childNodes[0].textContent);
360359

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);
366361
}
367362

368363

@@ -374,13 +369,113 @@ class OperationsWaiter {
374369
this.app.resetFavourites();
375370
}
376371

372+
377373
/**
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
379376
*
380377
* @param {Event} e
381378
*/
382379
onIconFavouriteClick(e) {
383380
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+
}));
384479
}
385480
}
386481

0 commit comments

Comments
 (0)