-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59401b0
commit 7938908
Showing
3 changed files
with
119 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
/** | ||
* @file Utilities to manipulate the DOM | ||
*/ | ||
|
||
module.exports = { | ||
$(query, element) { | ||
return (element || document).querySelector(query); | ||
}, | ||
$$(query, element) { | ||
return (element || document).querySelectorAll(query); | ||
}, | ||
on(element, event, handler) { | ||
element.addEventListener(event, handler); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* @file Provides drowdown menus for the main navbar, for better site navigation | ||
*/ | ||
|
||
let dom = require('./dom'); | ||
|
||
module.exports = function() { | ||
// Get user handle | ||
const handle = dom.$('.lang-chooser').children[1].children[0].innerText.trim(); | ||
|
||
let oldNav = dom.$('.main-menu-list'); | ||
let newNav = document.createElement('nav'); | ||
newNav.className = 'cfpp-navbar'; | ||
|
||
// Without this the dropdowns don't appear | ||
oldNav.parentNode.parentNode.style.overflow = 'visible'; | ||
|
||
let keys = { | ||
"/groups": { | ||
"My Groups": `/groups/with/${handle}`, | ||
"My Teams": `/teams/with/${handle}`, | ||
}, | ||
"/problemset": { | ||
"Status": "/problemset/status", | ||
"Friends Status": "/problemset/status?friends=on", | ||
"My Submissions": `/submissions/${handle}`, | ||
"Favourites": `/favourite/problems`, | ||
}, | ||
"/contests": { | ||
"My Contests": `/contests/with/${handle}`, | ||
}, | ||
"/gyms": { | ||
"Mashups": "/mashups", | ||
}, | ||
"/ratings": { | ||
"Friends": "/ratings/friend/true", | ||
}, | ||
}; | ||
|
||
// Iterate over all nav items and include them the new navbar | ||
for (let item of oldNav.children) { | ||
let link = item.children[0]; // <a> tag | ||
|
||
// Create new item and append the old <a> to it | ||
let newItem = document.createElement('div'); | ||
newItem.className = 'cfpp-navbar-item'; | ||
newItem.appendChild(link); | ||
|
||
// Add dropdown menu, if needed | ||
const href = link.getAttribute('href'); | ||
if (keys[href]) { | ||
let dropdown = document.createElement('div'); | ||
dropdown.className = 'cfpp-dropdown'; | ||
|
||
for (let ddText in keys[href]) { | ||
let ddItem = document.createElement('a'); | ||
ddItem.innerText = ddText; | ||
ddItem.href = keys[href][ddText]; | ||
dropdown.appendChild(ddItem); | ||
} | ||
|
||
newItem.appendChild(dropdown); | ||
} | ||
|
||
newNav.appendChild(newItem); | ||
} | ||
|
||
oldNav.replaceWith(newNav); | ||
|
||
|
||
// Create styling for the new menu | ||
const style = ` | ||
.cfpp-navbar { | ||
margin-left: 1.5em; | ||
} | ||
.cfpp-navbar-item { | ||
display: inline-block; | ||
position: relative; | ||
margin-right: 1.5em; | ||
} | ||
.cfpp-navbar-item>a { | ||
color: #212121; | ||
} | ||
.cfpp-dropdown { | ||
position: absolute; | ||
top: 100%; | ||
left: 0; | ||
width: 200%; | ||
z-index: 99; | ||
display: none; | ||
background: #212121; | ||
padding: 1em; | ||
box-shadow: 1px 7px 19px #00000054; | ||
} | ||
.cfpp-dropdown a { | ||
display: block; | ||
color: #E0E0E0; | ||
} | ||
.cfpp-navbar-item:hover .cfpp-dropdown { | ||
display: block; | ||
} | ||
`; | ||
|
||
let styleTag = document.createElement('style'); | ||
styleTag.innerHTML = style; | ||
document.body.appendChild(styleTag); | ||
}; |