-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed8c46a
commit 9a09103
Showing
12 changed files
with
169 additions
and
534 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import OnHelper from './on'; | ||
|
||
export default OnHelper.extend({ | ||
compute(positional, named) { | ||
return this._super([document, ...positional], named); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import OnHelper from './on'; | ||
|
||
export default OnHelper.extend({ | ||
compute(positional, named) { | ||
return this._super([window, ...positional], named); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* eslint no-param-reassign: "off" */ | ||
|
||
import Helper from '@ember/component/helper'; | ||
import { addEventListener, removeEventListener } from '../utils/event-listener'; | ||
import { assert } from '@ember/debug'; | ||
import { DEBUG } from '@glimmer/env'; | ||
|
||
/** | ||
* These are private API and only used for testing instrumentation. | ||
*/ | ||
let adds = 0; | ||
let removes = 0; | ||
export function __counts() { | ||
return { adds, removes }; | ||
} | ||
|
||
const assertValidEventOptions = | ||
DEBUG && | ||
(() => { | ||
const ALLOWED_EVENT_OPTIONS = ['capture', 'once', 'passive']; | ||
const joinOptions = opts => opts.map(o => `'${o}'`).join(', '); | ||
|
||
return function(eventOptions, eventName) { | ||
const invalidOptions = Object.keys(eventOptions).filter( | ||
o => !ALLOWED_EVENT_OPTIONS.includes(o) | ||
); | ||
|
||
assert( | ||
`ember-on-helper: Provided invalid event options (${joinOptions( | ||
invalidOptions | ||
)}) to '${eventName}' event listener. Only these options are valid: ${joinOptions( | ||
ALLOWED_EVENT_OPTIONS | ||
)}`, | ||
invalidOptions.length === 0 | ||
); | ||
}; | ||
})(); | ||
|
||
function setupListener(eventTarget, eventName, callback, eventOptions) { | ||
if (DEBUG) assertValidEventOptions(eventOptions, eventName); | ||
assert( | ||
`ember-on-helper: '${eventName}' is not a valid event name. It has to be a string with a minimum length of 1 character.`, | ||
typeof eventName === 'string' && eventName.length > 1 | ||
); | ||
assert( | ||
`ember-on-helper: '${callback}' is not a valid callback. Provide a function.`, | ||
typeof callback === 'function' | ||
); | ||
|
||
adds++; | ||
addEventListener(eventTarget, eventName, callback, eventOptions); | ||
|
||
return callback; | ||
} | ||
|
||
function destroyListener(eventTarget, eventName, callback, eventOptions) { | ||
if (eventTarget && eventName && callback) { | ||
removes++; | ||
removeEventListener(eventTarget, eventName, callback, eventOptions); | ||
} | ||
} | ||
|
||
export default Helper.extend({ | ||
eventTarget: null, | ||
eventName: undefined, | ||
callback: undefined, | ||
eventOptions: undefined, | ||
|
||
compute([eventTarget, eventName, callback], eventOptions) { | ||
destroyListener( | ||
this.eventTarget, | ||
this.eventName, | ||
this.callback, | ||
this.eventOptions | ||
); | ||
|
||
this.eventTarget = eventTarget; | ||
|
||
this.callback = setupListener( | ||
this.eventTarget, | ||
eventName, | ||
callback, | ||
eventOptions | ||
); | ||
|
||
this.eventName = eventName; | ||
this.eventOptions = eventOptions; | ||
}, | ||
|
||
willDestroy() { | ||
this._super(); | ||
|
||
destroyListener( | ||
this.eventTarget, | ||
this.eventName, | ||
this.callback, | ||
this.eventOptions | ||
); | ||
} | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-on-helper/helpers/on-document'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-on-helper/helpers/on-window'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-on-helper/helpers/on'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { render } from '@ember/test-helpers'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
module('Integration | Helper | on-document', function(hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
// Replace this with your real tests. | ||
test('it renders', async function(assert) { | ||
this.set('inputValue', '1234'); | ||
|
||
await render(hbs`{{on-document inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), '1234'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { render } from '@ember/test-helpers'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
module('Integration | Helper | on', function(hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
// Replace this with your real tests. | ||
test('it renders', async function(assert) { | ||
this.set('inputValue', '1234'); | ||
|
||
await render(hbs`{{on inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), '1234'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { render } from '@ember/test-helpers'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
module('Integration | Helper | on-window', function(hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
// Replace this with your real tests. | ||
test('it renders', async function(assert) { | ||
this.set('inputValue', '1234'); | ||
|
||
await render(hbs`{{on-window inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), '1234'); | ||
}); | ||
}); |
Oops, something went wrong.