Skip to content

Commit

Permalink
move categories with children logic from stimulus controller to ruby …
Browse files Browse the repository at this point in the history
…part
  • Loading branch information
Bilelkihal committed Jan 3, 2025
1 parent 18d87b7 commit 7081622
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
17 changes: 17 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,21 @@ def categories_select(id: nil, name: nil, selected: 'None')
render Input::SelectComponent.new(id: id, name: name, value: categories_for_select, selected: selected, multiple: true)
end

def categories_with_children(categories)
parent_to_children = Hash.new { |hash, key| hash[key] = [] }
categories.each do |category|
next unless category.parentCategory
category.parentCategory.each do |parent_id|
parent_acronym = id_to_acronym(parent_id)
child_acronym = id_to_acronym(category.id)
parent_to_children[parent_acronym] << child_acronym
end
end
parent_to_children
end

def id_to_acronym(id)
id.split('/').last
end

end
5 changes: 3 additions & 2 deletions app/helpers/submission_inputs_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module SubmissionInputsHelper

class SubmissionMetadataInput
include MetadataHelper
include MetadataHelper, ApplicationHelper

def initialize(attribute_key:, attr_metadata:, submission: nil, label: nil)
@attribute_key = attribute_key
Expand Down Expand Up @@ -128,9 +128,10 @@ def ontology_administered_by_input(ontology = @ontology, users_list = @user_sele

def ontology_categories_input(ontology = @ontology, categories = @categories)
categories ||= LinkedData::Client::Models::Category.all(display_links: false, display_context: false)
categories_children = categories_with_children(categories)

render Input::InputFieldComponent.new(name: '', label: 'Categories') do
content_tag(:div, class: 'upload-ontology-chips-container', 'data-controller': 'parent-categories-selector', 'data-parent-categories-selector-categories-value': "#{categories.to_json}", 'data-parent-categories-selector-target': "chips") do
content_tag(:div, class: 'upload-ontology-chips-container', 'data-controller': 'parent-categories-selector', 'data-parent-categories-selector-categories-children-value': "#{categories_children.to_json}", 'data-parent-categories-selector-target': "chips") do
hidden_field_tag('ontology[hasDomain][]') +
categories.map do |category|
content_tag(:div, 'data-action': 'click->parent-categories-selector#check') do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { Controller } from "@hotwired/stimulus"
// Connects to data-controller="parent-categories-selector"
export default class extends Controller {
static targets = ['chips']
static values = { categories: Array}
static values = { categoriesChildren: Object}

check(event){
const input = event.currentTarget.querySelector('input')
const allInputs = this.chipsTarget.querySelectorAll('input')
const parents = this.#categories_with_children()

const parents = this.categoriesChildrenValue
if(this.#id_to_acronym(input.value) in parents){
const parentChildren = parents[this.#id_to_acronym(input.value)]
allInputs.forEach(i => {
Expand All @@ -26,24 +25,6 @@ export default class extends Controller {
}
}

#categories_with_children(){
const parentToChildren = {};
this.categoriesValue.forEach(category => {
if (category.parentCategory) {
category.parentCategory.forEach(parentId => {
const parentAcronym = this.#id_to_acronym(parentId);
const childAcronym = this.#id_to_acronym(category.id);

if (!parentToChildren[parentAcronym]) {
parentToChildren[parentAcronym] = [];
}
parentToChildren[parentAcronym].push(childAcronym);
});
}
});
return parentToChildren
}

#id_to_acronym(id){
return id.split('/').pop();
}
Expand Down
2 changes: 1 addition & 1 deletion app/views/ontologies/browser/browse.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
%div.browse-filter
= switch_input(id:'filter-views', name:'views', checked: @show_views, label: t("ontologies.browser.show_ontology_views"))
= switch_input(id:'filter-retired', name:'retired',checked: @show_retired , label: t("ontologies.browser.show_retired_ontologies"))
%div{'data-controller': 'parent-categories-selector', 'data-parent-categories-selector-categories-value': "#{@categories.to_json}"}
%div{'data-controller': 'parent-categories-selector', 'data-parent-categories-selector-categories-children-value': "#{categories_with_children(@categories).to_json}"}
- @filters.each do |key, values|
%div{ id: "#{key}_filter_container", data:{controller: "browse_filters show-filter-count",
action: "change->show-filter-count#updateCount
Expand Down

0 comments on commit 7081622

Please sign in to comment.