From 1afac162506e9736258b6fbd1f3a42889cfbeab6 Mon Sep 17 00:00:00 2001 From: Tim Lancina Date: Tue, 12 Jan 2016 15:56:09 -0600 Subject: [PATCH] chore(): add types --- ionic/components/ion.ts | 13 ++++++++----- ionic/util/dom.ts | 18 +++++------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/ionic/components/ion.ts b/ionic/components/ion.ts index cc5bbf44e7..79b9c29267 100644 --- a/ionic/components/ion.ts +++ b/ionic/components/ion.ts @@ -3,6 +3,7 @@ import {Config} from '../config/config'; import {isArray} from '../util'; import * as dom from '../util/dom'; +let ids:number = 0; /** * Base class for all Ionic components. Exposes some common functionality @@ -10,8 +11,10 @@ import * as dom from '../util/dom'; * sending/receiving app-level events. */ export class Ion { - constructor(elementRef: ElementRef) { - this.elementRef = elementRef; + private _id: string; + + constructor(protected elementRef: ElementRef) { + this._id = 'i-' + ids++; } getElementRef() { @@ -23,15 +26,15 @@ export class Ion { } getDimensions() { - return dom.getDimensions(this); + return dom.getDimensions(this.elementRef.nativeElement, this._id); } width() { - return dom.getDimensions(this).width; + return dom.getDimensions(this.elementRef.nativeElement, this._id).width; } height() { - return dom.getDimensions(this).height; + return dom.getDimensions(this.elementRef.nativeElement, this._id).height; } } diff --git a/ionic/util/dom.ts b/ionic/util/dom.ts index 7598e917c6..8d3a833650 100644 --- a/ionic/util/dom.ts +++ b/ionic/util/dom.ts @@ -234,21 +234,14 @@ export function removeElement(ele) { * to reduce DOM reads. Cache is cleared on a window resize. * @param {TODO} ele TODO */ -export function getDimensions(ion, ele) { - if (!ion._dimId) { - ion._dimId = ++dimensionIds; - if (ion._dimId % 1000 === 0) { - // periodically flush dimensions - flushDimensionCache(); - } - } - - let dimensions = dimensionCache[ion._dimId]; +export function getDimensions(ele: HTMLElement, id: string): { + width: number, height: number, left: number, top: number +} { + let dimensions = dimensionCache[id]; if (!dimensions) { - let ele = ion.getNativeElement(); // make sure we got good values before caching if (ele.offsetWidth && ele.offsetHeight) { - dimensions = dimensionCache[ion._dimId] = { + dimensions = dimensionCache[id] = { width: ele.offsetWidth, height: ele.offsetHeight, left: ele.offsetLeft, @@ -285,7 +278,6 @@ export function flushDimensionCache() { } let dimensionCache:any = {}; -let dimensionIds = 0; function isStaticPositioned(element) { return (element.style.position || 'static') === 'static';