szurubooru/client/js/views/top_navigation_view.js

57 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-06-13 22:34:39 +02:00
'use strict';
const views = require('../util/views.js');
const template = views.getTemplate('top-navigation');
2016-06-13 22:34:39 +02:00
class TopNavigationView {
constructor() {
this._hostNode = document.getElementById('top-navigation-holder');
2016-06-13 22:34:39 +02:00
}
2017-12-16 03:45:51 +01:00
get _mobileNavigationToggleNode() {
return this._hostNode.querySelector('#mobile-navigation-toggle');
}
get _navigationListNode() {
return this._hostNode.querySelector('nav > ul');
}
get _navigationLinkNodes() {
return this._navigationListNode.querySelectorAll('li > a');
}
2016-06-13 22:34:39 +02:00
render(ctx) {
views.replaceContent(this._hostNode, template(ctx));
2017-12-16 03:45:51 +01:00
this._bindMobileNavigationEvents();
2016-06-13 22:34:39 +02:00
}
activate(key) {
for (let itemNode of this._hostNode.querySelectorAll('[data-name]')) {
2016-06-13 22:34:39 +02:00
itemNode.classList.toggle(
'active', itemNode.getAttribute('data-name') === key);
}
}
2017-12-16 03:45:51 +01:00
_bindMobileNavigationEvents() {
this._mobileNavigationToggleNode.addEventListener(
'click', e => this._mobileNavigationToggleClick(e));
for (let navigationLinkNode of this._navigationLinkNodes) {
navigationLinkNode.addEventListener(
'click', e => this._navigationLinkClick(e));
}
}
_mobileNavigationToggleClick(e) {
this._navigationListNode.classList.toggle('opened');
}
_navigationLinkClick(e) {
this._navigationListNode.classList.remove('opened');
}
2016-06-13 22:34:39 +02:00
}
module.exports = TopNavigationView;