electron-kit
Version:
Utility kits for middle-scale electron app
188 lines (152 loc) • 5.55 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var ContextMenuManager, Disposable, Emitter, Menu, _, remote,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
remote = require("electron").remote;
Menu = remote.Menu;
_ = require("lodash");
Disposable = require("event-kit").Disposable;
Emitter = require("../utils/Emitter");
module.exports = ContextMenuManager = (function(superClass) {
extend(ContextMenuManager, superClass);
ContextMenuManager.prototype.menus = null;
function ContextMenuManager() {
ContextMenuManager.__super__.constructor.apply(this, arguments);
this.menus = {};
}
/**
* @param {String} selector
* @param {Array<Object>} menu
*/
ContextMenuManager.prototype.add = function(selector, menu) {
var base;
if (!Array.isArray(menu)) {
throw new TypeError("Menu list must be array.");
}
((base = this.menus)[selector] != null ? base[selector] : base[selector] = new Set).add(menu);
};
/**
* @param {String} selector
* @param {Array<Object>} menu
*/
ContextMenuManager.prototype.remove = function(selector, menu) {
if (this.menus[selector] == null) {
return;
}
return this.menus[selector]["delete"](menu);
};
/**
* Clear all registered context menus
*/
ContextMenuManager.prototype.clear = function() {
this.menus = {};
};
ContextMenuManager.prototype._wrapClick = function(item, el) {
var clickListener;
clickListener = item.click;
return (function(_this) {
return function() {
if (item.selector != null) {
if (typeof Menu.sendActionToFirstResponder === "function") {
Menu.sendActionToFirstResponder(item.selector);
}
}
if (typeof clickListener === "function") {
clickListener.call(el, el);
}
_this.emit("did-click-item", item, el);
if (item.command != null) {
_this.emit("did-click-command-item", item.command, el, item);
}
};
})(this);
};
ContextMenuManager.prototype._translateTemplate = function(template, el) {
var item, items, j, len;
items = _.cloneDeep(template);
for (j = 0, len = items.length; j < len; j++) {
item = items[j];
if (item.metadata == null) {
item.metadata = {};
}
item.click = this._wrapClick(item, el);
if (item.submenu) {
item.submenu = this._translateTemplate(item.submenu, el);
}
}
return items;
};
ContextMenuManager.prototype._templateForElement = function(el) {
var i, item, j, last, len, menuList, presentMenus, prevItem, ref, ref1, selector, smm, unshift;
unshift = Array.prototype.unshift;
smm = this.menus;
presentMenus = [];
for (selector in smm) {
menuList = smm[selector];
if (!el.matches(selector)) {
continue;
}
menuList.forEach(function(item) {
return unshift.apply(presentMenus, item);
});
}
last = presentMenus.length - 1;
if (((ref = presentMenus[0]) != null ? ref.type : void 0) === "separator") {
presentMenus.splice(0, 1);
}
if (((ref1 = presentMenus[last]) != null ? ref1.type : void 0) === "separator") {
presentMenus.splice(last, 1);
}
for (i = j = 0, len = presentMenus.length; j < len; i = ++j) {
item = presentMenus[i];
prevItem = presentMenus[i - 1];
if ((prevItem != null) && prevItem.type === "separator" && item.type === "separator") {
presentMenus.splice(i, 1);
}
}
return this._translateTemplate(presentMenus, el);
};
/**
* Show context menu with related to the current focused element
* @param {HTMLElement} el
*/
ContextMenuManager.prototype.showForElement = function(el) {
var menu;
menu = Menu.buildFromTemplate(this._templateForElement(el));
menu.popup(remote.getCurrentWindow());
};
/**
* Show context menu with related to the current focused element and thats parent elements
* @param {Array<HTMLElement>} path MouseEvent.path array
*/
ContextMenuManager.prototype.showForElementPath = function(path) {
var menu, menuItems, push;
push = Array.prototype.push;
menuItems = path.reduce((function(_this) {
return function(menus, el) {
if (!(el instanceof HTMLElement)) {
return menus;
}
push.apply(menus, _this._templateForElement(el));
return menus;
};
})(this), []);
menu = Menu.buildFromTemplate(menuItems);
menu.popup(remote.getCurrentWindow());
};
/**
* @param {Function} fn listener
*/
ContextMenuManager.prototype.onDidClickCommandItem = function(fn) {
return this.on("did-click-command-item", fn);
};
/**
* @param {Function} fn listener
*/
ContextMenuManager.prototype.onDidClickItem = function(fn) {
return this.on("did-click-command-item", fn);
};
return ContextMenuManager;
})(Emitter);
}).call(this);