Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Development_of_Web_Applications
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Silas Meister
Development_of_Web_Applications
Commits
80612694
Commit
80612694
authored
1 year ago
by
Silas Meister
Browse files
Options
Downloads
Patches
Plain Diff
Highlight search results
parent
aad297ea
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
functions.js
+55
-13
55 additions, 13 deletions
functions.js
styles.css
+4
-0
4 additions, 0 deletions
styles.css
with
59 additions
and
13 deletions
functions.js
+
55
−
13
View file @
80612694
...
@@ -24,14 +24,14 @@ function handleSearchChanged() {
...
@@ -24,14 +24,14 @@ function handleSearchChanged() {
}
}
async
function
onInputChanged
(
event
)
{
async
function
onInputChanged
(
event
)
{
le
t
searchWord
=
event
.
target
.
value
;
cons
t
searchWord
=
event
.
target
.
value
;
le
t
json
=
await
fetchNews
(
searchWord
);
cons
t
json
=
await
fetchNews
(
searchWord
);
if
(
json
===
undefined
)
if
(
json
===
undefined
)
return
;
return
;
displaySearchResults
(
json
);
displaySearchResults
(
json
,
searchWord
);
}
}
function
fetchNews
(
searchWord
)
{
function
fetchNews
(
searchWord
)
{
...
@@ -48,7 +48,7 @@ function fetchNews(searchWord) {
...
@@ -48,7 +48,7 @@ function fetchNews(searchWord) {
return
request
;
return
request
;
}
}
function
displaySearchResults
(
json
)
{
function
displaySearchResults
(
json
,
searchWord
)
{
const
searchResults
=
document
.
getElementById
(
'
search-results
'
);
const
searchResults
=
document
.
getElementById
(
'
search-results
'
);
searchResults
.
innerText
=
""
;
searchResults
.
innerText
=
""
;
...
@@ -56,28 +56,26 @@ function displaySearchResults(json) {
...
@@ -56,28 +56,26 @@ function displaySearchResults(json) {
const
ul
=
document
.
createElement
(
'
ul
'
);
const
ul
=
document
.
createElement
(
'
ul
'
);
json
.
forEach
((
news
)
=>
{
json
.
forEach
((
news
)
=>
{
const
li
=
createNewsChild
(
news
);
const
li
=
createNewsChild
(
news
,
searchWord
);
ul
.
appendChild
(
li
);
ul
.
appendChild
(
li
);
});
});
console
.
log
(
ul
);
searchResults
.
appendChild
(
ul
);
searchResults
.
appendChild
(
ul
);
const
display
=
json
.
length
===
0
?
"
none
"
:
"
block
"
;;
const
display
=
json
.
length
===
0
?
"
none
"
:
"
block
"
;;
searchResults
.
style
.
display
=
display
;
searchResults
.
style
.
display
=
display
;
}
}
function
createNewsChild
(
news
)
{
function
createNewsChild
(
news
,
searchWord
)
{
const
title
=
news
[
'
title
'
];
const
id
=
news
[
'
id
'
];
const
li
=
document
.
createElement
(
'
li
'
);
const
li
=
document
.
createElement
(
'
li
'
);
const
a
=
document
.
createElement
(
'
a
'
);
const
a
=
createLinkWithHighlightedParts
(
title
,
searchWord
)
const
textNode
=
document
.
createTextNode
(
news
[
'
title
'
]);
a
.
appendChild
(
textNode
);
const
hrefAttr
=
document
.
createAttribute
(
'
href
'
);
const
hrefAttr
=
document
.
createAttribute
(
'
href
'
);
hrefAttr
.
value
=
'
NewsArticle.php?articleId=
'
+
encodeURIComponent
(
news
[
'
id
'
]
);
hrefAttr
.
value
=
'
NewsArticle.php?articleId=
'
+
encodeURIComponent
(
id
);
a
.
setAttributeNode
(
hrefAttr
);
a
.
setAttributeNode
(
hrefAttr
);
...
@@ -86,6 +84,50 @@ function createNewsChild(news) {
...
@@ -86,6 +84,50 @@ function createNewsChild(news) {
return
li
;
return
li
;
}
}
function
createLinkWithHighlightedParts
(
title
,
searchWord
)
{
const
a
=
document
.
createElement
(
'
a
'
);
const
indices
=
[...
title
.
toLowerCase
().
matchAll
(
new
RegExp
(
searchWord
,
'
gi
'
))].
map
((
a
)
=>
a
.
index
).
sort
((
a
,
b
)
=>
b
-
a
);
const
length
=
searchWord
.
length
;
const
separator
=
'
*
'
;
for
(
const
index
of
indices
)
{
title
=
title
.
substr
(
0
,
index
)
+
separator
+
title
.
substr
(
index
,
length
)
+
separator
+
title
.
substr
(
index
+
length
);
}
const
separatedBySearchWord
=
title
.
split
(
separator
);
const
nodes
=
separatedBySearchWord
.
map
((
element
)
=>
{
if
(
element
.
toLowerCase
()
===
searchWord
.
toLowerCase
())
return
createSpanNode
(
element
);
else
return
createTextNode
(
element
);
})
a
.
append
(...
nodes
);
return
a
;
}
function
createSpanNode
(
text
)
{
const
span
=
document
.
createElement
(
'
span
'
);
const
textNode
=
createTextNode
(
text
);
span
.
appendChild
(
textNode
);
const
classAttribute
=
document
.
createAttribute
(
'
class
'
);
classAttribute
.
value
=
'
highlight
'
;
span
.
setAttributeNode
(
classAttribute
);
return
span
;
}
function
createTextNode
(
text
)
{
const
textNode
=
document
.
createTextNode
(
text
);
return
textNode
;
}
function
slideImage
()
{
function
slideImage
()
{
slideNumber
=
(
slideNumber
+
1
)
%
3
;
slideNumber
=
(
slideNumber
+
1
)
%
3
;
...
...
This diff is collapsed.
Click to expand it.
styles.css
+
4
−
0
View file @
80612694
...
@@ -142,6 +142,10 @@ a {
...
@@ -142,6 +142,10 @@ a {
white-space
:
pre-wrap
;
white-space
:
pre-wrap
;
}
}
.highlight
{
color
:
yellow
;
}
.admin-button
a
{
.admin-button
a
{
display
:
block
;
display
:
block
;
padding
:
0.5rem
;
padding
:
0.5rem
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment