debian-mirror-gitlab/app/assets/javascripts/signin_tabs_memoizer.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint no-param-reassign: ["error", { "props": false }]*/
/* eslint no-new: "off" */
import AccessorUtilities from './lib/utils/accessor';
2017-09-10 17:25:29 +05:30
/**
* Memorize the last selected tab after reloading a page.
* Does that setting the current selected tab in the localStorage
*/
export default class SigninTabsMemoizer {
constructor({ currentTabKey = 'current_signin_tab', tabSelector = 'ul.nav-tabs' } = {}) {
this.currentTabKey = currentTabKey;
this.tabSelector = tabSelector;
this.isLocalStorageAvailable = AccessorUtilities.isLocalStorageAccessSafe();
this.bootstrap();
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
bootstrap() {
const tabs = document.querySelectorAll(this.tabSelector);
if (tabs.length > 0) {
tabs[0].addEventListener('click', (e) => {
if (e.target && e.target.nodeName === 'A') {
const anchorName = e.target.getAttribute('href');
this.saveData(anchorName);
2017-08-17 22:00:37 +05:30
}
2017-09-10 17:25:29 +05:30
});
2017-08-17 22:00:37 +05:30
}
2017-09-10 17:25:29 +05:30
this.showTab();
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
showTab() {
const anchorName = this.readData();
if (anchorName) {
const tab = document.querySelector(`${this.tabSelector} a[href="${anchorName}"]`);
if (tab) {
tab.click();
}
2017-08-17 22:00:37 +05:30
}
2017-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
saveData(val) {
if (!this.isLocalStorageAvailable) return undefined;
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
return window.localStorage.setItem(this.currentTabKey, val);
2017-08-17 22:00:37 +05:30
}
2017-09-10 17:25:29 +05:30
readData() {
if (!this.isLocalStorageAvailable) return null;
return window.localStorage.getItem(this.currentTabKey);
}
}