File size: 7,805 Bytes
913928f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | /**
* Author: Peter Dragicevic [peter@petschko.org]
* Authors-Website: https://petschko.org/
* Date: 19.02.2020
* Time: 19:03
*/
(function() {
'use strict';
var eventAddMethod = window.addEventListener ? 'addEventListener' : 'attachEvent';
var menus = [];
/**
* Returns the current anchor from the URL
*
* @returns {string} - Anchor or empty string if none
*/
function getAnchorFromUrl() {
return window.location.hash.substr(1);
}
/**
* Adds a silent anchor to avoid scrolling
*
* @param {string} anchor - Anchor
*/
function addSilentAnchor(anchor) {
if(history.replaceState)
history.replaceState(null, '', '#' + anchor);
else
window.location.hash = '#' + anchor; // Fallback
}
/**
* Removes a CSS-Class
*
* @param {string} elementClasses - Element CSS-Classes
* @param {string} removeClass - Class to remove
* @returns {string} - New CSS-Classes
*/
function removeCssClass(elementClasses, removeClass) {
var newClassNames = '';
var classes = elementClasses.split(' ');
// Check if element is in array else exit
if(classes.indexOf(removeClass) === -1)
return elementClasses;
for(var i = 0; i < classes.length; i++) {
if(classes[i] !== removeClass)
newClassNames += classes[i] + ' ';
}
return newClassNames.trim();
}
/**
* Adds a CSS-Class, it never adds an existing class twice
*
* @param {string} elementClasses - Element CSS-Classes
* @param {string} addClass - Class to add
* @returns {string} - New CSS-Classes
*/
function addCssClass(elementClasses, addClass) {
var classes = elementClasses.split(' ');
if(classes.indexOf(addClass) !== -1)
return elementClasses;
return (elementClasses + ' ' + addClass).trim();
}
/**
* Check if a the searched Class is within this class-string
*
* @param {string} elementClasses - Element CSS-Classes
* @param {string} searchClass - Class to check
* @returns {boolean} - true if the class exist else false
*/
function hasCssClass(elementClasses, searchClass) {
var classes = elementClasses.split(' ');
return (classes.indexOf(searchClass) !== -1);
}
/**
* Returns the correct function for the action of an Event-Listener
*
* @param {string} action
* @returns {string}
*/
function on(action) {
return window.addEventListener ? action : 'on' + action;
}
/**
* Adds a Event-Listener cross-browser
*
* @param {Element|Node|HTMLElement} element
* @param {string} action
* @param {function} callable
*/
function addEventListener(element, action, callable) {
element[eventAddMethod](on(action), callable);
}
// Menu Actions
/**
* Updates all menus when clicking on anchor link
*/
function updateMenuFromHref() {
var menuHref = document.getElementsByClassName('menu-link');
if(! menuHref)
return;
for(var i = 0; i < menuHref.length; i++) {
addEventListener(menuHref[i], 'click', function(e) {
if(this.href.indexOf('#') === -1)
return;
// e.preventDefault();
var anchor = this.href.split('#')[1];
// Handle all menus
for(var n = 0; n < menus.length; n++) {
menus[n].updateFromAnchor(anchor);
}
});
}
}
/**
* Searches Menus and adds functions on it
*/
function findMenus() {
var menuElements = document.getElementsByClassName('nav-tabs');
if(! menuElements)
return;
for(var i = 0; i < menuElements.length; i++)
menus.push(new Menu(menuElements[i]));
// Add external menu functions
updateMenuFromHref();
}
/**
* Creates a new interactive menu
*
* @param {Element|Node|HTMLElement} menuElement - Menu-List-Element
* @constructor - Menu
*/
function Menu(menuElement) {
this.menuItems = menuElement.getElementsByClassName('nav-link');
this.activeIndex = -1;
this.displayElementIds = [];
/**
* Updates the Active index
*/
this.findActiveIndex = function() {
for(var i = 0; i < this.menuItems.length; i++) {
if(hasCssClass(this.menuItems[i].className, 'active')) {
this.activeIndex = i;
return
}
}
this.activeIndex = -1;
};
/**
* Finds the active Menu-Item when a anchor is given
*/
this.findActiveIndexFromAnchor = function() {
var anchor = getAnchorFromUrl();
for(var i = 0; i < this.menuItems.length; i++) {
if(this.getIdDisplayId(this.menuItems[i]) === anchor) {
this.activeIndex = i;
return;
}
}
this.activeIndex = -1;
};
/**
* Updates the menu with the given anchor if found
*
* @param {string} anchor - Anchor
* @returns {boolean} - Was found and updated
*/
this.updateFromAnchor = function(anchor) {
var found = false;
for(var i = 0; i < this.menuItems.length; i++) {
if(this.getIdDisplayId(this.menuItems[i]) === anchor) {
this.activeIndex = i;
this.updateMenuTo(this.activeIndex);
found = true;
break;
}
}
return found;
};
/**
* Gets the Display Id of an element
*
* @param menuItem - Menu-Item
* @returns {string} - Display ID
*/
this.getIdDisplayId = function(menuItem) {
var anchor = menuItem.getElementsByTagName('a');
return anchor[0].href.split('#')[1];
};
/**
* Removes click ability from the links (To avoid scrolling)
*/
this.preventDefaultLink = function() {
for(var i = 0; i < this.menuItems.length; i++) {
var anchor = this.menuItems[i].getElementsByTagName('a')[0];
addEventListener(anchor, 'click', function(e) {
if(this.href.indexOf('#') === -1)
return;
e.preventDefault();
addSilentAnchor(this.href.split('#')[1]);
});
}
};
/**
* Hides a container with that index
*
* @param {number} index - Index to hide
*/
this.hideContainer = function(index) {
var container = document.getElementById(this.displayElementIds[index]);
if(! container)
return;
container.className = addCssClass(container.className, 'hidden');
};
/**
* Shows a container with that index
*
* @param {number} index - Index to show
*/
this.showContainer = function(index) {
var container = document.getElementById(this.displayElementIds[index]);
if(! container)
return;
container.className = removeCssClass(container.className, 'hidden');
};
/**
* Updates the Menu and container to the index
*
* @param {number} index - New active index
*/
this.updateMenuTo = function(index) {
this.activeIndex = index;
for(var i = 0; i < this.menuItems.length; i++) {
if(i === index) {
this.menuItems[i].className = addCssClass(this.menuItems[i].className, 'active');
this.showContainer(i);
} else {
this.menuItems[i].className = removeCssClass(this.menuItems[i].className, 'active');
this.hideContainer(i);
}
}
};
/**
* Inits the Menu
*/
this.init = function() {
// Check if an anchor is given
if(getAnchorFromUrl())
this.findActiveIndexFromAnchor();
// If nothing is found or given still search normal
if(this.activeIndex === -1)
this.findActiveIndex();
for(var i = 0; i < this.menuItems.length; i++) {
this.displayElementIds.push(this.getIdDisplayId(this.menuItems[i]));
// Add Listener
addEventListener(this.menuItems[i], 'click', function(that, updateIndex) {
return function(e) {
that.updateMenuTo(updateIndex);
}
}(this, i));
if(this.activeIndex !== i)
this.hideContainer(i);
}
if(this.activeIndex === -1)
this.updateMenuTo(0);
else
this.updateMenuTo(this.activeIndex);
this.preventDefaultLink();
};
// Init
this.init();
}
/**
* initialized when page is loaded
*/
function init() {
findMenus();
}
window[window.addEventListener ? 'addEventListener' : 'attachEvent'](
window.addEventListener ? 'load' : 'onload', init, false);
})();
|