This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add initial resize observer * add resize observer tests * add readme entry * remove commented code * remove commented code * updated readme, extra example
- Loading branch information
Showing
4 changed files
with
251 additions
and
0 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
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,78 @@ | ||
import { Base } from './Base'; | ||
import Map from '@dojo/shim/Map'; | ||
|
||
interface Observer { | ||
observe(node: HTMLElement): void; | ||
} | ||
|
||
declare const ResizeObserver: { | ||
prototype: Observer; | ||
new (callback: (entries: ResizeObserverEntry[]) => any): any; | ||
}; | ||
|
||
interface ResizeObserverEntry { | ||
contentRect: ContentRect; | ||
} | ||
|
||
export interface ContentRect { | ||
readonly bottom: number; | ||
readonly height: number; | ||
readonly left: number; | ||
readonly right: number; | ||
readonly top: number; | ||
readonly width: number; | ||
readonly x: number; | ||
readonly y: number; | ||
} | ||
|
||
export interface PredicateFunction { | ||
(contentRect: ContentRect): boolean; | ||
} | ||
|
||
export interface PredicateFunctions { | ||
[id: string]: PredicateFunction; | ||
} | ||
|
||
export type PredicateResponses<T = PredicateFunctions> = { [id in keyof T]: boolean }; | ||
|
||
export class Resize extends Base { | ||
private _details = new Map<string | number, PredicateResponses>(); | ||
|
||
public get<T extends PredicateFunctions>(key: string | number, predicates: T): PredicateResponses<T> { | ||
const node = this.getNode(key); | ||
|
||
if (!node) { | ||
const defaultResponse: PredicateResponses = {}; | ||
for (let predicateId in predicates) { | ||
defaultResponse[predicateId] = false; | ||
} | ||
return defaultResponse as PredicateResponses<T>; | ||
} | ||
|
||
if (!this._details.has(key)) { | ||
this._details.set(key, {}); | ||
const resizeObserver = new ResizeObserver(([entry]) => { | ||
const { contentRect } = entry; | ||
const previousDetails = this._details.get(key); | ||
let predicateChanged = false; | ||
let predicateResponses: PredicateResponses = {}; | ||
|
||
for (let predicateId in predicates) { | ||
const response = predicates[predicateId](contentRect); | ||
predicateResponses[predicateId] = response; | ||
if (!predicateChanged && response !== previousDetails![predicateId]) { | ||
predicateChanged = true; | ||
} | ||
} | ||
|
||
this._details.set(key, predicateResponses); | ||
predicateChanged && this.invalidate(); | ||
}); | ||
resizeObserver.observe(node); | ||
} | ||
|
||
return this._details.get(key) as PredicateResponses<T>; | ||
} | ||
} | ||
|
||
export default Resize; |
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,135 @@ | ||
const { registerSuite } = intern.getInterface('object'); | ||
const { assert } = intern.getPlugin('chai'); | ||
import global from '@dojo/shim/global'; | ||
import { stub, SinonStub } from 'sinon'; | ||
import Resize, { ContentRect } from '../../../src/meta/Resize'; | ||
import NodeHandler from '../../../src/NodeHandler'; | ||
import WidgetBase from '../../../src/WidgetBase'; | ||
|
||
let resizeObserver: any; | ||
let resizeCallback: ([]: any[]) => void; | ||
const bindInstance = new WidgetBase(); | ||
let isFoo: SinonStub; | ||
let isBar: SinonStub; | ||
|
||
registerSuite('meta - Resize', { | ||
beforeEach() { | ||
isFoo = stub(); | ||
isBar = stub(); | ||
resizeObserver = stub().callsFake(function(callback: any) { | ||
const observer = { | ||
observe: stub() | ||
}; | ||
resizeCallback = callback; | ||
return observer; | ||
}); | ||
|
||
global.ResizeObserver = resizeObserver; | ||
}, | ||
|
||
afterEach() { | ||
isFoo.reset(); | ||
isBar.reset(); | ||
resizeObserver.reset(); | ||
global.ResizeObserver = undefined; | ||
}, | ||
|
||
tests: { | ||
'Will return predicates defaulted to false if node not loaded'() { | ||
const nodeHandler = new NodeHandler(); | ||
|
||
const resize = new Resize({ | ||
invalidate: () => {}, | ||
nodeHandler, | ||
bind: bindInstance | ||
}); | ||
|
||
assert.deepEqual(resize.get('foo', { isFoo, isBar }), { isFoo: false, isBar: false }); | ||
assert.isFalse(isFoo.called); | ||
assert.isFalse(isBar.called); | ||
}, | ||
'Will create a new ResizeObserver when node exists'() { | ||
const nodeHandler = new NodeHandler(); | ||
const element = document.createElement('div'); | ||
document.body.appendChild(element); | ||
nodeHandler.add(element, 'foo'); | ||
|
||
const resize = new Resize({ | ||
invalidate: () => {}, | ||
nodeHandler, | ||
bind: bindInstance | ||
}); | ||
|
||
resize.get('foo', { isFoo, isBar }); | ||
assert.isTrue(resizeObserver.calledOnce); | ||
}, | ||
'Will call predicates when resize event is observed'() { | ||
const nodeHandler = new NodeHandler(); | ||
const element = document.createElement('div'); | ||
document.body.appendChild(element); | ||
nodeHandler.add(element, 'foo'); | ||
|
||
const resize = new Resize({ | ||
invalidate: () => {}, | ||
nodeHandler, | ||
bind: bindInstance | ||
}); | ||
|
||
const contentRect: Partial<ContentRect> = { | ||
width: 10 | ||
}; | ||
|
||
resize.get('foo', { isFoo, isBar }); | ||
resizeCallback([{ contentRect }]); | ||
|
||
assert.isTrue(isFoo.firstCall.calledWith(contentRect)); | ||
assert.isTrue(isBar.firstCall.calledWith(contentRect)); | ||
}, | ||
'Will only set up one observer per widget per key'() { | ||
const nodeHandler = new NodeHandler(); | ||
const element = document.createElement('div'); | ||
document.body.appendChild(element); | ||
nodeHandler.add(element, 'foo'); | ||
|
||
const resize = new Resize({ | ||
invalidate: () => {}, | ||
nodeHandler, | ||
bind: bindInstance | ||
}); | ||
|
||
resize.get('foo', { isFoo }); | ||
resize.get('foo', { isBar }); | ||
assert.isTrue(resizeObserver.calledOnce); | ||
}, | ||
'Will call invalidate when predicates have changed'() { | ||
const nodeHandler = new NodeHandler(); | ||
const invalidate = stub(); | ||
const element = document.createElement('div'); | ||
document.body.appendChild(element); | ||
nodeHandler.add(element, 'foo'); | ||
|
||
const resize = new Resize({ | ||
invalidate, | ||
nodeHandler, | ||
bind: bindInstance | ||
}); | ||
|
||
const contentRect: Partial<ContentRect> = { | ||
width: 10 | ||
}; | ||
|
||
isFoo.onFirstCall().returns(false); | ||
isFoo.onSecondCall().returns(true); | ||
|
||
resize.get('foo', { isFoo, isBar }); | ||
|
||
resizeCallback([{ contentRect }]); | ||
resizeCallback([{ contentRect }]); | ||
|
||
const predicates = resize.get('foo', { isFoo, isBar }); | ||
|
||
assert.isTrue(invalidate.calledTwice); | ||
assert.isTrue(predicates.isFoo); | ||
} | ||
} | ||
}); |
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