Move bridge javascript into existing folder structure
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { BridgeComponent } from "@hotwired/hotwire-native-bridge"
|
||||
import { BridgeElement } from "@hotwired/hotwire-native-bridge"
|
||||
|
||||
export default class extends BridgeComponent {
|
||||
static component = "buttons"
|
||||
static targets = [ "button" ]
|
||||
|
||||
static shouldLoad() {
|
||||
return true
|
||||
}
|
||||
|
||||
connect() {
|
||||
super.connect()
|
||||
if (!this.hasButtonTarget) return
|
||||
this.notifyBridgeOfConnect()
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
super.disconnect()
|
||||
this.notifyBridgeOfDisconnect()
|
||||
}
|
||||
|
||||
notifyBridgeOfConnect() {
|
||||
const buttons = this.buttonTargets.map((target, index) => {
|
||||
const element = new BridgeElement(target)
|
||||
return { ...element.getButton(), index }
|
||||
})
|
||||
|
||||
this.send("connect", { buttons }, message => {
|
||||
this.activateButton(message)
|
||||
})
|
||||
}
|
||||
|
||||
notifyBridgeOfDisconnect() {
|
||||
this.send("disconnect")
|
||||
}
|
||||
|
||||
activateButton(message) {
|
||||
const selectedIndex = message.data.selectedIndex
|
||||
this.buttonTargets[selectedIndex].click()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user