office-ui-fabric-react
Version: 
Reusable React components for building experiences for Office 365.
263 lines • 13.2 kB
JavaScript
define(["require", "exports", "tslib", "react", "../../Utilities", "../../utilities/grid/Grid", "./ColorPickerGridCell"], function (require, exports, tslib_1, React, Utilities_1, Grid_1, ColorPickerGridCell_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var getClassNames = Utilities_1.classNamesFunction();
    var SwatchColorPickerBase = /** @class */ (function (_super) {
        tslib_1.__extends(SwatchColorPickerBase, _super);
        function SwatchColorPickerBase(props) {
            var _this = _super.call(this, props) || this;
            _this.navigationIdleDelay = 250 /* ms */;
            /**
             * Sets a timeout so we won't process any mouse "hover" events
             * while navigating (via mouseWheel or arrowKeys)
             */
            _this.setNavigationTimeout = function () {
                if (!_this.isNavigationIdle && _this.navigationIdleTimeoutId !== undefined) {
                    _this.async.clearTimeout(_this.navigationIdleTimeoutId);
                    _this.navigationIdleTimeoutId = undefined;
                }
                else {
                    _this.isNavigationIdle = false;
                }
                _this.navigationIdleTimeoutId = _this.async.setTimeout(function () {
                    _this.isNavigationIdle = true;
                }, _this.navigationIdleDelay);
            };
            _this._id = props.id || Utilities_1.getId('swatchColorPicker');
            _this._warnMutuallyExclusive({
                'focusOnHover': 'onHover'
            });
            _this._warnConditionallyRequiredProps(['focusOnHover'], 'mouseLeaveParentSelector', !!_this.props.mouseLeaveParentSelector);
            _this.isNavigationIdle = true;
            _this.async = new Utilities_1.Async(_this);
            var selectedIndex;
            if (props.selectedId) {
                selectedIndex = _this._getSelectedIndex(props.colorCells, props.selectedId);
            }
            _this.state = {
                selectedIndex: selectedIndex
            };
            return _this;
        }
        SwatchColorPickerBase.prototype.componentWillReceiveProps = function (newProps) {
            var newSelectedIndex;
            if (newProps.selectedId) {
                newSelectedIndex = this._getSelectedIndex(newProps.colorCells, newProps.selectedId);
            }
            if (newSelectedIndex !== this.state.selectedIndex) {
                this.setState({
                    selectedIndex: newSelectedIndex
                });
            }
        };
        SwatchColorPickerBase.prototype.render = function () {
            var _a = this.props, colorCells = _a.colorCells, columnCount = _a.columnCount, positionInSet = _a.positionInSet, setSize = _a.setSize, shouldFocusCircularNavigate = _a.shouldFocusCircularNavigate, className = _a.className, doNotContainWithinFocusZone = _a.doNotContainWithinFocusZone, disabled = _a.disabled, getStyles = _a.getStyles;
            var classNames = getClassNames(getStyles, {
                theme: this.props.theme,
                className: className,
            });
            if (colorCells.length < 1 || columnCount < 1) {
                return null;
            }
            return (React.createElement(Grid_1.Grid, tslib_1.__assign({}, this.props, { items: colorCells.map(function (item, index) { return tslib_1.__assign({}, item, { index: index }); }), columnCount: columnCount, onRenderItem: this._renderOption, positionInSet: positionInSet && positionInSet, setSize: setSize && setSize, shouldFocusCircularNavigate: shouldFocusCircularNavigate, doNotContainWithinFocusZone: doNotContainWithinFocusZone, onBlur: this._onSwatchColorPickerBlur, theme: this.props.theme, 
                // tslint:disable-next-line:jsx-no-lambda
                getStyles: function (props) { return ({
                    root: classNames.root,
                    tableCell: classNames.tableCell,
                    focusedContainer: classNames.focusedContainer
                }); } })));
        };
        /**
         * When the whole swatchColorPicker is blurred,
         * make sure to clear the pending focused stated
         */
        SwatchColorPickerBase.prototype._onSwatchColorPickerBlur = function () {
            if (this.props.onCellFocused) {
                this.props.onCellFocused();
            }
        };
        /**
         * Get the selected item's index
         * @param items - The items to search
         * @param selectedId - The selected item's id to find
         * @returns {number} - The index of the selected item's id, -1 if there was no match
         */
        SwatchColorPickerBase.prototype._getSelectedIndex = function (items, selectedId) {
            var selectedIndex = Utilities_1.findIndex(items, (function (item) { return (item.id === selectedId); }));
            return selectedIndex >= 0 ? selectedIndex : undefined;
        };
        SwatchColorPickerBase.prototype._isSelected = function (index) {
            return this.state.selectedIndex !== undefined && (this.state.selectedIndex === index);
        };
        /**
         * Render a color cell
         * @param item - The item to render
         * @returns {JSX.Element} - Element representing the item
         */
        SwatchColorPickerBase.prototype._renderOption = function (item) {
            var id = this._id;
            return (React.createElement(ColorPickerGridCell_1.ColorPickerGridCell, { item: item, id: id, color: item.color, getStyles: this.props.getColorGridCellStyles, disabled: this.props.disabled, onClick: this._onCellClick, onHover: this._onGridCellHovered, onFocus: this._onGridCellFocused, selected: this.state.selectedIndex !== undefined && (this.state.selectedIndex === item.index), circle: this.props.cellShape === 'circle', label: item.label, onMouseEnter: this._onMouseEnter, onMouseMove: this._onMouseMove, onMouseLeave: this._onMouseLeave, onWheel: this._onWheel, onKeyDown: this._onKeyDown }));
        };
        /**
         * Callback passed to the GridCell that will manage triggering the onCellHovered callback for mouseEnter
         */
        SwatchColorPickerBase.prototype._onMouseEnter = function (ev) {
            if (!this.props.focusOnHover) {
                if (!this.isNavigationIdle || this.props.disabled) {
                    return true;
                }
                return false;
            }
            if (this.isNavigationIdle && !this.props.disabled) {
                ev.currentTarget.focus();
            }
            return true;
        };
        /**
         * Callback passed to the GridCell that will manage Hover/Focus updates
         */
        SwatchColorPickerBase.prototype._onMouseMove = function (ev) {
            if (!this.props.focusOnHover) {
                if (!this.isNavigationIdle || this.props.disabled) {
                    return true;
                }
                return false;
            }
            var targetElement = ev.currentTarget;
            // If navigation is idle and the targetElement is the focused element bail out
            // if (!this.isNavigationIdle || (document && targetElement === (document.activeElement as HTMLElement))) {
            if (this.isNavigationIdle && !(document && targetElement === document.activeElement)) {
                targetElement.focus();
            }
            return true;
        };
        /**
         * Callback passed to the GridCell that will manage Hover/Focus updates
         */
        SwatchColorPickerBase.prototype._onMouseLeave = function (ev) {
            var parentSelector = this.props.mouseLeaveParentSelector;
            if (!this.props.focusOnHover ||
                !parentSelector ||
                !this.isNavigationIdle ||
                this.props.disabled) {
                return;
            }
            // Get the the elements that math the given selector
            var elements = document.querySelectorAll(parentSelector);
            // iterate over the elements return to make sure it is a parent of the target and focus it
            for (var index = 0; index < elements.length; index += 1) {
                if (elements[index].contains(ev.currentTarget)) {
                    /**
                     * IE11 focus() method forces parents to scroll to top of element.
                     * Edge and IE expose a setActive() function for focusable divs that
                     * sets the page focus but does not scroll the parent element.
                     */
                    if (elements[index].setActive) {
                        elements[index].setActive();
                    }
                    else {
                        elements[index].focus();
                    }
                    break;
                }
            }
        };
        /**
         * Callback to make sure we don't update the hovered element during mouse wheel
         */
        SwatchColorPickerBase.prototype._onWheel = function () {
            this.setNavigationTimeout();
        };
        /**
         * Callback that
         */
        SwatchColorPickerBase.prototype._onKeyDown = function (ev) {
            if (ev.which === 38 /* up */ ||
                ev.which === 40 /* down */ ||
                ev.which === 37 /* left */ ||
                ev.which === 39 /* right */) {
                this.setNavigationTimeout();
            }
        };
        /**
         * Callback passed to the GridCell class that will trigger the onCellHovered callback of the SwatchColorPicker
         * NOTE: This will not be triggered if shouldFocusOnHover === true
         */
        SwatchColorPickerBase.prototype._onGridCellHovered = function (item) {
            var onCellHovered = this.props.onCellHovered;
            if (onCellHovered) {
                return item ? onCellHovered(item.id, item.color) : onCellHovered();
            }
        };
        /**
         * Callback passed to the GridCell class that will trigger the onCellFocus callback of the SwatchColorPicker
         */
        SwatchColorPickerBase.prototype._onGridCellFocused = function (item) {
            var onCellFocused = this.props.onCellFocused;
            if (onCellFocused) {
                return item ? onCellFocused(item.id, item.color) : onCellFocused();
            }
        };
        /**
         * Handle the click on a cell
         * @param item - The cell that the click was fired against
         */
        SwatchColorPickerBase.prototype._onCellClick = function (item) {
            if (this.props.disabled) {
                return;
            }
            var index = item.index;
            // If we have a valid index and it is not already
            // selected, select it
            if (index >= 0 && index !== this.state.selectedIndex) {
                if (this.props.onColorChanged) {
                    this.props.onColorChanged(item.id, item.color);
                }
                this.setState({
                    selectedIndex: index
                });
            }
        };
        SwatchColorPickerBase.defaultProps = {
            cellShape: 'circle',
            disabled: false,
            shouldFocusCircularNavigate: true
        };
        tslib_1.__decorate([
            Utilities_1.autobind
        ], SwatchColorPickerBase.prototype, "_onSwatchColorPickerBlur", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], SwatchColorPickerBase.prototype, "_renderOption", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], SwatchColorPickerBase.prototype, "_onMouseEnter", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], SwatchColorPickerBase.prototype, "_onMouseMove", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], SwatchColorPickerBase.prototype, "_onMouseLeave", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], SwatchColorPickerBase.prototype, "_onWheel", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], SwatchColorPickerBase.prototype, "_onKeyDown", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], SwatchColorPickerBase.prototype, "_onGridCellHovered", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], SwatchColorPickerBase.prototype, "_onGridCellFocused", null);
        tslib_1.__decorate([
            Utilities_1.autobind
        ], SwatchColorPickerBase.prototype, "_onCellClick", null);
        SwatchColorPickerBase = tslib_1.__decorate([
            Utilities_1.customizable('SwatchColorPicker', ['theme'])
        ], SwatchColorPickerBase);
        return SwatchColorPickerBase;
    }(Utilities_1.BaseComponent));
    exports.SwatchColorPickerBase = SwatchColorPickerBase;
});
//# sourceMappingURL=SwatchColorPicker.base.js.map