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
DATAUP-639 dynamic dropdown fixes #2911
Conversation
@@ -543,7 +543,7 @@ def test_get_all_job_states__ignore_refresh_flag(self): | |||
states, | |||
) | |||
|
|||
## get_job_states_by_cell_id | |||
# get_job_states_by_cell_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This made my flake8
freak out. Not sure how that happened.
value: undefined, | ||
}, | ||
userId = runtime.userId(); | ||
descriptionFields: new Set(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will hold the fields used in the description template. So if the template is:
NCBI Taxon ID: {{ncbi_taxon_id}}: {{scientific_name}}
it will have the values ncbi_taxon_id
and scientific_name
.
const newOption = new Option(value, value, true, true); | ||
$(control).append(newOption).trigger('change'); | ||
if (model.value) { | ||
const selectorValue = value.replace(/"/g, '\\"').replace(/'/g, "\\'"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is from an issue with the FTP staging version of the dropdown - if there are quotes in the value (say, a file name with quotes in it), it goes all kinds of wonky. Need to be escaped.
const dataAdapter = $(control).data('select2').dataAdapter; | ||
const newOption = dataAdapter.option( | ||
Object.assign({ id: model.value, value: model.value }, displayValue) | ||
); | ||
dataAdapter.addOptions(newOption); | ||
$(control).val(value).trigger('change'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From some deep diving in Select2 code and docs, this creates a new option with all of the data that the Select2 option would otherwise see from a successful search, and lets Select2 do the usual rendering.
* @param {string} searchTerm | ||
* @returns Array | ||
*/ | ||
async function fetchFtpStagingData(searchTerm) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Popped this out of the fetchData
function, effectively unchanged otherwise.
@@ -272,7 +300,7 @@ define([ | |||
// could check here that each item is a map? YAGNI | |||
obj = flattenObject(obj); | |||
if (!('id' in obj)) { | |||
obj.id = _index; // what the fuck | |||
obj.id = _index; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every object passed to Select2 needs a unique id
field.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if only the original author of that line had read the docs!
* @returns {JQueryElement} a jQuery-wrapped div element | ||
*/ | ||
function formatFtpStagingDisplay(fileData) { | ||
return $( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took this out of the formatObjectDisplay
function.
Really, the StagingService dropdown could be its own input, or a subclass of this one if we were doing object-oriented things properly.
else { | ||
formattedString = retObj.text || retObj.id; | ||
} | ||
// and squash the result in a jQuery object with a div. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Select2 wants either a string (which will be put in as a textContent field to avoid XSS things) or a jquery element. We do our own escaping with util/string
, so there should be less danger.
This pull request introduces 2 alerts when merging 61e252f into 3cb1d2a - view on LGTM.com new alerts:
|
delay: 250, | ||
processResults: (data) => { | ||
// update the currently selected one if applicable | ||
const currentData = dropdown.select2('data')[0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes the issue where display data doesn't get updated. So if you start with undefined
stuff in the currently selected option, then do a search that brings in a matching value, this will update that option with data from the service.
.on('change', () => { | ||
doChange(); | ||
const data = dropdown.select2('data'); | ||
return doChange(data ? data[0] : {}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Send the data object along when doing a change, so we save the display value as well.
const control = ui.getElement('input-container.input'); | ||
if (control) { | ||
$(control).select2('destroy').html(''); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Select2 puts all kinds of things in all kinds of places in the DOM. We need to specifically call destroy
to make sure it all goes away.
Codecov Report
@@ Coverage Diff @@
## develop #2911 +/- ##
========================================
Coverage 73.20% 73.20%
========================================
Files 36 36
Lines 3896 3896
========================================
Hits 2852 2852
Misses 1044 1044 Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This pull request introduces 2 alerts when merging a666982 into dd9200d - view on LGTM.com new alerts:
|
This pull request introduces 2 alerts when merging 63e0348 into dfe7696 - view on LGTM.com new alerts:
|
SonarCloud Quality Gate failed. 0 Bugs No Coverage information |
Description of PR purpose/changes
This makes progress toward fixing some issues with the dynamic dropdown. It specifically addresses how values get displayed on load or on update.
There are a few issues here.
First, when the dropdown loads, if there's an initialValue given, it creates and selects an initial option. The problem is, there's no display info. For the taxonomy id case, it gets an
id
value, say, 12345. To fill out the display template, it needs ascientific_name
andncbi_taxon_id
. Without those, the field would render in as "NCBI Tax ID undefined: undefined".Second, when doing a search that reveals the same value, the actual answer would never get updated - a search that came back with tax id 12345 would always display that "undefined" stuff.
The main problem is that Select2 is a big jerk and doesn't like to change its Option data. There've been Select2 tickets existing since like 2014. (i.e.: select2/select2#2830) The only solutions are semi-horrible hacks, so that's what happen here. Fun!
The core fixes introduced here are:
newDisplayValue
along withnewValue
on thechanged
bus message. Not consumed anywhere yet.initialDisplayValue
in its config, expected to be an object with the keys from thedescription_template
config expected.initialDisplayValue
is not included, it defaults to the actual value. Not any worse than showingundefined
s?Jira Ticket / Issue
Related Jira ticket: https://kbase-jira.atlassian.net/browse/DATAUP-639
DATAUP-69 Adds a PR template
)Testing Instructions
Dev Checklist:
Updating Version and Release Notes (if applicable)