From 650e19ff7732dba842743c3878d4517ff0180c38 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 16 Oct 2018 16:25:00 +0200 Subject: [PATCH] don't expose direction, handleIndex to distributor ctor --- src/resizer/distributors.js | 21 +++++++++++---------- src/resizer/resizer.js | 23 ++++++++++++++--------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/resizer/distributors.js b/src/resizer/distributors.js index 0d13360ef4..884402b1b0 100644 --- a/src/resizer/distributors.js +++ b/src/resizer/distributors.js @@ -1,8 +1,8 @@ class FixedDistributor { - constructor(container, items, handleIndex, direction, sizer) { - this.item = items[handleIndex + direction]; - this.beforeOffset = sizer.getItemOffset(this.item); + constructor(sizer, item) { this.sizer = sizer; + this.item = item; + this.beforeOffset = sizer.getItemOffset(this.item); } resize(offset) { @@ -17,8 +17,8 @@ class FixedDistributor { class CollapseDistributor extends FixedDistributor { - constructor(container, items, handleIndex, direction, sizer, config) { - super(container, items, handleIndex, direction, sizer); + constructor(sizer, item, config) { + super(sizer, item); this.toggleSize = config && config.toggleSize; this.onCollapsed = config && config.onCollapsed; } @@ -43,16 +43,17 @@ class CollapseDistributor extends FixedDistributor { class PercentageDistributor { - constructor(container, items, handleIndex, direction, sizer) { + constructor(sizer, item, _config, items, container) { this.container = container; this.totalSize = sizer.getTotalSize(); this.sizer = sizer; - this.beforeItems = items.slice(0, handleIndex); - this.afterItems = items.slice(handleIndex); + const itemIndex = items.indexOf(item); + this.beforeItems = items.slice(0, itemIndex); + this.afterItems = items.slice(itemIndex); const percentages = PercentageDistributor._getPercentages(sizer, items); - this.beforePercentages = percentages.slice(0, handleIndex); - this.afterPercentages = percentages.slice(handleIndex); + this.beforePercentages = percentages.slice(0, itemIndex); + this.afterPercentages = percentages.slice(itemIndex); } resize(offset) { diff --git a/src/resizer/resizer.js b/src/resizer/resizer.js index a2affa53ac..e2581e982b 100644 --- a/src/resizer/resizer.js +++ b/src/resizer/resizer.js @@ -58,20 +58,17 @@ export class Resizer { const resizeHandle = event.target; const vertical = resizeHandle.classList.contains(this.classNames.vertical); const reverse = resizeHandle.classList.contains(this.classNames.reverse); - const direction = reverse ? 0 : -1; const sizer = new this.sizerCtor(this.container, vertical, reverse); - const items = Array.from(this.container.children).filter(el => { - return !this._isResizeHandle(el) && ( - this._isResizeHandle(el.previousElementSibling) || - this._isResizeHandle(el.nextElementSibling)); - }); + const items = this._getResizableItems(); const prevItem = resizeHandle.previousElementSibling; - const handleIndex = items.indexOf(prevItem) + 1; + // if reverse, resize the item after the handle, so + 1 + const itemIndex = items.indexOf(prevItem) + (reverse ? 1 : 0); + const item = items[itemIndex]; const distributor = new this.distributorCtor( - this.container, items, handleIndex, - direction, sizer, this.distributorCfg); + sizer, item, this.distributorCfg, + items, this.container); const onMouseMove = (event) => { const offset = sizer.offsetFromEvent(event); @@ -91,4 +88,12 @@ export class Resizer { body.addEventListener("mouseup", onMouseUp, false); body.addEventListener("mousemove", onMouseMove, false); } + + _getResizableItems() { + return Array.from(this.container.children).filter(el => { + return !this._isResizeHandle(el) && ( + this._isResizeHandle(el.previousElementSibling) || + this._isResizeHandle(el.nextElementSibling)); + }); + } }