Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
70 changes: 7 additions & 63 deletions static/js/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,15 @@ function sortAlphabetically(list) {
})
}

function sanitize(unsafeMsg) {
const parser = new DOMParser();
let doc = parser.parseFromString(unsafeMsg, 'text/html');
return doc.body.innerText;
}

function toast(message, success) {
bulmaToast.toast({
message: `<span class="icon"><i class="fas fa-${success ? 'check' : 'exclamation'}"></i></span> ${message}`,
message: `<span class="icon"><i class="fas fa-${success ? 'check' : 'exclamation'}"></i></span> ${sanitize(message)}`,
type: `toast ${success ? 'is-success' : 'is-danger'}`,
position: 'bottom-right',
duration: '3000',
Expand Down Expand Up @@ -250,41 +256,6 @@ function uuidv4() {
});
}

// TODO: JQuery functions

function showHide(show, hide) {
$(show).each(function () {
$(this).prop('disabled', false).css('opacity', 1.0)
});
$(hide).each(function () {
$(this).prop('disabled', true).css('opacity', 0.5)
});
}

function validateFormState(conditions, selector) {
(conditions) ?
updateButtonState(selector, 'valid') :
updateButtonState(selector, 'invalid');
}

function updateButtonState(selector, state) {
(state === 'valid') ?
$(selector).attr('class', 'button-success atomic-button') :
$(selector).attr('class', 'button-notready atomic-button');
}

function stream(msg, speak = false) {
let streamer = $('#streamer');
if (streamer.text() != msg) {
streamer.fadeOut(function () {
if (speak) {
window.speechSynthesis.speak(new SpeechSynthesisUtterance(msg));
}
$(this).text(msg).fadeIn(1000);
});
}
}

/* SECTIONS */

// Alternative to JQuery parseHTML(keepScripts=true)
Expand All @@ -308,33 +279,6 @@ function removeSection(identifier) {
$('#' + identifier).remove();
}

/* AUTOMATIC functions for all pages */

$(document).ready(function () {
$(document).find("select").each(function () {
if (!$(this).hasClass('avoid-alphabetizing')) {
alphabetize_dropdown($(this));
let observer = new MutationObserver(function (mutations, obs) {
obs.disconnect();
alphabetize_dropdown($(mutations[0].target));
obs.observe(mutations[0].target, {childList: true});
});
observer.observe(this, {childList: true});
}
});
});

function alphabetize_dropdown(obj) {
let selected_val = $(obj).children("option:selected").val();
let disabled = $(obj).find('option:disabled');
let opts_list = $(obj).find('option:enabled').clone(true);
opts_list.sort(function (a, b) {
return a.text.toLowerCase() == b.text.toLowerCase() ? 0 : a.text.toLowerCase() < b.text.toLowerCase() ? -1 : 1;
});
$(obj).empty().append(opts_list).prepend(disabled);
obj.val(selected_val);
}

function b64DecodeUnicode(str) { //https://stackoverflow.com/a/30106551
if (str != null) {
// An error check is needed in case the wrong codec (i.e. not UTF-8) was used at source
Expand Down
1 change: 1 addition & 0 deletions templates/configurations.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ <h2>Configuration</h2>
},

enablePlugin(pluginName) {
pluginName = sanitize(pluginName);
let requestBody = {
value: pluginName,
prop: 'plugin'
Expand Down
4 changes: 3 additions & 1 deletion templates/operations.html
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,7 @@ <h2>Operations</h2>
} else if (!this.isJitterValid()) {
toast('Jitter values are invalid', false);
} else {
this.operationToStart.name = sanitize(this.operationToStart.name);
this.operationToStart.autonomous = Number(this.operationToStart.autonomous);
this.operationToStart.use_learning_parsers = parseInt(this.operationToStart.use_learning_parsers, 10) === 1;
this.operationToStart.auto_close = parseInt(this.operationToStart.auto_close, 10) === 1;
Expand All @@ -1458,7 +1459,8 @@ <h2>Operations</h2>
let stringToReplace = (dateMatches && dateMatches.length) ? dateMatches[dateMatches.length - 1] : '';
let date = `(${new Date().toLocaleString()})`;
newOp.name = stringToReplace ? (newOp.name.replace(stringToReplace, date)) : (`${newOp.name} ${date}`);

newOp.name = sanitize(newOp.name);

apiV2('POST', this.ENDPOINT, newOp).then((newOperation) => {
toast(`Started operation ${newOperation.name}!`, true);
this.operations.push(newOperation);
Expand Down