🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

kilid

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kilid - npm Package Compare versions

Comparing version
0.1.0-beta5
to
0.2.0
+61
lib/Kilid.js
// Generated by CoffeeScript 1.6.3
var Kilid, Scope, array,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Scope = require('./kilid/Scope');
array = require('utila/lib/array');
module.exports = Kilid = (function() {
function Kilid(rootNode, id) {
this.rootNode = rootNode != null ? rootNode : document;
this.id = id != null ? id : 'kilid';
this._blur = __bind(this._blur, this);
this._focus = __bind(this._focus, this);
this._keyup = __bind(this._keyup, this);
this._keydown = __bind(this._keydown, this);
this.rootNode.addEventListener('keydown', this._keydown);
this.rootNode.addEventListener('keyup', this._keyup);
window.addEventListener('focus', this._focus);
window.addEventListener('blur', this._blur);
this._keysCurrentlyDown = [];
this._rootScope = new Scope(null, this, this.id);
this._activeScope = null;
this._rootScope.activate();
}
Kilid.prototype._setActiveScope = function(scope) {
if (this._activeScope != null) {
this._activeScope._notActiveAnymore();
}
this._activeScope = scope;
};
Kilid.prototype.getRootScope = function() {
return this._rootScope;
};
Kilid.prototype._keydown = function(e) {
if (this._keysCurrentlyDown.indexOf(e.keyCode) === -1) {
this._keysCurrentlyDown.push(e.keyCode);
}
this._activeScope._keydown(e);
};
Kilid.prototype._keyup = function(e) {
array.pluckOneItem(this._keysCurrentlyDown, e.keyCode);
this._activeScope._keyup(e);
};
Kilid.prototype._focus = function() {
this._activeScope._focus();
};
Kilid.prototype._blur = function() {
this._keysCurrentlyDown.length = 0;
this._activeScope._blur();
};
return Kilid;
})();
// Generated by CoffeeScript 1.6.3
var keyCodeByName, nameByKeyCode;
module.exports.nameByKeyCode = nameByKeyCode = {
0: "\\",
8: "backspace",
9: "tab",
12: "num",
13: "enter",
16: "shift",
17: "ctrl",
18: "alt",
19: "pause",
20: "caps",
27: "esc",
32: "space",
33: "pageup",
34: "pagedown",
35: "end",
36: "home",
37: "left",
38: "up",
39: "right",
40: "down",
44: "print",
45: "insert",
46: "delete",
48: "0",
49: "1",
50: "2",
51: "3",
52: "4",
53: "5",
54: "6",
55: "7",
56: "8",
57: "9",
65: "a",
66: "b",
67: "c",
68: "d",
69: "e",
70: "f",
71: "g",
72: "h",
73: "i",
74: "j",
75: "k",
76: "l",
77: "m",
78: "n",
79: "o",
80: "p",
81: "q",
82: "r",
83: "s",
84: "t",
85: "u",
86: "v",
87: "w",
88: "x",
89: "y",
90: "z",
96: "num_0",
97: "num_1",
98: "num_2",
99: "num_3",
100: "num_4",
101: "num_5",
102: "num_6",
103: "num_7",
104: "num_8",
105: "num_9",
106: "num_multiply",
107: "num_add",
108: "num_enter",
109: "num_subtract",
110: "num_decimal",
111: "num_divide",
124: "print",
144: "num",
145: "scroll",
186: ";",
59: ";",
187: "=",
61: "=",
188: ",",
44: ',',
189: "-",
45: "-",
190: ".",
46: ".",
191: "/",
192: "`",
96: "`",
219: "[",
220: "\\",
221: "]",
222: "\'",
39: "\'",
91: "cmd"
};
module.exports.keyCodeByName = keyCodeByName = {
"\\": 0,
"backspace": 8,
"tab": 9,
"num": 12,
"enter": 13,
"shift": 16,
"ctrl": 17,
"alt": 18,
"pause": 19,
"caps": 20,
"esc": 27,
"space": 32,
"pageup": 33,
"pagedown": 34,
"end": 35,
"home": 36,
"left": 37,
"up": 38,
"right": 39,
"down": 40,
"print": 44,
"insert": 45,
"delete": 46,
"0": 48,
"1": 49,
"2": 50,
"3": 51,
"4": 52,
"5": 53,
"6": 54,
"7": 55,
"8": 56,
"9": 57,
"a": 65,
"b": 66,
"c": 67,
"d": 68,
"e": 69,
"f": 70,
"g": 71,
"h": 72,
"i": 73,
"j": 74,
"k": 75,
"l": 76,
"m": 77,
"n": 78,
"o": 79,
"p": 80,
"q": 81,
"r": 82,
"s": 83,
"t": 84,
"u": 85,
"v": 86,
"w": 87,
"x": 88,
"y": 89,
"z": 90,
"num_0": 96,
"num_1": 97,
"num_2": 98,
"num_3": 99,
"num_4": 100,
"num_5": 101,
"num_6": 102,
"num_7": 103,
"num_8": 104,
"num_9": 105,
"num_multiply": 106,
"num_add": 107,
"num_enter": 108,
"num_subtract": 109,
"num_decimal": 110,
"num_divide": 111,
"print": 124,
"num": 144,
"scroll": 145,
";": [186, 59],
"=": [187, 61],
",": [188, 44],
"-": [189, 45],
".": [190, 46],
"/": [191],
"`": [192, 96],
"[": [219],
"\\": [220],
"]": [221],
"\'": [222, 39],
"cmd": 91,
"super": [91, 17]
};
// Generated by CoffeeScript 1.6.3
var ComboListener, NoKeyListener, Scope, array, map,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
__slice = [].slice;
ComboListener = require('./scope/ComboListener');
NoKeyListener = require('./scope/NoKeyListener');
array = require('utila/lib/array');
map = require('./map');
module.exports = Scope = (function() {
var self;
function Scope(parent, _kilid, name) {
this.parent = parent;
this._kilid = _kilid;
this.name = name;
this.id = this.parent != null ? this.parent.id + '-' + this.name : this.name;
this._bubbles = false;
this._listeners = [];
this._isActive = false;
this._children = {};
this._scrollingDisabled = false;
}
Scope.prototype._keydown = function(e) {
var listener, _i, _len, _ref, _ref1;
if (this._scrollingDisabled && (_ref = e.keyCode, __indexOf.call(self.scrollKeyCodes, _ref) >= 0)) {
e.preventDefault();
}
_ref1 = this._listeners;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
listener = _ref1[_i];
listener._handleKeydown(e);
}
if (this._bubbles && (this.parent != null)) {
this.parent._keydown(e);
}
};
Scope.prototype._keyup = function(e) {
var listener, _i, _len, _ref;
_ref = this._listeners;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
listener = _ref[_i];
listener._handleKeyup(e);
}
if (this._bubbles && (this.parent != null)) {
this.parent._keyup(e);
}
};
Scope.prototype.bubble = function() {
this._bubbles = true;
return this;
};
Scope.prototype.dontBubble = function() {
this._bubbles = false;
return this;
};
Scope.prototype.on = function() {
var ar, args, combo;
combo = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
ar = this._comboToArray(combo);
if (!ar) {
combo = new NoKeyListener(this, args);
} else {
combo = new ComboListener(this, ar, args);
}
this._listeners.push(combo);
return combo;
};
Scope.prototype._comboToArray = function(combo) {
var x,
_this = this;
if (typeof combo !== 'string') {
throw Error("Combo must be a string");
}
combo = combo.trim().replace(/\s+/, ' ');
if (combo.length === 0) {
return false;
}
x = combo.split(/\s*\+\s*/).map(function(name) {
var code;
return code = map.keyCodeByName[name];
});
return x;
};
Scope.prototype._detachListener = function(listener) {
array.pluckOneItem(this._listeners, listener);
};
Scope.prototype.getScope = function(name) {
if (this._children[name] == null) {
this._children[name] = new Scope(this, this._kilid, name);
}
return this._children[name];
};
Scope.prototype.getTempScope = function() {
return new Scope(this, this._kilid, 'null');
};
Scope.prototype.activate = function() {
var listener, _i, _len, _ref;
if (this._isActive) {
return;
}
this._kilid._setActiveScope(this);
this._isActive = true;
_ref = this._listeners;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
listener = _ref[_i];
listener._recheck();
}
return this;
};
Scope.prototype.deactivate = function() {
if (!this._isActive) {
return;
}
if (this.parent == null) {
throw Error("Cannot deactivate the root scope");
}
this.parent.activate();
return this;
};
Scope.prototype.remove = function() {
if (this._isActive) {
this.deactivate();
}
this.parent._removeChild(this);
};
Scope.prototype._removeChild = function(child) {
array.pluckOneItem(this._children, child);
};
Scope.prototype._notActiveAnymore = function() {
var listener, _i, _len, _ref;
if (!this._isActive) {
throw Error("Called _notActiveAnymore() when scope is not active");
}
this._isActive = false;
_ref = this._listeners;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
listener = _ref[_i];
listener._forceEnd();
}
};
Scope.prototype._blur = function() {
var listener, _i, _len, _ref;
if (!this._isActive) {
throw Error("Called _blur when scope is not active");
}
_ref = this._listeners;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
listener = _ref[_i];
listener._forceEnd();
}
};
Scope.prototype._focus = function() {
var listener, _i, _len, _ref;
if (!this._isActive) {
throw Error("Called _focus when scope is not active");
}
_ref = this._listeners;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
listener = _ref[_i];
listener._recheck();
}
};
Scope.prototype.disableScrolling = function() {
return this._scrollingDisabled = true;
};
Scope.prototype.enableScrolling = function() {
return this._scrollingDisabled = false;
};
Scope.scrollKeyCodes = [32, 33, 34, 35, 36, 37, 38, 39, 40];
self = Scope;
return Scope;
})();
// Generated by CoffeeScript 1.6.3
var _Listener;
module.exports = _Listener = (function() {
function _Listener(_scope) {
var _this = this;
this._scope = _scope;
this._kilid = this._scope._kilid;
this._endCallback = null;
this._startCallback = null;
this._locked = false;
setTimeout(function() {
return _this._locked = true;
}, 0);
}
_Listener.prototype.detach = function() {
this._scope._detachListener(this);
this._scope = null;
this._kilid = null;
};
_Listener.prototype.onEnd = function(cb) {
this._endCallback = cb;
return this;
};
_Listener.prototype.onStart = function(cb) {
this._startCallback = cb;
return this;
};
return _Listener;
})();
// Generated by CoffeeScript 1.6.3
var ComboListener, _Listener,
__hasProp = {}.hasOwnProperty,
__extends = 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; },
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
_Listener = require('./_Listener');
module.exports = ComboListener = (function(_super) {
__extends(ComboListener, _super);
function ComboListener(scope, _combo, args) {
this._combo = _combo;
ComboListener.__super__.constructor.apply(this, arguments);
this._isExclusive = true;
this._hitCallback = null;
this._preventDefault = true;
if (typeof args[0] === 'function') {
this.onHit(args[0]);
}
}
ComboListener.prototype.preventDefault = function() {
this._preventDefault = true;
return this;
};
ComboListener.prototype.allowDefault = function() {
this._preventDefault = false;
return this;
};
ComboListener.prototype.beInclusive = function() {
if (this._locked) {
throw Error("You can only modify combo listeners on the same tick they were introduced");
}
this._isExclusive = false;
return this;
};
ComboListener.prototype.beExclusive = function() {
if (this._locked) {
throw Error("You can only modify combo listeners on the same tick they were introduced");
}
this._isExclusive = true;
return this;
};
ComboListener.prototype._comboIsDown = function() {
var keyCode, x1, x2, _i, _len, _ref, _ref1, _ref2;
_ref = this._combo;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
keyCode = _ref[_i];
if (keyCode.length != null) {
x1 = (_ref1 = keyCode[0], __indexOf.call(this._kilid._keysCurrentlyDown, _ref1) >= 0);
x2 = (_ref2 = keyCode[1], __indexOf.call(this._kilid._keysCurrentlyDown, _ref2) >= 0);
return x1 || x2;
}
if (__indexOf.call(this._kilid._keysCurrentlyDown, keyCode) < 0) {
return false;
}
}
return true;
};
ComboListener.prototype._comboIsExclusivelyDown = function() {
if (!this._comboIsDown()) {
return false;
}
return this._combo.length === this._kilid._keysCurrentlyDown.length;
};
ComboListener.prototype._handleKeyup = function(e) {
if (this._isExclusive) {
this._handleKeyupForExclusive(e);
} else {
this._handleKeyupForInclusive(e);
}
};
ComboListener.prototype._handleKeydown = function(e) {
if (this._isExclusive) {
this._handleKeydownForExclusive(e);
} else {
this._handleKeydownForInclusive(e);
}
};
ComboListener.prototype._handleKeydownForInclusive = function(e) {
if (this._wasDown) {
this._fireHit(e);
return;
}
if (this._comboIsDown()) {
this._fireHit(e);
this._fireStart(e);
}
};
ComboListener.prototype._handleKeyupForInclusive = function(e) {
if (!this._wasDown) {
return;
}
if (!this._comboIsDown()) {
this._fireEnd(e);
}
};
ComboListener.prototype._handleKeydownForExclusive = function(e) {
if (this._wasDown) {
if (this._comboIsExclusivelyDown()) {
this._fireHit(e);
} else {
this._fireEnd(e);
}
} else {
if (!this._comboIsExclusivelyDown()) {
return;
}
this._fireHit(e);
this._fireStart(e);
}
};
ComboListener.prototype._handleKeyupForExclusive = function(e) {
if (this._wasDown) {
this._fireEnd(e);
} else {
if (!this._comboIsExclusivelyDown()) {
return;
}
this._fireHit(e);
this._fireStart(e);
}
};
ComboListener.prototype._fireHit = function(e) {
if ((e != null) && this._preventDefault) {
e.preventDefault();
}
if (this._hitCallback != null) {
this._hitCallback(e);
}
};
ComboListener.prototype._fireEnd = function(e) {
if ((e != null) && this._preventDefault) {
e.preventDefault();
}
this._wasDown = false;
if (this._endCallback != null) {
this._endCallback(e);
}
};
ComboListener.prototype._fireStart = function(e) {
this._wasDown = true;
if (this._startCallback != null) {
this._startCallback(e);
}
};
ComboListener.prototype.onHit = function(cb) {
this._hitCallback = cb;
return this;
};
ComboListener.prototype.detach = function() {
if (this._wasDown) {
this._fireEnd();
}
ComboListener.__super__.detach.apply(this, arguments);
};
ComboListener.prototype._forceEnd = function() {
if (!this._wasDown) {
return;
}
this._fireEnd();
};
ComboListener.prototype._recheck = function() {
if (this._wasDown) {
console.warn('_recheck ws called when ComboListener was already down');
return;
}
this._handleKeydown();
};
return ComboListener;
})(_Listener);
// Generated by CoffeeScript 1.6.3
var NoKeyListener, _Listener,
__hasProp = {}.hasOwnProperty,
__extends = 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; };
_Listener = require('./_Listener');
module.exports = NoKeyListener = (function(_super) {
__extends(NoKeyListener, _super);
function NoKeyListener(scope) {
var _this = this;
NoKeyListener.__super__.constructor.apply(this, arguments);
this._wasStarted = false;
setTimeout(function() {
if (!_this._wasStarted) {
return _this._recheck();
}
}, 0);
}
NoKeyListener.prototype._recheck = function() {
if (this._wasStarted) {
return;
}
if (!((this._kilid != null) && this._kilid._keysCurrentlyDown.length === 0)) {
return;
}
this._wasStarted = true;
if (this._startCallback != null) {
this._startCallback();
}
};
NoKeyListener.prototype._handleKeydown = function(e) {
if (!this._wasStarted) {
return;
}
this._wasStarted = false;
if (this._endCallback != null) {
this._endCallback();
}
};
NoKeyListener.prototype._handleKeyup = function(e) {
if (this._wasStarted || (this._kilid == null)) {
return;
}
if (this._kilid._keysCurrentlyDown.length !== 0) {
return;
}
this._wasStarted = true;
if (this._startCallback != null) {
this._startCallback();
}
};
NoKeyListener.prototype.detach = function() {
if (this._wasStarted && (this._endCallback != null)) {
this._endCallback();
}
NoKeyListener.__super__.detach.apply(this, arguments);
};
NoKeyListener.prototype._forceEnd = function() {
if (!this._wasStarted) {
return;
}
this._wasStarted = false;
if (this._endCallback != null) {
this._endCallback();
}
};
return NoKeyListener;
})(_Listener);
+5
-6
{
"name": "kilid",
"version": "0.1.0-beta5",
"version": "0.2.0",
"description": "(work in progress)",
"main": "scripts/js/lib/Kilid.js",
"main": "lib/Kilid.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"prepublish": "node ./node_modules/coffee-script/bin/cake build"
"prepublish": "coffee --bare --compile --output ./lib ./src"
},

@@ -22,7 +21,7 @@ "repository": {

"dependencies": {
"utila": "~0.3.0-beta1"
"utila": "~0.4.0"
},
"devDependencies": {
"coffee-script": "~1.6.3"
"coffee-script": "~1.7.0"
}
}

Sorry, the diff of this file is not supported yet

Scope = require './kilid/Scope'
array = require 'utila/scripts/js/lib/array'
module.exports = class Kilid
constructor: (@rootNode = document, @id = 'kilid') ->
@rootNode.addEventListener 'keydown', @_keydown
@rootNode.addEventListener 'keyup', @_keyup
window.addEventListener 'focus', @_focus
window.addEventListener 'blur', @_blur
@_keysCurrentlyDown = []
@_rootScope = new Scope null, @, @id
@_activeScope = null
@_rootScope.activate()
_setActiveScope: (scope) ->
if @_activeScope?
@_activeScope._notActiveAnymore()
@_activeScope = scope
return
getRootScope: ->
@_rootScope
_keydown: (e) =>
# console.log 'down', @_keysCurrentlyDown
if @_keysCurrentlyDown.indexOf(e.keyCode) is -1
@_keysCurrentlyDown.push e.keyCode
@_activeScope._keydown e
return
_keyup: (e) =>
# console.log 'up', @_keysCurrentlyDown
array.pluckOneItem @_keysCurrentlyDown, e.keyCode
@_activeScope._keyup e
return
_focus: =>
@_activeScope._focus()
return
_blur: =>
@_keysCurrentlyDown.length = 0
@_activeScope._blur()
return
# Mappings from keypress: https://github.com/dmauro/Keypress.git
# Copyright 2013 David Mauro
module.exports.nameByKeyCode = nameByKeyCode =
0 : "\\" # Firefox reports this keyCode when shift is held
8 : "backspace"
9 : "tab"
12 : "num"
13 : "enter"
16 : "shift"
17 : "ctrl"
18 : "alt"
19 : "pause"
20 : "caps"
27 : "esc"
32 : "space"
33 : "pageup"
34 : "pagedown"
35 : "end"
36 : "home"
37 : "left"
38 : "up"
39 : "right"
40 : "down"
44 : "print"
45 : "insert"
46 : "delete"
48 : "0"
49 : "1"
50 : "2"
51 : "3"
52 : "4"
53 : "5"
54 : "6"
55 : "7"
56 : "8"
57 : "9"
65 : "a"
66 : "b"
67 : "c"
68 : "d"
69 : "e"
70 : "f"
71 : "g"
72 : "h"
73 : "i"
74 : "j"
75 : "k"
76 : "l"
77 : "m"
78 : "n"
79 : "o"
80 : "p"
81 : "q"
82 : "r"
83 : "s"
84 : "t"
85 : "u"
86 : "v"
87 : "w"
88 : "x"
89 : "y"
90 : "z"
# 91 : "cmd"
# 92 : "cmd"
# 93 : "cmd"
96 : "num_0"
97 : "num_1"
98 : "num_2"
99 : "num_3"
100 : "num_4"
101 : "num_5"
102 : "num_6"
103 : "num_7"
104 : "num_8"
105 : "num_9"
106 : "num_multiply"
107 : "num_add"
108 : "num_enter"
109 : "num_subtract"
110 : "num_decimal"
111 : "num_divide"
124 : "print"
144 : "num"
145 : "scroll"
186 : ";"
187 : "="
188 : ","
189 : "-"
190 : "."
191 : "/"
192 : "`"
219 : "["
220 : "\\"
221 : "]"
222 : "\'"
223 : "`"
# 224 : "cmd"
# 225 : "alt"
module.exports.keyCodeByName = keyCodeByName = {}
for keyCode, name of nameByKeyCode
keyCodeByName[name] = keyCode
ComboListener = require './scope/ComboListener'
NoKeyListener = require './scope/NoKeyListener'
array = require 'utila/scripts/js/lib/array'
map = require './map'
module.exports = class Scope
constructor: (@parent, @_kilid, @name) ->
@id = if @parent? then @parent.id + '-' + @name else @name
@_bubbles = no
@_listeners = []
@_isActive = no
@_children = {}
_keydown: (e) ->
for listener in @_listeners
listener._handleKeydown e
if @_bubbles and @parent?
@parent._keydown e
return
_keyup: (e) ->
for listener in @_listeners
listener._handleKeyup e
if @_bubbles and @parent?
@parent._keyup e
return
bubble: ->
@_bubbles = yes
@
dontBubble: ->
@_bubbles = no
@
on: (combo, args...) ->
ar = @_comboToArray combo
unless ar
combo = new NoKeyListener @, args
else
combo = new ComboListener @, ar, args
@_listeners.push combo
combo
_comboToArray: (combo) ->
unless typeof combo is 'string'
throw Error "Combo must be a string"
combo = combo.trim().replace /\s+/, ' '
return false if combo.length is 0
combo.split(/\s*\+\s*/)
.map (name) => map.keyCodeByName[name]|0
_detachListener: (listener) ->
array.pluckOneItem @_listeners, listener
return
getScope: (name) ->
unless @_children[name]?
@_children[name] = new Scope @, @_kilid, name
@_children[name]
getTempScope: ->
new Scope @, @_kilid, 'null'
activate: ->
if @_isActive
throw Error "Scope is already active"
@_kilid._setActiveScope @
@_isActive = yes
for listener in @_listeners
listener._recheck()
@
deactivate: ->
unless @_isActive
throw Error "Scope is not active to be deactivated"
unless @parent?
throw Error "Cannot deactivate the root scope"
@parent.activate()
@
remove: ->
do @deactivate if @_isActive
@parent._removeChild @
return
_removeChild: (child) ->
array.pluckOneItem @_children, child
return
_notActiveAnymore: ->
unless @_isActive
throw Error "Called _notActiveAnymore() when scope is not active"
@_isActive = no
for listener in @_listeners
listener._forceEnd()
return
_blur: ->
unless @_isActive
throw Error "Called _blur when scope is not active"
for listener in @_listeners
listener._forceEnd()
return
_focus: ->
unless @_isActive
throw Error "Called _focus when scope is not active"
for listener in @_listeners
listener._recheck()
return
module.exports = class _Listener
constructor: (@_scope) ->
@_kilid = @_scope._kilid
@_endCallback = null
@_startCallback = null
@_locked = no
setTimeout =>
@_locked = yes
, 0
detach: ->
@_scope._detachListener @
@_scope = null
@_kilid = null
return
onEnd: (cb) ->
@_endCallback = cb
@
onStart: (cb) ->
@_startCallback = cb
@
_Listener = require './_Listener'
module.exports = class ComboListener extends _Listener
constructor: (scope, @_combo, args) ->
super
@_isExclusive = yes
@_hitCallback = null
@_preventDefault = yes
if typeof args[0] is 'function'
@onHit args[0]
preventDefault: ->
@_preventDefault = yes
@
allowDefault: ->
@_preventDefault = no
@
beInclusive: ->
if @_locked
throw Error "You can only modify combo listeners on the same tick they were introduced"
@_isExclusive = no
@
beExclusive: ->
if @_locked
throw Error "You can only modify combo listeners on the same tick they were introduced"
@_isExclusive = yes
@
_comboIsDown: ->
for keyCode in @_combo
return no unless keyCode in @_kilid._keysCurrentlyDown
yes
_comboIsExclusivelyDown: ->
return no unless @_comboIsDown()
@_combo.length is @_kilid._keysCurrentlyDown.length
_handleKeyup: (e) ->
if @_isExclusive
@_handleKeyupForExclusive e
else
@_handleKeyupForInclusive e
return
_handleKeydown: (e) ->
if @_isExclusive
@_handleKeydownForExclusive e
else
@_handleKeydownForInclusive e
return
_handleKeydownForInclusive: (e) ->
if @_wasDown
@_fireHit e
return
if @_comboIsDown()
@_fireHit e
@_fireStart e
return
_handleKeyupForInclusive: (e) ->
return unless @_wasDown
unless @_comboIsDown()
@_fireEnd e
return
_handleKeydownForExclusive: (e) ->
if @_wasDown
if @_comboIsExclusivelyDown()
@_fireHit e
else
@_fireEnd e
else
return unless @_comboIsExclusivelyDown()
@_fireHit e
@_fireStart e
return
_handleKeyupForExclusive: (e) ->
if @_wasDown
@_fireEnd e
else
return unless @_comboIsExclusivelyDown()
@_fireHit e
@_fireStart e
return
_fireHit: (e) ->
if e? and @_preventDefault then e.preventDefault()
if @_hitCallback?
@_hitCallback e
return
_fireEnd: (e) ->
if e? and @_preventDefault then e.preventDefault()
@_wasDown = no
if @_endCallback?
@_endCallback e
return
_fireStart: (e) ->
@_wasDown = yes
if @_startCallback?
@_startCallback e
return
onHit: (cb) ->
@_hitCallback = cb
@
detach: ->
if @_wasDown then @_fireEnd()
super
return
_forceEnd: ->
return unless @_wasDown
do @_fireEnd
return
_recheck: ->
if @_wasDown
console.warn '_recheck ws called when ComboListener was already down'
return
do @_handleKeydown
return
_Listener = require './_Listener'
module.exports = class NoKeyListener extends _Listener
constructor: (scope) ->
super
@_wasStarted = no
setTimeout =>
unless @_wasStarted then do @_recheck
, 0
_recheck: ->
if @_wasStarted
console.log '_recheck ws called when NoKeyListener was already started'
return
return unless @_kilid._keysCurrentlyDown.length is 0
@_wasStarted = yes
if @_startCallback?
do @_startCallback
return
_handleKeydown: (e) ->
return unless @_wasStarted
@_wasStarted = no
if @_endCallback?
do @_endCallback
return
_handleKeyup: (e) ->
if @_wasStarted
# console.log 'NoKeyListener is already in "started" mode but has recieved a keyup event'
return
return unless @_kilid._keysCurrentlyDown.length is 0
@_wasStarted = yes
if @_startCallback?
do @_startCallback
return
detach: ->
if @_wasStarted and @_endCallback?
do @_endCallback
super
return
_forceEnd: ->
return unless @_wasStarted
@_wasStarted = no
if @_endCallback?
do @_endCallback
return
var Kilid, Scope, array,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Scope = require('./kilid/Scope');
array = require('utila/scripts/js/lib/array');
module.exports = Kilid = (function() {
function Kilid(rootNode, id) {
this.rootNode = rootNode != null ? rootNode : document;
this.id = id != null ? id : 'kilid';
this._blur = __bind(this._blur, this);
this._focus = __bind(this._focus, this);
this._keyup = __bind(this._keyup, this);
this._keydown = __bind(this._keydown, this);
this.rootNode.addEventListener('keydown', this._keydown);
this.rootNode.addEventListener('keyup', this._keyup);
window.addEventListener('focus', this._focus);
window.addEventListener('blur', this._blur);
this._keysCurrentlyDown = [];
this._rootScope = new Scope(null, this, this.id);
this._activeScope = null;
this._rootScope.activate();
}
Kilid.prototype._setActiveScope = function(scope) {
if (this._activeScope != null) {
this._activeScope._notActiveAnymore();
}
this._activeScope = scope;
};
Kilid.prototype.getRootScope = function() {
return this._rootScope;
};
Kilid.prototype._keydown = function(e) {
if (this._keysCurrentlyDown.indexOf(e.keyCode) === -1) {
this._keysCurrentlyDown.push(e.keyCode);
}
this._activeScope._keydown(e);
};
Kilid.prototype._keyup = function(e) {
array.pluckOneItem(this._keysCurrentlyDown, e.keyCode);
this._activeScope._keyup(e);
};
Kilid.prototype._focus = function() {
this._activeScope._focus();
};
Kilid.prototype._blur = function() {
this._keysCurrentlyDown.length = 0;
this._activeScope._blur();
};
return Kilid;
})();
var _Listener;
module.exports = _Listener = (function() {
function _Listener() {
this._endCallback = null;
this._startCallback = null;
this._locked = false;
setTimeout((function(_this) {
return function() {
return _this._locked = true;
};
})(this), 0);
}
_Listener.prototype.detach = function() {
this._kilid._detachListener(this);
this._kilid = null;
};
_Listener.prototype.onEnd = function(cb) {
this._endCallback = cb;
return this;
};
_Listener.prototype.onStart = function(cb) {
this._startCallback = cb;
return this;
};
return _Listener;
})();
var ComboListener, _Listener,
__hasProp = {}.hasOwnProperty,
__extends = 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; },
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
_Listener = require('./_Listener');
module.exports = ComboListener = (function(_super) {
__extends(ComboListener, _super);
function ComboListener(_kilid, _combo) {
this._kilid = _kilid;
this._combo = _combo;
ComboListener.__super__.constructor.apply(this, arguments);
this._isExclusive = true;
this._hitCallback = null;
}
ComboListener.prototype.beInclusive = function() {
if (this._locked) {
throw Error("You can only modify combo listeners on the same tick they were introduced");
}
this._isExclusive = false;
return this;
};
ComboListener.prototype.beExclusive = function() {
if (this._locked) {
throw Error("You can only modify combo listeners on the same tick they were introduced");
}
this._isExclusive = true;
return this;
};
ComboListener.prototype._comboIsDown = function() {
var keyCode, _i, _len, _ref;
_ref = this._combo;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
keyCode = _ref[_i];
if (__indexOf.call(this._kilid._keysCurrentlyDown, keyCode) < 0) {
return false;
}
}
return true;
};
ComboListener.prototype._comboIsExclusivelyDown = function() {
if (!this._comboIsDown()) {
return false;
}
return this._combo.length === this._kilid._keysCurrentlyDown.length;
};
ComboListener.prototype._handleKeyup = function(e) {
if (this._isExclusive) {
this._handleKeyupForExclusive(e);
} else {
this._handleKeyupForInclusive(e);
}
};
ComboListener.prototype._handleKeydown = function(e) {
if (this._isExclusive) {
this._handleKeydownForExclusive(e);
} else {
this._handleKeydownForInclusive(e);
}
};
ComboListener.prototype._handleKeydownForInclusive = function(e) {
if (this._wasDown) {
this._fireHit(e);
return;
}
if (this._comboIsDown()) {
this._fireHit(e);
this._fireStart(e);
}
};
ComboListener.prototype._handleKeyupForInclusive = function(e) {
if (!this._wasDown) {
return;
}
if (!this._comboIsDown()) {
this._fireEnd(e);
}
};
ComboListener.prototype._handleKeydownForExclusive = function(e) {
if (this._wasDown) {
if (this._comboIsExclusivelyDown()) {
this._fireHit(e);
} else {
this._fireEnd(e);
}
} else {
if (!this._comboIsExclusivelyDown()) {
return;
}
this._fireHit(e);
this._fireStart(e);
}
};
ComboListener.prototype._handleKeyupForExclusive = function(e) {
if (this._wasDown) {
this._fireEnd(e);
} else {
if (!this._comboIsExclusivelyDown()) {
return;
}
this._fireHit(e);
this._fireStart(e);
}
};
ComboListener.prototype._fireHit = function(e) {
if (this._hitCallback != null) {
this._hitCallback(e);
}
};
ComboListener.prototype._fireEnd = function(e) {
this._wasDown = false;
if (this._endCallback != null) {
this._endCallback(e);
}
};
ComboListener.prototype._fireStart = function(e) {
this._wasDown = true;
if (this._startCallback != null) {
this._startCallback(e);
}
};
ComboListener.prototype.onHit = function(cb) {
this._hitCallback = cb;
return this;
};
return ComboListener;
})(_Listener);
var keyCode, keyCodeByName, name, nameByKeyCode;
module.exports.nameByKeyCode = nameByKeyCode = {
0: "\\",
8: "backspace",
9: "tab",
12: "num",
13: "enter",
16: "shift",
17: "ctrl",
18: "alt",
19: "pause",
20: "caps",
27: "esc",
32: "space",
33: "pageup",
34: "pagedown",
35: "end",
36: "home",
37: "left",
38: "up",
39: "right",
40: "down",
44: "print",
45: "insert",
46: "delete",
48: "0",
49: "1",
50: "2",
51: "3",
52: "4",
53: "5",
54: "6",
55: "7",
56: "8",
57: "9",
65: "a",
66: "b",
67: "c",
68: "d",
69: "e",
70: "f",
71: "g",
72: "h",
73: "i",
74: "j",
75: "k",
76: "l",
77: "m",
78: "n",
79: "o",
80: "p",
81: "q",
82: "r",
83: "s",
84: "t",
85: "u",
86: "v",
87: "w",
88: "x",
89: "y",
90: "z",
96: "num_0",
97: "num_1",
98: "num_2",
99: "num_3",
100: "num_4",
101: "num_5",
102: "num_6",
103: "num_7",
104: "num_8",
105: "num_9",
106: "num_multiply",
107: "num_add",
108: "num_enter",
109: "num_subtract",
110: "num_decimal",
111: "num_divide",
124: "print",
144: "num",
145: "scroll",
186: ";",
187: "=",
188: ",",
189: "-",
190: ".",
191: "/",
192: "`",
219: "[",
220: "\\",
221: "]",
222: "\'",
223: "`"
};
module.exports.keyCodeByName = keyCodeByName = {};
for (keyCode in nameByKeyCode) {
name = nameByKeyCode[keyCode];
keyCodeByName[name] = keyCode;
}
var NoKeyListener, _Listener,
__hasProp = {}.hasOwnProperty,
__extends = 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; };
_Listener = require('./_Listener');
module.exports = NoKeyListener = (function(_super) {
__extends(NoKeyListener, _super);
function NoKeyListener(_kilid) {
this._kilid = _kilid;
this._wasStarted = false;
}
NoKeyListener.prototype._handleKeydown = function(e) {
if (!this._wasStarted) {
return;
}
this._wasStarted = false;
if (this._endCallback != null) {
this._endCallback();
}
};
NoKeyListener.prototype._handleKeyup = function(e) {
if (this._wasStarted) {
console.log('NoKeyListener is already in "started" mode but has recieved a keyup event');
return;
}
if (this._kilid._keysCurrentlyDown.length !== 0) {
return;
}
this._wasStarted = true;
if (this._startCallback != null) {
this._startCallback();
}
};
return NoKeyListener;
})(_Listener);
var ComboListener, NoKeyListener, Scope, array, map,
__slice = [].slice;
ComboListener = require('./scope/ComboListener');
NoKeyListener = require('./scope/NoKeyListener');
array = require('utila/scripts/js/lib/array');
map = require('./map');
module.exports = Scope = (function() {
function Scope(parent, _kilid, name) {
this.parent = parent;
this._kilid = _kilid;
this.name = name;
this.id = this.parent != null ? this.parent.id + '-' + this.name : this.name;
this._bubbles = false;
this._listeners = [];
this._isActive = false;
this._children = {};
}
Scope.prototype._keydown = function(e) {
var listener, _i, _len, _ref;
_ref = this._listeners;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
listener = _ref[_i];
listener._handleKeydown(e);
}
if (this._bubbles && (this.parent != null)) {
this.parent._keydown(e);
}
};
Scope.prototype._keyup = function(e) {
var listener, _i, _len, _ref;
_ref = this._listeners;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
listener = _ref[_i];
listener._handleKeyup(e);
}
if (this._bubbles && (this.parent != null)) {
this.parent._keyup(e);
}
};
Scope.prototype.bubble = function() {
this._bubbles = true;
return this;
};
Scope.prototype.dontBubble = function() {
this._bubbles = false;
return this;
};
Scope.prototype.on = function() {
var ar, args, combo;
combo = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
ar = this._comboToArray(combo);
if (!ar) {
combo = new NoKeyListener(this, args);
} else {
combo = new ComboListener(this, ar, args);
}
this._listeners.push(combo);
return combo;
};
Scope.prototype._comboToArray = function(combo) {
var _this = this;
if (typeof combo !== 'string') {
throw Error("Combo must be a string");
}
combo = combo.trim().replace(/\s+/, ' ');
if (combo.length === 0) {
return false;
}
return combo.split(/\s*\+\s*/).map(function(name) {
return map.keyCodeByName[name] | 0;
});
};
Scope.prototype._detachListener = function(listener) {
array.pluckOneItem(this._listeners, listener);
};
Scope.prototype.getScope = function(name) {
if (this._children[name] == null) {
this._children[name] = new Scope(this, this._kilid, name);
}
return this._children[name];
};
Scope.prototype.getTempScope = function() {
return new Scope(this, this._kilid, 'null');
};
Scope.prototype.activate = function() {
var listener, _i, _len, _ref;
if (this._isActive) {
throw Error("Scope is already active");
}
this._kilid._setActiveScope(this);
this._isActive = true;
_ref = this._listeners;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
listener = _ref[_i];
listener._recheck();
}
return this;
};
Scope.prototype.deactivate = function() {
if (!this._isActive) {
throw Error("Scope is not active to be deactivated");
}
if (this.parent == null) {
throw Error("Cannot deactivate the root scope");
}
this.parent.activate();
return this;
};
Scope.prototype.remove = function() {
if (this._isActive) {
this.deactivate();
}
this.parent._removeChild(this);
};
Scope.prototype._removeChild = function(child) {
array.pluckOneItem(this._children, child);
};
Scope.prototype._notActiveAnymore = function() {
var listener, _i, _len, _ref;
if (!this._isActive) {
throw Error("Called _notActiveAnymore() when scope is not active");
}
this._isActive = false;
_ref = this._listeners;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
listener = _ref[_i];
listener._forceEnd();
}
};
Scope.prototype._blur = function() {
var listener, _i, _len, _ref;
if (!this._isActive) {
throw Error("Called _blur when scope is not active");
}
_ref = this._listeners;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
listener = _ref[_i];
listener._forceEnd();
}
};
Scope.prototype._focus = function() {
var listener, _i, _len, _ref;
if (!this._isActive) {
throw Error("Called _focus when scope is not active");
}
_ref = this._listeners;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
listener = _ref[_i];
listener._recheck();
}
};
return Scope;
})();
var _Listener;
module.exports = _Listener = (function() {
function _Listener(_scope) {
var _this = this;
this._scope = _scope;
this._kilid = this._scope._kilid;
this._endCallback = null;
this._startCallback = null;
this._locked = false;
setTimeout(function() {
return _this._locked = true;
}, 0);
}
_Listener.prototype.detach = function() {
this._scope._detachListener(this);
this._scope = null;
this._kilid = null;
};
_Listener.prototype.onEnd = function(cb) {
this._endCallback = cb;
return this;
};
_Listener.prototype.onStart = function(cb) {
this._startCallback = cb;
return this;
};
return _Listener;
})();
var ComboListener, _Listener,
__hasProp = {}.hasOwnProperty,
__extends = 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; },
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
_Listener = require('./_Listener');
module.exports = ComboListener = (function(_super) {
__extends(ComboListener, _super);
function ComboListener(scope, _combo, args) {
this._combo = _combo;
ComboListener.__super__.constructor.apply(this, arguments);
this._isExclusive = true;
this._hitCallback = null;
this._preventDefault = true;
if (typeof args[0] === 'function') {
this.onHit(args[0]);
}
}
ComboListener.prototype.preventDefault = function() {
this._preventDefault = true;
return this;
};
ComboListener.prototype.allowDefault = function() {
this._preventDefault = false;
return this;
};
ComboListener.prototype.beInclusive = function() {
if (this._locked) {
throw Error("You can only modify combo listeners on the same tick they were introduced");
}
this._isExclusive = false;
return this;
};
ComboListener.prototype.beExclusive = function() {
if (this._locked) {
throw Error("You can only modify combo listeners on the same tick they were introduced");
}
this._isExclusive = true;
return this;
};
ComboListener.prototype._comboIsDown = function() {
var keyCode, _i, _len, _ref;
_ref = this._combo;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
keyCode = _ref[_i];
if (__indexOf.call(this._kilid._keysCurrentlyDown, keyCode) < 0) {
return false;
}
}
return true;
};
ComboListener.prototype._comboIsExclusivelyDown = function() {
if (!this._comboIsDown()) {
return false;
}
return this._combo.length === this._kilid._keysCurrentlyDown.length;
};
ComboListener.prototype._handleKeyup = function(e) {
if (this._isExclusive) {
this._handleKeyupForExclusive(e);
} else {
this._handleKeyupForInclusive(e);
}
};
ComboListener.prototype._handleKeydown = function(e) {
if (this._isExclusive) {
this._handleKeydownForExclusive(e);
} else {
this._handleKeydownForInclusive(e);
}
};
ComboListener.prototype._handleKeydownForInclusive = function(e) {
if (this._wasDown) {
this._fireHit(e);
return;
}
if (this._comboIsDown()) {
this._fireHit(e);
this._fireStart(e);
}
};
ComboListener.prototype._handleKeyupForInclusive = function(e) {
if (!this._wasDown) {
return;
}
if (!this._comboIsDown()) {
this._fireEnd(e);
}
};
ComboListener.prototype._handleKeydownForExclusive = function(e) {
if (this._wasDown) {
if (this._comboIsExclusivelyDown()) {
this._fireHit(e);
} else {
this._fireEnd(e);
}
} else {
if (!this._comboIsExclusivelyDown()) {
return;
}
this._fireHit(e);
this._fireStart(e);
}
};
ComboListener.prototype._handleKeyupForExclusive = function(e) {
if (this._wasDown) {
this._fireEnd(e);
} else {
if (!this._comboIsExclusivelyDown()) {
return;
}
this._fireHit(e);
this._fireStart(e);
}
};
ComboListener.prototype._fireHit = function(e) {
if ((e != null) && this._preventDefault) {
e.preventDefault();
}
if (this._hitCallback != null) {
this._hitCallback(e);
}
};
ComboListener.prototype._fireEnd = function(e) {
if ((e != null) && this._preventDefault) {
e.preventDefault();
}
this._wasDown = false;
if (this._endCallback != null) {
this._endCallback(e);
}
};
ComboListener.prototype._fireStart = function(e) {
this._wasDown = true;
if (this._startCallback != null) {
this._startCallback(e);
}
};
ComboListener.prototype.onHit = function(cb) {
this._hitCallback = cb;
return this;
};
ComboListener.prototype.detach = function() {
if (this._wasDown) {
this._fireEnd();
}
ComboListener.__super__.detach.apply(this, arguments);
};
ComboListener.prototype._forceEnd = function() {
if (!this._wasDown) {
return;
}
this._fireEnd();
};
ComboListener.prototype._recheck = function() {
if (this._wasDown) {
console.warn('_recheck ws called when ComboListener was already down');
return;
}
this._handleKeydown();
};
return ComboListener;
})(_Listener);
var NoKeyListener, _Listener,
__hasProp = {}.hasOwnProperty,
__extends = 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; };
_Listener = require('./_Listener');
module.exports = NoKeyListener = (function(_super) {
__extends(NoKeyListener, _super);
function NoKeyListener(scope) {
var _this = this;
NoKeyListener.__super__.constructor.apply(this, arguments);
this._wasStarted = false;
setTimeout(function() {
if (!_this._wasStarted) {
return _this._recheck();
}
}, 0);
}
NoKeyListener.prototype._recheck = function() {
if (this._wasStarted) {
console.log('_recheck ws called when NoKeyListener was already started');
return;
}
if (this._kilid._keysCurrentlyDown.length !== 0) {
return;
}
this._wasStarted = true;
if (this._startCallback != null) {
this._startCallback();
}
};
NoKeyListener.prototype._handleKeydown = function(e) {
if (!this._wasStarted) {
return;
}
this._wasStarted = false;
if (this._endCallback != null) {
this._endCallback();
}
};
NoKeyListener.prototype._handleKeyup = function(e) {
if (this._wasStarted) {
return;
}
if (this._kilid._keysCurrentlyDown.length !== 0) {
return;
}
this._wasStarted = true;
if (this._startCallback != null) {
this._startCallback();
}
};
NoKeyListener.prototype.detach = function() {
if (this._wasStarted && (this._endCallback != null)) {
this._endCallback();
}
NoKeyListener.__super__.detach.apply(this, arguments);
};
NoKeyListener.prototype._forceEnd = function() {
if (!this._wasStarted) {
return;
}
this._wasStarted = false;
if (this._endCallback != null) {
this._endCallback();
}
};
return NoKeyListener;
})(_Listener);

Sorry, the diff of this file is not supported yet