ndla-ui
Version:
UI component library for NDLA.
262 lines (230 loc) • 7.94 kB
JavaScript
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; }
/**
* 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, { Component } from 'react';
import PropTypes from 'prop-types';
import BEMHelper from 'react-bem-helper';
import { getLicenseByAbbreviation } from 'ndla-licenses';
import isString from 'lodash/isString';
import { isMobile } from 'react-device-detect';
import ArticleFootNotes from './ArticleFootNotes';
import ArticleContent from './ArticleContent';
import ArticleByline from './ArticleByline';
import LayoutItem from '../Layout';
import { ArticleShape } from '../shapes';
var classes = new BEMHelper({
name: 'article',
prefix: 'c-'
});
export var ArticleWrapper = function ArticleWrapper(_ref) {
var children = _ref.children,
modifier = _ref.modifier;
return React.createElement(
'article',
classes(undefined, modifier),
children
);
};
ArticleWrapper.propTypes = {
children: PropTypes.node.isRequired,
modifier: PropTypes.string
};
export var ArticleHeaderWrapper = function (_Component) {
_inherits(ArticleHeaderWrapper, _Component);
function ArticleHeaderWrapper() {
_classCallCheck(this, ArticleHeaderWrapper);
return _possibleConstructorReturn(this, (ArticleHeaderWrapper.__proto__ || Object.getPrototypeOf(ArticleHeaderWrapper)).apply(this, arguments));
}
_createClass(ArticleHeaderWrapper, [{
key: 'componentDidMount',
value: function componentDidMount() {
if (isMobile) {
var heroContentList = document.querySelectorAll('.c-article__header');
if (heroContentList.length === 1) {
heroContentList[0].scrollIntoView(true);
window.scrollBy(0, heroContentList[0].offsetTop - 120); // Adjust for header
}
}
}
}, {
key: 'render',
value: function render() {
return React.createElement(
'div',
classes('header'),
this.props.children
);
}
}]);
return ArticleHeaderWrapper;
}(Component);
ArticleHeaderWrapper.propTypes = {
children: PropTypes.node.isRequired
};
export var ArticleTitle = function ArticleTitle(_ref2) {
var children = _ref2.children,
icon = _ref2.icon,
label = _ref2.label,
hasCompetenceGoals = _ref2.hasCompetenceGoals;
var modifiers = [];
if (icon) {
modifiers.push('icon');
}
if (hasCompetenceGoals) {
modifiers.push('with-competence-goals');
}
var labelView = null;
if (label) {
labelView = React.createElement(
'p',
null,
label
);
}
return React.createElement(
'div',
classes('title', modifiers),
icon,
labelView,
React.createElement(
'h1',
null,
children
)
);
};
ArticleTitle.propTypes = {
hasCompetenceGoals: PropTypes.bool,
children: PropTypes.node.isRequired,
label: PropTypes.string,
icon: PropTypes.node
};
ArticleTitle.defaultProps = {
icon: null,
label: null
};
export var ArticleIntroduction = function ArticleIntroduction(_ref3) {
var children = _ref3.children;
if (isString(children)) {
/* Since article introduction is already escaped from the api
we run into a double escaping issues as React escapes all strings.
Use dangerouslySetInnerHTML to circumvent the issue */
return React.createElement('p', {
className: 'article_introduction',
dangerouslySetInnerHTML: { __html: children }
});
}
if (children) {
return React.createElement(
'p',
{ className: 'article_introduction' },
children
);
}
return null;
};
ArticleIntroduction.propTypes = {
children: PropTypes.node
};
export var Article = function Article(_ref4) {
var _ref4$article = _ref4.article,
title = _ref4$article.title,
introduction = _ref4$article.introduction,
updated = _ref4$article.updated,
content = _ref4$article.content,
footNotes = _ref4$article.footNotes,
_ref4$article$copyrig = _ref4$article.copyright,
licenseObj = _ref4$article$copyrig.license,
creators = _ref4$article$copyrig.creators,
rightsholders = _ref4$article$copyrig.rightsholders,
icon = _ref4.icon,
additional = _ref4.additional,
licenseBox = _ref4.licenseBox,
modifier = _ref4.modifier,
messages = _ref4.messages,
children = _ref4.children,
competenceGoals = _ref4.competenceGoals,
competenceGoalsNarrow = _ref4.competenceGoalsNarrow;
var license = getLicenseByAbbreviation(licenseObj.license).abbreviation;
var showCreators = Array.isArray(creators) && creators.length > 0;
var authors = showCreators ? creators : rightsholders;
return React.createElement(
ArticleWrapper,
{ modifier: modifier },
React.createElement(
LayoutItem,
{ layout: 'center' },
React.createElement(
ArticleHeaderWrapper,
null,
competenceGoals,
React.createElement(
ArticleTitle,
{
icon: icon,
label: messages.label,
hasCompetenceGoals: competenceGoals !== null },
title
),
React.createElement(
ArticleIntroduction,
null,
introduction
),
React.createElement(ArticleByline, {
authors: authors,
updated: updated,
license: license,
additional: additional,
licenseBox: licenseBox
}),
competenceGoalsNarrow
)
),
React.createElement(
LayoutItem,
{ layout: 'center' },
React.createElement(ArticleContent, { content: content })
),
React.createElement(
LayoutItem,
{ layout: 'center' },
footNotes && footNotes.length > 0 && React.createElement(ArticleFootNotes, { footNotes: footNotes })
),
React.createElement(
LayoutItem,
{ layout: 'extend' },
children
)
);
};
Article.propTypes = {
article: ArticleShape.isRequired,
modifier: PropTypes.string,
icon: PropTypes.node,
licenseBox: PropTypes.node,
additional: PropTypes.bool,
competenceGoals: PropTypes.node,
competenceGoalsNarrow: PropTypes.node,
children: PropTypes.node,
messages: PropTypes.shape({
label: PropTypes.string
}).isRequired
};
Article.defaultProps = {
licenseBox: null,
additional: null,
competenceGoals: null,
competenceGoalsNarrow: null,
icon: null,
children: null
};
export default Article;