From e9d6af0212e96c7bdd3431478219fdc15ac3ec50 Mon Sep 17 00:00:00 2001 From: Chonlasith Jucksriporn Date: Thu, 24 Oct 2024 14:29:33 +0700 Subject: [PATCH] feat: Auto color when all capabilities in block are checked --- cdmm/app/helpers/cdmm_helper.rb | 10 ++++++++++ .../controllers/checklist_ui_controller.js | 19 +++++++++++++++++++ cdmm/app/views/cdmm/_capability_list.html.erb | 7 +++++++ cdmm/app/views/cdmm/_form_table.html.erb | 8 +------- .../views/shared/_tristate_checkbox.html.erb | 6 +++--- 5 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 cdmm/app/javascript/controllers/checklist_ui_controller.js create mode 100644 cdmm/app/views/cdmm/_capability_list.html.erb diff --git a/cdmm/app/helpers/cdmm_helper.rb b/cdmm/app/helpers/cdmm_helper.rb index ded1ee6..4a36f5a 100644 --- a/cdmm/app/helpers/cdmm_helper.rb +++ b/cdmm/app/helpers/cdmm_helper.rb @@ -9,6 +9,16 @@ def get_data_at(table, row_index, col_index) end end + def cell_key(table, row_index, col_index) + "cell_#{row_index}_#{col_index}" + end + + def all_checked?(table, row_index, col_index) + data = get_data_at(table, row_index, col_index) + unselected_caps = data.select { |cap| cap[:value] == "unchecked" } + unselected_caps.count == 0 + end + def label_to_key(label) label.gsub(/::/, '__') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') diff --git a/cdmm/app/javascript/controllers/checklist_ui_controller.js b/cdmm/app/javascript/controllers/checklist_ui_controller.js new file mode 100644 index 0000000..1e8b7fb --- /dev/null +++ b/cdmm/app/javascript/controllers/checklist_ui_controller.js @@ -0,0 +1,19 @@ +import { Controller } from "@hotwired/stimulus" + +// Connects to data-controller="checklist-ui" +export default class extends Controller { + static values = { itemCount: Number, completeClasses: String } + + connect() { + } + + refresh(event) { + const uncheckedChecklists = this.element.querySelectorAll("input[type='hidden'][value='unchecked']"); + if (uncheckedChecklists.length === 0) { + this.element.classList.add(this.completeClassesValue); + } else { + this.element.classList.remove(this.completeClassesValue); + } + } + +} diff --git a/cdmm/app/views/cdmm/_capability_list.html.erb b/cdmm/app/views/cdmm/_capability_list.html.erb new file mode 100644 index 0000000..95ad1ac --- /dev/null +++ b/cdmm/app/views/cdmm/_capability_list.html.erb @@ -0,0 +1,7 @@ +"> +
+ <% get_data_at(table, row_index, col_index).each_with_index do |data, data_index| %> + <%= render :partial => "capability_item", :locals => { key: data[:key], label: data[:label], description: data[:description], value: data[:value] } %> + <% end %> +
+ diff --git a/cdmm/app/views/cdmm/_form_table.html.erb b/cdmm/app/views/cdmm/_form_table.html.erb index b40c506..b9c3b75 100644 --- a/cdmm/app/views/cdmm/_form_table.html.erb +++ b/cdmm/app/views/cdmm/_form_table.html.erb @@ -13,13 +13,7 @@ <%= row_header %> <% table[:col_headers].each_with_index do |col_header, col_index| %> - -
- <% get_data_at(table, row_index, col_index).each_with_index do |data, data_index| %> - <%= render :partial => "capability_item", :locals => { key: data[:key], label: data[:label], description: data[:description], value: data[:value] } %> - <% end %> -
- + <%= render :partial => "capability_list", :locals => { table: table, row_index: row_index, col_index: col_index } %> <% end %> <% end %> diff --git a/cdmm/app/views/shared/_tristate_checkbox.html.erb b/cdmm/app/views/shared/_tristate_checkbox.html.erb index ff87a65..677edd3 100644 --- a/cdmm/app/views/shared/_tristate_checkbox.html.erb +++ b/cdmm/app/views/shared/_tristate_checkbox.html.erb @@ -2,18 +2,18 @@
- +
- +
- +