UNPKG

@attivio/suit

Version:

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

131 lines (106 loc) 4.61 kB
'use strict'; exports.__esModule = true; exports.default = exports.SentimentTagCloudValue = undefined; var _class2, _temp; var _react = require('react'); var _react2 = _interopRequireDefault(_react); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var SentimentTagCloudValue = /** The value for this item. */ exports.SentimentTagCloudValue = function SentimentTagCloudValue(label, value, sentiment) { _classCallCheck(this, SentimentTagCloudValue); this.label = label; this.value = value; this.sentiment = sentiment; } /** The sentiment for this item. */ /** The string to display in the list. */ ; /** * Display a linear tag "cloud" where the items are proportionally sized * based on an associated value. */ var SentimentTagCloud = (_temp = _class2 = function (_React$Component) { _inherits(SentimentTagCloud, _React$Component); function SentimentTagCloud() { _classCallCheck(this, SentimentTagCloud); return _possibleConstructorReturn(this, _React$Component.apply(this, arguments)); } SentimentTagCloud.mapRange = function mapRange(num, sentiment) { if (sentiment === 'positive') { var inMin = 1; var inMax = 8; var outMin = 6; var outMax = 10; return (num - inMin) * (outMax - outMin) / (inMax - inMin) + outMin; } else if (sentiment === 'negative') { var _inMin = 1; var _inMax = 8; var _outMin = 1; var _outMax = 5; return (num - _inMin) * (_outMin - _outMax) / (_inMax - _inMin) + _outMax; } return -1; }; SentimentTagCloud.getAdjustedValue = function getAdjustedValue(value, max, sentiment) { var ratio = value / max; var timesSeven = ratio * 7; var oneThroughEight = Math.round(timesSeven) + 1; var mappedValue = this.mapRange(oneThroughEight, sentiment); return Math.round(mappedValue); }; SentimentTagCloud.prototype.render = function render() { var _this2 = this; var tagCloudValues = this.props.positiveTags.concat(this.props.negativeTags); var maxValue = tagCloudValues.reduce(function (max, tcv) { return Math.max(tcv.value, max); }, 0); if (tagCloudValues.length > this.props.maxValues) { // Sort numerically by value tagCloudValues.sort(function (tcv1, tcv2) { if (tcv1.value === tcv2.value) { return 0; } if (tcv1.value < tcv2.value) { return 1; } return -1; }); // Remove the items with the lowest values tagCloudValues = tagCloudValues.slice(0, this.props.maxValues); } // Sort alphabetically by label tagCloudValues.sort(function (tcv1, tcv2) { return tcv1.label.localeCompare(tcv2.label); }); var cloudItems = tagCloudValues.map(function (tcv) { var size = SentimentTagCloud.getAdjustedValue(tcv.value, maxValue, tcv.sentiment); var callback = function callback(event) { _this2.props.callback(tcv); event.target.blur(); }; return _react2.default.createElement( 'li', { key: tcv.label }, _react2.default.createElement( 'a', { className: 'attivio-sentiment-cloud-level-' + size, onClick: callback, role: 'button', tabIndex: 0 }, tcv.label ) ); }); return _react2.default.createElement( 'ul', { className: 'attivio-sentiment-cloud list-inline' }, cloudItems ); }; return SentimentTagCloud; }(_react2.default.Component), _class2.defaultProps = { maxValues: 15 }, _class2.displayName = 'SentimentTagCloud', _temp); exports.default = SentimentTagCloud; SentimentTagCloud.SentimentTagCloudValue = SentimentTagCloudValue;