UNPKG

@qooxdoo/framework

Version:

The JS Framework for Coders

255 lines (214 loc) 7.07 kB
/* ************************************************************************ qooxdoo - the new era of web development http://qooxdoo.org Copyright: 2004-2008 1&1 Internet AG, Germany, http://www.1und1.de License: MIT: https://opensource.org/licenses/MIT See the LICENSE file in the project's top-level directory for details. Authors: * Christian Hagendorn (chris_schmidt) ************************************************************************ */ /** * This mixin links all methods to manage the single selection. * * The class which includes the mixin has to implements two methods: * * <ul> * <li><code>_getItems</code>, this method has to return a <code>Array</code> * of <code>qx.ui.core.Widget</code> that should be managed from the manager. * </li> * <li><code>_isAllowEmptySelection</code>, this method has to return a * <code>Boolean</code> value for allowing empty selection or not. * </li> * </ul> */ qx.Mixin.define("qx.ui.core.MSingleSelectionHandling", { /* ***************************************************************************** EVENTS ***************************************************************************** */ events : { /** Fires after the value was modified */ "changeValue" : "qx.event.type.Data", /** Fires after the selection was modified */ "changeSelection" : "qx.event.type.Data" }, /* ***************************************************************************** MEMBERS ***************************************************************************** */ members : { /** @type {qx.ui.core.SingleSelectionManager} the single selection manager */ __manager : null, /* --------------------------------------------------------------------------- PUBLIC API --------------------------------------------------------------------------- */ /** * setValue implements part of the {@link qx.ui.form.IField} interface. * * @param item {null|qx.ui.core.Widget} Item to set as selected value. * @returns {null|TypeError} The status of this operation. */ setValue : function(item) { if (null === item) { this.resetSelection(); return null; } if (item instanceof qx.ui.core.Widget) { this.__getManager().setSelected(item); return null; } else { return new TypeError("Given argument is not null or a {qx.ui.core.Widget}."); } }, /** * getValue implements part of the {@link qx.ui.form.IField} interface. * * @returns {null|qx.ui.core.Widget} The currently selected widget or null if there is none. */ getValue : function() { return this.__getManager().getSelected() || null; }, /** * resetValue implements part of the {@link qx.ui.form.IField} interface. */ resetValue : function() { this.__getManager().resetSelected(); }, /** * Returns an array of currently selected items. * * Note: The result is only a set of selected items, so the order can * differ from the sequence in which the items were added. * * @return {qx.ui.core.Widget[]} List of items. */ getSelection : function() { var selected = this.__getManager().getSelected(); if (selected) { return [selected]; } else { return []; } }, /** * Replaces current selection with the given items. * * @param items {qx.ui.core.Widget[]} Items to select. * @throws {Error} if one of the items is not a child element and if * items contains more than one elements. */ setSelection : function(items) { switch(items.length) { case 0: this.resetSelection(); break; case 1: this.__getManager().setSelected(items[0]); break; default: throw new Error("Could only select one item, but the selection" + " array contains " + items.length + " items!"); } }, /** * Clears the whole selection at once. */ resetSelection : function() { this.__getManager().resetSelected(); }, /** * Detects whether the given item is currently selected. * * @param item {qx.ui.core.Widget} Any valid selectable item. * @return {Boolean} Whether the item is selected. * @throws {Error} if one of the items is not a child element. */ isSelected : function(item) { return this.__getManager().isSelected(item); }, /** * Whether the selection is empty. * * @return {Boolean} Whether the selection is empty. */ isSelectionEmpty : function() { return this.__getManager().isSelectionEmpty(); }, /** * Returns all elements which are selectable. * * @param all {Boolean} true for all selectables, false for the * selectables the user can interactively select * @return {qx.ui.core.Widget[]} The contained items. */ getSelectables: function(all) { return this.__getManager().getSelectables(all); }, /* --------------------------------------------------------------------------- EVENT HANDLER --------------------------------------------------------------------------- */ /** * Event listener for <code>changeSelected</code> event on single * selection manager. * * @param e {qx.event.type.Data} Data event. */ _onChangeSelected : function(e) { var newValue = e.getData(); var oldValue = e.getOldData(); this.fireDataEvent("changeValue", newValue, oldValue); newValue == null ? newValue = [] : newValue = [newValue]; oldValue == null ? oldValue = [] : oldValue = [oldValue]; this.fireDataEvent("changeSelection", newValue, oldValue); }, /** * Return the selection manager if it is already exists, otherwise creates * the manager. * * @return {qx.ui.core.SingleSelectionManager} Single selection manager. */ __getManager : function() { if (this.__manager == null) { var that = this; this.__manager = new qx.ui.core.SingleSelectionManager( { getItems : function() { return that._getItems(); }, isItemSelectable : function(item) { if (that._isItemSelectable) { return that._isItemSelectable(item); } else { return item.isVisible(); } } }); this.__manager.addListener("changeSelected", this._onChangeSelected, this); } this.__manager.setAllowEmptySelection(this._isAllowEmptySelection()); return this.__manager; } }, /* ***************************************************************************** CONSTRUCTOR ***************************************************************************** */ destruct : function() { this._disposeObjects("__manager"); } });