scroll(..), move(..) 추가

master
mjkhan21 12 months ago
parent 8f73c8651a
commit d729cddd56

@ -416,6 +416,7 @@ class Dataset {
"onModify",
"onReplace",
"onRemove",
"onMove",
"onDirtiesChange",
"onSort"
].forEach(on => {
@ -756,6 +757,72 @@ class Dataset {
this.onCurrentChange(item);
}
}
/**Scrolls up or down depending on the offset and fires the {@link Dataset#onCurrentChange onCurrentChange} event.
* If the new position surpasses either top or bottom of the Dataset, the first or last DataItem is set current.
* @param {number} offset
* <ul><li>negative integer to scroll up</li>
* <li>positive integer to scroll down</li>
* </ul>
* @returns {Dataset} this Dataset
*/
scroll(offset) {
if (!offset)
return this;
let items = this.getDataset("item"),
length = items.length;
if (length < 2)
return this;
let current = items.indexOf(this.getCurrent("item")),
pos = current + offset;
pos = offset < 0 ? Math.max(0, pos) : Math.min(length - 1, pos);
let item = items[pos];
if (!item)
return this;
this._current = item;
this.onCurrentChange(item);
return this;
}
/**Moves the item of the Dataset to the position at the index and fires the events of
* <ul><li>{@link Dataset#onDatasetChange onDatasetChange}</li>
* <li>{@link Dataset#onCurrentChange onCurrentChange}</li>
* <li>{@link Dataset#onSelectionChange onSelectionChange}</li>
* <li>{@link Dataset#onDirtyStateChange onDirtyStateChange}</li>
* <li>{@link Dataset#onSort onSort}</li>
* </ul>
* If the index surpasses either top or bottom of the Dataset,
* it is adjusted to the start or end of the items.
* @param {DataItem} item an item of the Dataset
* @param {number} index 0-based index
*/
move(item, index) {
if (!item) return this;
let items = this.getDataset("item"),
length = items.length;
if (length < 1) return this;
let pos = this._items.indexOf(item);
if (pos < 0) return this;
index = Math.max(0, index);
index = Math.min(length - 1, index);
if (pos == index) return this;
let dest = items[index];
index = this._items.indexOf(dest);
this._items.splice(pos, 1);
this._items.splice(index, 0, item);
this.onMove(item);
this.setState();
return this;
}
/**Returns the Dataset's current state in an object.
* The object has the properties as follows.
@ -1449,6 +1516,11 @@ class Dataset {
*/
onRemove(removed) {this.log("Data removed", removed)}
/**Called back when user data are removed.
* @param {array} removed array of removed dataItems
*/
onMove(item) {this.log("Data moved", item)}
/**Called back when the Dataset gets dirty or not dirty.
* @param {boolean} dirty
* <ul> <li>true if the Dataset is dirty</li>
@ -1491,6 +1563,7 @@ class DatasetControl {
conf.onDirtiesChange = dirty => this.onDirtiesChange(dirty);
conf.onReplace = obj => this.onReplace(obj);
conf.onSort = status => this.onSort(status);
conf.onMove = item => this.onMove(item);
this.dataset = new Dataset(conf);
@ -1747,6 +1820,10 @@ class DatasetControl {
this.reload({prev: selected.length == this.dataset.length});
}
onMove(item) {
debug("on move", item);
}
clear() {
this.dataset.clear();
}

Loading…
Cancel
Save