all files / lib/components/FocusZone/ FocusZone.js

68.06% Statements 179/263
54.87% Branches 107/195
74.07% Functions 20/27
68.46% Lines 178/260
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399                                      25× 25× 25× 25×                       25×                                                     28× 28×                   28×                                               20× 20×                                                               20× 20× 20× 20× 20× 20×     20× 20× 28×     28× 14× 12× 12× 12×   12×         14× 14×       20× 16× 16×                 20×                                                   20× 20× 20×   20× 20× 20×     20× 20× 20×         41×               25× 25×           25×                                                                            
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var React = require('react');
var FocusZone_Props_1 = require('./FocusZone.Props');
var EventGroup_1 = require('../../utilities/eventGroup/EventGroup');
var KeyCodes_1 = require('../../utilities/KeyCodes');
var rtl_1 = require('../../utilities/rtl');
var css_1 = require('../../utilities/css');
var focus_1 = require('../../utilities/focus');
var IS_FOCUSABLE_ATTRIBUTE = 'data-is-focusable';
var IS_ENTER_DISABLED_ATTRIBUTE = 'data-disable-click-on-enter';
var FOCUSZONE_ID_ATTRIBUTE = 'data-focuszone-id';
var TABINDEX = 'tabindex';
var _instance = 0;
var _allInstances = {};
var FocusZone = (function (_super) {
    __extends(FocusZone, _super);
    function FocusZone(props) {
        _super.call(this, props);
        this._id = String(_instance++);
        _allInstances[this._id] = this;
        this._focusAlignment = {
            left: 0,
            top: 0
        };
        this._events = new EventGroup_1.EventGroup(this);
        this._onKeyDown = this._onKeyDown.bind(this);
        this._onFocus = this._onFocus.bind(this);
        this._onMouseDown = this._onMouseDown.bind(this);
    }
    FocusZone.prototype.componentDidMount = function () {
        var windowElement = this.refs.root.ownerDocument.defaultView;
        var parentElement = this.refs.root.parentElement;
        while (parentElement && parentElement !== document.body) {
            Iif (focus_1.isElementFocusZone(parentElement)) {
                this._isInnerZone = true;
                break;
            }
            parentElement = parentElement.parentElement;
        }
        this._events.on(windowElement, 'keydown', this._onKeyDownCapture, true);
    };
    FocusZone.prototype.componentWillUnmount = function () {
        this._events.dispose();
        delete _allInstances[this._id];
    };
    FocusZone.prototype.render = function () {
        var _a = this.props, className = _a.className, ariaLabelledBy = _a.ariaLabelledBy;
        return (React.createElement("div", React.__spread({}, this.props, {className: css_1.css('ms-FocusZone', className), ref: 'root', "data-focuszone-id": this._id, "aria-labelledby": ariaLabelledBy, onMouseDownCapture: this._onMouseDown, onKeyDown: this._onKeyDown, onFocus: this._onFocus}), this.props.children));
    };
    /** Sets focus to the first tabbable item in the zone. */
    FocusZone.prototype.focus = function () {
        var firstChild = this.refs.root.firstChild;
        return this._focusElement(focus_1.getNextElement(this.refs.root, firstChild, true));
    };
    FocusZone.prototype._onFocus = function (ev) {
        var onActiveElementChanged = this.props.onActiveElementChanged;
        Eif (this._isImmediateDescendantOfZone(ev.target)) {
            this._activeElement = ev.target;
            this._setFocusAlignment(this._activeElement);
        }
        else {
            var parentElement = ev.target;
            while (parentElement && parentElement !== this.refs.root) {
                if (focus_1.isElementTabbable(parentElement) && this._isImmediateDescendantOfZone(parentElement)) {
                    this._activeElement = parentElement;
                    break;
                }
                parentElement = parentElement.parentElement;
            }
        }
        Iif (onActiveElementChanged) {
            onActiveElementChanged(this._activeElement, ev);
        }
    };
    /** Handle global tab presses so that we can patch tabindexes on the fly. */
    FocusZone.prototype._onKeyDownCapture = function (ev) {
        if (ev.which === KeyCodes_1.KeyCodes.tab) {
            this._updateTabIndexes();
        }
    };
    FocusZone.prototype._onMouseDown = function (ev) {
        var target = ev.target;
        var path = [];
        while (target && target !== this.refs.root) {
            path.push(target);
            target = target.parentElement;
        }
        while (path.length) {
            target = path.pop();
            if (focus_1.isElementFocusZone(target)) {
                break;
            }
            else if (target && focus_1.isElementTabbable(target)) {
                target.tabIndex = 0;
                this._setFocusAlignment(target, true, true);
            }
        }
    };
    /** Handle the keystrokes. */
    FocusZone.prototype._onKeyDown = function (ev) {
        var _a = this.props, direction = _a.direction, isInnerZoneKeystroke = _a.isInnerZoneKeystroke;
        Iif (isInnerZoneKeystroke &&
            this._isImmediateDescendantOfZone(ev.target) &&
            isInnerZoneKeystroke(ev)) {
            // Try to focus
            var innerZone = this._getFirstInnerZone();
            if (!innerZone || !innerZone.focus()) {
                return;
            }
        }
        else {
            switch (ev.which) {
                case KeyCodes_1.KeyCodes.left:
                    if (direction !== FocusZone_Props_1.FocusZoneDirection.vertical && this._moveFocusLeft()) {
                        break;
                    }
                    return;
                case KeyCodes_1.KeyCodes.right:
                    if (direction !== FocusZone_Props_1.FocusZoneDirection.vertical && this._moveFocusRight()) {
                        break;
                    }
                    return;
                case KeyCodes_1.KeyCodes.up:
                    if (direction !== FocusZone_Props_1.FocusZoneDirection.horizontal && this._moveFocusUp()) {
                        break;
                    }
                    return;
                case KeyCodes_1.KeyCodes.down:
                    if (direction !== FocusZone_Props_1.FocusZoneDirection.horizontal && this._moveFocusDown()) {
                        break;
                    }
                    return;
                case KeyCodes_1.KeyCodes.home:
                    var firstChild = this.refs.root.firstChild;
                    Eif (this._focusElement(focus_1.getNextElement(this.refs.root, firstChild, true))) {
                        break;
                    }
                    return;
                case KeyCodes_1.KeyCodes.end:
                    var lastChild = this.refs.root.lastChild;
                    Eif (this._focusElement(focus_1.getPreviousElement(this.refs.root, lastChild, true, true, true))) {
                        break;
                    }
                    return;
                case KeyCodes_1.KeyCodes.enter:
                    if (this._tryInvokeClickForFocusable(ev.target)) {
                        break;
                    }
                    return;
                default:
                    return;
            }
        }
        ev.preventDefault();
        ev.stopPropagation();
    };
    /** Walk up the dom try to find a focusable element. */
    FocusZone.prototype._tryInvokeClickForFocusable = function (target) {
        do {
            if (target.tagName === 'BUTTON' || target.tagName === 'A') {
                return false;
            }
            if (this._isImmediateDescendantOfZone(target) &&
                target.getAttribute(IS_FOCUSABLE_ATTRIBUTE) === 'true' &&
                target.getAttribute(IS_ENTER_DISABLED_ATTRIBUTE) !== 'true') {
                EventGroup_1.EventGroup.raise(target, 'click', null, true);
                return true;
            }
            target = target.parentElement;
        } while (target !== this.refs.root);
        return false;
    };
    /** Traverse to find first child zone. */
    FocusZone.prototype._getFirstInnerZone = function (rootElement) {
        rootElement = rootElement || this._activeElement || this.refs.root;
        var child = rootElement.firstElementChild;
        while (child) {
            if (focus_1.isElementFocusZone(child)) {
                return _allInstances[child.getAttribute(FOCUSZONE_ID_ATTRIBUTE)];
            }
            var match = this._getFirstInnerZone(child);
            if (match) {
                return match;
            }
            child = child.nextElementSibling;
        }
        return null;
    };
    FocusZone.prototype._moveFocus = function (isForward, getDistanceFromCenter, ev) {
        var element = this._activeElement;
        var candidateDistance = -1;
        var candidateElement;
        var changedFocus = false;
        var isBidirectional = this.props.direction === FocusZone_Props_1.FocusZoneDirection.bidirectional;
        Iif (!this._activeElement) {
            return;
        }
        var activeRect = isBidirectional ? this._activeElement.getBoundingClientRect() : null;
        do {
            element = isForward ?
                focus_1.getNextElement(this.refs.root, element) :
                focus_1.getPreviousElement(this.refs.root, element);
            if (isBidirectional) {
                if (element) {
                    var targetRect = element.getBoundingClientRect();
                    var elementDistance = getDistanceFromCenter(activeRect, targetRect);
                    if (elementDistance > -1 && (candidateDistance === -1 || elementDistance < candidateDistance)) {
                        candidateDistance = elementDistance;
                        candidateElement = element;
                    }
                    if (candidateDistance >= 0 && elementDistance < 0) {
                        break;
                    }
                }
            }
            else {
                candidateElement = element;
                break;
            }
        } while (element);
        // Focus the closest candidate
        if (candidateElement && candidateElement !== this._activeElement) {
            changedFocus = true;
            this._focusElement(candidateElement);
        }
        else Iif (this.props.isCircularNavigation) {
            if (isForward) {
                return this._focusElement(focus_1.getNextElement(this.refs.root, this.refs.root.firstElementChild, true));
            }
            else {
                return this._focusElement(focus_1.getPreviousElement(this.refs.root, this.refs.root.lastElementChild, true, true, true));
            }
        }
        return changedFocus;
    };
    FocusZone.prototype._moveFocusDown = function () {
        var targetTop = -1;
        var leftAlignment = this._focusAlignment.left;
        if (this._moveFocus(true, function (activeRect, targetRect) {
            var distance = -1;
            if ((targetTop === -1 && targetRect.top >= activeRect.bottom) ||
                (targetRect.top === targetTop)) {
                targetTop = targetRect.top;
                distance = Math.abs((targetRect.left + (targetRect.width / 2)) - leftAlignment);
            }
            return distance;
        })) {
            this._setFocusAlignment(this._activeElement, false, true);
            return true;
        }
        return false;
    };
    FocusZone.prototype._moveFocusUp = function () {
        var targetTop = -1;
        var leftAlignment = this._focusAlignment.left;
        if (this._moveFocus(false, function (activeRect, targetRect) {
            var distance = -1;
            if ((targetTop === -1 && targetRect.bottom <= activeRect.top) ||
                (targetRect.top === targetTop)) {
                targetTop = targetRect.top;
                distance = Math.abs((targetRect.left + (targetRect.width / 2)) - leftAlignment);
            }
            return distance;
        })) {
            this._setFocusAlignment(this._activeElement, false, true);
            return true;
        }
        return false;
    };
    FocusZone.prototype._moveFocusLeft = function () {
        var _this = this;
        var targetTop = -1;
        var topAlignment = this._focusAlignment.top;
        if (this._moveFocus(rtl_1.getRTL(), function (activeRect, targetRect) {
            var distance = -1;
            if ((targetTop === -1 &&
                targetRect.right <= activeRect.right &&
                (_this.props.direction === FocusZone_Props_1.FocusZoneDirection.horizontal || targetRect.top === activeRect.top)) ||
                (targetRect.top === targetTop)) {
                targetTop = targetRect.top;
                distance = Math.abs((targetRect.top + (targetRect.height / 2)) - topAlignment);
            }
            return distance;
        })) {
            this._setFocusAlignment(this._activeElement, true, false);
            return true;
        }
        return false;
    };
    FocusZone.prototype._moveFocusRight = function () {
        var _this = this;
        var targetTop = -1;
        var topAlignment = this._focusAlignment.top;
        if (this._moveFocus(!rtl_1.getRTL(), function (activeRect, targetRect) {
            var distance = -1;
            if ((targetTop === -1 &&
                targetRect.left >= activeRect.left &&
                (_this.props.direction === FocusZone_Props_1.FocusZoneDirection.horizontal || targetRect.top === activeRect.top)) ||
                (targetRect.top === targetTop)) {
                targetTop = targetRect.top;
                distance = Math.abs((targetRect.top + (targetRect.height / 2)) - topAlignment);
            }
            return distance;
        })) {
            this._setFocusAlignment(this._activeElement, true, false);
            return true;
        }
        return false;
    };
    FocusZone.prototype._focusElement = function (element) {
        Eif (element) {
            Eif (this._activeElement) {
                this._activeElement.tabIndex = -1;
            }
            this._activeElement = element;
            Eif (element) {
                Iif (!this._focusAlignment) {
                    this._setFocusAlignment(element, true, true);
                }
                this._activeElement.tabIndex = 0;
                element.focus();
                return true;
            }
        }
        return false;
    };
    FocusZone.prototype._setFocusAlignment = function (element, isHorizontal, isVertical) {
        if (this.props.direction === FocusZone_Props_1.FocusZoneDirection.bidirectional &&
            (!this._focusAlignment || isHorizontal || isVertical)) {
            var rect = element.getBoundingClientRect();
            var left = rect.left + (rect.width / 2);
            var top_1 = rect.top + (rect.height / 2);
            Iif (!this._focusAlignment) {
                this._focusAlignment = { left: left, top: top_1 };
            }
            if (isHorizontal) {
                this._focusAlignment.left = left;
            }
            if (isVertical) {
                this._focusAlignment.top = top_1;
            }
        }
    };
    FocusZone.prototype._isImmediateDescendantOfZone = function (element) {
        var parentElement = element.parentElement;
        while (parentElement && parentElement !== this.refs.root && parentElement !== document.body) {
            if (focus_1.isElementFocusZone(parentElement)) {
                return false;
            }
            parentElement = parentElement.parentElement;
        }
        return true;
    };
    FocusZone.prototype._updateTabIndexes = function (element) {
        if (!element) {
            element = this.refs.root;
            if (this._activeElement && !element.contains(this._activeElement)) {
                this._activeElement = null;
            }
        }
        var childNodes = element.children;
        for (var childIndex = 0; childNodes && childIndex < childNodes.length; childIndex++) {
            var child = childNodes[childIndex];
            if (!focus_1.isElementFocusZone(child)) {
                if (focus_1.isElementTabbable(child)) {
                    if (!this._isInnerZone && (!this._activeElement || this._activeElement === child)) {
                        this._activeElement = child;
                        if (child.getAttribute(TABINDEX) !== '0') {
                            child.setAttribute(TABINDEX, '0');
                        }
                    }
                    else if (child.getAttribute(TABINDEX) !== '-1') {
                        child.setAttribute(TABINDEX, '-1');
                    }
                }
                else if (child.tagName === 'svg' && child.getAttribute('focusable') !== 'false') {
                    // Disgusting IE hack. Sad face.
                    child.setAttribute('focusable', 'false');
                }
                this._updateTabIndexes(child);
            }
        }
    };
    FocusZone.defaultProps = {
        isCircularNavigation: false,
        direction: FocusZone_Props_1.FocusZoneDirection.bidirectional
    };
    return FocusZone;
}(React.Component));
exports.FocusZone = FocusZone;
 
//# sourceMappingURL=FocusZone.js.map