Sha256: c34710708109fc0179bbd6d9b79558b2c1ae31460c6bef0a79076eb38baf14d2

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

import { BridgeComponent } from "@hotwired/strada"
import { BridgeElement } from "@hotwired/strada"

// Example
// https://github.com/lazaronixon/strada-rails/wiki/Menu
export default class extends BridgeComponent {
  static component = "menu"
  static targets = [ "title", "item" ]

  show(event) {
    if (this.enabled) {
      event.stopImmediatePropagation()
      this.notifyBridgeToDisplayMenu(event)
    }
  }

  notifyBridgeToDisplayMenu(event) {
    const title = new BridgeElement(this.titleTarget).title
    const items = this.makeMenuItems(this.itemTargets)

    this.send("display", { title, items }, message =>  {
      const selectedIndex = message.data.selectedIndex
      const selectedItem = new BridgeElement(this.itemTargets[selectedIndex])

      selectedItem.click()
    })
  }

  makeMenuItems(elements) {
    const items = elements.map((element, index) => this.menuItem(element, index))
    const enabledItems = items.filter(item => item)

    return enabledItems
  }

  menuItem(element, index) {
    const bridgeElement = new BridgeElement(element)

    if (bridgeElement.disabled) return null

    return {
      title: bridgeElement.title,
      index: index
    }
  }
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
strada-rails-0.0.5 lib/install/app/javascript/controllers/bridge/menu_controller.js
strada-rails-0.0.4 lib/install/app/javascript/controllers/bridge/menu_controller.js
strada-rails-0.0.3 lib/install/app/javascript/controllers/bridge/menu_controller.js
strada-rails-0.0.2 lib/install/app/javascript/controllers/bridge/menu_controller.js