Skip to content
Snippets Groups Projects
Slider.js 6.12 KiB
Newer Older
glennvorhes's avatar
glennvorhes committed
"use strict";
Glenn Vorhes's avatar
Glenn Vorhes committed
/**
 * Created by glenn on 7/6/2017.
 */
Object.defineProperty(exports, "__esModule", { value: true });
Glenn Vorhes's avatar
Glenn Vorhes committed
const React = require("react");
const makeGuid_1 = require("../util/makeGuid");
const get_browser_1 = require("../util/get_browser");
class Slider extends React.Component {
    constructor(props, context) {
        super(props, context);
        this.uid = makeGuid_1.default();
        this.startUid = makeGuid_1.default();
        this.stopUid = makeGuid_1.default();
        this.intervalUid = makeGuid_1.default();
        this.previousUid = makeGuid_1.default();
        this.nextUid = makeGuid_1.default();
        this.running = false;
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
Glenn Vorhes's avatar
Glenn Vorhes committed
    componentDidMount() {
Glenn Vorhes's avatar
Glenn Vorhes committed
        this.el = document.getElementById(this.uid);
        this.minVal = parseFloat(this.el.min);
        this.maxVal = parseFloat(this.el.max);
        this.step = parseFloat(this.el.step);
        this.startButton = document.getElementById(this.startUid);
        this.stopButton = document.getElementById(this.stopUid);
        if (this.props.animate) {
            this.stopButton.style.display = 'none';
        }
        this.previousButton = document.getElementById(this.previousUid);
        this.nextButton = document.getElementById(this.nextUid);
        this.intervalSelect = document.getElementById(this.intervalUid);
        if (get_browser_1.get_browser().name.toUpperCase().indexOf('IE') > -1) {
Glenn Vorhes's avatar
Glenn Vorhes committed
            this.el.onchange = (e) => {
                this.props.change(parseFloat(e.target['value']));
Glenn Vorhes's avatar
Glenn Vorhes committed
            };
        }
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    updateRunning() {
Glenn Vorhes's avatar
Glenn Vorhes committed
        this.el.disabled = this.running;
        this.startButton.style.display = this.running ? 'none' : '';
        this.stopButton.style.display = this.running ? '' : 'none';
        this.nextButton.disabled = this.running;
        this.previousButton.disabled = this.running;
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    startAnimate() {
Glenn Vorhes's avatar
Glenn Vorhes committed
        this.running = true;
        this.updateRunning();
Glenn Vorhes's avatar
Glenn Vorhes committed
        this.interval = setInterval(() => {
            let val = parseFloat(this.el.value);
            val += this.step;
            if (val > this.maxVal) {
                val = this.minVal;
Glenn Vorhes's avatar
Glenn Vorhes committed
            }
Glenn Vorhes's avatar
Glenn Vorhes committed
            this.el.value = val.toString();
            this.props.change(val);
Glenn Vorhes's avatar
Glenn Vorhes committed
        }, parseInt(this.intervalSelect.value));
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    stopAnimate() {
Glenn Vorhes's avatar
Glenn Vorhes committed
        clearInterval(this.interval);
        this.running = false;
        this.updateRunning();
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    restartAnimate() {
Glenn Vorhes's avatar
Glenn Vorhes committed
        if (this.running) {
            this.stopAnimate();
            this.startAnimate();
        }
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    increment(v) {
        let val = parseFloat(this.el.value);
Glenn Vorhes's avatar
Glenn Vorhes committed
        val = v > 0 ? val + this.step : val - this.step;
        this.el.value = val.toString();
        this.props.change(val);
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    render() {
        let attrs = {
Glenn Vorhes's avatar
Glenn Vorhes committed
            id: this.uid,
            min: 0,
            type: 'range',
Glenn Vorhes's avatar
Glenn Vorhes committed
            onChange: (evt) => {
                this.props.change(parseFloat(evt.target.value));
Glenn Vorhes's avatar
Glenn Vorhes committed
            },
            style: { width: '100%', padding: '4px 0' },
            max: "100",
            step: '0.1',
            value: this.props.value ? this.props.value.toString() : '0',
            defaultValue: "0"
        };
        if (this.props.steps) {
            attrs.max = this.props.steps.toString();
            attrs.step = '1';
        }
        if (this.props.value) {
            delete attrs.defaultValue;
        }
        else {
            delete attrs.value;
        }
Glenn Vorhes's avatar
Glenn Vorhes committed
        let start = null;
        let stop = null;
        let previous = null;
        let next = null;
        let intervalSelect = null;
        let interval = "200";
Glenn Vorhes's avatar
Glenn Vorhes committed
        if (this.props.defaultAnimationInterval) {
            interval = this.props.defaultAnimationInterval.toFixed();
        }
        if (this.props.animate) {
Glenn Vorhes's avatar
Glenn Vorhes committed
            previous = React.createElement("button", { id: this.previousUid, className: "react-slider-previous", onClick: () => {
                    this.increment(-1);
Glenn Vorhes's avatar
Glenn Vorhes committed
                }, title: "Previous" });
Glenn Vorhes's avatar
Glenn Vorhes committed
            next = React.createElement("button", { id: this.nextUid, className: "react-slider-next", onClick: () => {
                    this.increment(1);
Glenn Vorhes's avatar
Glenn Vorhes committed
                }, title: "Next" });
Glenn Vorhes's avatar
Glenn Vorhes committed
            start = React.createElement("button", { id: this.startUid, className: "react-slider-start", onClick: () => {
                    this.startAnimate();
Glenn Vorhes's avatar
Glenn Vorhes committed
                }, title: "Start" });
Glenn Vorhes's avatar
Glenn Vorhes committed
            stop = React.createElement("button", { id: this.stopUid, className: "react-slider-stop", onClick: () => {
                    this.stopAnimate();
Glenn Vorhes's avatar
Glenn Vorhes committed
                }, title: "Stop" });
Glenn Vorhes's avatar
Glenn Vorhes committed
            intervalSelect = React.createElement("span", null,
                React.createElement("label", { style: { fontWeight: 'bold', marginRight: '3px' } }, "Interval (s)"),
                React.createElement("select", { defaultValue: interval, id: this.intervalUid, onChange: () => {
                        this.restartAnimate();
Glenn Vorhes's avatar
Glenn Vorhes committed
                    } },
Glenn Vorhes's avatar
Glenn Vorhes committed
                    React.createElement("option", { value: "100" }, "0.1"),
                    React.createElement("option", { value: "200" }, "0.2"),
                    React.createElement("option", { value: "300" }, "0.3"),
                    React.createElement("option", { value: "400" }, "0.4"),
                    React.createElement("option", { value: "500" }, "0.5"),
                    React.createElement("option", { value: "600" }, "0.6"),
                    React.createElement("option", { value: "700" }, "0.7"),
                    React.createElement("option", { value: "800" }, "0.8"),
                    React.createElement("option", { value: "900" }, "0.9"),
                    React.createElement("option", { value: "1000" }, "1.0")));
Glenn Vorhes's avatar
Glenn Vorhes committed
        }
Glenn Vorhes's avatar
Glenn Vorhes committed
        return React.createElement("div", { className: "react-slider" },
            React.createElement("input", Object.assign({}, attrs)),
            React.createElement("div", { className: "react-slider-controls", style: { textAlign: 'center' } },
Glenn Vorhes's avatar
Glenn Vorhes committed
                previous,
                start,
                stop,
                next,
                intervalSelect));
Glenn Vorhes's avatar
Glenn Vorhes committed
Slider.defaultProps = {
    steps: null,
    animate: null,
    defaultAnimationInterval: null,
    value: null
};
Glenn Vorhes's avatar
Glenn Vorhes committed
exports.Slider = Slider;
//# sourceMappingURL=Slider.js.map