Skip to content

Commit 4fcc92f

Browse files
authored
Merge pull request #1 from vash15/listview-header
Added listview header
2 parents c85e7f5 + 301f6ed commit 4fcc92f

File tree

1 file changed

+53
-19
lines changed

1 file changed

+53
-19
lines changed

lib/listviews/ListView.js

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ export default class ListView extends BaseView {
5555
infiniteLoadingSize: 44,
5656
distance: 100,
5757
pullToRefresh: false,
58+
pullToRefreshText: 'Pull to refresh...',
5859
pullToRefreshClass: 'ui-pull-to-refresh',
5960
touchActiveClassName: 'touch-active',
6061
emptyText: '',
61-
delayedRender: 0
62+
delayedRender: 0,
63+
headerView: null, // view to print on the header
64+
headerSize: 0, // height of the header in a vertical listview or width in an horizontal listview
65+
changeDirectionPan: 50
6266
});
6367

6468
this.items = [];
@@ -81,6 +85,7 @@ export default class ListView extends BaseView {
8185
this.touchY = 0;
8286
this.direction = null;
8387
this.previousDirection = null;
88+
this.startChangeDirectionAt = null;
8489

8590
this.setCollection(this.collection);
8691

@@ -146,7 +151,7 @@ export default class ListView extends BaseView {
146151
});
147152

148153
// Preparo gli elementi vuoti
149-
var anElement, anItem, aPosition;
154+
var anElement, anItem, aPosition, aXY;
150155
var itemStyle = {
151156
position: 'absolute',
152157
top: 0,
@@ -158,16 +163,14 @@ export default class ListView extends BaseView {
158163
index: i,
159164
$el: anElement
160165
};
161-
aPosition = this.getPositionAtIndex(i);
166+
aXY = this.getXYFromIndex(i);
167+
anItem.x = aXY.x;
168+
anItem.y = aXY.y;
162169

163170
if (this.options.orientation === ORIENTATION_VERTICAL) {
164-
anItem.x = aPosition.column * Math.floor(this.listWidth / this.options.itemsPerRow);
165-
anItem.y = aPosition.row * this.options.itemHeight;
166171
itemStyle.width = ((this.listWidth / this.options.itemsPerRow) / this.listWidth) * 100 + '%';
167172
}
168173
else {
169-
anItem.x = aPosition.row * this.options.itemWidth;
170-
anItem.y = aPosition.column * Math.floor(this.listHeight / this.options.itemsPerRow);
171174
itemStyle.height = ((this.listHeight / this.options.itemsPerRow) / this.listHeight) * 100 + '%';
172175
}
173176

@@ -180,6 +183,12 @@ export default class ListView extends BaseView {
180183

181184
this.$el.append($scrollContainer);
182185

186+
// Header
187+
if (this.options.headerView && this.options.headerView instanceof BaseView) {
188+
this.$el.prepend(this.options.headerView.el);
189+
this.options.headerView.render();
190+
}
191+
183192
// Pull to refresh
184193
if (this.options.pullToRefresh) {
185194
let $pullToRefresh = this.cache.$pullToRefresh = $('<div>').addClass(this.options.pullToRefreshClass);
@@ -189,7 +198,7 @@ export default class ListView extends BaseView {
189198
left: 0,
190199
'margin-top': '-60px',
191200
});
192-
$pullToRefresh.append($('<span>').text('Pull to refresh...'));
201+
$pullToRefresh.append($('<span>').text(this.options.pullToRefreshText));
193202
this.$el.prepend($pullToRefresh);
194203
}
195204
}
@@ -219,14 +228,9 @@ export default class ListView extends BaseView {
219228

220229
// Riposiziona l'elemento
221230
let position = this.getPositionAtIndex(item.index);
222-
if (this.options.orientation === ORIENTATION_VERTICAL) {
223-
item.x = position.column * Math.floor(this.listWidth / this.options.itemsPerRow);
224-
item.y = position.row * this.options.itemHeight;
225-
}
226-
else {
227-
item.x = position.row * this.options.itemWidth;
228-
item.y = position.column * Math.floor(this.listHeight / this.options.itemsPerRow);
229-
}
231+
let xy = this.getXYFromIndex(item.index);
232+
item.x = xy.x;
233+
item.y = xy.y;
230234
this.requestAnimationFrame(() => {
231235
for (var c = 0; c < this.options.itemsPerRow; c++) {
232236
if (position.column !== c)
@@ -479,6 +483,12 @@ export default class ListView extends BaseView {
479483
// TODO: animate scroll
480484
this.pause();
481485
this.el.scrollTop = 0;
486+
// Trigger change:direction when user taps the status bar
487+
this.direction = DOWN_TOP;
488+
if (this.direction != this.previousDirection) {
489+
this.trigger('change:direction', this.direction);
490+
this.previousDirection = this.direction;
491+
}
482492
this.resume();
483493
}
484494

@@ -498,9 +508,15 @@ export default class ListView extends BaseView {
498508
let y = this.y = -this.el.scrollTop;
499509
this.direction = y < this.previousY ? TOP_DOWN : DOWN_TOP;
500510
if (this.direction != this.previousDirection && y < 0) {
501-
this.trigger('change:direction', this.direction);
511+
this.startChangeDirectionAt = y;
502512
this.previousDirection = this.direction;
503513
}
514+
515+
if (this.startChangeDirectionAt !== null && Math.abs(this.startChangeDirectionAt - y) > this.options.changeDirectionPan) {
516+
this.startChangeDirectionAt = null;
517+
this.trigger('change:direction', this.direction);
518+
}
519+
504520
this.previousY = y;
505521
startIndex = this.getStartIndex();
506522
endIndex = this.getEndIndex();
@@ -654,10 +670,10 @@ export default class ListView extends BaseView {
654670
}
655671

656672
if (this.options.orientation === ORIENTATION_VERTICAL) {
657-
return rows * this.options.itemHeight + infiniteLoadingSize;
673+
return rows * this.options.itemHeight + infiniteLoadingSize + this.options.headerSize;
658674
}
659675
else {
660-
return rows * this.options.itemWidth + infiniteLoadingSize;
676+
return rows * this.options.itemWidth + infiniteLoadingSize + this.options.headerSize;
661677
}
662678
}
663679

@@ -679,6 +695,24 @@ export default class ListView extends BaseView {
679695
}
680696
}
681697

698+
getXYFromIndex(index) {
699+
let aPosition = this.getPositionAtIndex(index);
700+
let xy = {};
701+
if (this.options.orientation === ORIENTATION_VERTICAL) {
702+
xy = {
703+
x: aPosition.column * Math.floor(this.listWidth / this.options.itemsPerRow),
704+
y: aPosition.row * this.options.itemHeight + this.options.headerSize
705+
};
706+
}
707+
else {
708+
xy = {
709+
x: aPosition.row * this.options.itemWidth + this.options.headerSize,
710+
y: aPosition.column * Math.floor(this.listHeight / this.options.itemsPerRow)
711+
};
712+
}
713+
return xy;
714+
}
715+
682716
getIndexFromXY(x, y) {
683717
var index;
684718
var row;

0 commit comments

Comments
 (0)