prettyify code
This commit is contained in:
@ -1,69 +1,83 @@
|
||||
export const FAVORITE_SERVERS_STORAGE_KEY = 'minetrack_favorite_servers'
|
||||
export const FAVORITE_SERVERS_STORAGE_KEY = "minetrack_favorite_servers";
|
||||
|
||||
export class FavoritesManager {
|
||||
constructor (app) {
|
||||
this._app = app
|
||||
constructor(app) {
|
||||
this._app = app;
|
||||
}
|
||||
|
||||
loadLocalStorage () {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
let serverNames = localStorage.getItem(FAVORITE_SERVERS_STORAGE_KEY)
|
||||
loadLocalStorage() {
|
||||
if (typeof localStorage !== "undefined") {
|
||||
let serverNames = localStorage.getItem(FAVORITE_SERVERS_STORAGE_KEY);
|
||||
if (serverNames) {
|
||||
serverNames = JSON.parse(serverNames)
|
||||
serverNames = JSON.parse(serverNames);
|
||||
|
||||
for (let i = 0; i < serverNames.length; i++) {
|
||||
const serverRegistration = this._app.serverRegistry.getServerRegistration(serverNames[i])
|
||||
const serverRegistration =
|
||||
this._app.serverRegistry.getServerRegistration(serverNames[i]);
|
||||
|
||||
// The serverName may not exist in the backend configuration anymore
|
||||
// Ensure serverRegistration is defined before mutating data or considering valid
|
||||
if (serverRegistration) {
|
||||
serverRegistration.isFavorite = true
|
||||
serverRegistration.isFavorite = true;
|
||||
|
||||
// Update icon since by default it is unfavorited
|
||||
document.getElementById(`favorite-toggle_${serverRegistration.serverId}`).setAttribute('class', this.getIconClass(serverRegistration.isFavorite))
|
||||
document
|
||||
.getElementById(`favorite-toggle_${serverRegistration.serverId}`)
|
||||
.setAttribute(
|
||||
"class",
|
||||
this.getIconClass(serverRegistration.isFavorite)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updateLocalStorage () {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
updateLocalStorage() {
|
||||
if (typeof localStorage !== "undefined") {
|
||||
// Mutate the serverIds array into server names for storage use
|
||||
const serverNames = this._app.serverRegistry.getServerRegistrations()
|
||||
.filter(serverRegistration => serverRegistration.isFavorite)
|
||||
.map(serverRegistration => serverRegistration.data.name)
|
||||
const serverNames = this._app.serverRegistry
|
||||
.getServerRegistrations()
|
||||
.filter((serverRegistration) => serverRegistration.isFavorite)
|
||||
.map((serverRegistration) => serverRegistration.data.name);
|
||||
|
||||
if (serverNames.length > 0) {
|
||||
// Only save if the array contains data, otherwise clear the item
|
||||
localStorage.setItem(FAVORITE_SERVERS_STORAGE_KEY, JSON.stringify(serverNames))
|
||||
localStorage.setItem(
|
||||
FAVORITE_SERVERS_STORAGE_KEY,
|
||||
JSON.stringify(serverNames)
|
||||
);
|
||||
} else {
|
||||
localStorage.removeItem(FAVORITE_SERVERS_STORAGE_KEY)
|
||||
localStorage.removeItem(FAVORITE_SERVERS_STORAGE_KEY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handleFavoriteButtonClick = (serverRegistration) => {
|
||||
serverRegistration.isFavorite = !serverRegistration.isFavorite
|
||||
serverRegistration.isFavorite = !serverRegistration.isFavorite;
|
||||
|
||||
// Update the displayed favorite icon
|
||||
document.getElementById(`favorite-toggle_${serverRegistration.serverId}`).setAttribute('class', this.getIconClass(serverRegistration.isFavorite))
|
||||
document
|
||||
.getElementById(`favorite-toggle_${serverRegistration.serverId}`)
|
||||
.setAttribute("class", this.getIconClass(serverRegistration.isFavorite));
|
||||
|
||||
// Request the app controller instantly re-sort the server listing
|
||||
// This handles the favorite sorting logic internally
|
||||
this._app.sortController.sortServers()
|
||||
this._app.sortController.sortServers();
|
||||
|
||||
this._app.graphDisplayManager.handleServerIsFavoriteUpdate(serverRegistration)
|
||||
this._app.graphDisplayManager.handleServerIsFavoriteUpdate(
|
||||
serverRegistration
|
||||
);
|
||||
|
||||
// Write an updated settings payload
|
||||
this.updateLocalStorage()
|
||||
}
|
||||
this.updateLocalStorage();
|
||||
};
|
||||
|
||||
getIconClass (isFavorite) {
|
||||
getIconClass(isFavorite) {
|
||||
if (isFavorite) {
|
||||
return 'icon-star server-is-favorite'
|
||||
return "icon-star server-is-favorite";
|
||||
} else {
|
||||
return 'icon-star-o server-is-not-favorite'
|
||||
return "icon-star-o server-is-not-favorite";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user