From: Matthias Schmidt Date: Sun, 29 May 2016 10:35:50 +0000 (+0200) Subject: Add UiItemList.setValues() X-Git-Tag: 3.0.0_Beta_1~1580 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=664e497a831e724c47e571763e0f12248537af50;p=GitHub%2FWoltLab%2FWCF.git Add UiItemList.setValues() --- diff --git a/wcfsetup/install/files/js/WoltLab/WCF/Ui/ItemList.js b/wcfsetup/install/files/js/WoltLab/WCF/Ui/ItemList.js index 8acd1af504..4e72d8c67b 100644 --- a/wcfsetup/install/files/js/WoltLab/WCF/Ui/ItemList.js +++ b/wcfsetup/install/files/js/WoltLab/WCF/Ui/ItemList.js @@ -129,7 +129,7 @@ define(['Core', 'Dictionary', 'Language', 'Dom/Traverse', 'WoltLab/WCF/Ui/Sugges /** * Returns the list of current values. * - * @param {string} element id input element id + * @param {string} elementId input element id * @return {array} list of objects containing object id and value */ getValues: function(elementId) { @@ -153,6 +153,31 @@ define(['Core', 'Dictionary', 'Language', 'Dom/Traverse', 'WoltLab/WCF/Ui/Sugges return values; }, + /** + * Sets the list of current values. + * + * @param {string} elementDd input element id + * @param {array} values list of objects containing object id and value + */ + setValues: function(elementId, values) { + if (!_data.has(elementId)) { + throw new Error("Element id '" + elementId + "' is unknown."); + } + + var data = _data.get(elementId); + + // remove all existing items first + var items = DomTraverse.childrenByClass(data.list, 'item'); + for (var i = 0, length = items.length; i < length; i++) { + this._removeItem(null, items[i], true); + } + + // add new items + for (var i = 0, length = values.length; i < length; i++) { + this._addItem(elementId, values[i]); + } + }, + /** * Binds static event listeners. */ @@ -332,7 +357,7 @@ define(['Core', 'Dictionary', 'Language', 'Dom/Traverse', 'WoltLab/WCF/Ui/Sugges * Adds an item to the list. * * @param {string} elementId input element id - * @param {string} value item value + * @param {object} value item value */ _addItem: function(elementId, value) { var data = _data.get(elementId); @@ -369,8 +394,9 @@ define(['Core', 'Dictionary', 'Language', 'Dom/Traverse', 'WoltLab/WCF/Ui/Sugges * * @param {?object} event event object * @param {Element=} item list item + * @param {boolean?} noFocus input element will not be focused if true */ - _removeItem: function(event, item) { + _removeItem: function(event, item, noFocus) { item = (event === null) ? item : event.currentTarget.parentNode; var parent = item.parentNode; @@ -379,7 +405,7 @@ define(['Core', 'Dictionary', 'Language', 'Dom/Traverse', 'WoltLab/WCF/Ui/Sugges data.suggestion.removeExcludedValue(item.children[0].textContent); parent.removeChild(item); - data.element.focus(); + if (!noFocus) data.element.focus(); this._handleLimit(elementId); var values = this._syncShadow(data);