Sha256: 1b80894dff8aa359a559c643efe3044fe539853eafec6bf079a2e5a1cc3671dd

Contents?: true

Size: 1.13 KB

Versions: 13

Compression:

Stored size: 1.13 KB

Contents

import Item from "utils/navigation/editor/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

13 entries across 13 versions & 1 rubygems

Version Path
katalyst-navigation-1.4.1 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.4.0 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.3.4 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.3.3 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.3.2 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.3.1 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.3.0 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.2.0 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.1.2 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.1.1 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.1.0 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.0.3 app/assets/javascripts/utils/navigation/editor/menu.js
katalyst-navigation-1.0.2 app/assets/javascripts/utils/navigation/editor/menu.js