ember-keyboard
Advanced tools
Comparing version 2.1.11 to 2.2.0
@@ -1,31 +0,37 @@ | ||
import Ember from 'ember'; | ||
import handleKeyEvent from 'ember-keyboard/utils/handle-key-event'; | ||
import $ from 'jquery'; | ||
import Service from '@ember/service'; | ||
import { A } from '@ember/array'; | ||
import { getOwner } from '@ember/application'; | ||
import { get, getProperties, set } from '@ember/object'; | ||
import { computed } from '@ember/object'; | ||
import { filter, filterBy, sort } from '@ember/object/computed'; | ||
import { run } from '@ember/runloop'; | ||
import { keyDown, keyPress, keyUp } from 'ember-keyboard/listeners/key-events'; | ||
import { | ||
handleKeyEventWithPropagation, | ||
handleKeyEventWithLaxPriorities | ||
} from 'ember-keyboard/utils/handle-key-event'; | ||
const { | ||
Service, | ||
computed, | ||
get, | ||
getOwner, | ||
run | ||
} = Ember; | ||
export default Service.extend({ | ||
isPropagationEnabled: false, | ||
const { | ||
filterBy, | ||
sort | ||
} = computed; | ||
registeredResponders: computed(() => A()), | ||
export default Service.extend({ | ||
registeredResponders: computed(() => Ember.A()), | ||
activeResponders: filterBy('registeredResponders', 'keyboardActivated').volatile(), | ||
sortedResponders: sort('activeResponders', function(a, b) { | ||
if (get(a, 'keyboardFirstResponder')) { | ||
return -1; | ||
} else if (get(b, 'keyboardFirstResponder')) { | ||
return 1; | ||
} else { | ||
return get(b, 'keyboardPriority') - get(a, 'keyboardPriority'); | ||
} | ||
}).volatile(), | ||
activeResponders: filterBy('registeredResponders', 'keyboardActivated'), | ||
sortedRespondersSortDefinition: computed('isPropagationEnabled', function() { | ||
return get(this, 'isPropagationEnabled') ? | ||
['keyboardPriority:desc'] : | ||
['keyboardFirstResponder:desc', 'keyboardPriority:desc'] | ||
}), | ||
sortedResponders: sort('activeResponders', 'sortedRespondersSortDefinition'), | ||
firstResponders: filterBy('sortedResponders', 'keyboardFirstResponder'), | ||
normalResponders: filter( | ||
'sortedResponders.@each.keyboardFirstResponder', | ||
responder => !get(responder, 'keyboardFirstResponder') | ||
), | ||
init(...args) { | ||
@@ -39,2 +45,6 @@ this._super(...args); | ||
const config = getOwner(this).resolveRegistration('config:environment') || {}; | ||
const isPropagationEnabled = Boolean(get(config, 'emberKeyboard.propagation')); | ||
set(this, 'isPropagationEnabled', isPropagationEnabled); | ||
const listeners = get(config, 'emberKeyboard.listeners') || ['keyUp', 'keyDown', 'keyPress']; | ||
@@ -45,5 +55,9 @@ const eventNames = listeners.map(function(name) { | ||
Ember.$(document).on(eventNames, null, (event) => { | ||
$(document).on(eventNames, null, (event) => { | ||
run(() => { | ||
handleKeyEvent(event, get(this, 'sortedResponders')); | ||
if (get(this, 'isPropagationEnabled')) { | ||
handleKeyEventWithPropagation(event, getProperties(this, 'firstResponders', 'normalResponders')); | ||
} else { | ||
handleKeyEventWithLaxPriorities(event, get(this, 'sortedResponders')); | ||
} | ||
}); | ||
@@ -60,3 +74,3 @@ }); | ||
Ember.$(document).off('.ember-keyboard-listener'); | ||
$(document).off('.ember-keyboard-listener'); | ||
}, | ||
@@ -63,0 +77,0 @@ |
@@ -1,12 +0,6 @@ | ||
import Ember from 'ember'; | ||
import { get } from '@ember/object'; | ||
import getCode from 'ember-keyboard/utils/get-code'; | ||
import listenerName from 'ember-keyboard/utils/listener-name'; | ||
const { | ||
hasListeners, | ||
get, | ||
getProperties | ||
} = Ember; | ||
const gatherKeys = function gatherKeys(event) { | ||
function gatherKeys(event) { | ||
const key = getCode(event); | ||
@@ -21,5 +15,59 @@ | ||
}, [key]); | ||
}; | ||
} | ||
export default function handleKeyEvent(event, sortedResponders) { | ||
export function handleKeyEventWithPropagation(event, { firstResponders, normalResponders }) { | ||
const keys = gatherKeys(event); | ||
const listenerNames = [listenerName(event.type, keys), listenerName(event.type)]; | ||
let isImmediatePropagationStopped = false; | ||
let isPropagationStopped = false; | ||
const ekEvent = { | ||
stopImmediatePropagation() { | ||
isImmediatePropagationStopped = true; | ||
}, | ||
stopPropagation() { | ||
isPropagationStopped = true; | ||
} | ||
} | ||
for (const responder of firstResponders) { | ||
for (const listenerName of listenerNames) { | ||
responder.trigger(listenerName, event, ekEvent); | ||
} | ||
if (isImmediatePropagationStopped) { | ||
break; | ||
} | ||
} | ||
if (isPropagationStopped) { | ||
return; | ||
} | ||
isImmediatePropagationStopped = false; | ||
let previousPriorityLevel = Number.POSITIVE_INFINITY; | ||
for (const responder of normalResponders) { | ||
const currentPriorityLevel = Number(get(responder, 'keyboardPriority')); | ||
if (isImmediatePropagationStopped && currentPriorityLevel === previousPriorityLevel) { | ||
continue; | ||
} | ||
if (currentPriorityLevel < previousPriorityLevel) { | ||
if (isPropagationStopped) { | ||
return; | ||
} | ||
isImmediatePropagationStopped = false; | ||
previousPriorityLevel = currentPriorityLevel; | ||
} | ||
for (const listenerName of listenerNames) { | ||
responder.trigger(listenerName, event, ekEvent); | ||
} | ||
} | ||
} | ||
export function handleKeyEventWithLaxPriorities(event, sortedResponders) { | ||
let currentPriorityLevel; | ||
@@ -33,3 +81,5 @@ let noFirstResponders = true; | ||
sortedResponders.every((responder) => { | ||
const { keyboardFirstResponder, keyboardPriority } = getProperties(responder, 'keyboardFirstResponder', 'keyboardPriority'); | ||
const keyboardFirstResponder = get(responder, 'keyboardFirstResponder'); | ||
const keyboardPriority = get(responder, 'keyboardPriority'); | ||
if (keyboardFirstResponder || (noFirstResponders && keyboardPriority >= currentPriorityLevel) || isLax) { | ||
@@ -47,3 +97,3 @@ if (!get(responder, 'keyboardLaxPriority')) { | ||
listenerNames.forEach((triggerName) => { | ||
if (hasListeners(responder, triggerName)) { | ||
if (responder.has(triggerName)) { | ||
responder.trigger(triggerName, event); | ||
@@ -50,0 +100,0 @@ } |
{ | ||
"name": "ember-keyboard", | ||
"version": "2.1.11", | ||
"version": "2.2.0", | ||
"description": "An Ember.js addon for the painless support of keyboard events", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
586
22364
37