2013-09-06 07:04:20 +00:00
|
|
|
define([
|
2013-12-04 17:24:04 +00:00
|
|
|
"settings!menus,keys",
|
|
|
|
"editor",
|
|
|
|
"ui/dialog",
|
|
|
|
"command",
|
|
|
|
"util/dom2"
|
2013-09-06 07:04:20 +00:00
|
|
|
], 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;
|
|
|
|
}
|
2013-11-04 16:41:22 +00:00
|
|
|
fragment.append(preset);
|
2013-08-23 01:27:16 +00:00
|
|
|
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-11-09 00:32:11 +00:00
|
|
|
if (entry.retainFocus) {
|
|
|
|
li.addClass("no-refocus");
|
|
|
|
}
|
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-11-04 16:41:22 +00:00
|
|
|
ul.append(walker(entry.sub, depth + 1));
|
|
|
|
li.append(ul);
|
2013-08-23 01:08:04 +00:00
|
|
|
}
|
2013-11-04 16:41:22 +00:00
|
|
|
fragment.append(li);
|
2013-08-23 01:08:04 +00:00
|
|
|
}
|
|
|
|
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-12-20 15:56:56 +00:00
|
|
|
//transform old keys and lower-case
|
|
|
|
key = key
|
2013-12-20 17:01:53 +00:00
|
|
|
//back-compat
|
|
|
|
.replace(/(\^|M)-([A-Z]+)$/, "$1-Shift-$2")
|
2013-12-20 15:56:56 +00:00
|
|
|
.replace(/\^-/g, "Ctrl-")
|
|
|
|
.replace(/M-/g, "Alt-")
|
2013-12-20 17:01:53 +00:00
|
|
|
//capitalize keys for lazy people
|
|
|
|
.replace(/(^|-)([a-z])/g, function(match) { return match.toUpperCase(); });
|
2013-12-20 15:56:56 +00:00
|
|
|
return key;
|
2013-09-24 00:57:09 +00:00
|
|
|
}
|
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 = "";
|
2013-11-04 16:41:22 +00:00
|
|
|
this.element.append(elements);
|
2013-09-24 00:57:09 +00:00
|
|
|
},
|
|
|
|
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;
|
2013-11-21 17:40:09 +00:00
|
|
|
var clickElsewhere = function(e) {
|
|
|
|
if (e.target.matches(".toolbar *")) return;
|
|
|
|
self.deactivate();
|
|
|
|
self.active = false;
|
|
|
|
document.body.off("click", clickElsewhere);
|
|
|
|
};
|
2013-09-24 00:57:09 +00:00
|
|
|
menubar.addEventListener("click", function(e) {
|
2013-11-21 17:40:09 +00:00
|
|
|
document.body.on("click", clickElsewhere);
|
2013-09-24 00:57:09 +00:00
|
|
|
var el = e.target;
|
2013-11-04 16:41:22 +00:00
|
|
|
if (el.hasClass("top")) {
|
|
|
|
el.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-11-09 00:32:11 +00:00
|
|
|
if (!self.active && !el.hasClass("no-refocus")) {
|
2013-10-30 16:29:26 +00:00
|
|
|
editor.focus();
|
|
|
|
}
|
2013-09-24 00:57:09 +00:00
|
|
|
menubar
|
|
|
|
.findAll(".active")
|
|
|
|
.filter(function(n) { return n != el })
|
2013-11-04 16:41:22 +00:00
|
|
|
.forEach(function(n) { n.removeClass("active") });
|
2013-09-24 00:57:09 +00:00
|
|
|
});
|
2013-09-24 01:02:56 +00:00
|
|
|
menubar.addEventListener("mousemove", function(e) {
|
|
|
|
var el = e.target;
|
2013-11-04 16:41:22 +00:00
|
|
|
if (el.hasClass("top") && self.active) {
|
2013-09-24 02:22:03 +00:00
|
|
|
self.deactivate();
|
2013-11-04 16:41:22 +00:00
|
|
|
el.addClass("active");
|
2013-09-24 01:02:56 +00:00
|
|
|
}
|
|
|
|
});
|
2013-09-24 00:57:09 +00:00
|
|
|
},
|
|
|
|
deactivate: function() {
|
2013-11-04 16:41:22 +00:00
|
|
|
this.element.findAll(".active").forEach(function(node) { node.removeClass("active") });
|
2013-09-24 00:57:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
});
|