Skip to content

Commit

Permalink
Merge pull request #2729 from noodom/patch-24
Browse files Browse the repository at this point in the history
display scenario list options groups
  • Loading branch information
zoic21 authored Jul 7, 2024
2 parents f0a440f + 37e33c3 commit ad6444d
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions core/template/scenario/scenario.default.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,51 @@
},
success: function (scenarios) {
const select = document.querySelector('.expressionAttr[data-uid="#uid#"][data-l1key="options"][data-l2key="scenario_id"]')
let newOption

const groupedScenarios = {}
for (let i in scenarios) {
newOption = document.createElement('option')
newOption.text = scenarios[i].humanName
newOption.value = scenarios[i].id
select.appendChild(newOption)
const match = scenarios[i].humanName.match(/^\[(.*?)\]/)
if (match) {
const obj = match[1]
if (!groupedScenarios[obj]) {
groupedScenarios[obj] = []
}
groupedScenarios[obj].push(scenarios[i])
}
}

for (let group in groupedScenarios) {
const optgroup = document.createElement('optgroup')
optgroup.label = group

groupedScenarios[group].forEach(scenario => {
const newOption = document.createElement('option')
newOption.text = scenario.humanName.replace(/^\[.*?\]/, '').trim()
newOption.value = scenario.id
optgroup.appendChild(newOption)
})

select.appendChild(optgroup)
}

const filter = document.querySelector('.expressionAttr[data-uid="#uid#"][data-l1key="options"][data-l2key="filter"]')
filter.addEventListener('keyup', function(event) {
const text = event.target.value
const options = Array.from(select.options)
options.forEach (function(option) {
const regex = new RegExp("^" + text, "i")
const lowerText = text.toLowerCase()

options.forEach(option => {
const optionText = option.text
const lowerOptionText = optionText.toLowerCase()
const lowerText = text.toLowerCase()
const regex = new RegExp("^" + text, "i")
const match = optionText.match(regex)
const contains = lowerOptionText.indexOf(lowerText) != -1
option.hidden = match || contains ? false: true
const contains = lowerOptionText.indexOf(lowerText) !== -1
option.hidden = !(match || contains)
});

Array.from(select.getElementsByTagName('optgroup')).forEach(optgroup => {
const visibleOptions = Array.from(optgroup.children).some(option => !option.hidden)
optgroup.hidden = !visibleOptions
})
})

Expand Down

0 comments on commit ad6444d

Please sign in to comment.