@attivio/suit
Version:
Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.
168 lines (140 loc) • 5.57 kB
JavaScript
;
exports.__esModule = true;
exports.default = exports.TagCloudValue = 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 TagCloudValue =
/** The value for this item. */
exports.TagCloudValue = function TagCloudValue(label, value) {
var noLink = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
_classCallCheck(this, TagCloudValue);
this.label = label;
this.value = value;
this.noLink = noLink;
}
/** Whether the tag should be clickable */
/** The string to display in the list. */
;
/**
* Display a linear tag "cloud" where the items are proportionally sized
* based on an associated value.
*/
var TagCloud = (_temp = _class2 = function (_React$Component) {
_inherits(TagCloud, _React$Component);
function TagCloud() {
_classCallCheck(this, TagCloud);
return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
}
TagCloud.getAdjustedValue = function getAdjustedValue(value, min, max) {
if (max === min) {
// Special case where there's no variation... just use a "middling" value
return 4;
}
// Make sure we only base the adjusted values on the range of min - max
// rather than on the absolute values of the items.
var ratio = (value - min) / (max - min);
// Get a value from 0 to 7
var timesSeven = ratio * 7;
// Make it into an integer between 1 and 8, inclusive
var oneThroughEight = Math.round(timesSeven) + 1;
return oneThroughEight;
};
/**
* Get the CSS class name to use for the tag level.
* Note that we do this rather than something like
* `attivio-cloud-level-${level}` because having
* synthetic class names here can lead to them being
* missed when doing searches/replaces, etc.
*/
TagCloud.getClassNameForLevel = function getClassNameForLevel(level) {
switch (level) {
case 1:
return 'attivio-cloud-level-1';
case 2:
return 'attivio-cloud-level-2';
case 3:
return 'attivio-cloud-level-3';
case 4:
return 'attivio-cloud-level-4';
case 5:
return 'attivio-cloud-level-5';
case 6:
return 'attivio-cloud-level-6';
case 7:
return 'attivio-cloud-level-7';
case 8:
return 'attivio-cloud-level-8';
default:
return 'attivio-cloud-level-4';
}
};
TagCloud.prototype.render = function render() {
var _this2 = this;
var tagCloudValues = this.props.tags;
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);
}
var maxValue = tagCloudValues.reduce(function (max, tcv) {
return Math.max(tcv.value, max);
}, 0);
var minValue = tagCloudValues.reduce(function (min, tcv) {
return Math.min(tcv.value, min);
}, maxValue);
// Sort alphabetically by label
tagCloudValues.sort(function (tcv1, tcv2) {
return tcv1.label.localeCompare(tcv2.label);
});
var cloudItems = tagCloudValues.map(function (tcv) {
var size = TagCloud.getAdjustedValue(tcv.value, minValue, maxValue);
var callback = function callback(event) {
_this2.props.callback(tcv);
event.target.blur();
};
var className = TagCloud.getClassNameForLevel(size);
return _this2.props.noLink || tcv.noLink ? _react2.default.createElement(
'li',
{ key: tcv.label, className: 'attivio-cloud-noLink' },
_react2.default.createElement(
'span',
{ className: className },
tcv.label
)
) : _react2.default.createElement(
'li',
{ key: tcv.label },
_react2.default.createElement(
'a',
{ className: className, onClick: callback, role: 'button', tabIndex: 0 },
tcv.label
)
);
});
return _react2.default.createElement(
'ul',
{ className: 'attivio-cloud list-inline' },
cloudItems
);
};
return TagCloud;
}(_react2.default.Component), _class2.defaultProps = {
maxValues: 15,
noLink: false
}, _class2.displayName = 'TagCloud', _temp);
exports.default = TagCloud;
TagCloud.TagCloudValue = TagCloudValue;