2021-07-15 23:21:19 +03:00
|
|
|
const TopMenuModule = (() => {
|
|
|
|
|
2022-02-23 19:16:23 +03:00
|
|
|
const mainMenuItems = document.getElementById('main-menu')?.children ?? [];
|
|
|
|
let infoList = document.getElementById('editor-info') ?? document.createElement("div");
|
2022-02-01 01:12:28 +03:00
|
|
|
let infoElements = {};
|
2021-07-15 23:21:19 +03:00
|
|
|
|
|
|
|
initMenu();
|
|
|
|
|
|
|
|
function initMenu() {
|
2022-01-25 02:33:23 +03:00
|
|
|
// for each button in main menu (starting at 1 to avoid logo), ending at length-1 to avoid
|
|
|
|
// editor info
|
|
|
|
for (let i = 1; i < mainMenuItems.length-1; i++) {
|
2021-07-15 23:21:19 +03:00
|
|
|
|
|
|
|
//get the button that's in the list item
|
|
|
|
const menuItem = mainMenuItems[i];
|
|
|
|
const menuButton = menuItem.children[0];
|
|
|
|
|
|
|
|
//when you click a main menu items button
|
2021-07-20 23:52:51 +03:00
|
|
|
Events.on('click', menuButton, function (e) {
|
2021-11-09 14:19:57 +03:00
|
|
|
// Close the already open menus
|
|
|
|
closeMenu();
|
2021-07-15 23:21:19 +03:00
|
|
|
// Select the item
|
|
|
|
Util.select(e.target.parentElement);
|
2022-01-22 14:18:10 +03:00
|
|
|
e.stopPropagation();
|
2021-07-15 23:21:19 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const subMenu = menuItem.children[1];
|
|
|
|
const subMenuItems = subMenu.children;
|
|
|
|
|
|
|
|
//when you click an item within a menu button
|
|
|
|
for (var j = 0; j < subMenuItems.length; j++) {
|
|
|
|
|
|
|
|
const currSubmenuItem = subMenuItems[j];
|
|
|
|
const currSubmenuButton = currSubmenuItem.children[0];
|
|
|
|
|
|
|
|
switch (currSubmenuButton.textContent) {
|
|
|
|
case 'New':
|
2021-07-20 23:52:51 +03:00
|
|
|
Events.on('click', currSubmenuButton, Dialogue.showDialogue, 'new-pixel');
|
2021-07-15 23:21:19 +03:00
|
|
|
break;
|
|
|
|
case 'Save project':
|
2021-08-14 23:47:49 +03:00
|
|
|
Events.on('click', currSubmenuButton, FileManager.openSaveProjectWindow);
|
2021-07-15 23:21:19 +03:00
|
|
|
break;
|
|
|
|
case 'Open':
|
2021-07-20 23:52:51 +03:00
|
|
|
Events.on('click', currSubmenuButton, FileManager.open);
|
2021-07-15 23:21:19 +03:00
|
|
|
break;
|
|
|
|
case 'Export':
|
2021-08-14 23:47:49 +03:00
|
|
|
Events.on('click', currSubmenuButton, FileManager.openPixelExportWindow);
|
2021-07-15 23:21:19 +03:00
|
|
|
break;
|
|
|
|
case 'Exit':
|
|
|
|
//if a document exists, make sure they want to delete it
|
2021-12-07 14:11:40 +03:00
|
|
|
if (EditorState.documentCreated()) {
|
2021-07-15 23:21:19 +03:00
|
|
|
//ask user if they want to leave
|
|
|
|
if (confirm('Exiting will discard your current pixel. Are you sure you want to do that?'))
|
|
|
|
//skip onbeforeunload prompt
|
|
|
|
window.onbeforeunload = null;
|
|
|
|
else
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Paste':
|
2022-01-22 14:18:10 +03:00
|
|
|
Events.on('click', currSubmenuButton, function(e){
|
|
|
|
Events.emit("ctrl+v");
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2021-07-15 23:21:19 +03:00
|
|
|
break;
|
|
|
|
case 'Copy':
|
2022-01-22 14:18:10 +03:00
|
|
|
Events.on('click', currSubmenuButton, function(e){
|
|
|
|
Events.emit("ctrl+c");
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2021-07-15 23:21:19 +03:00
|
|
|
break;
|
|
|
|
case 'Cut':
|
2022-01-22 14:18:10 +03:00
|
|
|
Events.on('click', currSubmenuButton, function(e){
|
|
|
|
Events.emit("ctrl+x");
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2021-07-15 23:21:19 +03:00
|
|
|
break;
|
|
|
|
case 'Cancel':
|
2022-01-22 14:18:10 +03:00
|
|
|
Events.on('click', currSubmenuButton, function(e){
|
|
|
|
Events.emit("esc-pressed");
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2021-07-15 23:21:19 +03:00
|
|
|
break;
|
2021-12-07 14:11:40 +03:00
|
|
|
//Help Menu
|
2021-07-15 23:21:19 +03:00
|
|
|
case 'Settings':
|
2021-07-20 23:52:51 +03:00
|
|
|
Events.on('click', currSubmenuButton, Dialogue.showDialogue, 'settings');
|
2021-07-15 23:21:19 +03:00
|
|
|
break;
|
|
|
|
case 'Help':
|
2021-07-20 23:52:51 +03:00
|
|
|
Events.on('click', currSubmenuButton, Dialogue.showDialogue, 'help');
|
2021-07-15 23:21:19 +03:00
|
|
|
break;
|
|
|
|
case 'About':
|
2021-07-20 23:52:51 +03:00
|
|
|
Events.on('click', currSubmenuButton, Dialogue.showDialogue, 'about');
|
2021-07-15 23:21:19 +03:00
|
|
|
break;
|
|
|
|
case 'Changelog':
|
2021-07-20 23:52:51 +03:00
|
|
|
Events.on('click', currSubmenuButton, Dialogue.showDialogue, 'changelog');
|
2021-07-15 23:21:19 +03:00
|
|
|
break;
|
|
|
|
}
|
2021-11-09 14:19:57 +03:00
|
|
|
|
|
|
|
Events.on('click', currSubmenuButton, function() {TopMenuModule.closeMenu();});
|
2021-07-15 23:21:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeMenu () {
|
|
|
|
//remove .selected class from all menu buttons
|
|
|
|
for (var i = 0; i < mainMenuItems.length; i++) {
|
|
|
|
Util.deselect(mainMenuItems[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 01:12:28 +03:00
|
|
|
function resetInfos() {
|
|
|
|
infoList.innerHTML = "<ul></ul>";
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateField(fieldId, value) {
|
2022-02-23 19:16:23 +03:00
|
|
|
const elm = document.getElementById(fieldId);
|
|
|
|
if(elm) {
|
|
|
|
elm.value = value;
|
|
|
|
} else {
|
|
|
|
//console.warn('elm === ', elm);
|
|
|
|
}
|
2022-02-01 01:12:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function addInfoElement(fieldId, field) {
|
|
|
|
let liEl = document.createElement("li");
|
|
|
|
|
|
|
|
infoElements[fieldId] = field;
|
|
|
|
liEl.appendChild(field);
|
|
|
|
|
|
|
|
infoList.firstChild.appendChild(liEl);
|
|
|
|
}
|
|
|
|
|
2021-07-15 23:21:19 +03:00
|
|
|
return {
|
2022-02-01 01:12:28 +03:00
|
|
|
closeMenu,
|
|
|
|
updateField,
|
|
|
|
addInfoElement,
|
|
|
|
resetInfos
|
2021-07-15 23:21:19 +03:00
|
|
|
}
|
|
|
|
})();
|