From 5fb966c35b5b3d7d886213cb9e19d1e430be52fb Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Thu, 27 Oct 2016 14:13:39 -0400 Subject: [PATCH] utilSetDimensions/utilGetDimensions improvements utilSetDimensions should always return a selection for chaining Add `force` argument to utilGetDimensions to override cached dimensions --- modules/ui/init.js | 4 ++-- modules/util/dimensions.js | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/modules/ui/init.js b/modules/ui/init.js index 2dab1a569..801e0c461 100644 --- a/modules/ui/init.js +++ b/modules/ui/init.js @@ -6,7 +6,7 @@ import { tooltip } from '../util/tooltip'; import { svgDefs, svgIcon } from '../svg/index'; import { modeBrowse } from '../modes/index'; import { behaviorHash } from '../behavior/index'; -import { utilSetDimensions } from '../util/dimensions'; +import { utilGetDimensions } from '../util/dimensions'; import { uiAccount } from './account'; import { uiAttribution } from './attribution'; @@ -229,7 +229,7 @@ export function uiInit(context) { var mapDimensions = map.dimensions(); function onResize() { - mapDimensions = utilSetDimensions(content, null); + mapDimensions = utilGetDimensions(content, true); map.dimensions(mapDimensions); } diff --git a/modules/util/dimensions.js b/modules/util/dimensions.js index 34aa656e1..bf835ff59 100644 --- a/modules/util/dimensions.js +++ b/modules/util/dimensions.js @@ -5,23 +5,24 @@ function refresh(selection, node) { return prop; } - -export function utilGetDimensions(selection) { +export function utilGetDimensions(selection, force) { if (!selection || selection.empty()) { return [0, 0]; } - var node = selection.node(); - return selection.property('__dimensions__') || refresh(selection, node); + var node = selection.node(), + cached = selection.property('__dimensions__'); + return (!cached || force) ? refresh(selection, node) : cached; } export function utilSetDimensions(selection, dimensions) { if (!selection || selection.empty()) { - return [0, 0]; + return selection; } var node = selection.node(); if (dimensions === null) { - return refresh(selection, node); + refresh(selection, node); + return selection; } return selection .property('__dimensions__', [dimensions[0], dimensions[1]])