-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
6 deletions.
There are no files selected for viewing
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,54 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static targets = ["checkbox"] | ||
|
||
checkboxTargetConnected(checkbox) { | ||
const shotRow = checkbox.closest(".group") | ||
if (this.isChecked) { | ||
shotRow.setAttribute('data-comparing', true) | ||
} else { | ||
shotRow.removeAttribute('data-comparing') | ||
} | ||
} | ||
|
||
select(event) { | ||
this.isChecked = event.target.checked | ||
this.checkboxTargets.forEach(checkbox => { | ||
const shotRow = checkbox.closest(".group") | ||
if (this.isChecked) { | ||
shotRow.setAttribute('data-comparing', true) | ||
} else { | ||
shotRow.removeAttribute('data-comparing') | ||
} | ||
|
||
if (checkbox.checked && checkbox !== event.target) { | ||
checkbox.checked = false | ||
} | ||
}) | ||
} | ||
|
||
view(event) { | ||
if (this.isChecked) { | ||
event.preventDefault() | ||
// Find the closest group container first, then find the checkbox within it | ||
const shotRow = event.target.closest(".group") | ||
const checkbox = shotRow.querySelector('[data-compare-target="checkbox"]') | ||
if (checkbox.checked) { | ||
checkbox.checked = false | ||
checkbox.dispatchEvent(new Event('change')) | ||
} else { | ||
const checkedCheckbox = this.checkboxTargets.find(cb => cb.checked) | ||
if (checkedCheckbox) { | ||
const baseId = checkedCheckbox.closest(".group").dataset.shotId | ||
const compareId = shotRow.dataset.shotId | ||
window.location.href = `/shots/${baseId}/compare/${compareId}` | ||
} else { | ||
window.location.href = event.target.href | ||
} | ||
} | ||
} else { | ||
|
||
} | ||
} | ||
} |
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
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