office-ui-fabric-react
Version:
Reusable React components for building experiences for Office 365.
229 lines • 12.2 kB
JavaScript
import * as tslib_1 from "tslib";
import * as React from 'react';
import { autobind, BaseComponent, css, divProperties, getNativeProps, getRTL } from '../../Utilities';
import { FocusZone, FocusZoneDirection } from '../../FocusZone';
import { ActionButton } from '../../Button';
import { Icon } from '../../Icon';
import * as stylesImport from './Nav.scss';
var styles = stylesImport;
import { AnimationClassNames, mergeStyles } from '../../Styling';
// The number pixels per indentation level for Nav links.
var _indentationSize = 14;
// The number of pixels of left margin
var _baseIndent = 3;
// The number of pixels of padding to add to the far side of the button (allows ellipsis to happen)
var _farSidePadding = 20;
// global var used in _isLinkSelectedKey
var _urlResolver;
export function isRelativeUrl(url) {
// A URL is relative if it has no protocol.
return !!url && !/^[a-z0-9+-.]:\/\//i.test(url);
}
var NavBase = /** @class */ (function (_super) {
tslib_1.__extends(NavBase, _super);
function NavBase(props) {
var _this = _super.call(this, props) || this;
_this.state = {
isGroupCollapsed: {},
isLinkExpandStateChanged: false,
selectedKey: props.initialSelectedKey || props.selectedKey,
};
if (props.groups) {
for (var _i = 0, _a = props.groups; _i < _a.length; _i++) {
var group = _a[_i];
if (group.collapseByDefault && group.name) {
_this.state.isGroupCollapsed[group.name] = true;
}
}
}
_this._hasExpandButton = false;
return _this;
}
NavBase.prototype.componentWillReceiveProps = function (newProps) {
var newGroups = newProps.groups || [];
var isGroupCollapsed = this.state.isGroupCollapsed;
// If the component's props were updated, new groups may have been added, which may have
// collapseByDefault set. Ensure that setting is respected for any new groups.
// (If isGroupCollapsed is already set for a group, don't overwrite that.)
var hasUpdated = false;
for (var _i = 0, newGroups_1 = newGroups; _i < newGroups_1.length; _i++) {
var newGroup = newGroups_1[_i];
if (newGroup.name && newGroup.collapseByDefault && !isGroupCollapsed.hasOwnProperty(newGroup.name)) {
isGroupCollapsed[newGroup.name] = true;
hasUpdated = true;
}
}
if (hasUpdated) {
this.setState({
isGroupCollapsed: isGroupCollapsed
});
}
};
NavBase.prototype.render = function () {
var _a = this.props, groups = _a.groups, className = _a.className, isOnTop = _a.isOnTop;
if (!groups) {
return null;
}
// When groups[x].name is specified or any of the links have children, the expand/collapse
// chevron button is shown and different padding is needed. _hasExpandButton marks this condition.
this._hasExpandButton = groups.some(function (group) {
return group ? !!group.name || (group.links && group.links.some(function (link) {
return !!(link && link.links && link.links.length);
})) : false;
});
var groupElements = groups.map(this._renderGroup);
return (React.createElement(FocusZone, { direction: FocusZoneDirection.vertical },
React.createElement("nav", { role: 'navigation', className: css('ms-Nav', styles.root, className, isOnTop && css('is-onTop', styles.rootIsOnTop, AnimationClassNames.slideRightIn40)) }, groupElements)));
};
Object.defineProperty(NavBase.prototype, "selectedKey", {
get: function () {
return this.state.selectedKey;
},
enumerable: true,
configurable: true
});
NavBase.prototype._onRenderLink = function (link) {
return (React.createElement("div", { className: css('ms-Nav-linkText', styles.linkText) }, link.name));
};
NavBase.prototype._renderNavLink = function (link, linkIndex, nestingLevel) {
var isRtl = getRTL();
var paddingBefore = _indentationSize * nestingLevel + _baseIndent;
var buttonStyles = {
root: (_a = {},
_a[isRtl ? 'paddingRight' : 'paddingLeft'] = paddingBefore,
_a[isRtl ? 'paddingLeft' : 'paddingRight'] = _farSidePadding,
_a),
textContainer: {
overflow: 'hidden',
},
label: {
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
overflow: 'hidden',
lineHeight: '36px'
}
};
var _b = this.props.onRenderLink, onRenderLink = _b === void 0 ? this._onRenderLink : _b;
// Prevent hijacking of the parent window if link.target is defined
var rel = link.url && link.target && !isRelativeUrl(link.url) ? 'noopener noreferrer' : undefined;
return (React.createElement(ActionButton, { className: mergeStyles('ms-Nav-link' + link.onClick && 'ms-Nav-linkButton', styles.link, link.onClick && !link.forceAnchor && styles.buttonEntry, this._hasExpandButton && 'isOnExpanded'), styles: buttonStyles, href: link.url || (link.forceAnchor ? 'javascript:' : undefined), iconProps: { iconName: link.icon || '' }, description: link.title || link.name, onClick: link.onClick ? this._onNavButtonLinkClicked.bind(this, link) : this._onNavAnchorLinkClicked.bind(this, link), title: link.title || link.name, target: link.target, rel: rel, "aria-label": link.ariaLabel }, onRenderLink(link, this._onRenderLink)));
var _a;
};
NavBase.prototype._renderCompositeLink = function (link, linkIndex, nestingLevel) {
var isLinkSelected = this._isLinkSelected(link);
var isRtl = getRTL();
var absolutePositionString = _indentationSize * nestingLevel + 1 + "px";
return (React.createElement("div", tslib_1.__assign({}, getNativeProps(link, divProperties), { key: link.key || linkIndex, className: css('ms-Nav-compositeLink', styles.compositeLink, !!link.isExpanded && 'is-expanded', isLinkSelected && 'is-selected', !!link.isExpanded && styles.compositeLinkIsExpanded, isLinkSelected && styles.compositeLinkIsSelected) }),
(link.links && link.links.length > 0 ?
React.createElement("button", { className: mergeStyles('ms-Nav-chevronButton ms-Nav-chevronButton--link', styles.chevronButton, styles.chevronButtonLink, isRtl && {
right: absolutePositionString,
}, !isRtl && {
left: absolutePositionString,
}), onClick: this._onLinkExpandClicked.bind(this, link), "aria-label": this.props.expandButtonAriaLabel, "aria-expanded": link.isExpanded ? 'true' : 'false' },
React.createElement(Icon, { className: css('ms-Nav-chevron', styles.chevronIcon, link.isExpanded), iconName: 'ChevronDown' })) : null),
this._renderNavLink(link, linkIndex, nestingLevel)));
};
NavBase.prototype._renderLink = function (link, linkIndex, nestingLevel) {
return (React.createElement("li", { key: link.key || linkIndex, role: 'listitem', className: css(styles.navItem) },
this._renderCompositeLink(link, linkIndex, nestingLevel),
(link.isExpanded ? this._renderLinks(link.links, ++nestingLevel) : null)));
};
NavBase.prototype._renderLinks = function (links, nestingLevel) {
var _this = this;
if (!links || !links.length) {
return null;
}
var linkElements = links.map(function (link, linkIndex) { return _this._renderLink(link, linkIndex, nestingLevel); });
return (React.createElement("ul", { role: 'list', "aria-label": this.props.ariaLabel, className: css(styles.navItems) }, linkElements));
};
NavBase.prototype._renderGroup = function (group, groupIndex) {
var isGroupExpanded = !this.state.isGroupCollapsed[group.name];
return (React.createElement("div", { key: groupIndex, className: css('ms-Nav-group', styles.group, isGroupExpanded && ('is-expanded ' + styles.groupIsExpanded)) },
(group.name ?
React.createElement("button", { className: css('ms-Nav-chevronButton ms-Nav-chevronButton--group ms-Nav-groupHeaderFontSize', styles.chevronButton, styles.chevronButtonIsGroup, styles.groupHeaderFontSize), onClick: this._onGroupHeaderClicked.bind(this, group) },
React.createElement(Icon, { className: css('ms-Nav-chevron', styles.chevronIcon, isGroupExpanded && styles.chevronIsExpanded), iconName: 'ChevronDown' }),
group.name) : null),
React.createElement("div", { className: css('ms-Nav-groupContent', AnimationClassNames.slideDownIn20, styles.groupContent) }, this._renderLinks(group.links, 0 /* nestingLevel */))));
};
NavBase.prototype._onGroupHeaderClicked = function (group, ev) {
var isGroupCollapsed = this.state.isGroupCollapsed;
var groupKey = group.name;
var isCollapsed = !isGroupCollapsed[groupKey];
if (group.onHeaderClick) {
group.onHeaderClick(ev, isCollapsed);
}
isGroupCollapsed[groupKey] = isCollapsed;
this.setState({ isGroupCollapsed: isGroupCollapsed });
ev.preventDefault();
ev.stopPropagation();
};
NavBase.prototype._onLinkExpandClicked = function (link, ev) {
var onLinkExpandClick = this.props.onLinkExpandClick;
if (onLinkExpandClick) {
onLinkExpandClick(ev, link);
}
if (!ev.defaultPrevented) {
link.isExpanded = !link.isExpanded;
this.setState({ isLinkExpandStateChanged: true });
}
ev.preventDefault();
ev.stopPropagation();
};
NavBase.prototype._onNavAnchorLinkClicked = function (link, ev) {
if (this.props.onLinkClick) {
this.props.onLinkClick(ev, link);
}
this.setState({ selectedKey: link.key });
};
NavBase.prototype._onNavButtonLinkClicked = function (link, ev) {
if (link.onClick) {
link.onClick(ev, link);
}
this.setState({ selectedKey: link.key });
};
NavBase.prototype._isLinkSelected = function (link) {
// if caller passes in selectedKey, use it as first choice or
// if current state.selectedKey (from addressbar) is match to the link
if (this.props.selectedKey !== undefined) {
return link.key === this.props.selectedKey;
}
else if (this.state.selectedKey !== undefined && link.key === this.state.selectedKey) {
return true;
}
// resolve is not supported for ssr
if (typeof (window) === 'undefined') {
return false;
}
if (!link.url) {
return false;
}
_urlResolver = _urlResolver || document.createElement('a');
_urlResolver.href = link.url || '';
var target = _urlResolver.href;
if (location.href === target) {
return true;
}
if (location.protocol + '//' + location.host + location.pathname === target) {
return true;
}
if (location.hash) {
// Match the hash to the url.
if (location.hash === link.url) {
return true;
}
// Match a rebased url. (e.g. #foo becomes http://hostname/foo)
_urlResolver.href = location.hash.substring(1);
return _urlResolver.href === target;
}
return false;
};
NavBase.defaultProps = {
groups: null
};
tslib_1.__decorate([
autobind
], NavBase.prototype, "_renderGroup", null);
return NavBase;
}(BaseComponent));
export { NavBase };
//# sourceMappingURL=Nav.base.js.map