UNPKG

ndla-ui

Version:

UI component library for NDLA.

122 lines (97 loc) 4.82 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _reactBemHelper = require('react-bem-helper'); var _reactBemHelper2 = _interopRequireDefault(_reactBemHelper); var _ndlaUi = require('ndla-ui'); 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; } var classes = (0, _reactBemHelper2.default)('c-subject-concepts'); var SubjectConcept = function (_Component) { _inherits(SubjectConcept, _Component); function SubjectConcept(props) { _classCallCheck(this, SubjectConcept); var _this = _possibleConstructorReturn(this, (SubjectConcept.__proto__ || Object.getPrototypeOf(SubjectConcept)).call(this, props)); _this.state = { visible: false }; _this.showConceptDialog = _this.showConceptDialog.bind(_this); _this.hideConceptDialog = _this.hideConceptDialog.bind(_this); _this.handleKeyUp = _this.handleKeyUp.bind(_this); return _this; } _createClass(SubjectConcept, [{ key: 'handleKeyUp', value: function handleKeyUp() { this.showConceptDialog(); } }, { key: 'showConceptDialog', value: function showConceptDialog() { var _this2 = this; var linkBounds = this.conceptLink.getBoundingClientRect(); var viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); var dialogX = 250 + (linkBounds.left - linkBounds.width) + 500 > viewportWidth ? viewportWidth - (500 - linkBounds.width) : 250 + linkBounds.left; this.conceptDialog.style.top = linkBounds.bottom + window.pageYOffset + 'px'; this.conceptDialog.style.left = dialogX + 'px'; setTimeout(function () { _this2.setState({ visible: true }); }); } }, { key: 'hideConceptDialog', value: function hideConceptDialog() { this.setState({ visible: false }); } }, { key: 'render', value: function render() { var _this3 = this; var concept = this.props.concept; return _react2.default.createElement( 'li', classes('item'), _react2.default.createElement( _ndlaUi.Concept, { dialogRef: function dialogRef(el) { _this3.conceptDialog = el; }, content: concept.content, authors: concept.authors, source: concept.source, title: concept.title, messages: concept.messages, license: concept.license, id: concept.id, visible: this.state.visible, closeCallback: this.hideConceptDialog }, _react2.default.createElement( 'span', { className: 'c-concept__link', role: 'button', tabIndex: 0, onClick: this.showConceptDialog, onKeyUp: this.handleKeyUp, ref: function ref(btn) { _this3.conceptLink = btn; } }, concept.title ) ) ); } }]); return SubjectConcept; }(_react.Component); SubjectConcept.propTypes = { concept: _propTypes2.default.shape() }; exports.default = SubjectConcept;