Skip to content

Commit

Permalink
Selectors and tree
Browse files Browse the repository at this point in the history
  • Loading branch information
gryphon committed Dec 11, 2023
1 parent 69db870 commit 60cf9f0
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/assets/stylesheets/custom_table/table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ table {
}

.tree-opener {
cursor: pointer;
.opened {
display: none;
}
Expand Down
22 changes: 21 additions & 1 deletion app/helpers/custom_table/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,31 @@ def custom_table_variants_for model
end

def tree_opener item_id, has_children=false
content_tag :button, class: "btn btn-sm tree-opener", data: {action: (has_children ? "table#toggle" : ""), "table-css-param": ".child-of-#{item_id}"} do
content_tag :span, class: "tree-opener", data: {action: (has_children ? "click->table#toggle" : ""), "table-css-param": ".child-of-#{item_id}"} do
concat content_tag(:span, (has_children ? "▶" : "▷"), class: "closed")
concat content_tag(:span, "▼", class: "opened")
end
end

def custom_table_fields_totals fields:, totals:, item:, fields_totals:, variant:
fields.each do |field, defs|
if !totals.nil? && totals.has_key?(field) && totals[field].nil? # Auto-counting
fields_totals[field] = 0 if fields_totals[field].nil?
fields_totals[field] += raw_field_value_for(item, field, definitions: defs, variant: variant).to_f rescue 0
end
end
return fields_totals
end

def custom_table_batch_selector_check_box item, **params

# abort params.inspect

params[:param] = "#{item.model_name.plural}[]" if params[:param].nil?
params[:data] = {"toggle-target": "checkbox", "batch-actions-target": "checkbox", "action": "toggle#recalculateToggler batch-actions#refresh"}

check_box_tag params[:param], item.id, (!params[item.model_name.plural.to_sym].blank?) && (params[item.model_name.plural.to_sym].include?(item.id.to_s)), params
end

end
end
5 changes: 5 additions & 0 deletions app/helpers/custom_table/icons_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def custom_table_move_icon_class
return "bi bi-arrow-down-up" if CustomTable.configuration.icons_framework == :bi
end

def custom_table_tree_child_icon_class
return "fa fa-arrow-right" if CustomTable.configuration.icons_framework == :fa
return "bi bi-arrow-return-right" if CustomTable.configuration.icons_framework == :bi
end

def custom_table_cancel_icon
return custom_table_icon("fa fa-ban") if CustomTable.configuration.icons_framework == :fa
return custom_table_icon("bi bi-slash-circle") if CustomTable.configuration.icons_framework == :bi
Expand Down
53 changes: 53 additions & 0 deletions app/javascript/controllers/batch_actions_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {

static targets = [ "checkbox", "form" ]

connect() {
if (this.hasFormTarget && this.hasCheckboxTarget) {
console.log("Batch actions form", this.formTarget)
this.refresh();
}
}

submit(event) {

if (!this.hasCheckboxTarget) {
console.log("No checkbox targets at all")
return
}

// Clearing any matching hidden fields from form
this.formTarget.querySelectorAll('input[name="'+this.checkboxTargets[0].getAttribute("name")+'"]').forEach((cb) => {
cb.parentNode.removeChild(cb)
});

// Adding selected fields to form as hiddens
this.checkboxTargets.forEach((cb) => {

if (!cb.checked) return;

let input = document.createElement('input');
input.setAttribute('name', cb.getAttribute("name"));
input.setAttribute('value', cb.getAttribute("value"));
input.setAttribute('type', "hidden")

this.formTarget.appendChild(input);//append the input to the form

})

// event.preventDefault()
console.log("Batch actions submit form")

}

refresh(){
let v = false
this.checkboxTargets.forEach((cb) => {
if (cb.checked) v = true;
});
this.formTarget.querySelector('input[type="submit"]').disabled = !v
}

}
37 changes: 31 additions & 6 deletions app/views/custom_table/_table.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

- fields = fields.except(*local_assigns[:skip_fields]) if !local_assigns[:skip_fields].nil?

%div.table-wrapper
%div.table-wrapper{data: {controller: "batch-actions"}}
- if local_assigns[:batch_actions]
= self.send(local_assigns[:batch_actions])
%table{class: ["search-fields", "table", "table-hover", "table-sm", model_class.model_name.plural], data: {controller: "table toggle"}}
%thead.sticky
- if local_assigns[:quick_filter]
Expand All @@ -35,7 +39,7 @@

- if local_assigns[:with_select]
%th.checkbox-col
= check_box_tag "check-all", "", true, data: {"toggle-target": "toggler", "action": "toggle#toggle"}
= check_box_tag "check-all", "", true, data: {"toggle-target": "toggler", "action": "toggle#toggle batch-actions#refresh"}
- if local_assigns[:with_index]
%th= "#"
- fields.each do |field, defs|
Expand Down Expand Up @@ -94,10 +98,27 @@
- Hash[grouped_collection.sort_by{|k,v| k.to_s}].each do |group, items|

- if !group.nil?

-# Calculating group totals
- group_fields_totals = {}
- items.each do |item|
- custom_table_fields_totals(fields: fields, item: item, totals: totals, variant: variant, fields_totals: group_fields_totals)

%tr
%th{colspan: "100%"}
= tree_opener(group, true)
= group
- shown = false
- fields.each do |field, defs|
- next if defs[:table] == false
- if !group_fields_totals[field].nil?
%th{class: [field.to_s, "text-end"]}
= amount_color group_fields_totals[field]
- else
%th{class: [field.to_s, "text-start"]}
- if !shown
= tree_opener(group, true)
= group
- shown = true
%th.text-end
- items.each do |item|
- position+=1
Expand Down Expand Up @@ -189,10 +210,14 @@

%tr.totals{data: {"table-target": "move"}}
- if local_assigns[:tree]
%td
%th
- if local_assigns[:with_index]
%th
- if local_assigns[:with_select]
%th
- fields.each do |field, defs|
- next if defs[:table] == false
%th{class: [field.to_s, "text-end"]}
Expand Down
16 changes: 10 additions & 6 deletions app/views/custom_table/_table_row.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@
- if local_assigns[:tree]
%td
- if item.parent_id.nil?
= tree_opener(item.id, local_assigns[:has_children])
- if local_assigns[:has_children]
= tree_opener(item.id, local_assigns[:has_children])
- else
= custom_table_icon(custom_table_tree_child_icon_class)

- if local_assigns[:with_select]
%td.checkbox-col
= self.send(local_assigns[:with_select], item, position)
- if local_assigns[:with_select].to_s == "true"
= check_box_tag "#{item.model_name.plural}[]", item.id, (!params[item.model_name.plural.to_sym].blank?) && (params[item.model_name.plural.to_sym].include?(item.id.to_s)), data: {"toggle-target": "checkbox", "batch-actions-target": "checkbox", "action": "toggle#recalculateToggler batch-actions#refresh"}
- else
= self.send(local_assigns[:with_select], item, position)

- if local_assigns[:with_index]
%td
= position

- custom_table_fields_totals(fields: fields, item: item, totals: local_assigns[:totals], variant: variant, fields_totals: fields_totals)

- fields.each do |field, defs|
- next if defs[:table] == false
- td_classes = [field.to_s]
Expand All @@ -31,10 +39,6 @@
%td{class: td_classes, id: dom_id(item, field)}
- v = field_value_for(item, field, definitions: defs, variant: variant)

- if !local_assigns[:totals].nil? && local_assigns[:totals].has_key?(field) && local_assigns[:totals][field].nil? # Auto-counting
- fields_totals[field] = 0 if fields_totals[field].nil?
- fields_totals[field] += raw_field_value_for(item, field, definitions: defs, variant: variant).to_f rescue 0

- if defs[:editable]

= editable item, field do
Expand Down

0 comments on commit 60cf9f0

Please sign in to comment.