From ca3ef487fc6d6ce38131c925b02c219d3fdbc706 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 25 May 2015 01:26:39 +0200 Subject: [PATCH] Added WoltLab/WCF/List as a shim for Set --- wcfsetup/install/files/js/WoltLab/WCF/List.js | 112 ++++++++++++++++++ wcfsetup/install/files/js/require.config.js | 1 + 2 files changed, 113 insertions(+) create mode 100644 wcfsetup/install/files/js/WoltLab/WCF/List.js diff --git a/wcfsetup/install/files/js/WoltLab/WCF/List.js b/wcfsetup/install/files/js/WoltLab/WCF/List.js new file mode 100644 index 0000000000..f79d54ae77 --- /dev/null +++ b/wcfsetup/install/files/js/WoltLab/WCF/List.js @@ -0,0 +1,112 @@ +/** + * List implementation relying on an array or if supported on a Set to hold values. + * + * @author Alexander Ebert + * @copyright 2001-2015 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLab/WCF/List + */ +define([], function() { + "use strict"; + + var _hasSet = window.hasOwnProperty('Set') && typeof window.Set === 'function'; + + /** + * @constructor + */ + function List() { + this._set = (_hasSet) ? new Set() : []; + }; + List.prototype = { + /** + * Appends an element to the list, silently rejects adding an already existing value. + * + * @param {*} value unique element + */ + add: function(value) { + if (_hasSet) { + this._set.add(value); + } + else if (!this.has(value)) { + this._set.push(value); + } + }, + + /** + * Removes all elements from the list. + */ + clear: function() { + if (_hasSet) { + this._set.clear(); + } + else { + this._set = []; + } + }, + + /** + * Removes an element from the list, returns true if the element was in the list. + * + * @param {*} value element + * @return {boolean} true if element was in the list + */ + 'delete': function(value) { + if (_hasSet) { + return this._set['delete'](value); + } + else { + var index = this._set.indexOf(index); + if (index === -1) { + return false; + } + + this._set.splice(index, 1); + return true; + } + }, + + /** + * Calls `callback` for each element in the list. + */ + forEach: function(callback) { + if (_hasSet) { + this._set.forEach(callback); + } + else { + for (var i = 0, length = this._set.length; i < length; i++) { + callback(this._set[i]); + } + } + }, + + /** + * Returns true if the list contains the element. + * + * @param {*} value element + * @return {boolean} true if element is in the list + */ + has: function(value) { + if (_hasSet) { + return this._set.has(value); + } + else { + return (this._set.indexOf(value) !== -1); + } + } + }; + + Object.defineProperty(List.prototype, 'size', { + enumerable: false, + configurable: true, + get: function() { + if (_hasMap) { + return this._set.size; + } + else { + return this._set.length; + } + } + }); + + return List; +}); diff --git a/wcfsetup/install/files/js/require.config.js b/wcfsetup/install/files/js/require.config.js index 17a8071165..443999a248 100644 --- a/wcfsetup/install/files/js/require.config.js +++ b/wcfsetup/install/files/js/require.config.js @@ -17,6 +17,7 @@ requirejs.config({ 'Environment': 'WoltLab/WCF/Environment', 'EventHandler': 'WoltLab/WCF/Event/Handler', 'Language': 'WoltLab/WCF/Language', + 'List': 'WoltLab/WCF/List', 'ObjectMap': 'WoltLab/WCF/ObjectMap', 'UI/Alignment': 'WoltLab/WCF/UI/Alignment', 'UI/Dialog': 'WoltLab/WCF/UI/Dialog', -- 2.20.1