1
1
"use strict" ;
2
+
2
3
const state = {
3
- data : [ ] ,
4
- search : [ ] ,
4
+ data : [ ] ,
5
+ search : [ ] ,
5
6
pageSize : 15 ,
6
7
totalPages : + Infinity ,
7
8
pageIndex : 1 ,
8
9
paginatedData : [ ] ,
9
10
checkedFilters : [ ] ,
11
+ checkBoxMap : {
12
+ getStart : / .* g e t t i n g \s s t a r t e d .* / ig,
13
+ tutorial : / .* t u t o r i a l .* / ig,
14
+ conFunc : / .* c o n c e p t s \s a n d \s f u n c t i o n a l i t y .* / ig,
15
+ refDesEnd2End : / .* r e f e r e n c e \s d e s i g n s \s a n d \s e n d \s t o \s e n d .* / ig,
16
+ codeOpt : / .* c o d e \s o p t i m i z a t i o n .* / ig,
17
+ cpp : / .* c p p .* / ig,
18
+ fortran : / .* f o r t r a n .* / ig,
19
+ python : / .* p y t h o n .* / ig,
20
+ cpu : / .* c p u .* / ig,
21
+ gpu : / .* g p u .* / ig,
22
+ fpga : / .* f p g a .* / ig
23
+ }
10
24
}
11
25
12
- function reloadDocs ( ) {
13
- const reload = new Event ( 'DOMContentLoaded' ) ;
14
- document . dispatchEvent ( reload )
15
- }
26
+ HTMLElement . prototype . onEvent = function ( eventType , callBack , useCapture ) { // est eventListener for all HTML Objs.
27
+ this . addEventListener ( eventType , callBack , useCapture ) ;
28
+ if ( ! this . myListeners ) {
29
+ this . myListeners = [ ] ; // create empty arr to hold event type, callbacks, captures in DOM
30
+ }
31
+ this . myListeners . push ( { eType : eventType , callBack : callBack } ) ; // populate same arr
32
+ return this ;
33
+ } ;
34
+
35
+ HTMLElement . prototype . removeListeners = function ( ) {
36
+ if ( this . myListeners ) { // If myListeners arr exists, run for loop over all eventListeners
37
+ for ( let i = 0 ; i < this . myListeners . length ; i ++ ) {
38
+ this . removeEventListener ( this . myListeners [ i ] . eType , this . myListeners [ i ] . callBack ) ;
39
+ }
40
+ delete this . myListeners ; // clear all eventListeners in arr before next func inits state
41
+ }
42
+ } ;
16
43
17
44
document . addEventListener ( 'DOMContentLoaded' , function ( ) {
18
45
console . log ( 'document is ready.' ) ;
19
- fetchData ( ) . then ( ( data ) => { /* changed fr 'state' to 'data'*/
20
- renderUi ( data . paginatedData ) ; /* changed fr 'state' to 'data'*/
46
+ fetchData ( ) . then ( ( data ) => {
47
+ renderUi ( data . paginatedData ) ;
21
48
showTotalRecords ( ) ;
22
49
attachSearch ( ) ;
23
50
filterPaginatedData ( ) ;
24
- lastUpdated ( )
25
- } ) ;
26
-
51
+ lastUpdated ( ) ;
52
+ } )
27
53
} ) ;
28
54
55
+
29
56
function reloadData ( ) {
30
57
const reload = new Event ( 'DOMContentLoaded' ) ;
31
- document . dispatchEvent ( reload )
58
+ document . dispatchEvent ( reload )
59
+ console . log ( reload )
32
60
}
33
61
34
62
function lastUpdated ( ) {
@@ -92,95 +120,110 @@ function lastPage() {
92
120
renderUi ( state . paginatedData ) ;
93
121
}
94
122
123
+ function resetFiltersGoHome ( ) {
124
+ destroyView ( ) ;
125
+ resetFilters ( ) ;
126
+ reloadData ( ) ;
127
+ }
128
+
129
+ // function closeCollapsibleFilters(){
130
+ // //
131
+ // }
132
+
133
+
95
134
function resetFilters ( ) {
96
135
const checkboxes = document . querySelectorAll ( 'input[name=data-filter]' ) ;
97
136
console . log ( "NodeList:" , checkboxes ) ;
98
137
checkboxes . forEach ( c => {
99
- if ( c . checked == true ) {
138
+ if ( c . checked == true ) {
100
139
c . checked = false ;
101
140
const change = new Event ( 'change' ) ;
102
141
c . dispatchEvent ( change ) ;
103
- } else {
142
+ } else {
104
143
console . log ( c . checked ) ;
105
144
}
106
145
} ) ;
107
- } ;
146
+ }
108
147
109
148
function resetSearcher ( ) {
110
149
const input = document . getElementById ( 'textInput' ) ;
111
- input . value = "" ;
150
+ input . value = "" ;
112
151
destroyView ( ) ;
113
152
resetFilters ( ) ;
114
153
reloadData ( ) ;
115
154
// input.focus();
116
- } ;
117
-
155
+ }
118
156
119
- function showTotalRecords ( ) {
157
+ function showTotalRecords ( ) {
120
158
const records = document . getElementById ( "total-records" ) ;
121
159
records . innerHTML = `Total Records: ${ [ ...state . data ] . length } ` ;
122
160
}
123
161
124
162
function renderUi ( data ) {
125
- const filtered = state . checkedFilters . length === 0 ? data : data . filter ( item => {
163
+ const filtered = state . checkedFilters . length === 0 ? data : data . filter ( item => {
126
164
const languages = item . languages . reduce ( ( acc , value ) => {
127
165
const keys = Object . keys ( value ) ;
128
166
return [ ...acc , ...keys ] ;
129
167
} , [ ] ) ;
130
- const filters = [ ...state . checkedFilters ]
131
- const conditions = filters . map ( ( regex ) => {
132
- const condition = regex . test ( item . expertise ) ||
133
- regex . test ( languages . join ( ',' ) ) ||
134
- regex . test ( item . targetDevice . join ( ',' ) ) ;
135
- return condition
136
- } )
137
- return conditions . reduce ( ( acc , condition ) => {
168
+ const filters = [ ...state . checkedFilters ]
169
+ const conditions = filters . map ( ( checkboxValue ) => {
170
+ const regex = state . checkBoxMap [ checkboxValue ]
171
+ const condition = regex . test ( item . expertise ) ||
172
+ regex . test ( languages . join ( ',' ) ) ||
173
+ regex . test ( item . targetDevice . join ( ',' ) ) ;
174
+
175
+ console . log ( typeof ( filters ) )
176
+ console . log ( filters )
177
+ return condition
178
+ } )
179
+ return conditions . reduce ( ( acc , condition ) => {
138
180
return acc && condition
139
- } , true )
140
-
181
+ } , true )
182
+
141
183
} )
142
184
noResultsSwitch ( filtered )
143
185
qtyFilteredResults ( filtered )
144
186
renderCards ( filtered )
145
187
printPaginatorState ( )
146
188
}
147
189
148
- function qtyFilteredResults ( filtered ) {
190
+ function qtyFilteredResults ( filtered ) {
149
191
const qtyResults = filtered . length ;
150
192
const dataTotalLength = [ ...state . data ] . length ;
151
- console . log ( "Total len" , dataTotalLength )
152
- const results = document . getElementById ( "qty-show-results" ) || { } ;
193
+ console . log ( "Total len" , dataTotalLength )
194
+ const results = document . getElementById ( "qty-show-results" ) || { } ;
153
195
results . innerHTML = `${ qtyResults } Results` ;
154
- if ( qtyResults === undefined || qtyResults == 15 || qtyResults == dataTotalLength ) {
155
- results . style . display = "none" ;
196
+ if ( qtyResults === undefined || qtyResults == 15 || qtyResults == dataTotalLength ) {
197
+ results . style . display = "none" ;
156
198
} else {
157
- results . style . display = "block" ;
199
+ results . style . display = "block" ;
158
200
}
159
201
}
160
202
161
203
function attachSearch ( ) {
162
204
const searchInput = document . querySelector ( "[data-search]" )
163
- searchInput . addEventListener ( "input" , e => {
205
+ const handler = e => {
164
206
const value = e . target . value . replace ( / \+ / g, "\\+" )
165
207
const regex = new RegExp ( '.*' + saniStrings ( value ) + '.*' , 'ig' ) ;
166
- // const cleaned = noExtraSpc(regex) /*rm extra empty spcs in search string*/
167
208
search ( regex ) ;
168
- } )
169
- } ;
170
-
171
- function noResultsSwitch ( data ) {
209
+ }
210
+ searchInput . removeListeners ( )
211
+ searchInput . onEvent ( "input" , handler )
212
+ }
213
+
214
+ function noResultsSwitch ( data ) {
172
215
const noResults = document . getElementById ( "hide" )
173
- if ( data === undefined || data . length == 0 ) {
174
- noResults . style . display = "block" ;
216
+ if ( data === undefined || data . length == 0 ) {
217
+ noResults . style . display = "block" ;
175
218
} else {
176
- noResults . style . display = "none" ;
219
+ noResults . style . display = "none" ;
177
220
}
178
221
}
179
222
180
223
function saniStrings ( str ) {
181
224
/* remove reg/trademarks and all empty spaces */
182
- const reg_no_spcl = / [ \u00ae \u2122 \u2120 \ *] / g;
183
- const result = str . replace ( reg_no_spcl , "" ) ;
225
+ const regNoSpecial = / [ \u00ae \u2122 \u2120 * ] / g;
226
+ const result = str . replace ( regNoSpecial , "" ) ;
184
227
return result . trim ( ) . replace ( / \s + / g, " " ) ;
185
228
}
186
229
@@ -191,12 +234,12 @@ function search(regex) {
191
234
const keys = Object . keys ( value ) ;
192
235
return [ ...acc , ...keys ] ;
193
236
} , [ ] ) ;
194
- let result =
195
- regex . test ( saniStrings ( item . name ) ) ||
196
- regex . test ( saniStrings ( item . description ) ) ||
197
- regex . test ( languages . join ( ',' ) ) ||
198
- regex . test ( item . targetDevice . join ( ',' ) ) ||
199
- regex . test ( item . expertise ) ;
237
+ let result =
238
+ regex . test ( saniStrings ( item . name ) ) ||
239
+ regex . test ( saniStrings ( item . description ) ) ||
240
+ regex . test ( languages . join ( ',' ) ) ||
241
+ regex . test ( item . targetDevice . join ( ',' ) ) ||
242
+ regex . test ( item . expertise ) ;
200
243
return result
201
244
} ) ;
202
245
noResultsSwitch ( data )
@@ -205,60 +248,79 @@ function search(regex) {
205
248
state . paginatedData = [ ...data ] . slice ( 0 , state . pageSize ) ;
206
249
destroyView ( ) ;
207
250
renderUi ( data ) ;
208
-
209
251
}
210
252
211
-
212
- function onlyAlphaNumInput ( e ) {
253
+ function onlyAlphaNumInput ( e ) {
213
254
let regex1 = new RegExp ( "^[a-z-A-Z0-9\-\r|\n ]+|[\u00ae\u2122\u2120]+|[+]+$" )
214
255
let str = String . fromCharCode ( ! e . fromCharCode ? e . which : e . fromCharCode ) ;
215
256
saniStrings ( str )
216
257
const errormsg = document . getElementById ( "errormsg" ) ;
217
- if ( regex1 . test ( str ) ) {
218
- errormsg . style . display = "none" ;
258
+ if ( regex1 . test ( str ) ) {
259
+ errormsg . style . display = "none" ;
219
260
return true ;
220
- } else {
221
- errormsg . innerHTML = "Enter only letters, numbers, or plus sign." ;
261
+ } else {
262
+ errormsg . innerHTML = "Enter only letters, numbers, or plus sign." ;
222
263
errormsg . classList . toggle ( "showerror" ) ;
223
- setTimeout ( ( ) => {
264
+ setTimeout ( ( ) => {
224
265
errormsg . classList . remove ( "showerror" ) ;
225
266
} , 7000 ) ;
226
267
}
227
- e . preventDefault ( ) ;
268
+ e . preventDefault ( ) ;
228
269
}
229
270
230
- function checkboxFilterHandler ( checked , regex ) {
271
+ function checkboxFilterHandler ( checked , checkboxValue ) {
272
+ // where checkboxValue is arr
231
273
if ( checked ) {
232
- state . checkedFilters . push ( regex )
274
+ state . checkedFilters . push ( checkboxValue )
275
+ console . log ( "state.checkedFilters:" , state . checkedFilters )
233
276
} else {
234
277
const found = state . checkedFilters . findIndex ( item => {
235
- return item === regex
278
+ return item === checkboxValue
236
279
} )
237
- if ( typeof found !== 'undefined' ) {
280
+ console . log ( "found?" , found )
281
+ if ( found !== - 1 ) {
238
282
state . checkedFilters . splice ( found , 1 )
239
283
}
240
284
// console.log(`Checkbox not checked.`);
241
285
}
242
286
destroyView ( ) ;
243
- if ( state . checkedFilters . length > 0 ) {
287
+ if ( state . checkedFilters . length >= 0 ) {
288
+ // changed above from > 0 to > -1 // 25/10/2023
244
289
state . pageIndex = 1 ;
245
290
state . pageSize = + Infinity ;
246
291
state . totalPages = 1 ;
247
292
renderUi ( [ ...state . data ] ) ;
248
- } else {
293
+ } else {
294
+
249
295
reloadData ( ) ;
250
296
}
251
297
}
252
298
253
299
function filterPaginatedData ( ) {
254
300
const checkboxes = document . querySelectorAll ( 'input[name=data-filter]' ) ;
301
+ console . log ( checkboxes )
302
+
255
303
for ( let checkbox of checkboxes ) {
256
- checkbox . addEventListener ( 'change' , e => {
304
+ const handler = e => {
257
305
const checked = e . target . checked ;
258
- const regex = new RegExp ( '.*' + e . target . value + '.*' , 'ig' ) ;
259
- checkboxFilterHandler ( checked , regex )
306
+ // console.log("Checked is T/F?",checked)
307
+ const checkedParent = e . target . parentElement . parentElement
308
+ const checkBoxGroup = checkedParent . querySelectorAll ( 'input[name=data-filter]' )
309
+ console . log ( "checkBoxGroup" , checkBoxGroup )
310
+ //
311
+ checkBoxGroup . forEach ( item => {
312
+
313
+ if ( item . value !== e . target . value && item . checked == true ) {
314
+ item . checked = false
315
+ const uncheckRegex = state . checkBoxMap [ item . value ]
316
+ console . log ( "Unchecked regex...?" , uncheckRegex )
317
+ checkboxFilterHandler ( item . checked , item . value )
318
+ }
319
+ } )
320
+ checkboxFilterHandler ( checked , e . target . value )
260
321
}
261
- )
322
+ checkbox . removeListeners ( )
323
+ checkbox . onEvent ( 'change' , handler )
262
324
}
263
325
}
264
326
0 commit comments