Skip to content

Commit 64af0c8

Browse files
manucorporatadamdbradley
authored andcommitted
fix(item): item-options width calculated correctly
1 parent 0c88589 commit 64af0c8

File tree

4 files changed

+70
-27
lines changed

4 files changed

+70
-27
lines changed

src/components/item/item-sliding-gesture.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export class ItemSlidingGesture extends DragGesture {
5454
this.closeOpened();
5555
}
5656

57-
// Close all item sliding containers but the selected one
5857
this.selectedContainer = container;
5958
this.openContainer = container;
6059
container.startSliding(ev.center.x);
@@ -72,6 +71,8 @@ export class ItemSlidingGesture extends DragGesture {
7271

7372
onDragEnd(ev: any) {
7473
if (this.selectedContainer) {
74+
ev.preventDefault();
75+
7576
let openAmount = this.selectedContainer.endSliding(ev.velocityX);
7677
this.selectedContainer = null;
7778

@@ -109,7 +110,7 @@ export class ItemSlidingGesture extends DragGesture {
109110
function getContainer(ev: any): ItemSliding {
110111
let ele = closest(ev.target, 'ion-item-sliding', true);
111112
if (ele) {
112-
return ele['$ionComponent'];
113+
return (<any>ele)['$ionComponent'];
113114
}
114115
return null;
115116
}

src/components/item/item-sliding.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import {ChangeDetectionStrategy, Component, ContentChildren, ContentChild, Directive, ElementRef, EventEmitter, HostBinding, Input, Optional, Output, QueryList, Renderer, ViewEncapsulation} from '@angular/core';
22

33
import {List} from '../list/list';
4-
import {Ion} from '../ion';
54
import {Item} from './item';
65
import {isPresent} from '../../util/util';
7-
import {CSS} from '../../util/dom';
6+
import {CSS, nativeRaf, nativeTimeout} from '../../util/dom';
87

98
const SWIPE_FACTOR = 1.1;
109
const ELASTIC_FACTOR = 0.55;
@@ -22,19 +21,18 @@ export const enum SideFlags {
2221
@Directive({
2322
selector: 'ion-item-options',
2423
})
25-
export class ItemOptions extends Ion {
24+
export class ItemOptions {
2625
@Input() side: string;
2726
@Output() ionSwipe: EventEmitter<ItemSliding> = new EventEmitter();
2827

29-
constructor(elementRef: ElementRef, private _renderer: Renderer) {
30-
super(elementRef);
28+
constructor(private _elementRef: ElementRef, private _renderer: Renderer) {
3129
}
3230

3331
/**
3432
* @private
3533
*/
3634
setCssStyle(property: string, value: string) {
37-
this._renderer.setElementStyle(this.elementRef.nativeElement, property, value);
35+
this._renderer.setElementStyle(this._elementRef.nativeElement, property, value);
3836
}
3937

4038
/**
@@ -48,6 +46,10 @@ export class ItemOptions extends Ion {
4846
}
4947
}
5048

49+
width() {
50+
return this._elementRef.nativeElement.offsetWidth;
51+
}
52+
5153
}
5254

5355
const enum SlidingState {
@@ -224,10 +226,14 @@ export class ItemSliding {
224226
* @private
225227
*/
226228
startSliding(startX: number) {
229+
if (this._timer) {
230+
clearTimeout(this._timer);
231+
this._timer = null;
232+
}
227233
if (this._openAmount === 0) {
234+
this._optsDirty = true;
228235
this._setState(SlidingState.Enabled);
229236
}
230-
this._optsDirty = true;
231237
this._startX = startX + this._openAmount;
232238
this.item.setCssStyle(CSS.transition, 'none');
233239
}
@@ -236,7 +242,10 @@ export class ItemSliding {
236242
* @private
237243
*/
238244
moveSliding(x: number): number {
239-
this.calculateOptsWidth();
245+
if (this._optsDirty) {
246+
this.calculateOptsWidth();
247+
return;
248+
}
240249

241250
let openAmount = this._startX - x;
242251
switch (this._sides) {
@@ -290,17 +299,20 @@ export class ItemSliding {
290299
}
291300

292301
calculateOptsWidth() {
293-
if (this._optsDirty) {
294-
this._optsWidthRightSide = 0;
295-
if (this._rightOptions) {
296-
this._optsWidthRightSide = this._rightOptions.width();
302+
nativeRaf(() => {
303+
if (this._optsDirty) {
304+
this._optsWidthRightSide = 0;
305+
if (this._rightOptions) {
306+
this._optsWidthRightSide = this._rightOptions.width();
307+
}
308+
309+
this._optsWidthLeftSide = 0;
310+
if (this._leftOptions) {
311+
this._optsWidthLeftSide = this._leftOptions.width();
312+
}
313+
this._optsDirty = false;
297314
}
298-
this._optsWidthLeftSide = 0;
299-
if (this._leftOptions) {
300-
this._optsWidthLeftSide = this._leftOptions.width();
301-
}
302-
this._optsDirty = false;
303-
}
315+
});
304316
}
305317

306318
/**
@@ -317,7 +329,7 @@ export class ItemSliding {
317329
if (didEnd) {
318330
// TODO: refactor. there must exist a better way
319331
// if sliding ended, we wait 400ms until animation finishes
320-
this._timer = setTimeout(() => {
332+
this._timer = nativeTimeout(() => {
321333
this._setState(SlidingState.Disabled);
322334
this._timer = null;
323335
}, 400);
@@ -347,6 +359,10 @@ export class ItemSliding {
347359
this.setClass('active-slide', state !== SlidingState.Disabled);
348360
this.setClass('active-options-right', state === SlidingState.Right);
349361
this.setClass('active-options-left', state === SlidingState.Left);
362+
if (state === SlidingState.Disabled || state === SlidingState.Enabled) {
363+
this.setClass('active-swipe-right', false);
364+
this.setClass('active-swipe-left', false);
365+
}
350366
}
351367
}
352368

src/components/item/test/sliding/main.html

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@
1414
</div>
1515
<ion-list #myList>
1616

17+
<ion-item-sliding #item100>
18+
<a ion-item>
19+
<h2>HubStruck Notifications</h2>
20+
<p>A new message from a repo in your network</p>
21+
<p>Oceanic Next has joined your network</p>
22+
<ion-note item-right>
23+
10:45 AM
24+
</ion-note>
25+
</a>
26+
27+
<ion-item-options side="left">
28+
<button (click)="unread(item100)">
29+
<ion-icon name="mail"></ion-icon>
30+
</button>
31+
</ion-item-options>
32+
<ion-item-options side="right">
33+
<button danger (click)="share(item100)">
34+
<ion-icon name="trash"></ion-icon>
35+
</button>
36+
<button (click)="favorite(item100)" >
37+
<ion-icon name="star"></ion-icon>
38+
</button>
39+
</ion-item-options>
40+
41+
</ion-item-sliding>
42+
1743
<ion-item-sliding #item0>
1844
<button ion-item text-wrap (click)="didClick(item0)">
1945
<h2>RIGHT side - no icons</h2>
@@ -26,10 +52,10 @@ <h2>RIGHT side - no icons</h2>
2652
</ion-item-sliding>
2753

2854
<ion-item-sliding #item1>
29-
<ion-item text-wrap detail-push class="activated">
55+
<a ion-item text-wrap detail-push class="activated">
3056
<h2>LEFT side - no icons</h2>
3157
<p>I think I figured out how to get more Mountain Dew</p>
32-
</ion-item>
58+
</a>
3359
<ion-item-options side="left" (ionSwipe)="del($event)" *ngIf="slidingEnabled">
3460
<button primary (click)="archive(item1)">Archive</button>
3561
<button danger (click)="del(item1)">Delete</button>

src/components/tabs/test/advanced/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class Tab1Page1 {
137137
}
138138

139139
push() {
140-
this.nav.push(Tab1Page2)
140+
this.nav.push(Tab1Page2);
141141
}
142142

143143
goBack() {
@@ -193,7 +193,7 @@ class Tab1Page2 {
193193
constructor(private nav: NavController) {}
194194

195195
push() {
196-
this.nav.push(Tab1Page3)
196+
this.nav.push(Tab1Page3);
197197
}
198198

199199
ionViewWillEnter() {
@@ -273,7 +273,7 @@ class Tab2Page1 {
273273
constructor(private nav: NavController) {}
274274

275275
push() {
276-
this.nav.push(Tab2Page2)
276+
this.nav.push(Tab2Page2);
277277
}
278278

279279
ionViewWillEnter() {
@@ -314,7 +314,7 @@ class Tab2Page2 {
314314
constructor(private nav: NavController) {}
315315

316316
push() {
317-
this.nav.push(Tab2Page3)
317+
this.nav.push(Tab2Page3);
318318
}
319319

320320
ionViewWillEnter() {

0 commit comments

Comments
 (0)