Skip to content

Commit 5d60375

Browse files
authored
Merge pull request #152 from zachnthebox/zach-screen
feat(service): add screen height and screen width properties
2 parents acff9d0 + 1d55722 commit 5d60375

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

addon/mixins/resize-aware.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import Mixin from '@ember/object/mixin';
2+
import { readOnly } from '@ember/object/computed';
23
const { floor } = Math;
34

45
export default Mixin.create({
56
resizeEventsEnabled: true,
67
resizeDebouncedEventsEnabled: true,
78

9+
screenWidth: readOnly('resizeService.screenWidth'),
10+
screenHeight: readOnly('resizeService.screenHeight'),
11+
812
_oldViewWidth: null,
913
_oldViewHeight: null,
1014
_oldViewWidthDebounced: null,

addon/services/resize.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { keys as emberKeys } from '@ember/polyfills';
22
import Service from '@ember/service';
33
import Evented from '@ember/object/evented';
44
import { classify } from '@ember/string';
5-
import { oneWay } from '@ember/object/computed';
5+
import { oneWay, readOnly } from '@ember/object/computed';
66
import { debounce } from '@ember/runloop';
77
import EmberObject, { set, getWithDefault } from '@ember/object';
88

@@ -11,15 +11,18 @@ const Base = Service || EmberObject;
1111
const keys = Object.keys || emberKeys;
1212

1313
export default Base.extend(Evented, {
14-
_oldWidth: null,
15-
_oldHeight: null,
16-
_oldWidthDebounced: null,
17-
_oldHeightDebounced: null,
14+
_oldWidth: window.innerWidth,
15+
_oldHeight: window.innerHeight,
16+
_oldWidthDebounced: window.innerWidth,
17+
_oldHeightDebounced: window.innerHeight,
1818

1919
debounceTimeout: oneWay('defaultDebounceTimeout'),
2020
widthSensitive: oneWay('defaultWidthSensitive'),
2121
heightSensitive: oneWay('defaultHeightSensitive'),
2222

23+
screenWidth: readOnly('_oldWidth'),
24+
screenHeight: readOnly('_oldHeight'),
25+
2326
init() {
2427
this._super(...arguments);
2528
this._setDefaults();

tests/unit/services/resize-test.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,28 @@ test('it fires "didResize" when the window is resized', function(assert) {
2020
let evt = new window.Event('resize');
2121

2222
window.dispatchEvent(evt);
23-
assert.equal(didResizeCallCount, 1, 'didResize called 1 time on event firing');
23+
assert.equal(didResizeCallCount, 0, 'didResize called 0 time on event firing');
2424
service.incrementProperty('_oldHeight', -20);
2525
window.dispatchEvent(evt);
26-
assert.equal(didResizeCallCount, 2, 'didResize called another time on event firing again');
26+
assert.equal(didResizeCallCount, 1, 'didResize called 1 time on event firing');
2727
service.set('heightSensitive', false);
2828
service.incrementProperty('_oldHeight', -20);
2929
window.dispatchEvent(evt);
30-
assert.equal(didResizeCallCount, 2, 'didResize shouldn\'t be called again if heightSensitive is false');
30+
assert.equal(didResizeCallCount, 1, 'didResize shouldn\'t be called again if heightSensitive is false');
31+
32+
});
33+
34+
test('screenHeight is bound to the non debounced resize', function(assert) {
35+
36+
let service = this.subject({
37+
widthSensitive: false,
38+
heightSensitive: true
39+
});
40+
41+
let evt = new window.Event('resize');
42+
43+
window.dispatchEvent(evt);
44+
assert.equal(service.get('screenHeight'), window.innerHeight);
3145

3246
});
3347

@@ -55,6 +69,7 @@ test('it fires "debouncedDidResize" when the window is resized', function(asser
5569
later(triggerEvent, 10);
5670
}
5771

72+
service.incrementProperty('_oldHeightDebounced', -20);
5873
assert.equal(debouncedDidResizeCallCount, 0, 'debouncedDidResize not called yet');
5974
later(() => {
6075
assert.equal(debouncedDidResizeCallCount, 1, 'debouncedDidResize called 1 time after 500ms');

0 commit comments

Comments
 (0)