// updateCompasses.js
(function () {
/* global AdventureJS A */
var p = AdventureJS.Display.prototype;
/**
* Set exits in status bar and custom compass roses.
* @method AdventureJS.Display#updateCompasses
* @param {String} exits
*/
p.updateCompasses = function Display_updateCompasses(exits_list) {
let exit_names = "";
if (exits_list) {
exit_names = Object.keys(exits_list).join(" ");
} else {
exits_list = {};
let room_exits = this.game.getRoom().exits;
for (const id in room_exits) {
const exit_asset = this.game.getAsset(room_exits[id]);
if (exit_asset?.destination) {
exit_names += ` ${exit_asset.direction}`;
exits_list[exit_asset.direction] = exit_asset;
}
}
}
this.compasses.forEach(function (compass) {
compass?.setAttribute("class", `ajs-compass ${exit_names}`);
compass
?.querySelectorAll(".ajs-compass-direction")
.forEach(function (direction) {
const exit_asset = exits_list[direction.dataset.exit];
// reset all compass directions
direction.blur();
direction.setAttribute("aria-hidden", true);
direction.setAttribute("tabindex", "-1");
direction.removeAttribute("role");
direction.classList.remove(
"ajs-used",
"ajs-closed",
"ajs-locked",
"ajs-sealed",
"ajs-enabled"
);
direction.querySelector("title")?.remove();
// is this direction listed in room exits?
// if (exit_names.includes(direction.dataset.exit)) {
const dir = direction.dataset.exit;
const regex = new RegExp(`\\b${dir}\\b`);
if (regex.test(exit_names)) {
direction.setAttribute("aria-hidden", false);
direction.setAttribute("tabindex", "0");
direction.classList.add("ajs-enabled");
direction.setAttribute("role", "button");
if (!direction.querySelector("title")) {
const title = document.createElementNS(
"http://www.w3.org/2000/svg",
"title"
);
title.textContent = direction.getAttribute("aria-label");
direction.prepend(title);
}
if (exit_asset) {
if (exit_asset.is.used) direction.classList.add("ajs-used");
if (exit_asset.is.closed) direction.classList.add("ajs-closed");
if (exit_asset.is.locked) direction.classList.add("ajs-locked");
if (exit_asset.is.locked) direction.classList.add("ajs-sealed");
}
}
});
});
};
})();