the-glorious-startpage/js/dock-buttons.js

126 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-06-09 04:02:03 +02:00
class DockButtons {
2020-06-04 02:14:24 +02:00
2020-06-09 04:02:03 +02:00
constructor() {
this._dock = document.querySelector('#dock');
// Retrieve dockSites object from Config instance
this.dockSites = config.getDockSites();
// Populate dock
this._populateDock();
}
_buildDockButton = (id, className, callback = null) => {
2020-06-04 02:14:24 +02:00
2020-06-07 09:18:09 +02:00
const dockButton = document.createElement('div');
2020-06-09 04:02:03 +02:00
dockButton.id = `button${id}`;
2020-06-04 02:35:05 +02:00
dockButton.className = 'dockButton';
2020-06-09 04:02:03 +02:00
dockButton.onmouseup = callback;
return dockButton;
}
_buildDockButtonImage = (id, className, background) => {
2020-06-04 02:14:24 +02:00
2020-06-07 09:18:09 +02:00
const buttonImage = document.createElement('div');
2020-06-09 04:02:03 +02:00
buttonImage.id = id;
buttonImage.className = className;
buttonImage.style.background = background;
2020-06-04 02:14:24 +02:00
buttonImage.style.backgroundSize = 'cover';
2020-06-09 04:02:03 +02:00
return buttonImage;
}
_generateFromManual = (id, icon, callback) => {
const dockButton = this._buildDockButton(
`button${id}`,
'dockButton',
callback
);
const buttonImage = this._buildDockButtonImage(
`buttonImage${id}`,
'dockButtonImage',
`url('assets/buttons/${icon}.svg')`
);
2020-06-04 02:35:05 +02:00
dockButton.appendChild(buttonImage);
2020-06-04 02:39:09 +02:00
2020-06-09 04:02:03 +02:00
this._dock.appendChild(dockButton);
}
2020-06-04 02:39:09 +02:00
2020-06-09 04:02:03 +02:00
_generateFromList = () => {
2020-06-04 02:39:09 +02:00
2020-06-09 04:02:03 +02:00
for (let i = 0; i < (this.dockSites.length); i++) {
const site = this.dockSites[i].site;
const icon = this.dockSites[i].icon;
const url = this.dockSites[i].url;
// Create a href
const aDock = document.createElement('a');
aDock.className = 'dockLink';
aDock.href = url;
2020-06-09 12:13:12 +02:00
aDock.tabIndex = '-1';
2020-06-09 04:02:03 +02:00
// Create div container
const dockButton = this._buildDockButton(
site,
'dockButton'
);
// Create div container for button icon
const buttonImage = this._buildDockButtonImage(
`buttonImage${i}`,
'dockButtonImage',
`url('assets/webcons/${icon}.svg')`
);
// Append divs
dockButton.appendChild(buttonImage);
aDock.appendChild(dockButton);
2020-06-04 02:14:24 +02:00
2020-06-09 04:02:03 +02:00
this._dock.appendChild(aDock);
};
}
2020-06-04 02:39:09 +02:00
2020-06-09 04:02:03 +02:00
_populateDock = () => {
// Create launcher button
this._generateFromManual(
'Launch',
'launch',
() => {
// Toggle web menu
webMenu.toggleWebMenu();
}
);
// Create dock buttons fetched from sites-list.js
this._generateFromList();
// Create weather button
this._generateFromManual(
'Weather',
'weather',
() => {
// Toggle weather screen
weatherScreen.toggleWeatherScreen();
}
);
// Create menu button
this._generateFromManual(
'Dashboard',
'dashboard',
() => {
// Toggle dashboard
dashboard.toggleDashboard();
}
);
}
2020-06-04 02:14:24 +02:00
}