Skip to content

Commit

Permalink
ActionList: Add property items
Browse files Browse the repository at this point in the history
The list knows its items.
  • Loading branch information
sukhwinder33445 committed Jul 19, 2024
1 parent 9a7604c commit 520835f
Showing 1 changed file with 13 additions and 42 deletions.
55 changes: 13 additions & 42 deletions asset/js/widget/ActionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ define(["../notjQuery"], function ($) {
constructor(list, isPrimary) {
this.list = list;
this.isPrimary = isPrimary;
this.items = Array.from(this.list.querySelectorAll(LIST_ITEM_IDENTIFIER));
this.isMultiSelectable = this.list.matches('[data-icinga-multiselect-url]');

this.lastActivatedItemUrl = null;
Expand Down Expand Up @@ -114,19 +115,17 @@ define(["../notjQuery"], function ($) {
} else {
document.getSelection().removeAllRanges();

let allItems = this.getAllItems();

let startIndex = allItems.indexOf(item);
let startIndex = this.items.indexOf(item);
if(startIndex < 0) {
startIndex = 0;
}

let endIndex = activeItems.length ? allItems.indexOf(activeItems[0]) : 0;
let endIndex = activeItems.length ? this.items.indexOf(activeItems[0]) : 0;
if (startIndex > endIndex) {
toActiveItems = allItems.slice(endIndex, startIndex + 1);
toActiveItems = this.items.slice(endIndex, startIndex + 1);
} else {
endIndex = activeItems.length ? allItems.indexOf(activeItems[activeItems.length - 1]) : 0;
toActiveItems = allItems.slice(startIndex, endIndex + 1);
endIndex = activeItems.length ? this.items.indexOf(activeItems[activeItems.length - 1]) : 0;
toActiveItems = this.items.slice(startIndex, endIndex + 1);
}

toDeactivateItems = activeItems.filter(item => ! toActiveItems.includes(item));
Expand Down Expand Up @@ -252,12 +251,11 @@ define(["../notjQuery"], function ($) {
return;
}

let allItems = this.getAllItems();
let firstListItem = allItems[0];
let lastListItem = allItems[allItems.length -1];
let firstListItem = this.items[0];
let lastListItem = this.items[this.items.length -1];
let markAsLastActive = null; // initialized only if it is different from toActiveItem
let toActiveItem = null;
let wasAllSelected = activeItems.length === allItems.length;
let wasAllSelected = activeItems.length === this.items.length;
let lastActivatedItem = this.list.querySelector(
`[data-icinga-detail-filter="${ this.lastActivatedItemUrl }"]`
);
Expand Down Expand Up @@ -375,10 +373,9 @@ define(["../notjQuery"], function ($) {
* Select All list items
*/
selectAll() {
let allItems = this.getAllItems();
let activeItems = this.getActiveItems();
this.setActive(allItems.filter(item => ! activeItems.includes(item)));
this.setLastActivatedItemUrl(allItems[allItems.length -1].dataset.icingaDetailFilter);
this.setActive(this.items.filter(item => ! activeItems.includes(item)));
this.setLastActivatedItemUrl(this.items[this.items.length -1].dataset.icingaDetailFilter);
this.addSelectionCountToFooter();
this.loadDetailUrl();
}
Expand Down Expand Up @@ -472,33 +469,7 @@ define(["../notjQuery"], function ($) {
*/
getActiveItems()
{
let items;

if (this.list.tagName.toLowerCase() === 'table') {
items = this.list.querySelectorAll(`:scope > tbody > ${LIST_ITEM_IDENTIFIER}.active`);
} else {
items = this.list.querySelectorAll(`:scope > ${LIST_ITEM_IDENTIFIER}.active`);
}

return Array.from(items);
}

/**
* Get all available items
*
* @return array
*/
getAllItems()
{
let items;

if (this.list.tagName.toLowerCase() === 'table') {
items = this.list.querySelectorAll(`:scope > tbody > ${LIST_ITEM_IDENTIFIER}`);
} else {
items = this.list.querySelectorAll(`:scope > ${LIST_ITEM_IDENTIFIER}`);
}

return Array.from(items);
return this.items.filter(item => item.classList.contains('active'));
}

/**
Expand Down Expand Up @@ -563,7 +534,7 @@ define(["../notjQuery"], function ($) {
}
}

this.clearSelection(this.getAllItems().filter(item => ! toActiveItems.includes(item)));
this.clearSelection(this.items.filter(item => ! toActiveItems.includes(item)));
this.setActive(toActiveItems);
this.addSelectionCountToFooter();
}
Expand Down

0 comments on commit 520835f

Please sign in to comment.