UNPKG

ndla-ui

Version:

UI component library for NDLA.

186 lines (170 loc) 6.4 kB
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } /** * Copyright (c) 2016-present, NDLA. * * This source code is licensed under the GPLv3 license found in the * LICENSE file in the root directory of this source tree. * */ import React from 'react'; import PropTypes from 'prop-types'; import BEMHelper from 'react-bem-helper'; import { Additional, Core } from 'ndla-icons/common'; import { Tooltip, NoContentBox, SafeLink } from 'ndla-ui'; import { Trans } from 'ndla-i18n'; import { TopicShape, ShortcutShape } from '../shapes'; import TopicIntroductionShortcuts from './TopicIntroductionShortcuts'; var topicClasses = new BEMHelper({ prefix: 'c-', name: 'topic-introduction', outputIsString: true }); var TopicIntroduction = function TopicIntroduction(_ref) { var toTopic = _ref.toTopic, topic = _ref.topic, subjectPage = _ref.subjectPage, shortcuts = _ref.shortcuts, messages = _ref.messages, shortcutAlwaysExpanded = _ref.shortcutAlwaysExpanded, additional = _ref.additional, showAdditionalCores = _ref.showAdditionalCores, id = _ref.id; var contentTypeDescription = additional ? messages.tooltipAdditionalTopic : messages.tooltipCoreTopic; return React.createElement( 'li', { className: topicClasses('item', { subjectPage: subjectPage, additional: additional, showAdditionalCores: showAdditionalCores }) }, React.createElement( 'article', { className: topicClasses('body') }, React.createElement( 'div', { className: topicClasses('heading-wrapper') }, React.createElement( 'h1', { className: topicClasses('header') }, React.createElement( SafeLink, { to: toTopic(topic.id), 'aria-describedby': id }, topic.name ) ), React.createElement( 'span', { id: id, hidden: true }, contentTypeDescription ), additional && React.createElement( Tooltip, { tooltip: messages.tooltipAdditionalTopic, align: 'left' }, React.createElement(Additional, { className: 'c-icon--20 u-margin-left-tiny' }) ), !additional && showAdditionalCores && React.createElement( Tooltip, { tooltip: messages.tooltipCoreTopic, align: 'left' }, React.createElement(Core, { className: 'c-icon--20 u-margin-left-tiny' }) ) ), React.createElement('p', { dangerouslySetInnerHTML: { __html: topic.introduction } }), shortcuts && React.createElement(TopicIntroductionShortcuts, { alwaysExpanded: shortcutAlwaysExpanded, id: topic.id + '_shortcuts', shortcuts: shortcuts, messages: { toggleButtonText: messages.shortcutButtonText } }) ) ); }; TopicIntroduction.propTypes = { additional: PropTypes.bool, showAdditionalCores: PropTypes.bool, messages: PropTypes.shape({ shortcutButtonText: PropTypes.string.isRequired, tooltipAdditionalTopic: PropTypes.string, tooltipCoreTopic: PropTypes.string }), topic: TopicShape.isRequired, toTopic: PropTypes.func.isRequired, subjectPage: PropTypes.bool, shortcuts: PropTypes.arrayOf(ShortcutShape), twoColumns: PropTypes.bool, shortcutAlwaysExpanded: PropTypes.bool, id: PropTypes.string.isRequired }; TopicIntroduction.defaultProps = { showAdditionalCores: false, additional: false }; var TopicIntroductionList = function TopicIntroductionList(_ref2) { var topics = _ref2.topics, twoColumns = _ref2.twoColumns, shortcutAlwaysExpanded = _ref2.shortcutAlwaysExpanded, showAdditionalCores = _ref2.showAdditionalCores, toggleAdditionalCores = _ref2.toggleAdditionalCores, rest = _objectWithoutProperties(_ref2, ['topics', 'twoColumns', 'shortcutAlwaysExpanded', 'showAdditionalCores', 'toggleAdditionalCores']); var renderAdditionalTopicsTrigger = !showAdditionalCores && topics.filter(function (topic) { return topic.additional; }).length > 0 && topics.filter(function (topic) { return !topic.additional; }).length === 0; return React.createElement( Trans, null, function (_ref3) { var t = _ref3.t; return React.createElement( 'ul', { className: topicClasses('list', { twoColumns: twoColumns }) }, topics.map(function (topic, index) { var shortcuts = topic.shortcuts, additional = topic.additional; return React.createElement(TopicIntroduction, _extends({ key: topic.id }, rest, { topic: topic, shortcuts: shortcuts, additional: additional, showAdditionalCores: showAdditionalCores, shortcutAlwaysExpanded: shortcutAlwaysExpanded, messages: { shortcutButtonText: t('resource.label'), tooltipAdditionalTopic: t('resource.tooltipAdditionalTopic'), tooltipCoreTopic: t('resource.tooltipCoreTopic') }, id: topic.id + '_' + index })); }), renderAdditionalTopicsTrigger && React.createElement( 'li', null, React.createElement(NoContentBox, { onClick: toggleAdditionalCores, text: t('resource.noCoreResourcesAvailableUnspecific'), buttonText: t('resource.activateAdditionalResources') }) ) ); } ); }; TopicIntroductionList.propTypes = { toTopic: PropTypes.func.isRequired, topics: PropTypes.arrayOf(TopicShape).isRequired, twoColumns: PropTypes.bool, shortcutAlwaysExpanded: PropTypes.bool, showAdditionalCores: PropTypes.bool, toggleAdditionalCores: PropTypes.func.isRequired }; TopicIntroductionList.defaultProps = { twoColumns: false, shortcutAlwaysExpanded: false, showAdditionalCores: false }; export default TopicIntroductionList;