-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Stimulus controller for batch-action triggers
- Loading branch information
Showing
5 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,59 @@ | ||
import ApplicationController from './application_controller' | ||
|
||
export default class extends ApplicationController { | ||
static outlets = ['checkbox-select'] | ||
|
||
connect () { | ||
if (!this.hasCheckboxSelectOutlet) { | ||
// Set default outlet if none specified | ||
this.element.setAttribute(`data-${this.identifier}-checkbox-select-outlet`, '.trestle-table') | ||
} | ||
|
||
this.baseHref = this.element.href | ||
|
||
this.update() | ||
} | ||
|
||
update () { | ||
this.toggleEnabled() | ||
this.updateHref() | ||
} | ||
|
||
toggleEnabled () { | ||
this.element.classList.toggle('disabled', this.checkboxes.length === 0) | ||
} | ||
|
||
updateHref () { | ||
this.element.href = this.hrefWithSelectedIds | ||
} | ||
|
||
get checkboxes () { | ||
if (this.hasCheckboxSelectOutlet) { | ||
return this.checkboxSelectOutlet.checked | ||
} else { | ||
return [] | ||
} | ||
} | ||
|
||
get selectedIds () { | ||
return this.checkboxes.map(c => c.value) | ||
} | ||
|
||
get hrefWithSelectedIds () { | ||
const ids = this.selectedIds | ||
|
||
if (ids.length) { | ||
return `${this.baseHref}?ids=${ids.join(',')}` | ||
} else { | ||
return this.baseHref | ||
} | ||
} | ||
|
||
checkboxSelectOutletConnected (outlet, element) { | ||
element.addEventListener('checkbox-select:change', this.update.bind(this)) | ||
} | ||
|
||
checkboxSelectOutletDisconnected (outlet, element) { | ||
element.removeEventListener('checkbox-select:change', this.update.bind(this)) | ||
} | ||
} |
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
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,32 @@ | ||
<% content_for(:title, admin.t("titles.index", default: "Listing %{pluralized_model_name}")) %> | ||
|
||
<% toolbar(:primary) do |t| %> | ||
<%= t.new %> | ||
<% end %> | ||
|
||
<% toolbar(:secondary) do |t| %> | ||
<%= t.link "Batch Action (GET)", action: :batch_get, style: :info, data: { controller: "batch-action" } %> | ||
<%= t.link "Batch Action (POST)", action: :batch_post, style: :warning, data: { controller: "confirm batch-action", turbo_method: :post } %> | ||
<% end %> | ||
|
||
<% content_for(:utilities) do %> | ||
<%= render "scopes" %> | ||
<% end if admin.scopes.any? %> | ||
|
||
<%= render layout: "layout" do %> | ||
<%= index_turbo_frame do %> | ||
<% if hook?("resource.index.header") %> | ||
<header class="main-content-header"> | ||
<%= hook("resource.index.header") %> | ||
</header> | ||
<% end %> | ||
|
||
<%= render "trestle/table/table", table: admin.table, collection: collection %> | ||
|
||
<footer class="main-content-footer"> | ||
<%= hook("resource.index.footer") %> | ||
|
||
<%= pagination collection: collection, entry_name: admin.model_name, remote: admin.pagination_options[:ajax] %> | ||
</footer> | ||
<% end %> | ||
<% end %> |