Skip to content

Commit

Permalink
fix var height example
Browse files Browse the repository at this point in the history
  • Loading branch information
voloko committed Apr 12, 2011
1 parent ecaa695 commit 14d8d41
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
36 changes: 19 additions & 17 deletions examples/core-examples/dataList/dataList.js
Expand Up @@ -27,7 +27,7 @@ uki([
{ view: 'DataList', pos: 't:100px r:0 b:0 l:0', data: data,
multiselect: true }
]},

{ view: 'Container', pos: 't:10px l:390px w:150px b:10px',
addClass: 'scrollable', childViews: [
{ view: 'Header', text: 'Debounced list 100ms', size: 'small' },
Expand All @@ -44,7 +44,7 @@ var DummyAsyncData = uki.newClass({
this._source = data;
this.length = data.length;
},

loadRange: function(from, to, callback) {
var idxToLoad = [];
for (var i = from; i < to; i++) {
Expand All @@ -63,7 +63,7 @@ var DummyAsyncData = uki.newClass({
this._trigger(from, to, callback);
}
},

_trigger: function(from, to, callback) {
callback(this.slice(from, to));
},
Expand All @@ -90,35 +90,37 @@ var dataWithHeaders = uki.map(uki.range(1, 10000), function(i) {
});

var VarHeightMetrics = uki.newClass(uki.view.dataList.Metrics, {

update: function() {
this._cache = [];
this._data = this._view.data();
this.triggerChanges('totalHeight');
},

rowsForRange: function(range) {
return {
return {
from: this.rowForPosition(range.from),
to: this.rowForPosition(range.to)
to: this.rowForPosition(range.to) + 1
};
},

rowForPosition: function(px) {
var i = this._cache.length - 1;
while (this._cache[i] < px) {
this.prefilCache(++i);
while (this._cache[i] + this.rowHeight(i) < px) {
this.prefilCache(i);
}

var index = uki.binarySearch(this._cache, px);
return this._cache[index] > px ? index - 1 : index;
return this._cache[index] > px ? index - 1 :
Math.min(index, this._data.length - 1);
},

rowHeight: function(index) {
return this._data[index].header ? 40 : 24;
},

prefilCache: function(index) {
if (index === 10000) debugger;
var i = this._cache.length;
while (i <= index) {
if (i === 0) {
Expand All @@ -129,22 +131,22 @@ var VarHeightMetrics = uki.newClass(uki.view.dataList.Metrics, {
i++;
}
},

rowDimensions: function(index) {
this.prefilCache(index);
return {
top: this._cache[index],
height: this.rowHeight(index)
};
},

// this is expensive
totalHeight: function() {
var index = this._data.length - 1;
this.prefilCache(index);
return this._cache[index] + this.rowHeight(index);
}

});

uki([
Expand Down
5 changes: 3 additions & 2 deletions src/uki-view/view/dataList.js
Expand Up @@ -280,10 +280,10 @@ var DataList = view.newClass('DataList', Base, Focusable, {
if (range.to > range.from) {
var rowsRange = this.metrics().rowsForRange(range),
pack = this._scheduleRenderPack(rowsRange),
d = this.metrics().rowDimensions(rowsRange.to);
d = this.metrics().rowDimensions(rowsRange.to - 1);

pack.fromPX = this.metrics().rowDimensions(rowsRange.from).top;
pack.toPX = d.top + d.height - 1;
pack.toPX = d.top + d.height;
packs.push(pack);

this._packs = packs.sort(function(a, b) {
Expand All @@ -295,6 +295,7 @@ var DataList = view.newClass('DataList', Base, Focusable, {
_removePack: function(pack) {
if (pack.dom) {
this.dom().removeChild(pack.dom);
delete pack.dom;
}
pack.deleted = true;
},
Expand Down

0 comments on commit 14d8d41

Please sign in to comment.