Skip to content

Commit

Permalink
add dependable_controller.js, refresh_fields_controller.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pascallaliberte committed Jan 26, 2023
1 parent 5ae7d60 commit a331493
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static values = {
dependentsSelector: String
}

updateDependents(event) {
if (!this.hasDependents) { return false }

this.dependents.forEach((dependent) => {
dependent.dispatchEvent(new CustomEvent(`${this.identifier}:updated`, { detail: { event: event }, bubbles: true, cancelable: false }))
})
}

get hasDependents() {
return (this.dependents.length > 0)
}

get dependents() {
if (!this.dependentsSelectorValue) { return [] }
return document.querySelectorAll(this.dependentsSelectorValue)
}
}
6 changes: 6 additions & 0 deletions bullet_train-fields/app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import FileFieldController from './fields/file_field_controller'
import PasswordController from './fields/password_controller'
import PhoneController from './fields/phone_controller'
import SuperSelectController from './fields/super_select_controller'
import DependableController from './dependable_controller'
import RefreshFieldsController from './refresh_fields_controller'

export const controllerDefinitions = [
[FieldController, 'fields/field_controller.js'],
Expand All @@ -22,6 +24,8 @@ export const controllerDefinitions = [
[PasswordController, 'fields/password_controller.js'],
[PhoneController, 'fields/phone_controller.js'],
[SuperSelectController, 'fields/super_select_controller.js'],
[DependableController, 'dependable_controller.js'],
[RefreshFieldsController, 'refresh_fields_controller.js'],
].map(function(d) {
const key = d[1]
const controller = d[0]
Expand All @@ -42,4 +46,6 @@ export {
PasswordController,
PhoneController,
SuperSelectController,
DependableController,
RefreshFieldsController,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
updateFrameFromDependentSuperSelectValue(event) {
const dependentSuperSelect = event?.detail?.event?.detail?.event?.target // original super select jQuery event

const frame = this.element
frame.src = this.constructNewUrlUpdatingField(dependentSuperSelect.name, dependentSuperSelect.value)
}

constructNewUrlUpdatingField(fieldName, fieldValue) {
const url = new URL(window.location.href)
url.searchParams.set(fieldName, fieldValue)

return url.href
}
}

0 comments on commit a331493

Please sign in to comment.