-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from mike-north/typescript
Typescript
- Loading branch information
Showing
23 changed files
with
956 additions
and
622 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
{ | ||
"javascript.implicitProjectConfig.checkJs": true | ||
} | ||
"javascript.implicitProjectConfig.checkJs": true, | ||
"eslint.validate": [ | ||
"javascript", | ||
"typescript" | ||
] | ||
} |
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
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,115 @@ | ||
import { oneWay, readOnly } from '@ember-decorators/object/computed'; | ||
import { getWithDefault, set } from '@ember/object'; | ||
import Evented from '@ember/object/evented'; | ||
import { cancel, debounce } from '@ember/runloop'; | ||
import Service from '@ember/service'; | ||
import { classify } from '@ember/string'; | ||
|
||
declare global { | ||
// tslint:disable-next-line:variable-name | ||
const FastBoot: {} | undefined; | ||
} | ||
|
||
export interface ResizeDefaults { | ||
widthSensitive?: boolean; | ||
heightSensitive?: boolean; | ||
debounceTimeout?: number ; | ||
injectionFactories?: string[]; | ||
} | ||
|
||
class ResizeService extends Service.extend(Evented) { | ||
public _oldWidth = window.innerWidth; | ||
public _oldHeight = window.innerHeight; | ||
public _oldWidthDebounced = window.innerWidth; | ||
public _oldHeightDebounced = window.innerHeight; | ||
|
||
@oneWay('defaultDebounceTimeout') public debounceTimeout!: number; | ||
@oneWay('defaultWidthSensitive') public widthSensitive!: boolean; | ||
@oneWay('defaultHeightSensitive') public heightSensitive!: boolean; | ||
|
||
public resizeServiceDefaults!: ResizeDefaults; | ||
|
||
@readOnly('_oldWidth') public screenWidth!: number; | ||
@readOnly('_oldHeight') public screenHeight!: number; | ||
public _onResizeHandler?: (this: Window, evt: UIEvent) => void; | ||
public _scheduledDebounce?: ReturnType<typeof debounce>; | ||
constructor() { | ||
super(...arguments); | ||
this._setDefaults(); | ||
this._onResizeHandler = (evt) => { | ||
this._fireResizeNotification(evt); | ||
const scheduledDebounce = debounce(this, this._fireDebouncedResizeNotification, evt, this.get('debounceTimeout')); | ||
this._scheduledDebounce = scheduledDebounce; | ||
}; | ||
if (typeof FastBoot === 'undefined') { | ||
this._installResizeListener(); | ||
} | ||
} | ||
|
||
public destroy() { | ||
this._super(...arguments); | ||
if (typeof FastBoot === 'undefined') { | ||
this._uninstallResizeListener(); | ||
} | ||
this._cancelScheduledDebounce(); | ||
return this; | ||
} | ||
|
||
public _setDefaults() { | ||
const defaults = getWithDefault(this, 'resizeServiceDefaults', {}); | ||
|
||
Object.keys(defaults).map((key: keyof ResizeDefaults ) => { | ||
const classifiedKey = classify(key); | ||
const defaultKey = `default${classifiedKey}`; | ||
return set(this as any, defaultKey, defaults[key]); | ||
}); | ||
} | ||
|
||
public _hasWindowSizeChanged(w: number, h: number, debounced = false) { | ||
const wKey = debounced ? '_oldWidthDebounced' : '_oldWidth'; | ||
const hKey = debounced ? '_oldHeightDebounced' : '_oldHeight'; | ||
return ( | ||
(this.get('widthSensitive') && w !== this.get(wKey)) || | ||
(this.get('heightSensitive') && h !== this.get(hKey)) | ||
); | ||
} | ||
|
||
public _updateCachedWindowSize(w: number, h: number, debounced = false) { | ||
const wKey = debounced ? '_oldWidthDebounced' : '_oldWidth'; | ||
const hKey = debounced ? '_oldHeightDebounced' : '_oldHeight'; | ||
this.set(wKey, w); | ||
this.set(hKey, h); | ||
} | ||
|
||
public _installResizeListener() { | ||
if (!this._onResizeHandler) { return; } | ||
window.addEventListener('resize', this._onResizeHandler); | ||
} | ||
|
||
public _uninstallResizeListener() { | ||
if (!this._onResizeHandler) { return; } | ||
window.removeEventListener('resize', this._onResizeHandler); | ||
} | ||
|
||
public _cancelScheduledDebounce() { | ||
if (!this._scheduledDebounce) { return; } | ||
cancel(this._scheduledDebounce); | ||
} | ||
|
||
public _fireResizeNotification(evt: UIEvent) { | ||
const { innerWidth, innerHeight } = window; | ||
if (this._hasWindowSizeChanged(innerWidth, innerHeight)) { | ||
this.trigger('didResize', evt); | ||
this._updateCachedWindowSize(innerWidth, innerHeight); | ||
} | ||
} | ||
public _fireDebouncedResizeNotification(evt: UIEvent) { | ||
const { innerWidth, innerHeight } = window; | ||
if (this._hasWindowSizeChanged(innerWidth, innerHeight, true)) { | ||
this.trigger('debouncedDidResize', evt); | ||
this._updateCachedWindowSize(innerWidth, innerHeight, true); | ||
} | ||
} | ||
} | ||
|
||
export default ResizeService; |
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 { ResizeDefaults } from 'ember-resize/services/resize'; | ||
|
||
interface IEnvironment { | ||
resizeServiceDefaults: ResizeDefaults; | ||
} | ||
declare const env: IEnvironment; | ||
export = env; |
Oops, something went wrong.