Pre-release
AdventureJS Docs Downloads Bundler Devlog Score: 0 Moves: 0
// updateInventoryListWidgets.js

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

  var p = AdventureJS.Display.prototype;

  /**
   * Set inventory in inventory widgets.
   * @method AdventureJS.Display#updateInventoryListWidgets
   * @param {Object} inventory
   * @TODO different lists for specific widgets
   */
  p.updateInventoryListWidgets = function Display_updateInventoryListWidgets(
    inventory
  ) {
    if ("undefined" === typeof inventory) {
      inventory = this.game.getPlayer().getAllNestedContents();
    }

    this.inventory_list_widgets.forEach(function (widget) {
      let assetclasses, is, is_in;
      if (widget.dataset.assetclasses) {
        assetclasses = widget.dataset.assetclasses.split(",");
      }
      if (widget.dataset.is) {
        is = widget.dataset.is.split(",");
      }
      if (widget.dataset.is_in) {
        is_in = widget.dataset.is_in.split(",");
      }

      let html = "";

      // we put the inventory loop inside the widget loop
      // because each widget may have distinct settings
      inventory.forEach(function (item) {
        let asset = this.game.getAsset(item);

        let image = "";
        let src = "";
        let hasimage = "";
        let noname = "";
        let noimage = "ajs-noimage";
        if (widget.print_images && asset.image) {
          src = this.game.getImage(asset.image);
          hasimage = "hasimage";
          noimage = "";
          if ("undefined" !== typeof widget.print_name && !widget.print_name) {
            noname = "ajs-noname";
          }
        }
        let allow = true;

        // assetclasses limits to specified classes, like Weapon or Key
        if (assetclasses && assetclasses.length) {
          let allowed_class = false;
          for (const Class in assetclasses) {
            let c =
              AdventureJS[assetclasses[Class]] ||
              AdventureJS.Assets[assetclasses[Class]];
            if (c && asset instanceof c) {
              allowed_class = true;
              continue;
            }
          }
          if (!allowed_class) allow = false;
        }

        // is means limit to inventory with particular state, like is.worn for clothes
        if (is && is.length) {
          let allowed_is = false;
          for (var truth in is) {
            if (asset.is[is[truth]]) {
              allowed_is = true;
              continue;
            }
          }
          if (!allowed_is) allow = false;
        }

        // is_in means limit to inventory in a container, like a knapsack
        if (is_in && is_in.length) {
          let allowed_is_in = false;
          for (var id in is_in) {
            if (asset.isWithin(is_in[id])) {
              allowed_is_in = true;
              continue;
            }
          }
          if (!allowed_is_in) allow = false;
        }

        if (allow) {
          let itemhtml = `
            <button 
              data-id="${asset.id}" 
              data-name="${asset.name}" 
              class="ajs-widget-button ajs-inventory-button id_${asset.id} class_${asset.class} ${hasimage}" 
              data-name="${asset.name}"
              aria-label="${asset.name}"
              title="${asset.name}"
              aria-hidden=false
              tab-index="0"
            >
              <span class="ajs-inventory-image ajs-image-container ajs-button-image ${noimage}">
                ${src}
              </span>
              <span class="ajs-inventory-name ajs-button-label ${noname}">
                ${asset.name}
              </span>
            </button>
          `;
          html += itemhtml;
        }
      }, this);

      html = `
        <div class="ajs-inner ajs-scroll-container">
          <div class="ajs-scroll-content">
            ${html}
          </div>
        </div>
      `;

      widget.innerHTML = html;

      widget.querySelectorAll(".ajs-inventory-button").forEach(function (el) {
        el.game = this.game;
        el.addEventListener("click", function () {
          if (this.game.display.inputEl.value.length) {
            this.game.sendToInput(` ${el.dataset.name}`);
          } else {
            this.game.sendToParser(
              `${widget.verb || "examine"} ${el.dataset.name}`
            );
          }
        });
      }, this);
    }, this);
  };
})();