-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port BL8's checkbox submit behavior for visibility toggles
- Loading branch information
1 parent
6eabdb1
commit 2d022f5
Showing
7 changed files
with
140 additions
and
28 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
app/assets/javascripts/spotlight/admin/checkbox_submit.es6
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,75 @@ | ||
/* | ||
NOTE: this is copied & adapted from BL8's checkbox_submit.js in order to have | ||
it accessible in a BL7-based spotlight. Once we drop support for BL7, this file | ||
can be deleted and we can change visibility_toggle.es6 to import CheckboxSubmit | ||
from Blacklight. | ||
See https://github.com/projectblacklight/blacklight/blob/main/app/javascript/blacklight/checkbox_submit.js | ||
*/ | ||
export default class CheckboxSubmit { | ||
constructor(form) { | ||
this.form = form | ||
} | ||
|
||
async clicked(evt) { | ||
this.spanTarget.innerHTML = this.form.getAttribute('data-inprogress') | ||
this.labelTarget.setAttribute('disabled', 'disabled'); | ||
this.checkboxTarget.setAttribute('disabled', 'disabled'); | ||
const csrfMeta = document.querySelector('meta[name=csrf-token]') | ||
const response = await fetch(this.formTarget.getAttribute('action'), { | ||
body: new FormData(this.formTarget), | ||
method: this.formTarget.getAttribute('method').toUpperCase(), | ||
headers: { | ||
'Accept': 'application/json', | ||
'X-Requested-With': 'XMLHttpRequest', | ||
'X-CSRF-Token': csrfMeta ? csrfMeta.content : '' | ||
} | ||
}) | ||
this.labelTarget.removeAttribute('disabled') | ||
this.checkboxTarget.removeAttribute('disabled') | ||
if (response.ok) { | ||
this.updateStateFor(!this.checked) | ||
// Not used for our case in Spotlight (visibility toggle) | ||
// const json = await response.json() | ||
// document.querySelector('[data-role=bookmark-counter]').innerHTML = json.bookmarks.count | ||
} else { | ||
alert('Error') | ||
} | ||
} | ||
|
||
get checked() { | ||
return (this.form.querySelectorAll('input[name=_method][value=delete]').length != 0) | ||
} | ||
|
||
get formTarget() { | ||
return this.form | ||
} | ||
|
||
get labelTarget() { | ||
return this.form.querySelector('[data-checkboxsubmit-target="label"]') | ||
} | ||
|
||
get checkboxTarget() { | ||
return this.form.querySelector('[data-checkboxsubmit-target="checkbox"]') | ||
} | ||
|
||
get spanTarget() { | ||
return this.form.querySelector('[data-checkboxsubmit-target="span"]') | ||
} | ||
|
||
updateStateFor(state) { | ||
this.checkboxTarget.checked = state | ||
|
||
if (state) { | ||
this.labelTarget.classList.add('checked') | ||
//Set the Rails hidden field that fakes an HTTP verb | ||
//properly for current state action. | ||
this.formTarget.querySelector('input[name=_method]').value = 'delete' | ||
this.spanTarget.innerHTML = this.form.getAttribute('data-present') | ||
} else { | ||
this.labelTarget.classList.remove('checked') | ||
this.formTarget.querySelector('input[name=_method]').value = 'put' | ||
this.spanTarget.innerHTML = this.form.getAttribute('data-absent') | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/assets/javascripts/spotlight/admin/visibility_toggle.es6
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,23 @@ | ||
// Visibility toggle for items in an exhibit, based on Blacklight's bookmark toggle | ||
// See: https://github.com/projectblacklight/blacklight/blob/main/app/javascript/blacklight/bookmark_toggle.js | ||
|
||
import CheckboxSubmit from '/checkbox_submit' | ||
|
||
const VisibilityToggle = (e) => { | ||
if (e.target.matches('[data-checkboxsubmit-target="checkbox"]')) { | ||
const form = e.target.closest('form') | ||
if (form) { | ||
new CheckboxSubmit(form).clicked(e) | ||
|
||
// Add/remove the "private" label to the document row when visibility is toggled | ||
const docRow = form.closest('tr').get(0) | ||
if (docRow) docRow.classList.toggle('blacklight-private') | ||
} | ||
} | ||
} | ||
|
||
VisibilityToggle.selector = 'form.visibility-toggle' | ||
|
||
document.addEventListener('click', VisibilityToggle) | ||
|
||
export default VisibilityToggle |
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
23 changes: 21 additions & 2 deletions
23
app/views/spotlight/catalog/_document_visibility_control.html.erb
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 |
---|---|---|
@@ -1,5 +1,24 @@ | ||
<% action = document.private?(current_exhibit) ? :make_public : :make_private %> | ||
<%= form_tag( [:visibility, current_exhibit, document], :method => document.private?(current_exhibit) ? :put : :delete, :class => "visiblity_toggle", "data-doc-id" => document.id, :'data-present' => t('.make_private.label'), :'data-absent' => t('.make_public.label'), :'data-inprogress' => t('.inprogress.label'), :'data-label-toggle-target' => "[data-label-toggle='#{document.id}']") do %> | ||
<%= submit_tag(t(".#{action}.button"), :id => "visibility_toggle_#{document.id.to_s.parameterize}", :class => "visibility_#{action}") %> | ||
<%= form_tag([:visibility, current_exhibit, document], | ||
method: document.private?(current_exhibit) ? :put : :delete, | ||
class: 'visibility-toggle', | ||
data: { | ||
'doc-id': document.id, | ||
present: t('.make_private'), | ||
absent: t('.make_public'), | ||
inprogress: t('.inprogress'), | ||
}) do %> | ||
|
||
<div class="checkbox toggle-visibility"> | ||
<label class="toggle-visibility" data-checkboxsubmit-target="label"> | ||
<input type="checkbox" class="toggle-visibility" data-checkboxsubmit-target="checkbox" <%= 'checked="checked"' if document.public?(current_exhibit) %>> | ||
<span data-checkboxsubmit-target="span" class="sr-only"> | ||
<%= document.private?(current_exhibit) ? t('.make_public') : t('.make_private') %> | ||
</span> | ||
</label> | ||
</div> | ||
|
||
<%= submit_tag(t(".#{action}"), id: "visibility_toggle_#{document.id.to_s.parameterize}", class: "visibility-#{action}") %> | ||
<% end %> |
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