From f9b496939f781e4a65bdc616c33e9f761dc8793a Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Mon, 23 May 2016 18:46:55 +0200 Subject: [PATCH] v0.4.2 compilation --- lib/TagCloud.js | 61 ++++++++++++++++++----------------------- lib/defaultRenderer.js | 62 ++++++++++++++++++++++-------------------- lib/helpers.js | 35 ++++++++++++++++++++++++ lib/index.js | 2 +- package.json | 2 +- 5 files changed, 95 insertions(+), 67 deletions(-) create mode 100644 lib/helpers.js diff --git a/lib/TagCloud.js b/lib/TagCloud.js index 9422cc2..ec87706 100644 --- a/lib/TagCloud.js +++ b/lib/TagCloud.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; Object.defineProperty(exports, "__esModule", { value: true @@ -7,20 +7,18 @@ exports.TagCloud = undefined; 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; }; }(); -var _react = require("react"); +var _react = require('react'); var _react2 = _interopRequireDefault(_react); -var _defaultRenderer = require("./defaultRenderer"); +var _defaultRenderer = require('./defaultRenderer'); -var _objectAssign2 = require("object-assign"); - -var _objectAssign3 = _interopRequireDefault(_objectAssign2); - -var _arrayShuffle = require("array-shuffle"); +var _arrayShuffle = require('array-shuffle'); var _arrayShuffle2 = _interopRequireDefault(_arrayShuffle); +var _helpers = require('./helpers'); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } @@ -29,44 +27,37 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen 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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } +function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } -var omitted = ['tags', 'shuffle', 'renderer', 'maxSize', 'minSize', 'onClick']; -var omittedProps = omitted.reduce(function (r, k) { - return (0, _objectAssign3.default)(r, _defineProperty({}, k, undefined)); -}, {}); - -var fontSizeConverter = function fontSizeConverter(count, min, max, minSize, maxSize) { - return Math.round((count - min) * (maxSize - minSize) / (max - min) + minSize); -}; - -var createTags = function createTags(_ref) { - var tags = _ref.tags; - var minSize = _ref.minSize; - var maxSize = _ref.maxSize; - var renderer = _ref.renderer; - var onClick = _ref.onClick; +var createTags = function createTags(props) { + var tags = props.tags; + var minSize = props.minSize; + var maxSize = props.maxSize; + var renderer = props.renderer; + var handlers = (0, _helpers.includeProps)(props, eventHandlers); var counts = tags.map(function (tag) { return tag.count; }), - min = Math.min.apply(Math, counts), - max = Math.max.apply(Math, counts); + min = Math.min.apply(Math, _toConsumableArray(counts)), + max = Math.max.apply(Math, _toConsumableArray(counts)); var computeFontSize = function computeFontSize(tag) { return { tag: tag, - fontSize: fontSizeConverter(tag.count, min, max, minSize, maxSize) + fontSize: (0, _helpers.fontSizeConverter)(tag.count, min, max, minSize, maxSize) }; }; - var handlers = { onClick: onClick }; - var createComponent = function createComponent(_ref2, key) { - var tag = _ref2.tag; - var fontSize = _ref2.fontSize; + var createComponent = function createComponent(_ref, key) { + var tag = _ref.tag; + var fontSize = _ref.fontSize; return renderer(tag, fontSize, key, handlers); }; return tags.map(computeFontSize).map(createComponent); }; +var eventHandlers = ['onClick', 'onDoubleClick', 'onMouseMove']; +var cloudProps = ['tags', 'shuffle', 'renderer', 'maxSize', 'minSize']; + var TagCloud = exports.TagCloud = function (_React$Component) { _inherits(TagCloud, _React$Component); @@ -77,12 +68,12 @@ var TagCloud = exports.TagCloud = function (_React$Component) { } _createClass(TagCloud, [{ - key: "render", + key: 'render', value: function render() { - var props = (0, _objectAssign3.default)({}, this.props, omittedProps); + var props = (0, _helpers.omitProps)(this.props, [].concat(cloudProps, eventHandlers)); var tags = createTags(this.props); return _react2.default.createElement( - "div", + 'div', props, this.props.shuffle ? (0, _arrayShuffle2.default)(tags) : tags ); @@ -104,5 +95,5 @@ TagCloud.propTypes = { TagCloud.defaultProps = { renderer: (0, _defaultRenderer.defaultRenderer)(), shuffle: true, - className: "tag-cloud" + className: 'tag-cloud' }; \ No newline at end of file diff --git a/lib/defaultRenderer.js b/lib/defaultRenderer.js index 64f893f..a65c0f7 100644 --- a/lib/defaultRenderer.js +++ b/lib/defaultRenderer.js @@ -1,51 +1,47 @@ -"use strict"; +'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultRenderer = undefined; -var _react = require("react"); +var _react = require('react'); var _react2 = _interopRequireDefault(_react); -var _randomcolor = require("randomcolor"); +var _randomcolor = require('randomcolor'); var _randomcolor2 = _interopRequireDefault(_randomcolor); -var _objectAssign = require("object-assign"); +var _objectAssign = require('object-assign'); var _objectAssign2 = _interopRequireDefault(_objectAssign); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -var defaultClassName = "tag-cloud-tag"; - -var defaultStyles = { - margin: "0px 3px", - verticalAlign: "middle", - display: "inline-block" -}; - -var defaultTagRenderer = function defaultTagRenderer(tag) { - return tag.value; -}; - var defaultRenderer = exports.defaultRenderer = function defaultRenderer() { - var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - var _ref$tagRenderer = _ref.tagRenderer; - var tagRenderer = _ref$tagRenderer === undefined ? defaultTagRenderer : _ref$tagRenderer; - var _ref$colorOptions = _ref.colorOptions; - var colorOptions = _ref$colorOptions === undefined ? {} : _ref$colorOptions; - var _ref$props = _ref.props; - var props = _ref$props === undefined ? {} : _ref$props; + var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; return function (tag, size, key) { var handlers = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3]; - - var className = defaultClassName, - fontSize = size + "px", - color = props.disableRandomColor ? tag.color || 'black' : (0, _randomcolor2.default)(colorOptions); + var _options$tagRenderer = options.tagRenderer; + var tagRenderer = _options$tagRenderer === undefined ? function (tag) { + return tag.value; + } : _options$tagRenderer; + var _options$colorOptions = options.colorOptions; + var colorOptions = _options$colorOptions === undefined ? {} : _options$colorOptions; + var _options$disableRando = options.disableRandomColor; + var disableRandomColor = _options$disableRando === undefined ? false : _options$disableRando; + var _options$props = options.props; + var props = _options$props === undefined ? {} : _options$props; + + + var className = 'tag-cloud-tag'; + var fontSize = size + 'px'; + if (props.disableRandomColor) { + // eslint-disable-next-line no-console + console.warn("Using deprecated property 'disableRandomColor' passed to prop option of defaultRenderer. " + "It will be removed in the next major release and replaced with it's own option 'disableRandomColor'."); + } + var color = props.disableRandomColor || disableRandomColor ? tag.color || undefined : (0, _randomcolor2.default)(colorOptions); var eventHandlers = {}; Object.keys(handlers).forEach(function (key) { @@ -55,12 +51,18 @@ var defaultRenderer = exports.defaultRenderer = function defaultRenderer() { }); var elementProps = (0, _objectAssign2.default)({}, { className: className }, eventHandlers, props, { key: key }); - elementProps.style = (0, _objectAssign2.default)({}, defaultStyles, { color: color }, props.style, { fontSize: fontSize }); + elementProps.style = (0, _objectAssign2.default)({}, styles, { color: color }, props.style, { fontSize: fontSize }); return _react2.default.createElement( - "span", + 'span', elementProps, tagRenderer(tag) ); }; +}; + +var styles = { + margin: '0px 3px', + verticalAlign: 'middle', + display: 'inline-block' }; \ No newline at end of file diff --git a/lib/helpers.js b/lib/helpers.js new file mode 100644 index 0000000..63c565c --- /dev/null +++ b/lib/helpers.js @@ -0,0 +1,35 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +/** + * Creates new object from target excluding given properties. + */ +var omitProps = exports.omitProps = function omitProps(target, props) { + return Object.keys(target).reduce(function (r, key) { + if (! ~props.indexOf(key)) { + r[key] = target[key]; + } + return r; + }, {}); +}; + +/** + * Creates new object from target including all available properties. + */ +var includeProps = exports.includeProps = function includeProps(target, props) { + return Object.keys(target).reduce(function (r, key) { + if (~props.indexOf(key) && key in target) { + r[key] = target[key]; + } + return r; + }, {}); +}; + +/** + * Computes appropriate font size of tag. + */ +var fontSizeConverter = exports.fontSizeConverter = function fontSizeConverter(count, min, max, minSize, maxSize) { + return Math.round((count - min) * (maxSize - minSize) / (max - min) + minSize); +}; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index ae92c23..34ed59c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -18,7 +18,7 @@ var _defaultRenderer = require('./defaultRenderer'); var deprecatedRendererExport = function deprecatedRendererExport() { // eslint-disable-next-line no-console - console.warn('Using deprecated \'DefaultRenderer\' import, it will be removed in the next release and replaced with \'defaultRenderer\'.'); + console.warn('Using deprecated \'DefaultRenderer\' import, it will be removed in the next major release and replaced with \'defaultRenderer\'.'); return _defaultRenderer.defaultRenderer.apply(undefined, arguments); }; diff --git a/package.json b/package.json index 2982b59..d625aa6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-tagcloud", - "version": "0.4.1", + "version": "0.4.2", "description": "Tag/word cloud component for react", "main": "lib/index.js", "scripts": {