Skip to content

Commit b6ec3fd

Browse files
committed
config-ip-filter: Move filter-logic to its own module.
1 parent 06fadef commit b6ec3fd

File tree

2 files changed

+158
-95
lines changed

2 files changed

+158
-95
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*//////////////////////////////// ABOUT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*\
2+
3+
FILTER LOGIC
4+
5+
\*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ * //////////////////////////////////////*/
6+
7+
const DBG = false;
8+
9+
/// LIBRARIES /////////////////////////////////////////////////////////////////
10+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
11+
import FILTER from './components/filter/FilterEnums';
12+
const UNISYS = require("unisys/client");
13+
14+
/// INITIALIZE MODULE /////////////////////////////////////////////////////////
15+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
16+
var MOD = UNISYS.NewModule(module.id);
17+
var UDATA = UNISYS.NewDataLink(MOD);
18+
19+
/// APP STATE/DATA STRUCTURES /////////////////////////////////////////////////
20+
var D3DATA = null; // see above for description
21+
var TEMPLATE = null; // template definition for prompts
22+
const PROMPTS = require("system/util/prompts");
23+
const NCLOGIC = require("./nc-logic");
24+
25+
/// CONSTANTS /////////////////////////////////////////////////////////////////
26+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
27+
const DATASET = window.NC_CONFIG.dataset || "netcreate";
28+
const TEMPLATE_URL = `templates/${DATASET}.json`;
29+
30+
31+
/// UNISYS HANDLERS ///////////////////////////////////////////////////////////
32+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33+
/*/ lifecycle INITIALIZE handler
34+
/*/
35+
MOD.Hook("INITIALIZE", () => {
36+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
37+
/*/ FILTER is called by FiltersPanel when user has updated filter.
38+
/*/
39+
UDATA.HandleMessage("FILTER", function(data) {
40+
D3DATA = UDATA.AppState("D3DATA"); // 8/10/20 REVIEW: Is this the best way to get current data?
41+
m_HandleFilter(data);
42+
});
43+
44+
}); // end UNISYS_INIT
45+
46+
47+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48+
/*/ INIT HANDLERS
49+
/*/
50+
51+
/**
52+
*
53+
* @param {object} data {action, filter}
54+
*
55+
*/
56+
function m_HandleFilter(data) {
57+
console.log('HandleFilter!', data);
58+
59+
if (data.action === undefined) throw "m_HandleFilter called without action";
60+
61+
switch (data.action) {
62+
case FILTER.ACTIONS.CLEAR:
63+
m_ClearFilters();
64+
break;
65+
case FILTER.ACTIONS.FILTER_EDGES:
66+
break;
67+
case FILTER.ACTIONS.FILTER_NODES:
68+
default:
69+
m_FilterNodes(data.filter.value);
70+
break;
71+
}
72+
UDATA.SetAppState("D3DATA", D3DATA);
73+
}
74+
75+
function m_FilterNodes(nodeLabelSnippet) {
76+
const marked = { isFilteredOut: true };
77+
const normal = { isFilteredOut: false };
78+
m_SetMatchingByNodeLabel(nodeLabelSnippet, marked, normal);
79+
}
80+
function m_ClearFilters() {
81+
const props = { isFilteredOut: false };
82+
NCLOGIC.SetAllObjs(D3DATA.nodes, props);
83+
NCLOGIC.SetAllObjs(D3DATA.edges, props);
84+
}
85+
86+
87+
88+
/// OBJECT HELPERS ////////////////////////////////////////////////////////////
89+
90+
91+
/// NODE HELPERS //////////////////////////////////////////////////////////////
92+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
93+
/*/ Set nodes & EDGES that PARTIALLY match 'str' to 'yes' props.
94+
All others nodes are set to 'no' props. Return matches.
95+
Optionally resets all the NON matching nodes as well.
96+
97+
Edges are matched if they link to the node.
98+
/*/
99+
function m_SetMatchingByNodeLabel(str = "", yes = {}, no = {}) {
100+
let returnMatches = [];
101+
str = NCLOGIC.EscapeRegexChars(str.trim());
102+
if (str === "") return undefined;
103+
const regex = new RegExp(/*'^'+*/ str, "i");
104+
// First find the nodes
105+
D3DATA.nodes.forEach(node => {
106+
if (regex.test(node.label)) {
107+
for (let key in yes) node[key] = yes[key];
108+
returnMatches.push(node);
109+
} else {
110+
for (let key in no) node[key] = no[key];
111+
}
112+
});
113+
// Then hide all related edges
114+
m_SetMatchingEdgesByNodes(returnMatches, yes, no);
115+
return returnMatches;
116+
}
117+
118+
/**
119+
* Set edges that link to any node in nodeIDs to 'yes' props.
120+
* All others nodes are set to 'no' props. Return matches.
121+
*
122+
* We set look for ALL nodes at once otherwise, one node can unset
123+
* antoher node.
124+
*
125+
* This is a specialized function because edges need to be matched
126+
* against both source and target.
127+
*
128+
* @param {Array} nodes Array of node objects
129+
* @param {Object} yes e.g. marked = { isFilteredOut: true };
130+
* @param {Object} no e.g. normal = { isFilteredOut: false };
131+
*/
132+
function m_SetMatchingEdgesByNodes(nodes, yes = {}, no = {}) {
133+
const nodeIDs = nodes.map(node => node.id);
134+
let returnMatches = [];
135+
D3DATA.edges.forEach(edge => {
136+
if ( nodeIDs.includes(edge.source.id) || nodeIDs.includes(edge.target.id) ) {
137+
for (let key in yes) edge[key] = yes[key];
138+
returnMatches.push(edge);
139+
} else {
140+
for (let key in no) edge[key] = no[key];
141+
}
142+
});
143+
return returnMatches;
144+
}
145+
146+
147+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
148+
149+
/// EXPORT CLASS DEFINITION ///////////////////////////////////////////////////
150+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
151+
module.exports = MOD;

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

Lines changed: 7 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const DBG = false;
3939

4040
/// LIBRARIES /////////////////////////////////////////////////////////////////
4141
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42-
import FILTER from './components/filter/FilterEnums';
4342
const SETTINGS = require("settings");
4443
const UNISYS = require("unisys/client");
4544
const JSCLI = require("system/util/jscli");
@@ -670,57 +669,20 @@ MOD.Hook("INITIALIZE", () => {
670669
m_HandleAutoCompleteSelect(data);
671670
});
672671

673-
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
674-
/*/ FILTER is called by FiltersPanel when user has updated filter.
675-
/*/
676-
UDATA.HandleMessage("FILTER", function(data) {
677-
m_HandleFilter(data);
678-
});
679-
680672
}); // end UNISYS_INIT
681673

674+
675+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
676+
/*/ INIT HANDLERS
677+
/*/
678+
682679
function m_HandleAutoCompleteSelect(data) {
683680
if (DBG) console.log("ACL: Setting activeAutoCompleteId to", data.id);
684681
UDATA.SetAppState("ACTIVEAUTOCOMPLETE", {
685682
activeAutoCompleteId: data.id
686683
});
687684
}
688685

689-
/**
690-
*
691-
* @param {object} data {action, filter}
692-
*
693-
*/
694-
function m_HandleFilter(data) {
695-
console.log('HandleFilter!', data);
696-
697-
if (data.action === undefined) throw "m_HandleFilter called without action";
698-
699-
switch (data.action) {
700-
case FILTER.ACTIONS.CLEAR:
701-
m_ClearFilters();
702-
break;
703-
case FILTER.ACTIONS.FILTER_EDGES:
704-
break;
705-
case FILTER.ACTIONS.FILTER_NODES:
706-
default:
707-
m_FilterNodes(data.filter.value);
708-
break;
709-
}
710-
UDATA.SetAppState("D3DATA", D3DATA);
711-
}
712-
713-
function m_FilterNodes(nodeLabelSnippet) {
714-
const marked = { isFilteredOut: true };
715-
const normal = { isFilteredOut: false };
716-
m_SetMatchingByNodeLabel(nodeLabelSnippet, marked, normal);
717-
}
718-
function m_ClearFilters() {
719-
const props = { isFilteredOut: false };
720-
m_SetAllObjs(D3DATA.nodes, props);
721-
m_SetAllObjs(D3DATA.edges, props);
722-
}
723-
724686
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
725687
/*/ lifecycle RESET handler
726688
/*/
@@ -827,6 +789,7 @@ function m_SetAllObjs(obj_list, all = {}) {
827789
for (let key in all) obj[key] = all[key];
828790
});
829791
}
792+
MOD.SetAllObj = m_SetAllObjs; // Expose for filter-logic.js
830793

831794
/// NODE HELPERS //////////////////////////////////////////////////////////////
832795
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -887,59 +850,7 @@ function m_SetMatchingNodesByLabel(str = "", yes = {}, no = {}) {
887850
});
888851
return returnMatches;
889852
}
890-
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
891-
/*/ Set nodes & EDGES that PARTIALLY match 'str' to 'yes' props.
892-
All others nodes are set to 'no' props. Return matches.
893-
Optionally resets all the NON matching nodes as well.
894-
895-
Edges are matched if they link to the node.
896-
/*/
897-
function m_SetMatchingByNodeLabel(str = "", yes = {}, no = {}) {
898-
let returnMatches = [];
899-
str = u_EscapeRegexChars(str.trim());
900-
if (str === "") return undefined;
901-
const regex = new RegExp(/*'^'+*/ str, "i");
902-
// First find the nodes
903-
D3DATA.nodes.forEach(node => {
904-
if (regex.test(node.label)) {
905-
for (let key in yes) node[key] = yes[key];
906-
returnMatches.push(node);
907-
} else {
908-
for (let key in no) node[key] = no[key];
909-
}
910-
});
911-
// Then hide all related edges
912-
m_SetMatchingEdgesByNodes(returnMatches, yes, no);
913-
return returnMatches;
914-
}
915853

916-
/**
917-
* Set edges that link to any node in nodeIDs to 'yes' props.
918-
* All others nodes are set to 'no' props. Return matches.
919-
*
920-
* We set look for ALL nodes at once otherwise, one node can unset
921-
* antoher node.
922-
*
923-
* This is a specialized function because edges need to be matched
924-
* against both source and target.
925-
*
926-
* @param {Array} nodes Array of node objects
927-
* @param {Object} yes e.g. marked = { isFilteredOut: true };
928-
* @param {Object} no e.g. normal = { isFilteredOut: false };
929-
*/
930-
function m_SetMatchingEdgesByNodes(nodes, yes = {}, no = {}) {
931-
const nodeIDs = nodes.map(node => node.id);
932-
let returnMatches = [];
933-
D3DATA.edges.forEach(edge => {
934-
if ( nodeIDs.includes(edge.source.id) || nodeIDs.includes(edge.target.id) ) {
935-
for (let key in yes) edge[key] = yes[key];
936-
returnMatches.push(edge);
937-
} else {
938-
for (let key in no) edge[key] = no[key];
939-
}
940-
});
941-
return returnMatches;
942-
}
943854

944855

945856
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -1025,6 +936,7 @@ const REGEX_REGEXCHARS = /[.*+?^${}()|[\]\\]/g;
1025936
function u_EscapeRegexChars(string) {
1026937
return string.replace(REGEX_REGEXCHARS, "\\$&"); // $& means the whole matched string
1027938
}
939+
MOD.EscapeRegexChars = u_EscapeRegexChars; // Expose for filter-logic.js
1028940
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1029941
/*/ Convert all IDs to integers
1030942
Node and Edge IDs should be integers.

0 commit comments

Comments
 (0)