let register = Toolkit => Toolkit.RadioGroup = // Radio button group manager class RadioGroup { ///////////////////////// Initialization Methods ////////////////////////// constructor() { this._active = null; this.items = []; } ///////////////////////////// Public Methods ////////////////////////////// // The current active radio button get active() { return this._active; } set active(item) { // Error checking if ( item == this.active || item != null && this.items.indexOf(item) == -1 ) return; // De-select the current active item if (this.active != null) { this.active.checked = false; this.active.element.setAttribute("tabindex", "-1"); } // Select the new active item this._active = item ? item : null; if (item == null) return; if (!item.checked) item.checked = true; item.element.setAttribute("tabindex", "0"); } // Add a radio button item add(item) { // Error checking if ( item == null || this.items.indexOf(item) != -1 ) return; // Remove the item from its current group if (item.group != null) item.group.remove(item); // Add the item to this group this.items.push(item); item.group = this; item.element.setAttribute("tabindex", this.items.length == 0 ? "0" : "-1"); return item; } // Check whether an element is contained in the radio group contains(element) { if (element instanceof Toolkit.Component) element = element.element; return !!this.items.find(i=>i.element.contains(element)); } // Remove a radio button item remove(item) { let index = this.items.indexOf(item); // The item is not in the group if (index == -1) return null; // Remove the item from the group this.items.splice(index, 1); item.element.setAttribute("tabindex", "0"); if (this.active == item) { this.active = null; if (this.items.length != 0) this.items[0].element.setAttribute("tabindex", "0"); } return item; } ///////////////////////////// Package Methods ///////////////////////////// // Determine the next radio button in the group next(item) { let index = this.items.indexOf(item); return index == -1 ? null : this.items[(index + 1) % this.items.length]; } // Determine the previous radio button in the group previous(item) { let index = this.items.indexOf(item); return index == -1 ? null : this.items[(index + this.items.length - 1) % this.items.length]; } } export { register };