'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _CustomKeyboard = require('./CustomKeyboard');

var _CustomKeyboard2 = _interopRequireDefault(_CustomKeyboard);

var _class = require('../_util/class');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var NumberInput = function (_React$Component) {
    (0, _inherits3['default'])(NumberInput, _React$Component);

    function NumberInput() {
        (0, _classCallCheck3['default'])(this, NumberInput);

        var _this = (0, _possibleConstructorReturn3['default'])(this, (NumberInput.__proto__ || Object.getPrototypeOf(NumberInput)).apply(this, arguments));

        _this.state = {
            focus: false
        };
        _this.addBlurListener = function () {
            document.addEventListener('click', _this.doBlur, false);
        };
        _this.removeBlurListen = function () {
            document.removeEventListener('click', _this.doBlur, false);
        };
        _this.getComponent = function () {
            var _this$props = _this.props,
                keyboardPrefixCls = _this$props.keyboardPrefixCls,
                confirmLabel = _this$props.confirmLabel;

            return _react2['default'].createElement(_CustomKeyboard2['default'], { onClick: _this.onKeyboardClick, preixCls: keyboardPrefixCls, confirmLabel: confirmLabel });
        };
        _this.renderCustomKeyboard = function () {
            var container = document.querySelector('#' + _this.props.keyboardPrefixCls + '-container');
            if (!container) {
                container = document.createElement('div');
                container.setAttribute('id', _this.props.keyboardPrefixCls + '-container');
                document.body.appendChild(container);
                window.antmCustomKeyboard = _reactDom2['default'].unstable_renderSubtreeIntoContainer(_this, _this.getComponent(), container);
            }
        };
        _this.doBlur = function (ev) {
            var value = _this.props.value;

            if (ev.target !== _this.inputRef) {
                _this.onInputBlur(value);
            }
        };
        _this.unLinkInput = function () {
            var antmCustomKeyboard = window.antmCustomKeyboard;
            if (antmCustomKeyboard.linkedInput === _this) {
                antmCustomKeyboard.linkedInput = null;
                (0, _class.addClass)(antmCustomKeyboard.antmKeyboard, _this.props.keyboardPrefixCls + '-wrapper-hide');
                _this.removeBlurListen();
            }
        };
        _this.onInputBlur = function (value) {
            var focus = _this.state.focus;

            if (focus) {
                _this.setState({
                    focus: false
                });
                _this.props.onBlur(value);
                setTimeout(function () {
                    _this.unLinkInput();
                }, 50);
            }
        };
        _this.onInputFocus = function () {
            var value = _this.props.value;

            _this.props.onFocus(value);
            _this.setState({
                focus: true
            }, function () {
                var antmCustomKeyboard = window.antmCustomKeyboard;
                antmCustomKeyboard.linkedInput = _this;
                (0, _class.removeClass)(antmCustomKeyboard.antmKeyboard, _this.props.keyboardPrefixCls + '-wrapper-hide');
                antmCustomKeyboard.confirmDisabled = value === '';
                if (value === '') {
                    (0, _class.addClass)(antmCustomKeyboard.confirmKeyboardItem, _this.props.keyboardPrefixCls + '-item-disabled');
                } else {
                    (0, _class.removeClass)(antmCustomKeyboard.confirmKeyboardItem, _this.props.keyboardPrefixCls + '-item-disabled');
                }
            });
        };
        _this.onKeyboardClick = function (KeyboardItemValue) {
            var _this$props2 = _this.props,
                value = _this$props2.value,
                onChange = _this$props2.onChange,
                maxLength = _this$props2.maxLength;

            var valueAfterChange = void 0;
            // 删除键
            if (KeyboardItemValue === 'delete') {
                valueAfterChange = value.substring(0, value.length - 1);
                onChange({ target: { value: valueAfterChange } });
                // 确认键
            } else if (KeyboardItemValue === 'confirm') {
                valueAfterChange = value;
                onChange({ target: { value: valueAfterChange } });
                _this.onInputBlur(value);
                // 收起键
            } else if (KeyboardItemValue === 'hide') {
                valueAfterChange = value;
                _this.onInputBlur(valueAfterChange);
            } else {
                if (maxLength !== undefined && +maxLength >= 0 && (value + KeyboardItemValue).length > maxLength) {
                    valueAfterChange = (value + KeyboardItemValue).substr(0, maxLength);
                    onChange({ target: { value: valueAfterChange } });
                } else {
                    valueAfterChange = value + KeyboardItemValue;
                    onChange({ target: { value: valueAfterChange } });
                }
            }
            var antmCustomKeyboard = window.antmCustomKeyboard;
            antmCustomKeyboard.confirmDisabled = valueAfterChange === '';
            if (valueAfterChange === '') {
                (0, _class.addClass)(antmCustomKeyboard.confirmKeyboardItem, _this.props.keyboardPrefixCls + '-item-disabled');
            } else {
                (0, _class.removeClass)(antmCustomKeyboard.confirmKeyboardItem, _this.props.keyboardPrefixCls + '-item-disabled');
            }
        };
        _this.onFakeInputClick = function () {
            _this.focus();
        };
        _this.focus = function () {
            // this focus may invocked by users page button click, so this click may trigger blurEventListener at the same time
            _this.removeBlurListen();
            var focus = _this.state.focus;

            if (!focus) {
                _this.onInputFocus();
            }
            setTimeout(function () {
                _this.addBlurListener();
            }, 50);
        };
        return _this;
    }

    (0, _createClass3['default'])(NumberInput, [{
        key: 'componentDidMount',
        value: function componentDidMount() {
            if (!window.antmCustomKeyboard) {
                this.renderCustomKeyboard();
            }
        }
    }, {
        key: 'componentWillUnmount',
        value: function componentWillUnmount() {
            this.unLinkInput();
        }
    }, {
        key: 'render',
        value: function render() {
            var _this2 = this;

            var _props = this.props,
                placeholder = _props.placeholder,
                value = _props.value,
                disabled = _props.disabled,
                editable = _props.editable;
            var focus = this.state.focus;

            var preventKeyboard = disabled || !editable;
            var fakeInputCls = (0, _classnames2['default'])('fake-input', {
                focus: focus,
                'fake-input-disabled': disabled
            });
            return _react2['default'].createElement(
                'div',
                { className: 'fake-input-container' },
                value === '' && _react2['default'].createElement(
                    'div',
                    { className: 'fake-input-placeholder' },
                    placeholder
                ),
                _react2['default'].createElement(
                    'div',
                    { className: fakeInputCls, ref: function ref(el) {
                            return _this2.inputRef = el;
                        }, onClick: preventKeyboard ? function () {} : this.onFakeInputClick },
                    value
                )
            );
        }
    }]);
    return NumberInput;
}(_react2['default'].Component); /* tslint:disable:jsx-no-multiline-js */


NumberInput.defaultProps = {
    onChange: function onChange() {},
    onFocus: function onFocus() {},
    onBlur: function onBlur() {},
    placeholder: '',
    value: '',
    disabled: false,
    editable: true,
    prefixCls: 'am-input',
    keyboardPrefixCls: 'am-number-keyboard'
};
exports['default'] = NumberInput;
module.exports = exports['default'];