diff --git a/src/main/webapp/resources/js/base/dataset.js b/src/main/webapp/resources/js/base/dataset.js index 0e60caa..b85b092 100644 --- a/src/main/webapp/resources/js/base/dataset.js +++ b/src/main/webapp/resources/js/base/dataset.js @@ -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 + * + * @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 + * + * 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 *