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 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
25 changes: 23 additions & 2 deletions assets/js/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ES6 imports
let { messages, variables } = require('./messages.json');
let { messages, variables, categories } = require('./messages.json');
osfanbuff63 marked this conversation as resolved.
Show resolved Hide resolved
window.bootstrap = require('bootstrap');
window.$ = window.jQuery = require('jquery');
window.Popper = require('popper.js');
Expand All @@ -20,6 +20,13 @@ var code = "-1";
// map for dropdown values and their corresponding messages
var dropdownMap = new Map();

// map for category keys and their corresponding names
var categoryMap = new Map();

for (const {category, name} of categories) {
categoryMap.set(category, name);
}

/**
* Main app entry point
*/
Expand Down Expand Up @@ -184,6 +191,8 @@ function getStringValue(val) {
function updateDisplay() {
var text = '<option value="-1">Select a message...</option>';
var selected = false;
// Create a map for categorized messages
var messageMap = new Map();
// Clear dropdown map
dropdownMap = new Map();
// Set project dropdown title
Expand All @@ -198,13 +207,25 @@ function updateDisplay() {
if (i == code) {
selected = true;
}
text += `<option value="${i}" ${i == code ? 'selected': ''}>` + messageArray[j].name + '</option>';
var catText = messageMap.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;
messageMap.set(messageArray[j].category, catText);
// Map message to dropdown ID for later recognition
dropdownMap.set(i, messageArray[j]);
}
}
}

// Sort categorized messages alphabetically
messageMap = new Map([...messageMap].sort());

// Format dropdown menu labels
for (const [category, options] of messageMap.entries()) {
text += `<optgroup label="${categoryMap.get(category) || category}">${options}</optgroup>`
}

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