//////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2014-present, Egret Technology.
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of the Egret nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////
namespace eui {
let groupCount:number = 0;
/**
* @private
* 显示列表深度排序
*/
function breadthOrderCompare(a:egret.DisplayObject, b:egret.DisplayObject):number {
let aParent = a.parent;
let bParent = b.parent;
if (!aParent || !bParent)
return 0;
let aNestLevel = a.$nestLevel;
let bNestLevel = b.$nestLevel;
let aIndex = 0;
let bIndex = 0;
if (aParent == bParent) {
aIndex = aParent.getChildIndex(a);
bIndex = bParent.getChildIndex(b);
}
if (aNestLevel > bNestLevel || aIndex > bIndex)
return 1;
if (aNestLevel < bNestLevel || bIndex > aIndex)
return -1;
if (a == b)
return 0;
return breadthOrderCompare(aParent, bParent);
}
/**
* The RadioButtonGroup component defines a group of RadioButton components
* that act as a single mutually exclusive component; therefore,
* a user can select only one RadioButton component at a time.
*
* @event egret.Event.CHANGE Dispatched when the value of the selected RadioButton component in
* this group changes.
*
* @version Egret 2.4
* @version eui 1.0
* @platform Web,Native
* @includeExample extension/eui/components/RadioButtonGroupExample.ts
* @language en_US
*/
/**
* RadioButtonGroup 组件定义一组 RadioButton 组件,这些组件相互排斥;因此,用户每次只能选择一个 RadioButton 组件
*
* @event egret.Event.CHANGE 此组中所选 RadioButton 组件的值更改时分派。
*
* @version Egret 2.4
* @version eui 1.0
* @platform Web,Native
* @includeExample extension/eui/components/RadioButtonGroupExample.ts
* @language zh_CN
*/
export class RadioButtonGroup extends egret.EventDispatcher {
/**
* Constructor.
*
* @version Egret 2.4
* @version eui 1.0
* @platform Web,Native
* @language en_US
*/
/**
* 构造函数。
*
* @version Egret 2.4
* @version eui 1.0
* @platform Web,Native
* @language zh_CN
*/
public constructor() {
super();
this.$name = "_radioButtonGroup" + groupCount++;
}
/**
* @private
* 组名
*/
$name:string = null;
/**
* @private
* 单选按钮列表
*/
private radioButtons:RadioButton[] = [];
/**
* Returns the RadioButton component at the specified index.
*
* @param index The 0-based index of the RadioButton in the
* RadioButtonGroup.
*
* @return The specified RadioButton component if index is between
* 0 and numRadioButtons - 1. Returns
* null if the index is invalid.
*
* @see eui.RadioButtonGroup#numRadioButtons
*
* @version Egret 2.4
* @version eui 1.0
* @platform Web,Native
* @language en_US
*/
/**
* 返回指定索引处的 RadioButton 组件。
*
* @param index RadioButtonGroup 中的 RadioButton 的从零开始的索引。
*
* @return 当索引位于 0 和 numRadioButtons 之间时,指定的 RadioButton 组件为 1。
* 如果索引无效,则返回 null。
*
* @see eui.RadioButtonGroup#numRadioButtons
*
* @version Egret 2.4
* @version eui 1.0
* @platform Web,Native
* @language zh_CN
*/
public getRadioButtonAt(index:number):RadioButton {
return this.radioButtons[index];
}
/**
* @private
*/
$enabled:boolean = true;
/**
* Determines whether selection is allowed. Note that the value returned
* only reflects the value that was explicitly set on the
* RadioButtonGroup and does not reflect any values explicitly
* set on the individual RadioButtons.
*
* @default true
*
* @version Egret 2.4
* @version eui 1.0
* @platform Web,Native
* @language en_US
*/
/**
* 确定是否允许选择。请注意,返回的值仅反映对 RadioButtonGroup 显式设置的值,
* 而不反映对各个 RadioButton 显式设置的任何值。
*
* @default true
*
* @version Egret 2.4
* @version eui 1.0
* @platform Web,Native
* @language zh_CN
*/
public get enabled():boolean {
return this.$enabled;
}
public set enabled(value:boolean) {
value = !!value;
if (this.$enabled === value)
return;
this.$enabled = value;
let buttons = this.radioButtons;
let length = buttons.length;
for (let i = 0; i < length; i++)
buttons[i].invalidateState();
}
/**
* The number of RadioButtons that belong to this RadioButtonGroup.
*
* @default 0
*
* @version Egret 2.4
* @version eui 1.0
* @platform Web,Native
* @language en_US
*/
/**
* 属于此 RadioButtonGroup 的 RadioButton 数。
*
* @default 0
*
* @version Egret 2.4
* @version eui 1.0
* @platform Web,Native
* @language zh_CN
*/
public get numRadioButtons():number {
return this.radioButtons.length;
}
/**
* @private
*/
private _selectedValue:any = null;
/**
* The value property of the selected
* RadioButton component in the group, if it has been set,
* otherwise, the label property of the selected RadioButton.
* If no RadioButton is selected, this property is null.
*
*
If you set selectedValue, selects the
* first RadioButton component whose value or
* label property matches this value.
value 属性(如果未设置),
* 否则为所选 RadioButton 组件的 label 属性。
* 如果未选择任何 RadioButton,则此属性为 null。
*
* 如果设置了 selectedValue,则会选择 value 或 label 属性
* 与此值匹配的第一个 RadioButton 组件。