UNPKG

@symblight/react-stockcharts

Version:

Highly customizable stock charts with ReactJS and d3

398 lines (335 loc) 13.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; 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 _react2 = _interopRequireDefault(_react); var _propTypes = require("prop-types"); var _propTypes2 = _interopRequireDefault(_propTypes); var _utils = require("../utils"); var _utils2 = require("./utils"); var _EachFreeArrows = require("./wrapper/EachFreeArrows"); var _EachFreeArrows2 = _interopRequireDefault(_EachFreeArrows); var _Arrows = require("./components/Arrows"); var _Arrows2 = _interopRequireDefault(_Arrows); var _MouseLocationIndicator = require("./components/MouseLocationIndicator"); var _MouseLocationIndicator2 = _interopRequireDefault(_MouseLocationIndicator); var _HoverTextNearMouse = require("./components/HoverTextNearMouse"); var _HoverTextNearMouse2 = _interopRequireDefault(_HoverTextNearMouse); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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); } } 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; } var FreeArrows = function (_Component) { _inherits(FreeArrows, _Component); function FreeArrows(props) { _classCallCheck(this, FreeArrows); var _this = _possibleConstructorReturn(this, (FreeArrows.__proto__ || Object.getPrototypeOf(FreeArrows)).call(this, props)); _this.handleStart = _this.handleStart.bind(_this); _this.handleEnd = _this.handleEnd.bind(_this); _this.handleDrawLine = _this.handleDrawLine.bind(_this); _this.handleDragLine = _this.handleDragLine.bind(_this); _this.handleDragLineComplete = _this.handleDragLineComplete.bind(_this); _this.terminate = _utils2.terminate.bind(_this); _this.saveNodeType = _utils2.saveNodeType.bind(_this); _this.getSelectionState = (0, _utils2.isHoverForInteractiveType)("arrows").bind(_this); _this.getHoverInteractive = _this.getHoverInteractive.bind(_this); _this.state = {}; _this.nodes = []; return _this; } _createClass(FreeArrows, [{ key: "handleDragLine", value: function handleDragLine(index, newXYValue) { this.setState({ override: _extends({ index: index }, newXYValue) }); } }, { key: "handleDragLineComplete", value: function handleDragLineComplete(moreProps) { var _this2 = this; var override = this.state.override; if ((0, _utils.isDefined)(override)) { var arrows = this.props.arrows; var newArrows = arrows.map(function (each, idx) { return idx === override.index ? _extends({}, each, { start: [override.x1Value, override.y1Value], end: [override.x2Value, override.y2Value], selected: true }) : _extends({}, each, { selected: false }); }); this.setState({ override: null }, function () { _this2.props.onComplete(newArrows, moreProps); }); } } }, { key: "handleDrawLine", value: function handleDrawLine(xyValue) { var current = this.state.current; if ((0, _utils.isDefined)(current) && (0, _utils.isDefined)(current.start)) { this.mouseMoved = true; this.setState({ current: { start: current.start, end: xyValue } }); } } }, { key: "handleStart", value: function handleStart(xyValue, moreProps, e) { var _this3 = this; var current = this.state.current; if ((0, _utils.isNotDefined)(current) || (0, _utils.isNotDefined)(current.start)) { this.mouseMoved = false; this.setState({ current: { start: xyValue, end: null } }, function () { _this3.props.onStart(moreProps, e); }); } } }, { key: "handleEnd", value: function handleEnd(xyValue, moreProps, e) { var _this4 = this; var current = this.state.current; var _props = this.props, arrows = _props.arrows, appearance = _props.appearance, type = _props.type; if (this.mouseMoved && (0, _utils.isDefined)(current) && (0, _utils.isDefined)(current.start)) { var newArrows = [].concat(_toConsumableArray(arrows.map(function (d) { return _extends({}, d, { selected: false }); })), [{ start: current.start, end: xyValue, selected: true, appearance: appearance, type: type }]); this.setState({ current: null, arrows: newArrows }, function () { _this4.props.onComplete(newArrows, moreProps, e); }); } } }, { key: "getHoverInteractive", value: function getHoverInteractive(hovering, arrow) { arrow.hovering = hovering; var isHover = this.props.isHover; isHover(hovering, arrow); } }, { key: "render", value: function render() { var _this5 = this; var appearance = this.props.appearance; var _props2 = this.props, enabled = _props2.enabled, snap = _props2.snap, shouldDisableSnap = _props2.shouldDisableSnap, snapTo = _props2.snapTo, type = _props2.type; var _props3 = this.props, currentPositionRadius = _props3.currentPositionRadius, currentPositionStroke = _props3.currentPositionStroke; var _props4 = this.props, currentPositionstrokeOpacity = _props4.currentPositionstrokeOpacity, currentPositionStrokeWidth = _props4.currentPositionStrokeWidth; var _props5 = this.props, hoverText = _props5.hoverText, arrows = _props5.arrows; var _state = this.state, current = _state.current, override = _state.override; var tempLine = (0, _utils.isDefined)(current) && (0, _utils.isDefined)(current.end) ? _react2.default.createElement(_Arrows2.default, { type: type, noHover: true, x1Value: current.start[0], y1Value: current.start[1], x2Value: current.end[0], y2Value: current.end[1], stroke: getAppearance(type, appearance).stroke, strokeWidth: getAppearance(type, appearance).strokeWidth, strokeOpacity: getAppearance(type, appearance).strokeOpacity }) : null; return _react2.default.createElement( "g", null, arrows.map(function (each, idx) { var eachAppearance = (0, _utils.isDefined)(each.appearance) ? _extends({}, appearance, each.appearance) : appearance; var hoverTextWithDefault = _extends({}, FreeArrows.defaultProps.hoverText, hoverText); return _react2.default.createElement(_EachFreeArrows2.default, { key: idx, ref: _this5.saveNodeType(idx), index: idx, type: each.type, selected: each.selected, x1Value: (0, _utils2.getValueFromOverride)(override, idx, "x1Value", each.start[0]), y1Value: (0, _utils2.getValueFromOverride)(override, idx, "y1Value", each.start[1]), x2Value: (0, _utils2.getValueFromOverride)(override, idx, "x2Value", each.end[0]), y2Value: (0, _utils2.getValueFromOverride)(override, idx, "y2Value", each.end[1]), stroke: getAppearance(type, eachAppearance).stroke, strokeWidth: getAppearance(type, eachAppearance).strokeWidth, strokeOpacity: getAppearance(type, eachAppearance).strokeOpacity, strokeDasharray: getAppearance(type, eachAppearance).strokeDasharray, edgeStroke: eachAppearance.edgeStroke, edgeFill: eachAppearance.edgeFill, edgeStrokeWidth: eachAppearance.edgeStrokeWidth, r: eachAppearance.r, hoverText: hoverTextWithDefault, onDrag: _this5.handleDragLine, onDragComplete: _this5.handleDragLineComplete, edgeInteractiveCursor: "react-stockcharts-move-cursor", lineInteractiveCursor: "react-stockcharts-move-cursor", getHoverInteractive: function getHoverInteractive(hovering) { return _this5.getHoverInteractive(hovering, each); } }); }), tempLine, _react2.default.createElement(_MouseLocationIndicator2.default, { enabled: enabled, snap: snap, shouldDisableSnap: shouldDisableSnap, snapTo: snapTo, r: currentPositionRadius, stroke: currentPositionStroke, strokeOpacity: currentPositionstrokeOpacity, strokeWidth: currentPositionStrokeWidth, onMouseDown: this.handleStart, onClick: this.handleEnd, onMouseMove: this.handleDrawLine }) ); } }]); return FreeArrows; }(_react.Component); function getAppearance(type, props) { switch (type) { case "ARROW-OPEN": { return props.open; } case "ARROW-CLOSE": { return props.close; } case "ARROW": { return props.arrow; } default: return props.arrow; } } FreeArrows.propTypes = { snap: _propTypes2.default.bool.isRequired, enabled: _propTypes2.default.bool.isRequired, snapTo: _propTypes2.default.func, shouldDisableSnap: _propTypes2.default.func.isRequired, onStart: _propTypes2.default.func.isRequired, onComplete: _propTypes2.default.func.isRequired, onSelect: _propTypes2.default.func, onDoubleClick: _propTypes2.default.func, currentPositionStroke: _propTypes2.default.string, currentPositionStrokeWidth: _propTypes2.default.number, currentPositionstrokeOpacity: _propTypes2.default.number, currentPositionRadius: _propTypes2.default.number, type: _propTypes2.default.oneOf(["ARROW", // extends from -Infinity to +Infinity "ARROW-OPEN", // extends to +/-Infinity in one direction "ARROW-CLOSE"] // extends between the set bounds ), hoverText: _propTypes2.default.object.isRequired, arrows: _propTypes2.default.array.isRequired, appearance: _propTypes2.default.shape({ edgeStrokeWidth: _propTypes2.default.number.isRequired, edgeFill: _propTypes2.default.string.isRequired, edgeStroke: _propTypes2.default.string.isRequired, arrow: _propTypes2.default.shape({ stroke: _propTypes2.default.string.isRequired, strokeOpacity: _propTypes2.default.number.isRequired, strokeWidth: _propTypes2.default.number.isRequired, strokeDasharray: _propTypes2.default.oneOf(_utils.strokeDashTypes) }), open: _propTypes2.default.shape({ stroke: _propTypes2.default.string.isRequired, strokeOpacity: _propTypes2.default.number.isRequired, strokeWidth: _propTypes2.default.number.isRequired, strokeDasharray: _propTypes2.default.oneOf(_utils.strokeDashTypes) }), close: _propTypes2.default.shape({ stroke: _propTypes2.default.string.isRequired, strokeOpacity: _propTypes2.default.number.isRequired, strokeWidth: _propTypes2.default.number.isRequired, strokeDasharray: _propTypes2.default.oneOf(_utils.strokeDashTypes) }) }).isRequired }; FreeArrows.defaultProps = { type: "ARROW", onStart: _utils.noop, onComplete: _utils.noop, onSelect: _utils.noop, currentPositionStroke: "#000000", currentPositionstrokeOpacity: 1, currentPositionStrokeWidth: 3, currentPositionRadius: 0, shouldDisableSnap: function shouldDisableSnap(e) { return e.button === 2 || e.shiftKey; }, hoverText: _extends({}, _HoverTextNearMouse2.default.defaultProps, { enable: true, bgHeight: "auto", bgWidth: "auto", text: "Click to select object", selectedText: "" }), arrows: [], appearance: { arrow: { stroke: "#000000", strokeOpacity: 1, strokeWidth: 4, strokeDasharray: "Solid" }, edgeStrokeWidth: 1, edgeFill: "#FFFFFF", edgeStroke: "#000000", r: 6, open: { stroke: "green", strokeOpacity: 1, strokeWidth: 4, strokeDasharray: "Solid" }, close: { stroke: "red", strokeOpacity: 1, strokeWidth: 4, strokeDasharray: "Solid" } } }; exports.default = FreeArrows; //# sourceMappingURL=FreeArrows.js.map