diff options
Diffstat (limited to 'app/assets/javascripts/issue_show/utils/update_description.js')
-rw-r--r-- | app/assets/javascripts/issue_show/utils/update_description.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/app/assets/javascripts/issue_show/utils/update_description.js b/app/assets/javascripts/issue_show/utils/update_description.js new file mode 100644 index 00000000000..315f6c23b02 --- /dev/null +++ b/app/assets/javascripts/issue_show/utils/update_description.js @@ -0,0 +1,38 @@ +import _ from 'underscore'; + +/** + * Function that replaces the open attribute for the <details> element. + * + * @param {String} descriptionHtml - The html string passed back from the server as a result of polling + * @param {Array} details - All detail nodes inside of the issue description. + */ + +const updateDescription = (descriptionHtml = '', details) => { + let detailNodes = details; + + if (_.isEmpty(details)) { + detailNodes = []; + } + + const placeholder = document.createElement('div'); + placeholder.innerHTML = descriptionHtml; + + const newDetails = placeholder.getElementsByTagName('details'); + + if (newDetails.length !== detailNodes.length) { + return descriptionHtml; + } + + Array.from(newDetails).forEach((el, i) => { + /* + * <details> has an open attribute that can have a value, "", "true", "false" + * and will show the dropdown, which is why we are setting the attribute + * explicitly to true. + */ + if (detailNodes[i].open) el.setAttribute('open', true); + }); + + return placeholder.innerHTML; +}; + +export default updateDescription; |