Sha256: 6ceea09552a4d99ff8cc3e63dd14314c844e99ab631a3a283520778371ab7b61

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

import Item from "./item";

/**
 * @param nodes {NodeList}
 * @returns {Item[]}
 */
function createItemList(nodes) {
  return Array.from(nodes).map((node) => new Item(node));
}

export default class Menu {
  /**
   * @param node {Element} navigation editor list
   */
  constructor(node) {
    this.node = node;
  }

  /**
   * @return {Item[]} an ordered list of all items in the menu
   */
  get items() {
    return createItemList(
      this.node.querySelectorAll("[data-navigation-index]")
    );
  }

  /**
   *  @return {String} a serialized description of the structure of the menu
   */
  get state() {
    const inputs = this.node.querySelectorAll("li input[type=hidden]");
    return Array.from(inputs)
      .map((e) => e.value)
      .join("/");
  }

  /**
   * Set the index of items based on their current position.
   */
  reindex() {
    this.items.map((item, index) => (item.index = index));
  }

  /**
   * Resets the order of items to their defined index.
   * Useful after an aborted drag.
   */
  reset() {
    this.items.sort(Item.comparator).forEach((item) => {
      this.node.appendChild(item.node);
    });
  }
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
katalyst-navigation-1.5.2 app/javascript/navigation/editor/menu.js
katalyst-navigation-1.5.1 app/javascript/navigation/editor/menu.js
katalyst-navigation-1.5.0 app/javascript/navigation/editor/menu.js
katalyst-navigation-1.0.1 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.0.0 app/assets/javascripts/utils/navigation/editor/menu.js