2016-12-13 22:01:05 -05:00
|
|
|
/* eslint-disable arrow-parens, class-methods-use-this, no-param-reassign, padded-blocks */
|
|
|
|
/* global Cookies */
|
|
|
|
|
2016-09-02 02:33:43 -04:00
|
|
|
((global) => {
|
|
|
|
let singleton;
|
|
|
|
|
|
|
|
const pinnedStateCookie = 'pin_nav';
|
|
|
|
const sidebarBreakpoint = 1024;
|
|
|
|
|
|
|
|
const pageSelector = '.page-with-sidebar';
|
|
|
|
const navbarSelector = '.navbar-fixed-top';
|
|
|
|
const sidebarWrapperSelector = '.sidebar-wrapper';
|
|
|
|
const sidebarContentSelector = '.nav-sidebar';
|
|
|
|
|
|
|
|
const pinnedToggleSelector = '.js-nav-pin';
|
|
|
|
const sidebarToggleSelector = '.toggle-nav-collapse, .side-nav-toggle';
|
|
|
|
|
|
|
|
const pinnedPageClass = 'page-sidebar-pinned';
|
|
|
|
const expandedPageClass = 'page-sidebar-expanded';
|
|
|
|
|
2016-09-02 11:34:04 -04:00
|
|
|
const pinnedNavbarClass = 'header-sidebar-pinned';
|
|
|
|
const expandedNavbarClass = 'header-sidebar-expanded';
|
2016-09-02 02:33:43 -04:00
|
|
|
|
|
|
|
class Sidebar {
|
|
|
|
constructor() {
|
|
|
|
if (!singleton) {
|
|
|
|
singleton = this;
|
|
|
|
singleton.init();
|
|
|
|
}
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2016-10-24 17:26:41 -04:00
|
|
|
this.isPinned = Cookies.get(pinnedStateCookie) === 'true';
|
2016-09-02 02:33:43 -04:00
|
|
|
this.isExpanded = (
|
|
|
|
window.innerWidth >= sidebarBreakpoint &&
|
|
|
|
$(pageSelector).hasClass(expandedPageClass)
|
|
|
|
);
|
|
|
|
$(document)
|
2016-10-03 05:38:21 -04:00
|
|
|
.on('click', sidebarToggleSelector, () => this.toggleSidebar())
|
|
|
|
.on('click', pinnedToggleSelector, () => this.togglePinnedState())
|
2016-09-02 21:23:35 -04:00
|
|
|
.on('click', 'html, body', (e) => this.handleClickEvent(e))
|
2016-08-08 17:19:46 -04:00
|
|
|
.on('page:change', () => this.renderState())
|
|
|
|
.on('todo:toggle', (e, count) => this.updateTodoCount(count));
|
2016-09-02 02:33:43 -04:00
|
|
|
this.renderState();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClickEvent(e) {
|
|
|
|
if (this.isExpanded && (!this.isPinned || window.innerWidth < sidebarBreakpoint)) {
|
|
|
|
const $target = $(e.target);
|
|
|
|
const targetIsToggle = $target.closest(sidebarToggleSelector).length > 0;
|
|
|
|
const targetIsSidebar = $target.closest(sidebarWrapperSelector).length > 0;
|
|
|
|
if (!targetIsToggle && (!targetIsSidebar || $target.closest('a'))) {
|
2016-10-03 05:38:21 -04:00
|
|
|
this.toggleSidebar();
|
2016-09-02 02:33:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 17:19:46 -04:00
|
|
|
updateTodoCount(count) {
|
|
|
|
$('.js-todos-count').text(gl.text.addDelimiter(count));
|
|
|
|
}
|
|
|
|
|
2016-10-03 05:38:21 -04:00
|
|
|
toggleSidebar() {
|
2016-09-02 02:33:43 -04:00
|
|
|
this.isExpanded = !this.isExpanded;
|
|
|
|
this.renderState();
|
|
|
|
}
|
|
|
|
|
2016-10-03 05:38:21 -04:00
|
|
|
togglePinnedState() {
|
2016-09-02 02:33:43 -04:00
|
|
|
this.isPinned = !this.isPinned;
|
|
|
|
if (!this.isPinned) {
|
|
|
|
this.isExpanded = false;
|
|
|
|
}
|
2016-10-24 17:54:38 -04:00
|
|
|
Cookies.set(pinnedStateCookie, this.isPinned ? 'true' : 'false', { expires: 3650 });
|
2016-09-02 02:33:43 -04:00
|
|
|
this.renderState();
|
|
|
|
}
|
|
|
|
|
|
|
|
renderState() {
|
|
|
|
$(pageSelector)
|
|
|
|
.toggleClass(pinnedPageClass, this.isPinned && this.isExpanded)
|
2016-09-02 11:34:04 -04:00
|
|
|
.toggleClass(expandedPageClass, this.isExpanded);
|
2016-09-02 02:33:43 -04:00
|
|
|
$(navbarSelector)
|
|
|
|
.toggleClass(pinnedNavbarClass, this.isPinned && this.isExpanded)
|
2016-09-02 11:34:04 -04:00
|
|
|
.toggleClass(expandedNavbarClass, this.isExpanded);
|
2016-09-02 02:33:43 -04:00
|
|
|
|
|
|
|
const $pinnedToggle = $(pinnedToggleSelector);
|
|
|
|
const tooltipText = this.isPinned ? 'Unpin navigation' : 'Pin navigation';
|
|
|
|
const tooltipState = $pinnedToggle.attr('aria-describedby') && this.isExpanded ? 'show' : 'hide';
|
|
|
|
$pinnedToggle.attr('title', tooltipText).tooltip('fixTitle').tooltip(tooltipState);
|
|
|
|
|
|
|
|
if (this.isExpanded) {
|
|
|
|
setTimeout(() => $(sidebarContentSelector).niceScroll().updateScrollBar(), 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global.Sidebar = Sidebar;
|
|
|
|
|
|
|
|
})(window.gl || (window.gl = {}));
|