UNPKG

@attivio/suit

Version:

Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.

239 lines (205 loc) 7.42 kB
'use strict'; exports.__esModule = true; var _class, _temp; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _reactRouterDom = require('react-router-dom'); var _Configurable = require('./Configurable'); var _Configurable2 = _interopRequireDefault(_Configurable); var _MastheadUser = require('./MastheadUser'); var _MastheadUser2 = _interopRequireDefault(_MastheadUser); var _AuthUtils = require('../util/AuthUtils'); var _AuthUtils2 = _interopRequireDefault(_AuthUtils); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * Display a masthead header at the top of your page. It displays a logo, * the name of the application, and the currently logged-in user. It can * contain arbitrary components, but components particularly suited for * being in masthead have names that start with "Masthead," such as * MastheadNavBar and MastheadNavTabs. */ var Masthead = (_temp = _class = function (_React$Component) { _inherits(Masthead, _React$Component); function Masthead(props) { _classCallCheck(this, Masthead); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.state = { userInfo: null }; _this.navigateHome = _this.navigateHome.bind(_this); _this.updateUser = _this.updateUser.bind(_this); return _this; } Masthead.prototype.componentDidMount = function componentDidMount() { this.updateUser(); }; Masthead.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { if (this.props.username !== nextProps.username) { this.updateUser(); } }; Masthead.prototype.updateUser = function updateUser() { var _this2 = this; if (this.props.username) { this.setState({ userInfo: { userId: this.props.username } }); } else { var currentUser = _AuthUtils2.default.getSavedUser(); if (currentUser) { this.setState({ userInfo: currentUser }); } else { // If we need to, ask the server... _AuthUtils2.default.getLoggedInUserInfo(function (loggedInUserInfo) { _this2.setState({ userInfo: loggedInUserInfo }); }); } } }; Masthead.prototype.navigateHome = function navigateHome() { if (this.homeLink) { this.homeLink.blur(); } if (this.context && this.context.searcher) { this.context.searcher.reset(); } this.props.history.push({ pathname: this.props.homeRoute, search: this.props.location.search }); }; Masthead.prototype.renderButton = function renderButton() { var _this3 = this; var engineInfo = null; if (this.props.searchEngineType === 'solr') { engineInfo = _react2.default.createElement( 'span', { style: { display: 'inline-block', float: 'right', fontSize: '0.6875em', fontWeight: 100, color: '#fff', width: '100%', textAlign: 'right', padding: 0 } }, 'for Apache Solr' ); } else if (this.props.searchEngineType === 'elastic') { engineInfo = _react2.default.createElement( 'span', { style: { display: 'inline-block', float: 'right', fontSize: '0.6875em', fontWeight: 100, color: '#fff', width: '100%', textAlign: 'right', padding: 0 } }, 'for Elasticsearch' ); } var logo = _react2.default.createElement('img', { src: this.props.logoUri, alt: this.props.logoAlt, className: 'attivio-globalmast-logo-img' }); if (this.props.homeUrl) { return _react2.default.createElement( 'a', { style: { backgroundColor: 'transparent', borderWidth: 0, textDecoration: 'none' }, role: 'button', href: this.props.homeUrl, className: 'attivio-globalmast-logo attivio-globalmast-separator after' }, logo, engineInfo ); } return _react2.default.createElement( 'button', { style: { backgroundColor: 'transparent', borderWidth: 0 }, onClick: this.navigateHome, className: 'attivio-globalmast-logo attivio-globalmast-separator after', ref: function ref(c) { _this3.homeLink = c; } }, logo, engineInfo ); }; Masthead.prototype.render = function render() { var logoutFunction = this.props.logoutFunction; if (this.state.userInfo) { if (this.state.userInfo.saml) { // If the user is logged in via SAML, diable logging out. logoutFunction = null; } } else { // If there's no user logged in, then disable logging out. logoutFunction = null; } return _react2.default.createElement( 'header', { className: 'attivio-globalmast attivio-minwidth' }, _react2.default.createElement( 'div', { className: 'attivio-container' }, this.renderButton(), _react2.default.createElement( 'div', { className: 'attivio-globalmast-appname attivio-globalmast-separator after ' + (this.props.multiline ? '' : 'nowrap') }, this.props.applicationName ), this.props.children, _react2.default.createElement('div', { className: 'attivio-globalmast-spacer' }), _react2.default.createElement(_MastheadUser2.default, { username: _AuthUtils2.default.getUserName(this.state.userInfo), logoutFunction: logoutFunction, helpUri: this.props.helpUri }) ) ); }; return Masthead; }(_react2.default.Component), _class.defaultProps = { logoUri: 'img/attivio-logo-reverse.png', logoAlt: 'Attivio Home', homeRoute: '/', homeUrl: null, logoutFunction: null, applicationName: 'Cognitive Search', multiline: false, searchEngineType: 'attivio', helpUri: null, username: null }, _class.contextTypes = { searcher: _propTypes2.default.any }, _class.displayName = 'Masthead', _temp); exports.default = (0, _reactRouterDom.withRouter)((0, _Configurable2.default)(Masthead)); module.exports = exports['default'];