Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
TP3 - API DOM
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Lucas Hottin
TP3 - API DOM
Commits
2fc84d45
Commit
2fc84d45
authored
4 years ago
by
Lucas Hottin
Browse files
Options
Downloads
Patches
Plain Diff
"test"
parent
1226be4b
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
src/Router.js
+31
-19
31 additions, 19 deletions
src/Router.js
src/main.js
+2
-1
2 additions, 1 deletion
src/main.js
with
33 additions
and
20 deletions
src/Router.js
+
31
−
19
View file @
2fc84d45
export
default
class
Router
{
static
titleElement
;
static
contentElement
;
static
routes
=
[];
static
#menuElement
;
static
routes
=
[];
/**
* Indique au Router la balise HTML contenant le menu de navigation
* Écoute le clic sur chaque lien et déclenche la méthode navigate
* @param element Élément HTML de la page qui contient le menu principal
*/
static
set
menuElement
(
element
)
{
this
.
#menuElement
=
element
;
console
.
log
(
this
.
#menuElement
);
this
.
#menuElement
.
addEventListener
(
'
click
'
,
this
.
handleClick
);
const
links
=
element
.
querySelectorAll
(
'
a
'
);
links
.
forEach
(
link
=>
link
.
addEventListener
(
'
click
'
,
event
=>
{
event
.
preventDefault
();
this
.
navigate
(
event
.
target
.
getAttribute
(
'
href
'
));
})
);
}
static
handleClick
(
event
)
{
event
.
preventDefault
();
console
.
log
(
event
);
Router
.
navigate
(
event
.
target
.
getAttribute
(
'
href
'
));
event
.
target
.
classList
.
add
(
'
active
'
);
}
static
navigate
(
path
)
{
/**
* Navigue dans l'application
* Affiche la page correspondant à `path` dans le tableau `routes`
* @param {String} path URL de la page courante
* @param {Boolean} pushState active/désactive le pushState (ajout d'une entrée dans l'historique de navigation)
*/
static
navigate
(
path
,
pushState
=
true
)
{
const
route
=
this
.
routes
.
find
(
route
=>
route
.
path
===
path
);
if
(
route
)
{
this
.
titleElement
.
innerHTML
=
`<h1>
${
route
.
title
}
</h1>`
;
this
.
contentElement
.
innerHTML
=
route
.
page
.
render
();
console
.
log
(
route
);
route
.
page
.
mount
?.(
this
.
contentElement
);
window
.
history
.
pushState
({},
''
,
path
);
const
d
=
document
.
querySelectorAll
(
'
.mainMenu a
'
);
d
.
forEach
(
element
=>
{
if
(
element
.
textContent
==
route
.
title
)
element
.
classList
.
add
(
'
active
'
);
else
element
.
classList
.
remove
(
'
active
'
);
});
// gestion menu (classe "active")
const
menuLink
=
this
.
#menuElement
.
querySelector
(
`a[href="
${
route
.
path
}
"]`
),
prevActiveLink
=
this
.
#menuElement
.
querySelector
(
'
a.active
'
);
prevActiveLink
?.
classList
.
remove
(
'
active
'
);
menuLink
?.
classList
.
add
(
'
active
'
);
if
(
pushState
)
{
window
.
history
.
pushState
(
null
,
null
,
path
);
}
}
}
}
This diff is collapsed.
Click to expand it.
src/main.js
+
2
−
1
View file @
2fc84d45
...
...
@@ -36,5 +36,6 @@ close.addEventListener('click', handleClick);
window
.
onpopstate
=
e
=>
{
console
.
log
(
document
.
location
);
Router
.
navigate
(
document
.
location
.
pathname
);
Router
.
navigate
(
document
.
location
.
pathname
,
false
);
};
window
.
onpopstate
();
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