Skip to content

Commit 65ebf82

Browse files
committed
config-ip-filter: Add filter functions to nc-logic.
1 parent bdb4ab7 commit 65ebf82

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

build/app/view/netcreate/nc-logic.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,14 @@ MOD.Hook("INITIALIZE", () => {
668668
UDATA.HandleMessage("AUTOCOMPLETE_SELECT", function(data) {
669669
m_HandleAutoCompleteSelect(data);
670670
});
671+
672+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
673+
/*/ FILTER is called by FiltersPanel when user has updated filter.
674+
/*/
675+
UDATA.HandleMessage("FILTER", function(data) {
676+
m_HandleFilter(data);
677+
});
678+
671679
}); // end UNISYS_INIT
672680

673681
function m_HandleAutoCompleteSelect(data) {
@@ -677,6 +685,21 @@ function m_HandleAutoCompleteSelect(data) {
677685
});
678686
}
679687

688+
/**
689+
*
690+
* @param {object} filter {name, type, value}
691+
*
692+
*/
693+
function m_HandleFilter(filter) {
694+
console.log('HandleFilter!', filter);
695+
696+
// Filter Nodes and Edges
697+
let marked = { isFilteredOut: true };
698+
let normal = { isFilteredOut: false };
699+
m_SetMatchingByNodeLabel(filter.value, marked, normal);
700+
UDATA.SetAppState("D3DATA", D3DATA);
701+
}
702+
680703
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
681704
/*/ lifecycle RESET handler
682705
/*/
@@ -843,6 +866,61 @@ function m_SetMatchingNodesByLabel(str = "", yes = {}, no = {}) {
843866
});
844867
return returnMatches;
845868
}
869+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
870+
/*/ Set nodes & EDGES that PARTIALLY match 'str' to 'yes' props.
871+
All others nodes are set to 'no' props. Return matches.
872+
Optionally resets all the NON matching nodes as well.
873+
874+
Edges are matched if they link to the node.
875+
/*/
876+
function m_SetMatchingByNodeLabel(str = "", yes = {}, no = {}) {
877+
let returnMatches = [];
878+
str = u_EscapeRegexChars(str.trim());
879+
if (str === "") return undefined;
880+
const regex = new RegExp(/*'^'+*/ str, "i");
881+
// First find the nodes
882+
D3DATA.nodes.forEach(node => {
883+
if (regex.test(node.label)) {
884+
for (let key in yes) node[key] = yes[key];
885+
returnMatches.push(node);
886+
} else {
887+
for (let key in no) node[key] = no[key];
888+
}
889+
});
890+
// Then hide all related edges
891+
m_SetMatchingEdgesByNodes(returnMatches, yes, no);
892+
return returnMatches;
893+
}
894+
895+
/**
896+
* Set edges that link to any node in nodeIDs to 'yes' props.
897+
* All others nodes are set to 'no' props. Return matches.
898+
*
899+
* We set look for ALL nodes at once otherwise, one node can unset
900+
* antoher node.
901+
*
902+
* This is a specialized function because edges need to be matched
903+
* against both source and target.
904+
*
905+
* @param {Array} nodes Array of node objects
906+
* @param {Object} yes e.g. marked = { isFilteredOut: true };
907+
* @param {Object} no e.g. normal = { isFilteredOut: false };
908+
*/
909+
function m_SetMatchingEdgesByNodes(nodes, yes = {}, no = {}) {
910+
const nodeIDs = nodes.map(node => node.id);
911+
let returnMatches = [];
912+
D3DATA.edges.forEach(edge => {
913+
if ( nodeIDs.includes(edge.source.id) || nodeIDs.includes(edge.target.id) ) {
914+
for (let key in yes) edge[key] = yes[key];
915+
returnMatches.push(edge);
916+
} else {
917+
for (let key in no) edge[key] = no[key];
918+
}
919+
});
920+
return returnMatches;
921+
}
922+
923+
846924
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
847925
/*/ Update props of exact matching nodes, returns matches
848926
Optionally resets all the NON matching nodes as well

0 commit comments

Comments
 (0)