UNPKG

ndla-ui

Version:

UI component library for NDLA.

105 lines (89 loc) 4.39 kB
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; }; }(); 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; } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import BEMHelper from 'react-bem-helper'; import { Concept } from 'ndla-ui'; var classes = BEMHelper('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 React.createElement( 'li', classes('item'), React.createElement( 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 }, React.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; }(Component); SubjectConcept.propTypes = { concept: PropTypes.shape() }; export default SubjectConcept;