2013-09-06 07:04:20 +00:00
|
|
|
define([
|
2013-12-04 17:24:04 +00:00
|
|
|
"settings!menus,keys",
|
|
|
|
"editor",
|
|
|
|
"ui/dialog",
|
|
|
|
"command",
|
2014-01-22 17:26:15 +00:00
|
|
|
"util/template!templates/menuItem.html",
|
2013-12-04 17:24:04 +00:00
|
|
|
"util/dom2"
|
2014-01-22 15:54:06 +00:00
|
|
|
], function(Settings, editor, dialog, command, inflate) {
|
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;
|
|
|
|
}
|
2014-01-22 17:26:15 +00:00
|
|
|
var isAce = entry.command == "ace:command";
|
|
|
|
var data = {
|
|
|
|
command: entry.command,
|
|
|
|
argument: entry.argument,
|
|
|
|
shortcut: isAce ? findKeyCombo(entry.argument) : findKeyCombo(entry.command, entry.argument),
|
|
|
|
hasChildren: entry.sub && !!entry.sub.length,
|
|
|
|
isRoot: !depth,
|
|
|
|
retainFocus: entry.retainFocus,
|
|
|
|
label: entry.label
|
|
|
|
};
|
|
|
|
var element = inflate.get("templates/menuItem.html", data);
|
2013-08-23 01:08:04 +00:00
|
|
|
if (entry.sub) {
|
2014-01-22 17:26:15 +00:00
|
|
|
var children = walker(entry.sub, depth + 1);
|
|
|
|
element.find("ul").append(children);
|
2013-08-23 01:08:04 +00:00
|
|
|
}
|
2014-01-22 17:26:15 +00:00
|
|
|
fragment.append(element);
|
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() {
|
2014-01-22 15:54:06 +00:00
|
|
|
inflate.load("templates/about.html").then(function() {
|
|
|
|
dialog(
|
|
|
|
inflate.getHTML("templates/about.html", {
|
|
|
|
version: chrome.runtime.getManifest().version
|
|
|
|
}),
|
|
|
|
["ok"]
|
|
|
|
);
|
|
|
|
});
|
2013-09-11 06:15:04 +00:00
|
|
|
});
|
2013-08-23 01:08:04 +00:00
|
|
|
|
|
|
|
});
|