Skip to content
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

Categorize messages in dropdown menu #195

Merged
merged 6 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 20 additions & 1 deletion assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ var project = "mc";
// Currently selected message code
var code = "-1";

// map for messages sorted by their categories
var categoryMap = new Map();
Marcono1234 marked this conversation as resolved.
Show resolved Hide resolved

// map for dropdown values and their corresponding messages
var dropdownMap = new Map();

Expand Down Expand Up @@ -86,6 +89,9 @@ $("#copybutton").click(function () {
let messageBody = getStringValue(message.message);
// Replace predefined variables in the message body
for (const variable in variables) {
if (!variables[variable][0]?.project) {
continue;
}
for (const { project: expected, value } of variables[variable]) {
if (isExpectedProject(expected, project)) {
messageBody = messageBody.replace(new RegExp(`%${variable}%`, 'g'), getStringValue(value));
Expand Down Expand Up @@ -184,6 +190,8 @@ function getStringValue(val) {
function updateDisplay() {
var text = '<option value="-1">Select a message...</option>';
var selected = false;
// Clear category map
categoryMap = new Map();
// Clear dropdown map
dropdownMap = new Map();
// Set project dropdown title
Expand All @@ -198,13 +206,24 @@ function updateDisplay() {
if (i == code) {
selected = true;
}
text += `<option value="${i}" ${i == code ? 'selected': ''}>` + messageArray[j].name + '</option>';
var catText = categoryMap.get(messageArray[j].category);
var option = `<option value="${i}"${i == code ? ' selected': ''}>` + messageArray[j].name + '</option>';
Marcono1234 marked this conversation as resolved.
Show resolved Hide resolved
// Append option to category
catText = !catText ? option : catText + option;
categoryMap.set(messageArray[j].category, catText);
Marcono1234 marked this conversation as resolved.
Show resolved Hide resolved
// Map message to dropdown ID for later recognition
dropdownMap.set(i, messageArray[j]);
}
}
}

// Format dropdown menu labels
for (const category of variables.categories) {
if (categoryMap.get(category.category)) {
Marcono1234 marked this conversation as resolved.
Show resolved Hide resolved
text += `<optgroup label="${category.value}">${categoryMap.get(category.category)}</optgroup>`
}
}
dericksonmark marked this conversation as resolved.
Show resolved Hide resolved

// Update dropdown HTML
$(".custom-select").html(text);
if (!selected) {
Expand Down
Loading