-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dependable_controller.js, refresh_fields_controller.js
- Loading branch information
1 parent
5ae7d60
commit a331493
Showing
3 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
bullet_train-fields/app/javascript/controllers/dependable_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
bullet_train-fields/app/javascript/controllers/refresh_fields_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |