From 4c8bbdf02d0676dcb65dd98fbfbf8767ffe0a82c Mon Sep 17 00:00:00 2001 From: Ev S Date: Wed, 19 Oct 2016 21:29:27 +0200 Subject: [PATCH 1/3] fixes wrong index bug ( https://github.com/driftyco/ionic/issues/8782 ) --- src/components/item/item-reorder.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/components/item/item-reorder.ts b/src/components/item/item-reorder.ts index 50f7e38629e..5aeeb686a0f 100644 --- a/src/components/item/item-reorder.ts +++ b/src/components/item/item-reorder.ts @@ -145,6 +145,9 @@ export class ItemReorder { /** @private */ _lastToIndex: number = -1; + /** @private */ + _lastDirection: number = 0; + /** @private */ _element: HTMLElement; @@ -221,6 +224,15 @@ export class ItemReorder { */ reorderEmit(fromIndex: number, toIndex: number) { this.reorderReset(); + + // fixes bug: https://github.com/driftyco/ionic/issues/8782 + let diff = fromIndex - toIndex; + if( this._lastDirection > 0 && diff === 1 ) { + toIndex = toIndex + 1 + } else if ( this._lastDirection < 0 && diff === -1 ) { + toIndex = toIndex - 1 + } + if (fromIndex !== toIndex) { this._zone.run(() => { this.ionItemReorder.emit({ @@ -264,6 +276,8 @@ export class ItemReorder { if (this._lastToIndex === -1) { this._lastToIndex = fromIndex; } + //store last direction + this._lastDirection = this._lastToIndex > toIndex ? -1 : 1; let lastToIndex = this._lastToIndex; this._lastToIndex = toIndex; From 97801ad2a88ff7a1445560d4b88391641f7225b6 Mon Sep 17 00:00:00 2001 From: Ev S Date: Thu, 20 Oct 2016 15:08:00 +0200 Subject: [PATCH 2/3] fixed again --- src/components/item/item-reorder.ts | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/components/item/item-reorder.ts b/src/components/item/item-reorder.ts index 5aeeb686a0f..177eaf6d238 100644 --- a/src/components/item/item-reorder.ts +++ b/src/components/item/item-reorder.ts @@ -145,9 +145,6 @@ export class ItemReorder { /** @private */ _lastToIndex: number = -1; - /** @private */ - _lastDirection: number = 0; - /** @private */ _element: HTMLElement; @@ -204,6 +201,8 @@ export class ItemReorder { */ reorderPrepare() { let ele = this._element; + // append child to allow reordering to bottom position + ele.insertAdjacentHTML('beforeend', '
'); let children: any = ele.children; for (let i = 0, ilen = children.length; i < ilen; i++) { var child = children[i]; @@ -227,9 +226,7 @@ export class ItemReorder { // fixes bug: https://github.com/driftyco/ionic/issues/8782 let diff = fromIndex - toIndex; - if( this._lastDirection > 0 && diff === 1 ) { - toIndex = toIndex + 1 - } else if ( this._lastDirection < 0 && diff === -1 ) { + if(diff < 0 ) { toIndex = toIndex - 1 } @@ -258,6 +255,9 @@ export class ItemReorder { * @private */ reorderReset() { + // remove temp bottom item. + this._element.querySelector('.ion-reorder-temp-item').remove() + let children = this._element.children; let len = children.length; @@ -276,8 +276,6 @@ export class ItemReorder { if (this._lastToIndex === -1) { this._lastToIndex = fromIndex; } - //store last direction - this._lastDirection = this._lastToIndex > toIndex ? -1 : 1; let lastToIndex = this._lastToIndex; this._lastToIndex = toIndex; @@ -290,16 +288,16 @@ export class ItemReorder { /********* DOM WRITE ********* */ let transform = CSS.transform; if (toIndex >= lastToIndex) { - for (var i = lastToIndex; i <= toIndex; i++) { + for (var i = lastToIndex; i <= toIndex-1; i++) { if (i !== fromIndex) { - (children[i]).style[transform] = (i > fromIndex) + (children[i]).style[transform] = (i >= fromIndex) ? `translateY(${-itemHeight}px)` : ''; } } } - if (toIndex <= lastToIndex) { - for (var i = toIndex; i <= lastToIndex; i++) { + if (toIndex < lastToIndex) { + for (var i = toIndex; i <= lastToIndex+1; i++) { if (i !== fromIndex) { (children[i]).style[transform] = (i < fromIndex) ? `translateY(${itemHeight}px)` : ''; From c621efb702cf83f1395da04b0d8a4ab6054929b8 Mon Sep 17 00:00:00 2001 From: Ev S Date: Thu, 20 Oct 2016 15:10:54 +0200 Subject: [PATCH 3/3] fixed reorder bugs --- src/components/item/item-reorder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/item/item-reorder.ts b/src/components/item/item-reorder.ts index 177eaf6d238..34a53d3a590 100644 --- a/src/components/item/item-reorder.ts +++ b/src/components/item/item-reorder.ts @@ -256,7 +256,7 @@ export class ItemReorder { */ reorderReset() { // remove temp bottom item. - this._element.querySelector('.ion-reorder-temp-item').remove() + this._element.querySelector('.ion-reorder-temp-item').remove(); let children = this._element.children; let len = children.length;