Source: controls/GlobalControlManager.js

/**
 * @requirefiles {Iterator.js}
 */

/**
 * An array of controls with global control management functions.
 * @class GlobalControlManagerControlsCollection
 */

/**
 * Creates an instance of the GlobalControlManager.
 * @constructor
 */
function GlobalControlManager() {
    // noinspection JSValidateTypes
    /**
     * List of all managed controls.
     * @name GlobalControlManager#Controls
     * @type {GlobalControlManagerControlsCollection}
     */
    this.Controls = [];
    /**
     * Activates a control.
     * @name GlobalControlManagerControlsCollection#Activate
     * @param {Control} control The control to be activated.
     * @param {boolean} [delayRender=false] Whether or not to delay rendering for bulk operations.
     * @function
     */
    this.Controls.Activate = function (control, delayRender) {
        if (arguments.length < 2) { delayRender = false; }
        if (window.ControlGlobal.Controls[window.ControlGlobal.Controls.length - 1] !== control) {
            if ((window.ControlGlobal.Controls[window.ControlGlobal.Controls.length - 1].Deactivate !== undefined) && (window.ControlGlobal.Controls[window.ControlGlobal.Controls.length - 1].Deactivate !== null)) { window.ControlGlobal.Controls[window.ControlGlobal.Controls.length - 1].Deactivate(); }
        }
        window.ControlGlobal.Controls.RemoveByID(control.ControlUniqueID);
        window.ControlGlobal.Controls.push(control);
        /** @type {boolean|Control} */
        let delayActivate = false;
        if ((control.ParentControl !== undefined) && (control.ParentControl !== null) && (control.ParentControl.Controls !== undefined) && (control.ParentControl.Controls != null)) {
            if (control.CanGetFocus === true) {
                control.ParentControl.Controls.Focused = control;
            } else if ((control.ForFocus !== undefined) && (control.ForFocus !== null)) {
                delayActivate = control.ForFocus;
            } else if (control.ParentControl.Controls.Focused == null) {
                let i = control.ParentControl.Controls.indexOf(control);
                let hit = false;
                if (i < (control.ParentControl.Controls.length - 1)) {
                    for (let j = i + 1; j < control.ParentControl.Controls.length; j++) {
                        if (control.ParentControl.Controls[j].CanGetFocus === true) {
                            control.ParentControl.Controls.Focused = control.ParentControl.Controls[j];
                            hit = true;
                            break;
                        }
                    }
                }
                if (hit !== true) {
                    for (let j = 0; j < i; j++) {
                        if (control.ParentControl.Controls[j].CanGetFocus === true) {
                            control.ParentControl.Controls.Focused = control.ParentControl.Controls[j];
                            hit = true;
                            break;
                        }
                    }
                    if (hit !== true) {
                        control.ParentControl.Controls.Focused = control;
                    }
                }
            } else {
                delayActivate = control.ParentControl.Controls.Focused;
            }
        }
        if (delayActivate === false) {
            if ((control.Activate !== undefined) && (control.Activate !== null)) { control.Activate(); }
            if (delayRender === false) { this.Render(); }
        } else {
            window.ControlGlobal.Controls.Activate(delayActivate, delayRender);
        }
    };
    /**
     * Deactivates a control.
     * @name GlobalControlManagerControlsCollection#Deactivate
     * @param {Control} control The control to be deactivated.
     * @param {boolean} [delayRender=false] Whether or not to delay rendering for bulk operations.
     * @function
     */
    this.Controls.Deactivate = function (control, delayRender) {
        if (arguments.length < 2) { delayRender = false; }
        if ((control.Deactivate !== undefined) && (control.Deactivate !== null)) { control.Deactivate(); }
        window.ControlGlobal.Controls.RemoveByID(control.ControlUniqueID);
        window.ControlGlobal.Controls.unshift(control);
        if (delayRender === false) { this.Render(); }
    };
    /**
     * Removes a control from the collection.
     * @name GlobalControlManagerControlsCollection#RemoveByID
     * @param {number|Control} id The ID of the control to be removed.
     * @function
     */
    this.Controls.RemoveByID = function (id) {
        if (id instanceof Control) { id = id.ControlUniqueID; }
        for (let i = this.length - 1; i >= 0; i--) {
            if (this[i].ControlUniqueID === id) {
                this.splice(i, 1);
            }
        }
    };
    /**
     * Loops through all controls in the collection setting focus and blur as appropriate.
     * @name GlobalControlManagerControlsCollection#Render
     * @function
     */
    this.Controls.Render = function () {
        for (let i = 0; i < this.length; i++) {
            if (i === (this.length - 1)) {
                this[i].Focus(true);
            } else {
                this[i].Blur();
            }
        }
    };
    /**
     * Flag to skip next contextmenu event.
     * @name GlobalControlManager#DirtyContextMenu
     * @type {boolean}
     */
    this.DirtyContextMenu = false;
    /**
     * Flag to skip next mousedown event.
     * @name GlobalControlManager#DirtyMouseDown
     * @type {boolean}
     */
    this.DirtyMouseDown = false;
    /**
     * Control ID iterator.
     * @name GlobalControlManager#UniqueID
     * @type {Iterator}
     */
    this.Iterator = new Iterator();
}
GlobalControlManager.prototype.constructor = GlobalControlManager;

/**
 * Global control management object.
 * @type {GlobalControlManager}
 * @global
 */
window.ControlGlobal = new GlobalControlManager();