Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Enhance agents search input component #703

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/assets/stylesheets/components/search_input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
position: absolute;
}

.search-container.search-container-scroll{
max-height: 400px;
overflow-y: auto;
}


.search-content{
display: flex;
flex-wrap: wrap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
= hidden_field_tag "#{@name_prefix}[#{@id}]"
= render SearchInputComponent.new(id: 'agent' + @id, name:'agent' + @id, ajax_url: "/ajax/agents?agent_type=#{@agent_type}&name=",
= render SearchInputComponent.new(id: 'agent' + @id, name:'agent' + @id, ajax_url: "/ajax/agents?query=", display_all: true,
item_base_url:"/agents/#{@id}?parent_id=#{@parent_id}&edit_on_modal=#{@edit_on_modal}&name_prefix=#{@name_prefix}&deletable=#{@deletable}&agent_id=", id_key: 'id',
use_cache: false,
actions_links: {create_new_agent: {link: "/agents/new?name=&id=#{@id}&parent_id=#{@parent_id}&type=#{@agent_type}&show_affiliations=#{@show_affiliations}&deletable=#{@deletable}&edit_on_modal=#{@edit_on_modal}&name_prefix=#{@name_prefix}[#{@id}]", target:'_self'}}) do |s|
- s.template do
%a{href: "LINK", class: "search-content", 'data-turbo-frame': '_self'}
%p.search-element.home-searched-ontology
NAME (IDENTIFIERS)
NAME ACRONYM IDENTIFIERS
%p.home-result-type
TYPE
7 changes: 6 additions & 1 deletion app/components/search_input_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def initialize(id: '',
item_base_url:,
id_key:,
links_target: '_top',
search_icon_type: nil)
search_icon_type: nil,
display_all: false)
@id = id
@name = name
@placeholder = placeholder
Expand All @@ -23,6 +24,7 @@ def initialize(id: '',
@id_key = id_key
@links_target = links_target
@search_icon_type = search_icon_type
@display_all = display_all
end
def action_link_info(value)
if value.is_a?(Hash)
Expand All @@ -34,4 +36,7 @@ def action_link_info(value)
def nav_icon_class
@search_icon_type.eql?('nav') ? 'search-input-nav-icon' : ''
end
def display_all_mode_class
@display_all ? 'search-container-scroll' : ''
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
'data-search-input-id-key-value': @id_key,
'data-search-input-cache-value': @use_cache.to_s,
'data-search-input-scroll-down-value': @scroll_down.to_s,
'data-search-input-selected-item-value': 0
'data-search-input-selected-item-value': 0,
'data-search-input-display-all-value': @display_all
}

- if @search_icon_type
Expand All @@ -21,7 +22,7 @@
= render Input::InputFieldComponent.new(name: @name, placeholder: @placeholder,
data: {'action': 'input->search-input#search blur->search-input#blur keydown.down->search-input#arrow_down keydown.up->search-input#arrow_up keydown.enter->search-input#enter_key',
'search-input-target': 'input'})
.search-container{'data-search-input-target': 'dropDown', 'data-action': 'mousedown->search-input#prevent'}
%div{class: "search-container #{display_all_mode_class}", 'data-search-input-target': 'dropDown', 'data-action': 'mousedown->search-input#prevent'}
- @actions_links.each do |key, value|
- link, target = action_link_info(value)
%a.search-content#search-content{href: link, 'data-turbo-frame': target, 'data-search-input-target': 'actionLink'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default class extends Controller {
idKey: String,
cache: {type: Boolean, default: true},
selectedItem: Number,
searchEndpoint: {type: String, default: '/search'}
searchEndpoint: {type: String, default: '/search'},
displayAll: Boolean
}

connect() {
Expand Down Expand Up @@ -114,14 +115,14 @@ export default class extends Controller {
this.dropDown.innerHTML = ""
let breaker = 0
for (let i = 0; i < this.items.length; i++) {
if (breaker === 4) {
if (!this.displayAllValue && breaker === 4) {
break;
}
// Get the current item from the ontologies array
const item = this.items[i];

let text = Object.values(item).reduce((acc, value) => acc + value, "")

// Check if the item contains the substring
if (!this.cacheValue || text.toLowerCase().includes(inputValue.toLowerCase())) {
results_list.push(item);
Expand Down Expand Up @@ -149,18 +150,22 @@ export default class extends Controller {

#renderLine(item) {
let template = this.templateTarget.content
let newElement = template.firstElementChild
let string = newElement.outerHTML

let newElement = template.firstElementChild.outerHTML
Object.entries(item).forEach( ([key, value]) => {
key = key.toString().toUpperCase()
if (key === 'TYPE'){
value = value.toString().split('/').slice(-1)
}
if (key === 'ACRONYM'){
Copy link
Collaborator

@syphax-bouazzouni syphax-bouazzouni Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specific code of Agents in generic stimulus controller

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a better solution

value = value ? `(${value.toString()})` : ''
}
if (key === 'IDENTIFIERS'){
value = value ? `- ${value.toString()}` : ''
}
const regex = new RegExp('\\b' + key + '\\b', 'gi');
string = string.replace(regex, value ? value.toString() : "")
newElement = newElement.replace(regex, value ? value.toString() : "")
})

return new DOMParser().parseFromString(string, "text/html").body.firstElementChild
return new DOMParser().parseFromString(newElement, "text/html").body.firstElementChild
}
}
6 changes: 3 additions & 3 deletions app/controllers/agents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def show
end

def ajax_agents
filters = { name: params[:name] }
Bilelkihal marked this conversation as resolved.
Show resolved Hide resolved
filters[:agentType] = params[:agent_type] if params[:agent_type]
filters = { query: params[:query]}
@agents = LinkedData::Client::Models::Agent.all(filters)
agents_json = @agents.map do |x|
{
id: x.id,
name: x.name,
type: x.agentType,
identifiers: x.identifiers.map { |i| "#{i.schemaAgency}:#{i.notation}" }.join(', ')
identifiers: x.identifiers.map { |i| "#{i.schemaAgency}:#{i.notation}" }.join(', '),
Bilelkihal marked this conversation as resolved.
Show resolved Hide resolved
acronym: x.acronym
}
end

Expand Down
Loading