import React from 'react'; import PropTypes from 'prop-types'; import ReactCSSTransitionGroup from 'react-addons-css-transition-group'; import { Card, NgIf, Icon, EventEmitter, Dropdown, DropdownButton, DropdownList, DropdownItem, Container } from '../../components/'; import { pathBuilder, debounce, prompt } from '../../helpers/'; import "./submenu.scss"; @EventEmitter export class Submenu extends React.Component { constructor(props){ super(props); this.state = { search_input_visible: false, search_keyword: "" }; this.onSearchChange_Backpressure = debounce(this.onSearchChange, 400); this._onKeyPress = (e) => { if(e.keyCode === 27){ // escape key this.setState({ search_keyword: "", search_input_visible: false }); this.refs.$input.blur(); this.props.onSearch(null); }else if(e.ctrlKey && e.keyCode === 70){ // 'Ctrl F' shortcut to search e.preventDefault(); this.setState({ search_input_visible: true }); this.refs.$input.focus(); }else if(e.altKey && (e.keyCode === 49 || e.keyCode === 50)){ // 'alt 1' 'alt 2' shortcut e.preventDefault(); this.onViewChange(); } }; } componentDidMount(){ window.addEventListener("keydown", this._onKeyPress); } componentWillUnmount(){ window.removeEventListener("keydown", this._onKeyPress); } onNew(type){ this.props.emit("new::"+type); } onDelete(arrayOfPaths){ prompt.now( "Confirm by typing \"remove\"", (answer) => { if(answer !== "remove"){ return Promise.resolve(); } this.props.emit("file.delete.multiple", arrayOfPaths); return Promise.resolve(); }, () => { /* click on cancel */ } ); } onViewChange(){ requestAnimationFrame(() => this.props.onViewUpdate()); } onSortChange(e){ this.props.onSortUpdate(e); } onSearchChange(search, e){ this.props.onSearch(search.trim()); } onSearchToggle(){ if(new Date () - this.search_last_toggle < 200){ // avoid bluring event cancelling out the toogle return; } this.refs.$input.focus(); this.setState({search_input_visible: !this.state.search_input_visible}, () => { if(this.state.search_input_visible == false){ this.props.onSearch(null); this.setState({search_keyword: ""}); } }); } closeIfEmpty(){ if(this.state.search_keyword.trim().length > 0) return; this.search_last_toggle = new Date(); this.setState({ search_input_visible: false, search_keyword: "" }); this.props.onSearch(null); } onSearchKeypress(s, backpressure = true, e){ if(backpressure){ this.onSearchChange_Backpressure(s); }else{ this.onSearchChange(s); } this.setState({search_keyword: s}); if(e && e.preventDefault){ e.preventDefault(); } } render(){ return (
New File New Directory 0} type="inline" onMouseDown={this.onDelete.bind(this, this.props.selected)}> Remove Sort By Type Sort By Date Sort By Name
this.onSearchKeypress(this.state.search_keyword, false, e)}> this.onSearchKeypress(e.target.value, true)} type="text" id="search" placeholder="search" name="search" autoComplete="off" />
); }; } Submenu.propTypes = { accessRight: PropTypes.object, onSortUpdate: PropTypes.func.isRequired, sort: PropTypes.string.isRequired };