import _defineProperty from"babel-runtime/helpers/defineProperty";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";import*as React from"react";import*as ReactDOM from"react-dom";import Menu,{SubMenu,Item as MenuItem}from"rc-menu";import closest from"dom-closest";import classNames from"classnames";import Dropdown from"../dropdown";import Icon from"../icon";import Checkbox from"../checkbox";import Radio from"../radio";import FilterDropdownMenuWrapper from"./FilterDropdownMenuWrapper";var FilterMenu=function(e){function t(e){_classCallCheck(this,t);var r=_possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));r.setNeverShown=function(e){var t=ReactDOM.findDOMNode(r);!!closest(t,".dt-table-scroll")&&(r.neverShown=!!e.fixed)},r.setSelectedKeys=function(e){var t=e.selectedKeys;r.setState({selectedKeys:t})},r.handleClearFilters=function(){r.setState({selectedKeys:[]},r.handleConfirm)},r.handleConfirm=function(){r.setVisible(!1),r.confirmFilter()},r.onVisibleChange=function(e){r.setVisible(e),e||r.confirmFilter()},r.handleMenuItemClick=function(e){if(!(e.keyPath.length<=1)){var t=r.state.keyPathOfSelectedItem;r.state.selectedKeys.indexOf(e.key)>=0?delete t[e.key]:t[e.key]=e.keyPath,r.setState({keyPathOfSelectedItem:t})}},r.renderFilterIcon=function(){var e=r.props,t=e.column,n=e.locale,l=e.prefixCls,i=t.filterIcon,s=r.props.selectedKeys.length>0?l+"-selected":"";return i?React.cloneElement(i,{title:n.filterTitle,className:classNames(i.className,_defineProperty({},l+"-icon",!0))}):React.createElement(Icon,{title:n.filterTitle,type:"filter",className:s})};var n="filterDropdownVisible"in e.column&&e.column.filterDropdownVisible;return r.state={selectedKeys:e.selectedKeys,keyPathOfSelectedItem:{},visible:n},r}return _inherits(t,e),_createClass(t,[{key:"componentDidMount",value:function(){var e=this.props.column;this.setNeverShown(e)}},{key:"componentWillReceiveProps",value:function(e){var t=e.column;this.setNeverShown(t);var r={};"selectedKeys"in e&&(r.selectedKeys=e.selectedKeys),"filterDropdownVisible"in t&&(r.visible=t.filterDropdownVisible),Object.keys(r).length>0&&this.setState(r)}},{key:"setVisible",value:function(e){var t=this.props.column;"filterDropdownVisible"in t||this.setState({visible:e}),t.onFilterDropdownVisibleChange&&t.onFilterDropdownVisibleChange(e)}},{key:"confirmFilter",value:function(){this.state.selectedKeys!==this.props.selectedKeys&&this.props.confirmFilter(this.props.column,this.state.selectedKeys)}},{key:"renderMenuItem",value:function(e){var t=this.props.column,r=!("filterMultiple"in t)||t.filterMultiple?React.createElement(Checkbox,{checked:this.state.selectedKeys.indexOf(e.value.toString())>=0}):React.createElement(Radio,{checked:this.state.selectedKeys.indexOf(e.value.toString())>=0});return React.createElement(MenuItem,{key:e.value},r,React.createElement("span",null,e.text))}},{key:"hasSubMenu",value:function(){var e=this.props.column.filters;return(void 0===e?[]:e).some(function(e){return!!(e.children&&e.children.length>0)})}},{key:"renderMenus",value:function(e){var t=this;return e.map(function(e){if(e.children&&e.children.length>0){var r=t.state.keyPathOfSelectedItem,n=Object.keys(r).some(function(t){return r[t].indexOf(e.value)>=0})?t.props.dropdownPrefixCls+"-submenu-contain-selected":"";return React.createElement(SubMenu,{title:e.text,className:n,key:e.value.toString()},t.renderMenus(e.children))}return t.renderMenuItem(e)})}},{key:"render",value:function(){var e=this.props,t=e.column,r=e.locale,n=e.prefixCls,l=e.dropdownPrefixCls,i=e.getPopupContainer,s=!("filterMultiple"in t)||t.filterMultiple,o=classNames(_defineProperty({},l+"-menu-without-submenu",!this.hasSubMenu())),a=t.filterDropdown?React.createElement(FilterDropdownMenuWrapper,null,t.filterDropdown):React.createElement(FilterDropdownMenuWrapper,{className:n+"-dropdown"},React.createElement(Menu,{multiple:s,onClick:this.handleMenuItemClick,prefixCls:l+"-menu",className:o,onSelect:this.setSelectedKeys,onDeselect:this.setSelectedKeys,selectedKeys:this.state.selectedKeys},this.renderMenus(t.filters)),React.createElement("div",{className:n+"-dropdown-btns"},React.createElement("a",{className:n+"-dropdown-link confirm",onClick:this.handleConfirm},r.filterConfirm),React.createElement("a",{className:n+"-dropdown-link clear",onClick:this.handleClearFilters},r.filterReset)));return React.createElement(Dropdown,{trigger:["click"],overlay:a,visible:!this.neverShown&&this.state.visible,onVisibleChange:this.onVisibleChange,getPopupContainer:i,forceRender:!0},this.renderFilterIcon())}}]),t}(React.Component);export default FilterMenu;FilterMenu.defaultProps={handleFilter:function(){},column:{}};