UNPKG

@up-group/react-controls

Version:

We know that there are a ton of react UI library projects to choose from. Our hope with this one is to provide the next generation of react components that you can use to bootstrap your next project, or as a reference for building a UIKit. Read on to get

196 lines 9.43 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var React = require("react"); var moment = require("moment"); var utils_1 = require("../utils"); var Header = (function (_super) { __extends(Header, _super); function Header(props) { var _this = _super.call(this, props) || this; _this.periodClick = function (e) { var _a = e.target.dataset, time = _a.time, unit = _a.unit; if (time && unit) { _this.props.showPeriod(moment(time - 0), unit); } }; _this.touchStart = function (e) { if (e.touches.length === 1) { _this.setState({ touchTarget: e.target || e.touchTarget, touchActive: true }); } }; _this.touchEnd = function (e) { if (!_this.state.touchActive) { return _this.resetTouchState(); } var changedTouches = e.changedTouches[0]; if (changedTouches) { var elem = document.elementFromPoint(changedTouches.pageX, changedTouches.pageY); if (elem !== _this.state.touchTarget) { return _this.resetTouchState(); } } _this.resetTouchState(); _this.periodClick(e); }; _this.state = { scrollTop: 0, componentTop: 0, touchTarget: null, touchActive: false }; return _this; } Header.prototype.scroll = function (e) { if (this.props.fixedHeader === 'absolute' && window && window.document) { var scroll_1 = window.document.body.scrollTop; this.setState({ scrollTop: scroll_1 }); } }; Header.prototype.setComponentTop = function () { var viewportOffset = this.refs.header.getBoundingClientRect(); this.setState({ componentTop: viewportOffset.top }); }; Header.prototype.componentDidMount = function () { var _this = this; this.setComponentTop(); this.scroll(); this.scrollEventListener = { handleEvent: function (event) { _this.scroll(); } }; window.addEventListener('scroll', this.scrollEventListener); }; Header.prototype.componentWillUnmount = function () { window.removeEventListener('scroll', this.scrollEventListener); }; Header.prototype.componentWillReceiveProps = function () { this.setComponentTop(); }; Header.prototype.headerLabel = function (time, unit, width) { if (unit === 'year') { return time.format(width < 46 ? 'YY' : 'YYYY'); } else if (unit === 'month') { return time.format(width < 65 ? 'MM/YY' : width < 75 ? 'MM/YYYY' : width < 120 ? 'MMM YYYY' : 'MMMM YYYY'); } else if (unit === 'day') { return time.format(width < 150 ? 'L' : 'dddd, LL'); } else if (unit === 'hour') { return time.format(width < 50 ? 'HH' : width < 130 ? 'HH:00' : width < 150 ? 'L, HH:00' : 'dddd, LL, HH:00'); } else { return time.format('LLL'); } }; Header.prototype.subHeaderLabel = function (time, unit, width) { if (unit === 'year') { return time.format(width < 46 ? 'YY' : 'YYYY'); } else if (unit === 'month') { return time.format(width < 37 ? 'MM' : width < 85 ? 'MMM' : 'MMMM'); } else if (unit === 'day') { return time.format(width < 47 ? 'D' : width < 80 ? 'dd D' : width < 120 ? 'ddd, Do' : 'dddd, Do'); } else if (unit === 'hour') { return time.format(width < 50 ? 'HH' : 'HH:00'); } else if (unit === 'minute') { return time.format(width < 60 ? 'mm' : 'HH:mm'); } else { return time.get(unit); } }; Header.prototype.resetTouchState = function () { this.setState({ touchTarget: null, touchActive: false }); }; Header.prototype.render = function () { var _this = this; var timeLabels = []; var _a = this.props, canvasTimeStart = _a.canvasTimeStart, canvasTimeEnd = _a.canvasTimeEnd, canvasWidth = _a.canvasWidth, lineHeight = _a.lineHeight, visibleTimeStart = _a.visibleTimeStart, visibleTimeEnd = _a.visibleTimeEnd, minUnit = _a.minUnit, timeSteps = _a.timeSteps, fixedHeader = _a.fixedHeader, headerLabelGroupHeight = _a.headerLabelGroupHeight, headerLabelHeight = _a.headerLabelHeight; var scrollTop = this.state.scrollTop; var ratio = canvasWidth / (canvasTimeEnd - canvasTimeStart); var twoHeaders = minUnit !== 'year'; if (twoHeaders) { var nextUnit_1 = utils_1.getNextUnit(minUnit); utils_1.iterateTimes(visibleTimeStart, visibleTimeEnd, nextUnit_1, timeSteps, function (time, nextTime) { var startTime = Math.max(visibleTimeStart, time.valueOf()); var endTime = Math.min(visibleTimeEnd, nextTime.valueOf()); var left = Math.round((startTime.valueOf() - canvasTimeStart) * ratio); var right = Math.round((endTime.valueOf() - canvasTimeStart) * ratio); var labelWidth = right - left; var leftCorrect = fixedHeader === 'fixed' ? Math.round((canvasTimeStart - visibleTimeStart) * ratio) - 1 : 0; timeLabels.push(React.createElement("div", { key: "top-label-" + time.valueOf(), className: 'rct-label-group', "data-time": time, "data-unit": nextUnit_1, style: { left: left + leftCorrect + "px", width: labelWidth + "px", height: headerLabelGroupHeight + "px", lineHeight: headerLabelGroupHeight + "px", cursor: 'pointer' } }, _this.headerLabel(time, nextUnit_1, labelWidth))); }); } utils_1.iterateTimes(canvasTimeStart, canvasTimeEnd, minUnit, timeSteps, function (time, nextTime) { var left = Math.round((time.valueOf() - canvasTimeStart) * ratio); var minUnitValue = time.get(minUnit === 'day' ? 'date' : minUnit); var firstOfType = minUnitValue === (minUnit === 'day' ? 1 : 0); var labelWidth = Math.round((nextTime.valueOf() - time.valueOf()) * ratio); var borderWidth = firstOfType ? 2 : 1; var leftCorrect = fixedHeader === 'fixed' ? Math.round((canvasTimeStart - visibleTimeStart) * ratio) - borderWidth + 1 : 0; timeLabels.push(React.createElement("div", { key: "label-" + time.valueOf(), className: "rct-label " + (twoHeaders ? '' : 'rct-label-only') + " " + (firstOfType ? 'rct-first-of-type' : '') + " ", "data-time": time, "data-unit": minUnit, style: { top: (minUnit === 'year' ? 0 : headerLabelGroupHeight) + "px", left: left + leftCorrect + "px", width: labelWidth + "px", height: (minUnit === 'year' ? headerLabelGroupHeight + headerLabelHeight : headerLabelHeight) + "px", lineHeight: (minUnit === 'year' ? headerLabelGroupHeight + headerLabelHeight : headerLabelHeight) + "px", fontSize: (labelWidth > 30 ? '14' : labelWidth > 20 ? '12' : '10') + "px", cursor: 'pointer' } }, _this.subHeaderLabel(time, minUnit, labelWidth))); }); var zIndex = this.props.zIndex; var headerStyle = { height: headerLabelGroupHeight + headerLabelHeight + "px", lineHeight: lineHeight + "px" }; if (fixedHeader === 'fixed') { headerStyle.position = 'fixed'; headerStyle.width = '100%'; headerStyle.zIndex = zIndex; } else if (fixedHeader === 'absolute') { var componentTop = this.state.componentTop; if (scrollTop >= componentTop) { headerStyle.position = 'absolute'; headerStyle.top = scrollTop - componentTop + "px"; headerStyle.width = canvasWidth + "px"; headerStyle.left = '0'; } } return (React.createElement("div", { ref: 'header', key: 'header', className: 'rct-header', onTouchStart: this.touchStart, onTouchEnd: this.touchEnd, onClick: this.periodClick, style: headerStyle }, timeLabels)); }; return Header; }(React.Component)); exports.default = Header; //# sourceMappingURL=Header.js.map