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
|
|
|
|
2013-09-06 18:24:20 +00:00
|
|
|
var commands = editor.commands.commands;
|
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-11-01 23:24:57 +00:00
|
|
|
if (entry.minVersion && entry.minVersion > chrome.version) {
|
|
|
|
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);
|
2013-10-03 18:26:02 +00:00
|
|
|
var command = entry.command == "ace:command" ? entry.argument : entry.command;
|
|
|
|
var arg = entry.command == "ace:command" ? undefined : entry.argument;
|
|
|
|
var shortcut = findKeyCombo(command, arg);
|
2013-09-06 18:24:20 +00:00
|
|
|
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-24 00:57:09 +00:00
|
|
|
};
|
2013-08-23 01:08:04 +00:00
|
|
|
|
2013-10-03 18:26:02 +00:00
|
|
|
var findKeyCombo = function(command, arg) {
|
2013-09-24 00:57:09 +00:00
|
|
|
var keys = Settings.get("keys");
|
|
|
|
//check key config
|
|
|
|
for (var key in keys) {
|
|
|
|
var action = keys[key];
|
|
|
|
var verb = action.ace || action.command || action;
|
2013-10-03 18:26:02 +00:00
|
|
|
var object = action.argument;
|
2013-09-24 00:57:09 +00:00
|
|
|
if (verb == command) {
|
2013-10-03 18:26:02 +00:00
|
|
|
if (arg && object !== arg) continue;
|
2013-09-24 00:57:09 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-09-06 07:04:20 +00:00
|
|
|
}
|
2013-09-24 00:57:09 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
var Menu = function() {
|
|
|
|
this.element = document.find(".toolbar");
|
2013-09-24 01:02:56 +00:00
|
|
|
this.active = false;
|
2013-09-24 00:57:09 +00:00
|
|
|
this.bindEvents();
|
|
|
|
}
|
|
|
|
Menu.prototype = {
|
|
|
|
create: function() {
|
|
|
|
var cfg = Settings.get("menus");
|
|
|
|
var elements = walker(cfg, 0);
|
|
|
|
this.element.innerHTML = "";
|
|
|
|
this.element.appendChild(elements);
|
|
|
|
},
|
|
|
|
bindEvents: function() {
|
2013-09-24 01:02:56 +00:00
|
|
|
var self = this;
|
2013-09-24 00:57:09 +00:00
|
|
|
var menubar = this.element;
|
|
|
|
menubar.addEventListener("click", function(e) {
|
|
|
|
menubar.focus();
|
|
|
|
var el = e.target;
|
|
|
|
if (el.classList.contains("top")) {
|
|
|
|
el.classList.toggle("active");
|
2013-09-24 01:02:56 +00:00
|
|
|
self.active = !self.active;
|
|
|
|
} else {
|
|
|
|
self.active = false;
|
2013-09-24 00:57:09 +00:00
|
|
|
}
|
2013-10-30 16:29:26 +00:00
|
|
|
if (!self.active) {
|
|
|
|
editor.focus();
|
|
|
|
}
|
2013-09-24 00:57:09 +00:00
|
|
|
menubar
|
|
|
|
.findAll(".active")
|
|
|
|
.filter(function(n) { return n != el })
|
|
|
|
.forEach(function(n) { n.classList.remove("active") });
|
|
|
|
});
|
2013-09-24 01:02:56 +00:00
|
|
|
menubar.addEventListener("mousemove", function(e) {
|
|
|
|
var el = e.target;
|
|
|
|
if (el.classList.contains("top") && self.active) {
|
2013-09-24 02:22:03 +00:00
|
|
|
self.deactivate();
|
2013-09-24 01:02:56 +00:00
|
|
|
el.classList.add("active");
|
|
|
|
}
|
|
|
|
});
|
2013-09-24 00:57:09 +00:00
|
|
|
},
|
|
|
|
deactivate: function() {
|
|
|
|
this.element.findAll(".active").forEach(function(node) { node.classList.remove("active") });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var menu = new Menu();
|
|
|
|
|
|
|
|
command.on("init:startup", menu.create.bind(menu));
|
|
|
|
command.on("init:restart", menu.create.bind(menu));
|
2013-09-06 07:04:20 +00:00
|
|
|
|
2013-10-08 00:59:10 +00:00
|
|
|
editor.on("focus", function() {
|
|
|
|
menu.deactivate();
|
|
|
|
menu.active = false;
|
|
|
|
});
|
2013-09-06 07:04:20 +00:00
|
|
|
|
|
|
|
command.on("app:about", function() {
|
2013-09-16 17:22:49 +00:00
|
|
|
var content = document.find("#about").content.cloneNode(true).find("div").innerHTML;
|
|
|
|
var manifest = chrome.runtime.getManifest();
|
|
|
|
content = content.replace("%VERSION%", manifest.version);
|
2013-09-06 07:04:20 +00:00
|
|
|
dialog(
|
2013-09-16 17:22:49 +00:00
|
|
|
content,
|
2013-09-06 07:04:20 +00:00
|
|
|
["ok"]
|
|
|
|
);
|
2013-09-11 06:15:04 +00:00
|
|
|
});
|
2013-08-23 01:08:04 +00:00
|
|
|
|
|
|
|
});
|