import _extends from"babel-runtime/helpers/extends";import _toConsumableArray from"babel-runtime/helpers/toConsumableArray";import _classCallCheck from"babel-runtime/helpers/classCallCheck";import _createClass from"babel-runtime/helpers/createClass";import _possibleConstructorReturn from"babel-runtime/helpers/possibleConstructorReturn";import _inherits from"babel-runtime/helpers/inherits";var __rest=this&&this.__rest||function(e,t){var n={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(n[o]=e[o]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(o=Object.getOwnPropertySymbols(e);r<o.length;r++)t.indexOf(o[r])<0&&(n[o[r]]=e[o[r]])}return n};import*as React from"react";import classNames from"classnames";import omit from"omit.js";import debounce from"lodash.debounce";import{conductExpandParent,convertTreeToEntities}from"rc-tree/es/util";import Tree from"./Tree";import{calcRangeKeys,getFullKeyList}from"./util";import Icon from"../icon";function getIcon(e){var t=e.isLeaf,n=e.expanded;return t?React.createElement(Icon,{type:"file"}):React.createElement(Icon,{type:n?"folder-open":"folder"})}var DirectoryTree=function(e){function t(e){_classCallCheck(this,t);var n=_possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));n.onExpand=function(e,t){var o=n.props.onExpand;if(n.setUncontrolledState({expandedKeys:e}),o)return o(e,t)},n.onClick=function(e,t){var o=n.props,r=o.onClick;"click"===o.expandAction&&n.onDebounceExpand(e,t),r&&r(e,t)},n.onDoubleClick=function(e,t){var o=n.props,r=o.onDoubleClick;"doubleClick"===o.expandAction&&n.onDebounceExpand(e,t),r&&r(e,t)},n.onSelect=function(e,t){var o=n.props,r=o.onSelect,s=o.multiple,a=o.children,l=n.state,c=l.expandedKeys,i=void 0===c?[]:c,d=l.selectedKeys,p=void 0===d?[]:d,y=t.node,u=t.nativeEvent,m=y.props.eventKey,f=void 0===m?"":m,x={},K=u.ctrlKey||u.metaKey,h=u.shiftKey,b=p.slice();s&&K?(b=e,n.lastSelectedKey=f,n.cachedSelectedKeys=b):s&&h?b=Array.from(new Set([].concat(_toConsumableArray(n.cachedSelectedKeys||[]),_toConsumableArray(calcRangeKeys(a,i,f,n.lastSelectedKey))))):(b=[f],n.lastSelectedKey=f,n.cachedSelectedKeys=b),x.selectedKeys=b,r&&r(b,t),n.setUncontrolledState(x)},n.expandFolderNode=function(e,t){var o=n.state.expandedKeys,r=void 0===o?[]:o,s=n.props.onExpand,a=t.props,l=a.eventKey,c=void 0===l?"":l,i=a.expanded;if(!(a.isLeaf||e.shiftKey||e.metaKey||e.ctrlKey)){var d=r.slice(),p=d.indexOf(c);i&&p>=0?d.splice(p,1):i||-1!==p||d.push(c),n.setUncontrolledState({expandedKeys:d}),s&&s(d,{expanded:!i,node:t,nativeEvent:e.nativeEvent})}},n.setUncontrolledState=function(e){var t=omit(e,Object.keys(n.props));Object.keys(t).length&&n.setState(t)};var o=e.defaultExpandAll,r=e.defaultExpandParent,s=e.expandedKeys,a=e.defaultExpandedKeys,l=e.children,c=convertTreeToEntities(l).keyEntities;return n.state={selectedKeys:e.selectedKeys||e.defaultSelectedKeys||[]},n.state.expandedKeys=o?getFullKeyList(e.children):r?conductExpandParent(s||a,c):a,n.onDebounceExpand=debounce(n.expandFolderNode,200,{leading:!0}),n}return _inherits(t,e),_createClass(t,[{key:"componentWillReceiveProps",value:function(e){"expandedKeys"in e&&this.setState({expandedKeys:e.expandedKeys}),"selectedKeys"in e&&this.setState({selectedKeys:e.selectedKeys})}},{key:"render",value:function(){var e=this.props,t=e.prefixCls,n=e.className,o=__rest(e,["prefixCls","className"]),r=this.state,s=r.expandedKeys,a=r.selectedKeys,l=classNames(t+"-directory",n);return React.createElement(Tree,_extends({icon:getIcon},o,{prefixCls:t,className:l,expandedKeys:s,selectedKeys:a,onSelect:this.onSelect,onClick:this.onClick,onDoubleClick:this.onDoubleClick,onExpand:this.onExpand}))}}]),t}(React.Component);export default DirectoryTree;DirectoryTree.defaultProps={prefixCls:"dt-tree",showIcon:!0,expandAction:"click"};