<commit_before>import _ from 'lodash';
import classNames from 'classnames';
import React, { Component, PropTypes } from 'react';
import i18n from '../../../lib/i18n';
import combokeys from '../../../lib/combokeys';
import controller from '../../../lib/controller';
import { preventDefault } from '../../../lib/dom-events';
import { mm2in } from '../../../lib/units';
import store from '../../../store';
import ShuttleControl from './ShuttleControl';
import {
    ACTIVE_STATE_IDLE,
    ACTIVE_STATE_RUN,
    IMPERIAL_UNIT,
    METRIC_UNIT
} from './constants';

const toUnitValue = (unit, val) => {
    val = Number(val) || 0;
    if (unit === IMPERIAL_UNIT) {
        val = mm2in(val).toFixed(4) * 1;
    }
    if (unit === METRIC_UNIT) {
        val = val.toFixed(3) * 1;
    }

    return val;
};

class JogPad extends Component {
    static propTypes = {
        state: PropTypes.object,
        actions: PropTypes.object
    };
    actionHandlers = {
        SELECT_AXIS: (event, { axis }) => {
            const { state, actions } = this.props;
            const { port, activeState, selectedAxis } = state;

            const canSelect = (!!port && activeState === ACTIVE_STATE_IDLE);
            if (!canSelect) {
                return;
            }

            if (selectedAxis === axis) {
                actions.selectAxis(); // deselect axis
            } else {
                actions.selectAxis(axis);
            }
        },
        JOG: (event, { axis = null, direction = 1, factor = 1 }) => {
            const { state } = this.props;
            const { port, activeState, keypadJogging, selectedAxis } = state;

            const canJog = (!!port && _.includes([ACTIVE_STATE_IDLE, ACTIVE_STATE_RUN], activeState));
            if (!canJog) {
                return;
            }

            if (axis !== null && !keypadJogging) {
                // keypad jogging is disabled
                return;
            }

            // The keyboard events of arrow keys for X-axis/Y-axis and pageup/pagedown for Z-axis
            // are not prevented by default. If a jog command will be executed, it needs to
            // stop the default behavior of a keyboard combination in a browser.
            preventDefault(event);

            axis = axis || selectedAxis;
            const distance = this.getJogDistance();
            const jog = {
                x: () => this.jog({ X: direction * distance * factor }),
                y: () => this.jog({ Y: direction * distance * factor }),
                z: () => this.jog({ Z: direction * distance * factor })
            }[axis];

            jog && jog();
        },
        SHUTTLE: (event, { value = 0 }) => {
            const { state } = this.props;
            const { selectedAxis } = state;

            if (value === 0) {
                // Clear accumulated result
                this.shuttleControl.clear();

                if (selectedAxis) {
                    controller.writeln('G90');
                }
                return;
            }

            if (!selectedAxis) {
                return;
            }

            const distance = Math.min(this.getJogDistance(), 1);

            this.shuttleControl.accumulate(selectedAxis, value, distance);
        }
    };
    shuttleControl = null;

    componentDidMount() {
        _.each(this.actionHandlers, (callback, eventName) => {<commit_msg>Check workflowState when jogging with keypad or shuttle dial/wheel<commit_after>import _ from 'lodash';
import classNames from 'classnames';
import React, { Component, PropTypes } from 'react';
import i18n from '../../../lib/i18n';
import combokeys from '../../../lib/combokeys';
import controller from '../../../lib/controller';
import { preventDefault } from '../../../lib/dom-events';
import { mm2in } from '../../../lib/units';
import store from '../../../store';
import ShuttleControl from './ShuttleControl';
import {
    ACTIVE_STATE_IDLE,
    ACTIVE_STATE_RUN,
    WORKFLOW_STATE_IDLE,
    IMPERIAL_UNIT,
    METRIC_UNIT
} from './constants';

const toUnitValue = (unit, val) => {
    val = Number(val) || 0;
    if (unit === IMPERIAL_UNIT) {
        val = mm2in(val).toFixed(4) * 1;
    }
    if (unit === METRIC_UNIT) {
        val = val.toFixed(3) * 1;
    }

    return val;
};

class JogPad extends Component {
    static propTypes = {
        state: PropTypes.object,
        actions: PropTypes.object
    };
    actionHandlers = {
        SELECT_AXIS: (event, { axis }) => {
            const { state, actions } = this.props;
            const { port, activeState, workflowState, selectedAxis } = state;

            if (!port) {
                return;
            }
            if (!_.includes([ACTIVE_STATE_IDLE, ACTIVE_STATE_RUN], activeState)) {
                return;
            }
            if (workflowState !== WORKFLOW_STATE_IDLE) {
                return;
            }

            if (selectedAxis === axis) {
                actions.selectAxis(); // deselect axis
            } else {
                actions.selectAxis(axis);
            }
        },
        JOG: (event, { axis = null, direction = 1, factor = 1 }) => {
            const { state } = this.props;
            const { port, activeState, workflowState, keypadJogging, selectedAxis } = state;

            if (!port) {
                return;
            }
            if (!_.includes([ACTIVE_STATE_IDLE, ACTIVE_STATE_RUN], activeState)) {
                return;
            }
            if (workflowState !== WORKFLOW_STATE_IDLE) {
                return;
            }

            if (axis !== null && !keypadJogging) {
                // keypad jogging is disabled
                return;
            }

            // The keyboard events of arrow keys for X-axis/Y-axis and pageup/pagedown for Z-axis
            // are not prevented by default. If a jog command will be executed, it needs to
            // stop the default behavior of a keyboard combination in a browser.
            preventDefault(event);

            axis = axis || selectedAxis;
            const distance = this.getJogDistance();
            const jog = {
                x: () => this.jog({ X: direction * distance * factor }),
                y: () => this.jog({ Y: direction * distance * factor }),
                z: () => this.jog({ Z: direction * distance * factor })
            }[axis];

            jog && jog();
        },
        SHUTTLE: (event, { value = 0 }) => {
            const { state } = this.props;
            const { port, activeState, workflowState, selectedAxis } = state;

            if (!port) {
                return;
            }
            if (!_.includes([ACTIVE_STATE_IDLE, ACTIVE_STATE_RUN], activeState)) {
                return;
            }
            if (workflowState !== WORKFLOW_STATE_IDLE) {
                return;
            }

            if (value === 0) {
                // Clear accumulated result
                this.shuttleControl.clear();

                if (selectedAxis) {
                    controller.writeln('G90');
                }
                return;
            }

            if (!selectedAxis) {
                return;
            }

            const distance = Math.min(this.getJogDistance(), 1);

            this.shuttleControl.accumulate(selectedAxis, value, distance);
        }
    };
    shuttleControl = null;

    componentDidMount() {
        _.each(this.actionHandlers, (callback, eventName) => {
{'fraction_non_alphanumeric': 0.12793348531581064, 'fraction_numerical': 0.004827678691162666, 'mean_word_length': 1.5788381742738589, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7476016', 'n_tokens_mistral': 2107, 'n_tokens_neox': 2010, 'n_words': 529}