Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// setStyle.js

(function () {
  /* global adventurejs A */

  var p = adventurejs.Game.prototype;

  /**
   * <code>setStyle</code> offers a method to add custom
   * stylesheets via the game definition file, as opposed
   * to embedding styles in HTML or loading stylesheets.
   * @memberOf adventurejs.Game
   * @method adventurejs.Game#setStyle
   * @param {Object} style A room object to which to move subject.
   * @param {Object} params
   */
  p.setStyle = function Game_setRoom(style = {}) {
    let sheet = document.styleSheets[0];
    if (!sheet) {
      const styleEl = document.createElement("style");
      document.head.appendChild(styleEl);
      sheet = styleEl.sheet;
    }

    this.game.log("L1572", "log", "high", `[setStyle.js] setStyle()`, "Game");

    for (const selector in style) {
      const declarations = style[selector];
      const cssText = Object.entries(declarations)
        .map(([prop, value]) => `${prop}: ${value};`)
        .join(" ");

      try {
        sheet.insertRule(`${selector} { ${cssText} }`, sheet.cssRules.length);
      } catch (err) {
        this.game.log(
          "L1573",
          "error",
          "high",
          `[setStyle.js] setStyle() insertRule failed\n${selector}: { ${cssText} } `,
          "Verbs"
        );
        console.error("insertRule failed", selector, cssText, err);
      }
    }
  };
})();