'use strict';

exports.__esModule = true;
exports.default = undefined;

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 _class, _temp; // require PrototypeArray.js


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _instance = require('./instance.js');

var _instance2 = _interopRequireDefault(_instance);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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 Tree = (_temp = _class = function (_Component) {
  _inherits(Tree, _Component);

  function Tree(props) {
    _classCallCheck(this, Tree);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

    _this.componentDidUpdate = function (prevProps) {
      if (JSON.stringify(prevProps.list) !== JSON.stringify(_this.props.list)) {
        if (_this.props.list && _this.props.list.length) {
          var selected = _this.props.selected;

          var list = Object.clone(_this.props.list);
          if (JSON.stringify(list).indexOf('"children"') !== -1) {
            list = list.flattenTree();
          }
          // 设置已选中
          if (Array.isArray(selected) && selected.length) {
            for (var _iterator = selected, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
              var _ref;

              if (_isArray) {
                if (_i >= _iterator.length) break;
                _ref = _iterator[_i++];
              } else {
                _i = _iterator.next();
                if (_i.done) break;
                _ref = _i.value;
              }

              var opt = _ref;

              _this.instance.addSelected(opt);
            }
          }
          // 开始渲染
          _this.instance.setData(list);
          _this.instance.update();
        } else {
          _this.instance.setData([]);
          _this.instance.update();
        }
      }
    };

    _this.componentDidMount = function () {
      if (_this.instance) return;
      var _this$props = _this.props,
          multiple = _this$props.multiple,
          checkbox = _this$props.checkbox,
          bar = _this$props.bar,
          _this$props$buttonAdd = _this$props.buttonAddAttribute,
          buttonAddAttribute = _this$props$buttonAdd === undefined ? {} : _this$props$buttonAdd,
          _this$props$buttonDel = _this$props.buttonDelAttribute,
          buttonDelAttribute = _this$props$buttonDel === undefined ? {} : _this$props$buttonDel,
          onClickLastChild = _this$props.onClickLastChild,
          onData = _this$props.onData;
      // 更新数据

      var list = Object.clone(_this.props.list);
      if (JSON.stringify(list).indexOf('"children"') !== -1) {
        list = list.flattenTree();
      }
      var instance = new _instance2.default(_this.$tree, {
        data: list,
        multiple: multiple,
        checkbox: checkbox,
        bar: bar,
        buttonAddClass: buttonAddAttribute.className,
        onClickAdd: buttonAddAttribute.onClick,
        buttonDelClass: buttonDelAttribute.className,
        onClickDel: buttonDelAttribute.onClick,
        onClickLastChild: onClickLastChild, // 没有子节点
        onClick: _this.onClick,
        onData: onData
      });
      _this.instance = instance;
      // 设置已选中
      var selected = _this.props.selected;

      if (Array.isArray(selected) && selected.length) {
        for (var _iterator2 = selected, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
          var _ref2;

          if (_isArray2) {
            if (_i2 >= _iterator2.length) break;
            _ref2 = _iterator2[_i2++];
          } else {
            _i2 = _iterator2.next();
            if (_i2.done) break;
            _ref2 = _i2.value;
          }

          var opt = _ref2;

          _this.instance.addSelected(opt);
        }
      }
      _this.instance.update();
    };

    _this.onClick = function (s) {
      var selected = _this.props.selected;

      var list = Object.clone(_this.props.list);
      if (JSON.stringify(list).indexOf('"children"') !== -1) {
        list = list.flattenTree();
      }
      // item
      var id = s.targetLine.getAttribute('data-id');
      var item = list.getFlattenTreeNode(id);
      // isActived
      var isActived = selected ? selected.filter(function (option) {
        if (option.id === id) return true;
        return false;
      }) : null;
      if (isActived && isActived.length > 0) {
        isActived = true;
      } else {
        isActived = false;
      }
      // childrenCount
      var childrenCount = 0;
      var ul = s.targetLine.nextElementSibling;
      if (ul && ul.tagName === 'UL') {
        childrenCount = ul.children.length;
      }
      // isExtend
      var isExtend = s.targetLine.classList.contains('extend');

      if (_this.props.onClick) _this.props.onClick(s, item, isActived, isExtend, childrenCount);
    };

    return _this;
  }

  Tree.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        _props$treeAttribute = _props.treeAttribute,
        treeAttribute = _props$treeAttribute === undefined ? {} : _props$treeAttribute,
        multiple = _props.multiple,
        checkbox = _props.checkbox,
        bar = _props.bar,
        selected = _props.selected,
        list = _props.list,
        buttonAddAttribute = _props.buttonAddAttribute,
        buttonDelAttribute = _props.buttonDelAttribute,
        onClickLastChild = _props.onClickLastChild,
        onClick = _props.onClick,
        onData = _props.onData,
        others = _objectWithoutProperties(_props, ['treeAttribute', 'multiple', 'checkbox', 'bar', 'selected', 'list', 'buttonAddAttribute', 'buttonDelAttribute', 'onClickLastChild', 'onClick', 'onData']);

    return _react2.default.createElement(
      'div',
      _extends({ ref: function ref(el) {
          _this2.$el = el;
        } }, others, { className: 'tree-box' + (others.className ? ' ' + others.className : '') }),
      _react2.default.createElement('ul', _extends({ ref: function ref(el) {
          _this2.$tree = el;
        } }, treeAttribute, { className: 'tree' + (treeAttribute.className ? ' ' + treeAttribute.className : '') }))
    );
  };

  return Tree;
}(_react.Component), _class.defaultProps = {
  multiple: true,
  list: []
}, _temp);
exports.default = Tree;
Tree.propTypes = process.env.NODE_ENV !== "production" ? {
  treeAttribute: _propTypes2.default.object,

  multiple: _propTypes2.default.bool, // 是否需要多选
  checkbox: _propTypes2.default.bool, // 是否可选
  bar: _propTypes2.default.oneOfType([// 选中栏
  _propTypes2.default.string, _propTypes2.default.node]),
  selected: _propTypes2.default.array, // [{id: '', name: '', parentid: ''}]
  list: _propTypes2.default.array, // [{id: '', name: '', parentid: ''}]

  buttonAddAttribute: _propTypes2.default.object, // {className: '', onClick: func()}
  buttonDelAttribute: _propTypes2.default.object, // {className: '', onClick: func()}

  onClickLastChild: _propTypes2.default.func,

  onClick: _propTypes2.default.func,
  onData: _propTypes2.default.func
} : {};
module.exports = exports['default'];