Skip to content

Commit

Permalink
Minor improvement 8.1.7.3
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoudkooi committed Jul 10, 2024
1 parent 02e2eb0 commit 44230c6
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 71 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG.md

## 8.1.7.0 (2024-07-10)
Features:
- Improvements to the studio artefact filter. In large scopes it could block and make the tab unresposive.
- Adding sysparm_transaction_scope parameter when saving from the SN Utils Monaco code editor, so that it works after the instance switched to a different scope.
- Adding a icon next to the SN Utils Monaco code editor select list, to open the selected underlying sys_update_version record in a new tab.


## 8.1.7.0 (2024-07-08)
Features:
- The Monaco code editor that opens when clicking the icon next to a script, now shows a select with versions. Selecting a version shows the diff editor with the selected version left and the current version right.
Expand Down
1 change: 1 addition & 0 deletions codeeditor.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<select class="versions" title="Select version to compare with current">

</select>
<a title="Open selected sys_update_version record" class="version" target="_blank" href="#"></a>
</div>
<div id="state">

Expand Down
7 changes: 6 additions & 1 deletion css/codeeditor.css
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,9 @@ select.versions option {
background-color: #032d42;
color: #ffffff;
font-family: 'Courier New', Courier, monospace;
}
}

a.version {
text-decoration: none;
display: none;
}
151 changes: 84 additions & 67 deletions inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,14 @@ if (typeof jQuery != "undefined") {
// We have to call the function twice since we don't know what type of related list loading is selected by a user (with the form or after forms loads).
snuDoubleClickToSetQueryListV2();
if (typeof CustomEvent.observe == 'function') {
CustomEvent.observe('related_lists.ready', function () {
snuDoubleClickToSetQueryListV2();
});
try{ //sometimes issues with the prototype library
CustomEvent.observe('related_lists.ready', function () {
snuDoubleClickToSetQueryListV2();
});
} catch (e) {
//console.log('SN Utils: CustomEvent.observe error', e);
}

}
snuDoubleClickToShowFieldOrReload();
snuCaptureFormClick();
Expand Down Expand Up @@ -4505,40 +4510,28 @@ function snuAddDblClick() {
}

function snuSortStudioLists() {
snuDoGroupSearch(""); //call to remove var__m_ from flowdesigner

var elULs = document.querySelectorAll('.app-explorer-tree ul.file-section :not(a) > ul');
snuDoGroupSearch(""); // Call to remove var__m_ from flowdesigner

Array.prototype.forEach.call(elULs, function (ul) {
// Cache the query selector results
const elULs = document.querySelectorAll('.app-explorer-tree ul.file-section :not(a) > ul');

var nestedUls = ul.querySelectorAll('ul.file-section');
elULs.forEach(ul => {
const nestedUls = ul.querySelectorAll('ul.file-section');
if (nestedUls.length > 0) {
Array.prototype.forEach.call(nestedUls, function (nu) {
sortList(nu);
});
}
else
nestedUls.forEach(nu => sortList(nu));
} else {
sortList(ul);
}
});

function sortList(list) {
var i, switching, b, shouldSwitch;
switching = true;
while (switching) {
switching = false;
b = list.getElementsByTagName("li");
for (i = 0; i < (b.length - 1); i++) {
shouldSwitch = false;
if (b[i].innerHTML.toLowerCase() > b[i + 1].innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
b[i].parentNode.insertBefore(b[i + 1], b[i]);
switching = true;
}
}
const itemsArray = Array.from(list.getElementsByTagName("li"));
itemsArray.sort((a, b) => a.innerHTML.toLowerCase().localeCompare(b.innerHTML.toLowerCase()));

// Remove all items from the list and re-add them in sorted order
const fragment = document.createDocumentFragment();
itemsArray.forEach(item => fragment.appendChild(item));
list.appendChild(fragment);
}
}

Expand Down Expand Up @@ -4571,62 +4564,86 @@ function snuAddStudioScriptSync() {

//Some magic to filter the file tree in studio
function snuDoGroupSearch(search) {
//expand all when searching
Array.prototype.forEach.call(document.querySelectorAll('.app-explorer-tree li.collapsed'), function (el, i) {
el.classList.remove('collapsed');
});
// Cache the query selector results
const collapsedElements = document.querySelectorAll('.app-explorer-tree li.collapsed');
const dataViewElements = document.querySelectorAll('[data-view-count]');
const treeElements = document.querySelectorAll('.app-explorer-tree li:not(.nav-group)');

// Expand all when searching
collapsedElements.forEach(el => el.classList.remove('collapsed'));

Array.prototype.forEach.call(document.querySelectorAll('[data-view-count]'), function (el, i) {
// Reset dataset attributes and display
dataViewElements.forEach(el => {
el.dataset.viewCount = 0;
el.dataset.searching = false;
el.parentElement.style.display = "";
});

search = search.split(',');
var srch = search[0].toLowerCase();
const srch = search[0].toLowerCase();

const toHide = [];
const toShow = [];
const toUpdateViewCount = [];

// Function to get parents and their text content
function getParentsText(el) {
const parents = [];
let parent = el.closest('ul');
let text = '';

while (parent) {
parents.push(parent);
const span = parent.parentElement.querySelector('span');
if (span) text += span.innerText.toLowerCase() + ' ';
parent = parent.parentElement.closest('ul');
}
return { parents, text: text.trim() };
}

//filter based on item text.
var elms = document.querySelectorAll('.app-explorer-tree li:not(.nav-group)');
// Filter based on item text
treeElements.forEach(el => {
el.setAttribute("title", el.innerText);
const itemText = el.innerText.toLowerCase();
const { parents, text } = getParentsText(el);

Array.prototype.forEach.call(elms, function (el, i) {
const combinedText = (search.length === 1) ? itemText + ' ' + text : text;
let match = combinedText.includes(srch) && !combinedText.includes("var__m_");

var parents = snuGetParents(el, 'ul').reverse();
el.setAttribute("title", el.innerText);
var text = (search.length == 1) ? el.innerText.toLowerCase() + ' ' : '';
var pars = [];
Array.prototype.forEach.call(parents, function (par, i) {
parents.forEach(par => {
par.dataset.searching = true;
text += par.parentElement.getElementsByTagName('span')[0].innerText.toLowerCase() + ' ';
pars.push(par);
for (par of pars) {
if (text.includes(srch) && !text.includes("var__m_")) {
par.dataset.viewCount = (Number(par.dataset.viewCount) || 0) + 1;
}
else {
par.dataset.viewCount = (Number(par.dataset.viewCount) || 0);
el.style.display = "none";
}
if (match) {
par.dataset.viewCount = (Number(par.dataset.viewCount) || 0) + 1;
}

if (text.includes(srch) && !text.includes("var__m_")) {
el.style.display = "";
}
else
el.style.display = "none";
});

if (match) {
toShow.push(el);
} else {
toHide.push(el);
}
});

Array.prototype.forEach.call(document.querySelectorAll('[data-view-count]'), function (el, i) {
if (el.dataset.viewCount == "0" && el.dataset.searching == "true")
// Batch update display styles
toHide.forEach(el => el.style.display = "none");
toShow.forEach(el => el.style.display = "");

// Hide elements with no matching children
dataViewElements.forEach(el => {
if (el.dataset.viewCount === "0" && el.dataset.searching === "true") {
el.parentElement.style.display = "none";
}
});


// Additional search if there are multiple search terms
if (search.length > 1) {
srch = search[1];
var elms = document.querySelectorAll('.app-explorer-tree li:not(.nav-group)');
Array.prototype.forEach.call(elms, function (el, i) {
el.style.display = el.innerText.toLowerCase().includes(srch.toLowerCase()) ? "" : "none";
const additionalSearchTerm = search[1].toLowerCase();
treeElements.forEach(el => {
if (el.innerText.toLowerCase().includes(additionalSearchTerm)) {
el.style.display = "";
} else {
el.style.display = "none";
}
});
}
}
Expand Down
8 changes: 6 additions & 2 deletions js/monaco/codeeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ async function loadVersionSelect(){

if (fieldValue) {
const fieldContent = fieldValue.textContent;
let url =
document.querySelector('a.version').style.display = 'inline';
document.querySelector('a.version').href = data.instance.url + '/sys_update_version.do?sys_id=' + selected;
changeToDiffEditor(fieldContent);
if (!e.target.selectedOptions[0].innerHTML.includes('*'))
e.target.selectedOptions[0].innerHTML = e.target.selectedOptions[0].innerHTML + '*';
Expand All @@ -264,7 +267,7 @@ async function loadVersionSelect(){
}
else {
changeToEditor(getEditor().getValue());
console.log(`No ${data.field} element found`);
document.querySelector('a.version').style.display = 'none';
}
});

Expand Down Expand Up @@ -314,7 +317,8 @@ async function updateRecord() {
if (!confirm('Your code has errors!\nContinue with save action?')) return;
}
try {
const url = `${data.instance.url}/api/now/table/${data.table}/${data.sys_id}?sysparm_fields=sys_id`;
let url = `${data.instance.url}/api/now/table/${data.table}/${data.sys_id}?sysparm_fields=sys_id`;
if (data?.scope) url += `&sysparm_transaction_scope=${data.scope}`;
const postData = {
[data.field]: getEditor().getModel().getValue()
};
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"short_name": "SN Utils",
"description": "Productivity tools for ServiceNow. (Personal work, not affiliated to ServiceNow)",
"author": "Arnoud Kooi / arnoudkooi.com",
"version": "8.1.7.0",
"version": "8.1.7.3",
"manifest_version": 3,
"permissions": [
"activeTab",
Expand Down

0 comments on commit 44230c6

Please sign in to comment.