diff --git a/app/assets/stylesheets/responsive/_global_style.scss b/app/assets/stylesheets/responsive/_global_style.scss index 119065eead..304a078b78 100644 --- a/app/assets/stylesheets/responsive/_global_style.scss +++ b/app/assets/stylesheets/responsive/_global_style.scss @@ -191,7 +191,7 @@ form button { padding: 2px 6px 3px; transition: none; &:hover, &:focus { - background: none; + background-color: inherit; color: inherit; } } diff --git a/app/assets/stylesheets/responsive/alaveteli_pro/_projects_layout.scss b/app/assets/stylesheets/responsive/alaveteli_pro/_projects_layout.scss index 3bd4c4cb65..72946d8ce1 100644 --- a/app/assets/stylesheets/responsive/alaveteli_pro/_projects_layout.scss +++ b/app/assets/stylesheets/responsive/alaveteli_pro/_projects_layout.scss @@ -294,41 +294,57 @@ } #project-key-set { - .project-key-set__key { - &.project-key-set__key--removed { - display: none; - } + padding-bottom: 1em; - width: 75%; + .project-key-set__key { + padding: 1em; + margin-bottom: 1em; display: flex; flex-flow: row nowrap; justify-content: space-between; + border-radius: 5px; + box-shadow: rgba(0, 0, 0, 0.05) 0px 6px 24px 0px, rgba(0, 0, 0, 0.08) 0px 0px 0px 1px; + + &.project-key-set__key--removed { + display: none; + } + .project-key-set__key__drag-handle { text-indent: -9999px; background-image: image-url("drag.svg"); background-repeat: no-repeat; - background-position: center center; + background-position: center 2em; cursor: grab; width: 24px; } - .project-key-set__key__title, - .project-key-set__key__format { - width: 35%; - input, - select { - width: 100%; - } + .project-key-set__key__attributes { + width: 100%; + margin: 0 1em; + + display: flex; + flex-flow: row wrap; + justify-content: space-between; + + .project-key-set__key__value { + width: 50%; + padding-right: 1em; + + input[type='text'], select { + width: 100%; + } - .fieldWithErrors { - padding: 0; + .fieldWithErrors { + padding: 0; + } } } .project-key-set__key__remove-question { padding-top: 1.2em; + button { width: max-content; } } } } diff --git a/app/controllers/alaveteli_pro/projects_controller.rb b/app/controllers/alaveteli_pro/projects_controller.rb index d4c589aede..93cdbbfbe7 100644 --- a/app/controllers/alaveteli_pro/projects_controller.rb +++ b/app/controllers/alaveteli_pro/projects_controller.rb @@ -141,7 +141,11 @@ def project_params when 'edit_key_set', 'update_key_set' params.fetch(:project, {}).permit( key_set_attributes: [ - :id, keys_attributes: %i[id title format order _destroy] + :id, keys_attributes: [ + :id, :title, :format, :order, :_destroy, options: [ + :select_allow_blank, :select_allow_muliple, { select_options: [] } + ] + ] ] ) when 'edit_contributors', 'update_contributors' diff --git a/app/controllers/projects/extracts_controller.rb b/app/controllers/projects/extracts_controller.rb index 9cc041ab24..1014ebb90e 100644 --- a/app/controllers/projects/extracts_controller.rb +++ b/app/controllers/projects/extracts_controller.rb @@ -71,7 +71,7 @@ def find_info_request def extract_params params.require(:extract).permit( - :dataset_key_set_id, values_attributes: [:dataset_key_id, :value] + :dataset_key_set_id, values_attributes: [:dataset_key_id, :value, value: []] ) end diff --git a/app/javascript/controllers/projects/key_set/select_controller.js b/app/javascript/controllers/projects/key_set/select_controller.js new file mode 100644 index 0000000000..11b02b356a --- /dev/null +++ b/app/javascript/controllers/projects/key_set/select_controller.js @@ -0,0 +1,24 @@ +import { Controller } from "@hotwired/stimulus"; +import { streamUpdate } from "helpers/stream_update"; + +export default class extends Controller { + static targets = ["newInput"]; + static values = { name: String }; + + addOption(event) { + event.preventDefault(); + + const newValue = this.newInputTarget.value; + if (!newValue) return; + + const form = this.element.closest("form"); + streamUpdate(form, { [this.nameValue]: newValue }); + } + + removeOption(event) { + event.preventDefault(); + + const li = event.target.closest("li"); + if (li) li.remove(); + } +} diff --git a/app/javascript/controllers/projects/key_set_controller.js b/app/javascript/controllers/projects/key_set_controller.js index 483161623e..cd63b811de 100644 --- a/app/javascript/controllers/projects/key_set_controller.js +++ b/app/javascript/controllers/projects/key_set_controller.js @@ -26,6 +26,10 @@ export default class extends Controller { streamUpdate(this.element); } + updateKey(event) { + streamUpdate(this.element); + } + updateOrder() { this.element .querySelectorAll('[name*="[order]"]') diff --git a/app/models/dataset/key.rb b/app/models/dataset/key.rb index f48db23ce0..2d4aef091f 100644 --- a/app/models/dataset/key.rb +++ b/app/models/dataset/key.rb @@ -1,5 +1,5 @@ # == Schema Information -# Schema version: 20210114161442 +# Schema version: 20240926164308 # # Table name: dataset_keys # @@ -10,6 +10,7 @@ # order :integer # created_at :datetime not null # updated_at :datetime not null +# options :jsonb # ## @@ -24,6 +25,7 @@ class Dataset::Key < ApplicationRecord FORMATS = { text: { title: _('Text'), regexp: /\A.*\z/m }, + select: { title: _('Select'), regexp: /\A.*\z/m }, numeric: { title: _('Numeric'), regexp: /\A[0-9,%\+\-\s]*\z/ }, boolean: { title: _('Yes/No'), regexp: /\A(0|1)\z/ } }.freeze @@ -40,4 +42,16 @@ def self.format_options def format_regexp FORMATS[format.to_sym][:regexp] end + + def select_options + options.fetch('select_options', []) + end + + def select_allow_blank? + options.fetch('select_allow_blank', '0').to_i == 1 + end + + def select_allow_muliple? + options.fetch('select_allow_muliple', '0').to_i == 1 + end end diff --git a/app/models/dataset/value.rb b/app/models/dataset/value.rb index 735b99a3b0..c1488009cb 100644 --- a/app/models/dataset/value.rb +++ b/app/models/dataset/value.rb @@ -29,6 +29,15 @@ def title key.title end + def value=(new_value) + case key.format + when 'select' + super(Array(new_value).reject(&:blank?).join(', ')) + else + super + end + end + def mapped_value case key.format when 'boolean' diff --git a/app/views/alaveteli_pro/projects/_key_set.html.erb b/app/views/alaveteli_pro/projects/_key_set.html.erb index 71a242ca1d..aa8184285f 100644 --- a/app/views/alaveteli_pro/projects/_key_set.html.erb +++ b/app/views/alaveteli_pro/projects/_key_set.html.erb @@ -1,29 +1,39 @@ -<%= f.fields_for :key_set, @key_set do |key_set_fields| %> - <%= key_set_fields.fields_for :keys, @keys do |key_fields| %> -