2013-09-06 07:04:20 +00:00
|
|
|
define([
|
2013-09-06 18:24:20 +00:00
|
|
|
"settings!menus,keys",
|
2013-09-06 07:04:20 +00:00
|
|
|
"editor",
|
|
|
|
"dialog",
|
|
|
|
"command",
|
|
|
|
"dom2"
|
|
|
|
], function(Settings, editor, dialog, command) {
|
2013-09-03 15:47:59 +00:00
|
|
|
|
|
|
|
var cfg = Settings.get("menus");
|
2013-09-06 18:24:20 +00:00
|
|
|
var keys = Settings.get("keys");
|
|
|
|
var commands = editor.commands.commands;
|
|
|
|
|
|
|
|
var findKeyCombo = function(command) {
|
|
|
|
//check key config
|
|
|
|
for (var key in keys) {
|
|
|
|
var action = keys[key];
|
|
|
|
var verb = action.ace || action.command || action;
|
|
|
|
if (verb == command) {
|
|
|
|
var char = key.split("-").pop();
|
|
|
|
if (/[A-Z]$/.test(char)) {
|
|
|
|
char = "Shift-" + char.toUpperCase();
|
|
|
|
} else {
|
|
|
|
char = char[0].toUpperCase() + char.substr(1);
|
|
|
|
}
|
|
|
|
var prefix = "";
|
|
|
|
if (key.indexOf("^") > -1) {
|
|
|
|
prefix += "Ctrl-";
|
|
|
|
}
|
|
|
|
if (key.indexOf("M-") > -1) {
|
|
|
|
prefix += "Alt-";
|
|
|
|
}
|
|
|
|
return prefix + char;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var cmd in editor.commands.commands) {
|
|
|
|
if (cmd == command && editor.commands.commands[cmd].bindKey.win) {
|
|
|
|
return editor.commands.commands[cmd].bindKey.win.split("|").shift();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-23 01:08:04 +00:00
|
|
|
|
2013-09-01 18:09:09 +00:00
|
|
|
var menubar = document.find(".toolbar");
|
2013-08-23 01:08:04 +00:00
|
|
|
|
2013-09-06 07:04:20 +00:00
|
|
|
var walker = function(list, depth) {
|
2013-08-23 01:08:04 +00:00
|
|
|
var fragment = document.createDocumentFragment();
|
|
|
|
for (var i = 0; i < list.length; i++) {
|
|
|
|
var entry = list[i];
|
2013-08-23 01:27:16 +00:00
|
|
|
if (typeof entry == "string") {
|
|
|
|
var preset;
|
|
|
|
switch (entry) {
|
|
|
|
case "divider":
|
|
|
|
preset = document.createElement("hr");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fragment.appendChild(preset);
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-23 01:08:04 +00:00
|
|
|
var li = document.createElement("li");
|
|
|
|
li.innerHTML = entry.label;
|
2013-09-06 18:24:20 +00:00
|
|
|
if (entry.command) {
|
|
|
|
li.setAttribute("command", entry.command);
|
|
|
|
var shortcut = findKeyCombo(entry.command == "ace:command" ? entry.argument : entry.command);
|
|
|
|
if (shortcut) {
|
|
|
|
li.innerHTML += "<span class=shortcut>" + shortcut + "</span>";
|
|
|
|
}
|
|
|
|
if (entry.argument) li.setAttribute("argument", entry.argument);
|
|
|
|
}
|
2013-08-23 01:08:04 +00:00
|
|
|
if (entry.sub) {
|
2013-09-06 07:04:20 +00:00
|
|
|
if (depth) {
|
|
|
|
li.className = "parent";
|
|
|
|
} else {
|
|
|
|
li.className = "top";
|
|
|
|
}
|
2013-08-23 01:08:04 +00:00
|
|
|
var ul = document.createElement("ul");
|
|
|
|
ul.className = "menu";
|
2013-09-06 07:04:20 +00:00
|
|
|
ul.appendChild(walker(entry.sub, depth + 1));
|
2013-08-23 01:08:04 +00:00
|
|
|
li.appendChild(ul);
|
|
|
|
}
|
|
|
|
fragment.appendChild(li);
|
|
|
|
}
|
|
|
|
return fragment;
|
|
|
|
}
|
2013-09-06 18:24:20 +00:00
|
|
|
|
|
|
|
var create = function() {
|
|
|
|
var menuElements = walker(cfg, 0);
|
|
|
|
menubar.innerHTML = "";
|
|
|
|
menubar.appendChild(menuElements);
|
|
|
|
}
|
2013-08-23 01:08:04 +00:00
|
|
|
|
2013-09-06 18:24:20 +00:00
|
|
|
command.on("init:startup", create);
|
|
|
|
command.on("init:restart", function() {
|
|
|
|
cfg = Settings.get("menus");
|
|
|
|
keys = Settings.get("keys");
|
|
|
|
create();
|
|
|
|
})
|
2013-09-06 07:04:20 +00:00
|
|
|
|
|
|
|
menubar.addEventListener("click", function(e) {
|
|
|
|
menubar.focus();
|
|
|
|
var el = e.target;
|
|
|
|
if (el.classList.contains("top")) {
|
|
|
|
el.classList.toggle("active");
|
|
|
|
}
|
|
|
|
menubar
|
|
|
|
.findAll(".active")
|
|
|
|
.filter(function(n) { return n != el })
|
|
|
|
.forEach(function(n) { n.classList.remove("active") });
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.on("focus", function(e) {
|
|
|
|
menubar.findAll(".active").forEach(function(node) { node.classList.remove("active"); });
|
|
|
|
});
|
|
|
|
|
|
|
|
command.on("app:about", function() {
|
|
|
|
dialog(
|
|
|
|
document.find("#about").content.cloneNode(true).find("div").innerHTML,
|
|
|
|
["ok"]
|
|
|
|
);
|
|
|
|
})
|
2013-08-23 01:08:04 +00:00
|
|
|
|
|
|
|
});
|