forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(list): list items can be reordered
references ionic-team#5595
- Loading branch information
1 parent
0c88589
commit e3ff1a4
Showing
8 changed files
with
350 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import {Item} from './item'; | ||
import {List} from '../list/list'; | ||
import {closest, Coordinates, pointerCoord, CSS, raf} from '../../util/dom'; | ||
|
||
/** | ||
* @private | ||
*/ | ||
export class ItemReorderGesture { | ||
private selectedItem: Item = null; | ||
private offset: Coordinates; | ||
private itemHeight: number; | ||
private lastToIndex: number; | ||
private emptyZone: boolean; | ||
|
||
private onTouchstart: Function; | ||
private onTouchmove: Function; | ||
private onTouchend: Function; | ||
|
||
constructor(public list: List) { | ||
this.listen(); | ||
} | ||
|
||
private onDragStart(ev: any) { | ||
let itemEle = ev.target; | ||
if (itemEle.nodeName === 'ION-REORDER') { | ||
let item = itemEle['$ionComponent']; | ||
if (!item) { | ||
console.error('item does not contain ion component'); | ||
return; | ||
} | ||
|
||
this.offset = pointerCoord(ev); | ||
this.selectedItem = item; | ||
this.itemHeight = item.height(); | ||
this.lastToIndex = item.index; | ||
item.setCssClass('reorder-active', true); | ||
ev.preventDefault(); | ||
} | ||
} | ||
|
||
private onDrag(ev: any) { | ||
if (!this.selectedItem) { | ||
return; | ||
} | ||
|
||
let coord = pointerCoord(ev); | ||
let ydiff = Math.round(coord.y - this.offset.y); | ||
|
||
this.selectedItem.setCssStyle(CSS.transform, 'translateY(' + ydiff + 'px)'); | ||
ev.preventDefault(); | ||
|
||
let item = this.itemForCoord(coord); | ||
if (!item) { | ||
this.emptyZone = true; | ||
return; | ||
} | ||
let fromIndex = this.selectedItem.index; | ||
let toIndex = item.index; | ||
if (toIndex !== this.lastToIndex || this.emptyZone) { | ||
this.lastToIndex = toIndex; | ||
this.emptyZone = false; | ||
this.list.reorderMove(fromIndex, toIndex, this.itemHeight); | ||
} | ||
} | ||
|
||
private onDragEnd(ev: any) { | ||
if (!this.selectedItem) { | ||
return; | ||
} | ||
|
||
let item = this.selectedItem; | ||
let fromIndex = item.index; | ||
|
||
item.setCssClass('reorder-active', false); | ||
this.selectedItem = null; | ||
|
||
let toIndex = this.lastToIndex; | ||
this.list.reorderEmit(fromIndex, toIndex); | ||
} | ||
|
||
private itemForCoord(coord: Coordinates): Item { | ||
let element = <any>document.elementFromPoint(this.offset.x - 100, coord.y); | ||
if (!element) { | ||
return null; | ||
} | ||
element = closest(element, 'ion-item', true); | ||
if (!element) { | ||
return null; | ||
} | ||
let item = <Item>(<any>element)['$ionComponent']; | ||
if (!item) { | ||
return null; | ||
} | ||
return item; | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
listen() { | ||
let ele = this.list.getNativeElement(); | ||
this.onTouchstart = (ev: any) => this.onDragStart(ev); | ||
this.onTouchmove = (ev: any) => this.onDrag(ev); | ||
this.onTouchend = (ev: any) => this.onDragEnd(ev); | ||
|
||
ele.addEventListener('touchstart', this.onTouchstart); | ||
ele.addEventListener('touchmove', this.onTouchmove); | ||
ele.addEventListener('touchend', this.onTouchend); | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
unlisten() { | ||
let ele = this.list.getNativeElement(); | ||
ele.removeEventListener('touchstart', this.onTouchstart); | ||
ele.removeEventListener('touchmove', this.onTouchmove); | ||
ele.removeEventListener('touchend', this.onTouchend); | ||
this.onTouchstart = null; | ||
this.onTouchmove = null; | ||
this.onTouchend = null; | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
destroy() { | ||
this.unlisten(); | ||
this.list = null; | ||
} | ||
} |
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
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 @@ | ||
|
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,32 @@ | ||
import {Component, ChangeDetectorRef} from '@angular/core'; | ||
import {ionicBootstrap} from '../../../../../src'; | ||
|
||
|
||
@Component({ | ||
templateUrl: 'main.html' | ||
}) | ||
class E2EPage { | ||
items: any[] = []; | ||
isReordering: boolean = false; | ||
|
||
constructor(private d: ChangeDetectorRef) { | ||
let nu = 30; | ||
for (let i = 0; i < nu; i++) { | ||
this.items.push(i); | ||
} | ||
} | ||
|
||
toggle() { | ||
this.isReordering = !this.isReordering; | ||
} | ||
|
||
reorder(indexes: any) { | ||
let element = this.items[indexes.from]; | ||
this.items.splice(indexes.from, 1); | ||
this.items.splice(indexes.to, 0, element); | ||
// console.log(this.items); | ||
// this.d.detectChanges(); | ||
} | ||
} | ||
|
||
ionicBootstrap(E2EPage); |
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,21 @@ | ||
<ion-toolbar primary> | ||
<ion-title>Reorder items</ion-title> | ||
<ion-buttons end> | ||
<button (click)="toggle()"> | ||
Edit | ||
</button> | ||
</ion-buttons> | ||
</ion-toolbar> | ||
|
||
<ion-content> | ||
|
||
<ion-list [reorder]="isReordering" (ionItemReorder)="reorder($event)"> | ||
<ion-item *ngFor="let item of items; let index=index" | ||
[index]="index" | ||
[style.background]="'rgb('+(255-item*4)+','+(255-item*4)+','+(255-item*4)+')'"> | ||
{{item}} | ||
</ion-item> | ||
</ion-list> | ||
|
||
</ion-content> | ||
|
Oops, something went wrong.