Created
May 3, 2017 11:03
-
-
Save ralfstx/66b402381466287a91544418adf8a28d to your computer and use it in GitHub Desktop.
Tabris 1.x compatible CollectionView
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
const tabris = require('tabris'); | |
/** | |
* Tabris 1.x compatible CollectionView | |
* Does not include compatibility for events | |
*/ | |
class CollectionView_1 extends tabris.CollectionView { | |
constructor(properties) { | |
super(properties); | |
this._items = this._items || []; | |
this.on('select', (event) => { | |
event.item = this._items[event.index]; | |
}); | |
} | |
set itemHeight(height) { | |
this._itemHeight = height; | |
this.cellHeight = typeof height === 'number' ? height : (index, type) => height(this._items[index], type); | |
} | |
get itemHeight() { | |
return this._itemHeight; | |
} | |
set cellType(value) { | |
this._cellType = typeof value === 'string' ? value : (index) => { | |
let res = value(this._items[index]); | |
console.log('celltype', res); | |
return res; | |
}; | |
} | |
get cellType() { | |
return this._cellType; | |
} | |
set items(items) { | |
this._items = items; | |
this.itemCount = items.length; | |
this.refresh(); | |
} | |
get items() { | |
return this._items; | |
} | |
set initializeCell(cb) { | |
this.createCell = (type) => { | |
let cell = new Cell_1(); | |
cb.call(this, cell, type); | |
return cell; | |
}; | |
this.updateCell = (cell, index) => { | |
cell.itemIndex = index; | |
cell.item = this._items[index]; | |
}; | |
} | |
insert(items, index) { | |
if (!Array.isArray(items)) { | |
throw new Error('items is not an array'); | |
} | |
if (arguments.length === 1) { | |
index = this._items.length; | |
} else { | |
index = Math.max(0, Math.min(this._items.length, this._checkIndex(index))); | |
} | |
Array.prototype.splice.apply(this._items, [index, 0].concat(items)); | |
this._storeProperty('itemCount', this._items.length); | |
this._adjustIndicies(index, items.length); | |
this._nativeCall('update', {insert: [index, items.length]}); | |
} | |
remove(index, count) { | |
if (arguments.length === 1) { | |
count = 1; | |
} else if (typeof count === 'number' && isFinite(count) && count >= 0) { | |
count = Math.min(count, this._items.length - index); | |
} else { | |
throw new Error('illegal remove count'); | |
} | |
if (index >= 0 && index < this._items.length && count > 0) { | |
this._items.splice(index, count); | |
this._storeProperty('itemCount', this._items.length); | |
this._adjustIndicies(index + count, -count); | |
this._nativeCall('update', {remove: [index, count]}); | |
} | |
if (index >= 0 && index < this._items.length && count > 0) { | |
this._items.splice(index, count); | |
this._storeProperty('itemCount', this._items.length); | |
this._adjustIndicies(index + count, -count); | |
this._nativeCall('update', {remove: [index, count]}); | |
} | |
} | |
_adjustIndicies(offset, diff) { | |
let cells = this._children || []; | |
for (let i = 0; i < cells.length; i++) { | |
let cell = cells[i]; | |
let itemIndex = cell.itemIndex; | |
if (itemIndex >= offset) { | |
cell.itemIndex = itemIndex + diff; | |
} | |
} | |
} | |
} | |
class Cell_1 extends tabris.Composite { | |
set item(item) { | |
this._item = item; | |
this._triggerChangeEvent('item', item); | |
} | |
get item() { | |
return this._item; | |
} | |
set itemIndex(itemIndex) { | |
this._itemIndex = itemIndex; | |
this._triggerChangeEvent('itemIndex', itemIndex); | |
} | |
get itemIndex() { | |
return this._itemIndex; | |
} | |
} | |
tabris.CollectionView_1 = CollectionView_1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment