UNPKG

electron-kit

Version:

Utility kits for middle-scale electron app

193 lines (159 loc) 5.69 kB
// Generated by CoffeeScript 1.10.0 (function() { var CommandManager, Disposable, Emitter, _, ipcRenderer, 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, slice = [].slice; ipcRenderer = require("electron").ipcRenderer; _ = require("lodash"); Disposable = require("event-kit").Disposable; Emitter = require("../utils/Emitter"); /** * Renderer side Command manager */ module.exports = CommandManager = (function(superClass) { extend(CommandManager, superClass); function CommandManager() { CommandManager.__super__.constructor.apply(this, arguments); this._emitter = new Emitter(); this._domObservers = {}; this._handleEvents(); } /** * @protected */ CommandManager.prototype._handleEvents = function() { ipcRenderer.on("command", this._didReceived.bind(this)); }; /** * Dispatch command to Renderer and Browser process * @param {String} command * @param {Any...} args */ CommandManager.prototype.dispatch = function() { var args, command; command = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; this.emit.apply(this, [command].concat(slice.call(args))); this.dispatchToBrowser.apply(this, [command].concat(slice.call(args))); }; /** * Dispatch command to Browser process * @param {String} command * @param {Any...} args */ CommandManager.prototype.dispatchToBrowser = function() { var args, command; command = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; ipcRenderer.send.apply(ipcRenderer, ["command", command].concat(slice.call(args))); this._emitter.emit("did-send", { command: command, args: args }); }; CommandManager.prototype.on = function(command, handler) { var disposables, eventName, listener; if (_.isPlainObject(command)) { disposables = (function() { var results; results = []; for (eventName in command) { listener = command[eventName]; results.push(this.on(eventName, listener)); } return results; }).call(this); return new Disposable(function() { disposables.forEach(function(disposable) { return disposable.dispose(); }); disposables = null; }); } return CommandManager.__super__.on.apply(this, arguments); }; /** * Add command observer on DOMElement * @param {String|HTMLElement} selector handler element (or CSS selector) * @param {String|Object.<String,Function>} command handle command * @param {Function} callback command listener * @return {Disposable} */ CommandManager.prototype.observeOn = function(selector, command, handler) { var base, commandName, commandObserver, disposables, disposer, listener, observerSet; if (_.isPlainObject(command)) { disposables = (function() { var results; results = []; for (commandName in event) { handler = event[commandName]; results.push(this.observeOn(selector, commandName, handler)); } return results; }).call(this); return new Disposable(function() { disposables.forEach(function(disposable) { return disposable.dispose(); }); disposables = null; }); } listener = function() { var currentElement; currentElement = document.activeElement; if (_.isElement(selector) && currentElement === selector) { return callback.call(currentElement); } else if (currentElement.matches(selector)) { return callback.call(currentElement); } }; observerSet = { selector: selector, listener: listener }; commandObserver = (base = this._domObservers)[command] != null ? base[command] : base[command] = new Set; commandObserver.add(observerSet); disposer = this.on(command, listener); return new Disposable((function(_this) { return function() { commandObserver["delete"](observerSet); return disposer.dispose(); }; })(this)); }; /** * @private */ CommandManager.prototype._didReceived = function() { var args, command; command = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; this.emit.apply(this, [command].concat(slice.call(args))); this._emitter.emit("did-receive", { command: command, args: args }); }; /** * @param {Function} fn listener */ CommandManager.prototype.onDidSend = function(fn) { this._emitter.on("did-send", fn); return new Disposable((function(_this) { return function() { return _this.off("did-send", fn); }; })(this)); }; /** * @param {Function} fn listener */ CommandManager.prototype.onDidReceive = function(fn) { this._emitter.on("did-receive", fn); return new Disposable((function(_this) { return function() { return _this.off("did-receive", fn); }; })(this)); }; return CommandManager; })(Emitter); }).call(this);