web-apps/apps/common/mobile/lib/view/Search.jsx

298 lines
11 KiB
React
Raw Normal View History

2021-01-22 21:20:31 +00:00
import React, { Component } from 'react';
import { Searchbar, Popover, Popup, View, Page, List, ListItem, Navbar, NavRight, Link } from 'framework7-react';
2021-01-27 13:36:16 +00:00
import { Toggle } from 'framework7-react';
2021-01-28 10:27:18 +00:00
import { f7 } from 'framework7-react';
import { Dom7 } from 'framework7';
2021-01-22 21:20:31 +00:00
import { Device } from '../../../../common/mobile/utils/device';
2021-01-28 10:27:18 +00:00
import { observable } from "mobx";
import { observer } from "mobx-react";
import { useTranslation, withTranslation } from 'react-i18next';
2021-01-28 10:27:18 +00:00
const searchOptions = observable({
usereplace: false
});
2021-01-22 21:20:31 +00:00
const popoverStyle = {
height: '300px'
};
2021-01-29 07:01:02 +00:00
const SEARCH_BACKWARD = 'back';
const SEARCH_FORWARD = 'next';
2021-01-22 21:20:31 +00:00
class SearchSettingsView extends Component {
constructor(props) {
super(props);
this.state = {
useReplace: false,
caseSensitive: false,
markResults: false
};
}
onFindReplaceClick(action) {
2021-01-28 10:27:18 +00:00
searchOptions.usereplace = action == 'replace';
2021-01-22 21:20:31 +00:00
this.setState({
2021-01-28 10:27:18 +00:00
useReplace: searchOptions.usereplace
2021-01-22 21:20:31 +00:00
});
2021-01-28 10:27:18 +00:00
if (this.onReplaceChecked) {}
2021-01-22 21:20:31 +00:00
}
2021-01-28 16:23:28 +00:00
extraSearchOptions() {
}
2021-01-22 21:20:31 +00:00
render() {
const show_popover = !Device.phone;
2021-01-22 21:20:31 +00:00
const navbar =
<Navbar title="Find and replace">
{!show_popover &&
<NavRight>
<Link popupClose=".search-settings-popup">Done</Link>
</NavRight>
}
2021-01-22 21:20:31 +00:00
</Navbar>;
2021-01-28 16:23:28 +00:00
const extra = this.extraSearchOptions();
2021-01-22 21:20:31 +00:00
const content =
<View style={popoverStyle}>
<Page>
{navbar}
<List>
2021-01-27 13:36:16 +00:00
<ListItem radio title="Find" name="find-replace-checkbox" checked={!this.state.useReplace} onClick={e => this.onFindReplaceClick('find')} />
<ListItem radio title="Find and replace" name="find-replace-checkbox" checked={this.state.useReplace} onClick={e => this.onFindReplaceClick('replace')} />
</List>
2021-01-28 16:23:28 +00:00
{ extra }
2021-01-22 21:20:31 +00:00
</Page>
</View>;
return (
show_popover ?
<Popover id="idx-search-settings" className="popover__titled">{content}</Popover> :
<Popup id="idx-search-settings" className="search-settings-popup">{content}</Popup>
2021-01-22 21:20:31 +00:00
)
}
}
2021-01-28 10:27:18 +00:00
@observer
2021-01-22 21:20:31 +00:00
class SearchView extends Component {
constructor(props) {
super(props);
2021-03-02 19:33:26 +00:00
this.state = {
searchQuery: '',
replaceQuery: ''
};
2021-01-22 21:20:31 +00:00
$$(document).on('page:init', (e, page) => {
if ( page.name == 'home' ) {
2021-01-28 10:27:18 +00:00
this.searchbar = f7.searchbar.create({
2021-01-22 21:20:31 +00:00
el: '.searchbar',
customSearch: true,
expandable: true,
backdrop: false,
on: {
search: (bar, curval, prevval) => {
2021-01-29 07:01:02 +00:00
},
enable: this.onSearchbarShow.bind(this, true),
disable: this.onSearchbarShow.bind(this, false)
2021-01-22 21:20:31 +00:00
}
});
2021-01-29 07:01:02 +00:00
// function iOSVersion() {
// var ua = navigator.userAgent;
// var m;
// return (m = /(iPad|iPhone|iphone).*?(OS |os |OS\_)(\d+((_|\.)\d)?((_|\.)\d)?)/.exec(ua)) ? parseFloat(m[3]) : 0;
// }
const $$ = Dom7;
const $editor = $$('#editor_sdk');
const $replaceLink = $$('#replace-link');
2021-01-29 07:01:02 +00:00
if (false /*iOSVersion() < 13*/) {
// $editor.single('mousedown touchstart', _.bind(me.onEditorTouchStart, me));
// $editor.single('mouseup touchend', _.bind(me.onEditorTouchEnd, me));
} else {
// $editor.single('pointerdown', this.onEditorTouchStart, me));
// $editor.single('pointerup', _.bind(me.onEditorTouchEnd, me));
}
$editor.on('pointerdown', this.onEditorTouchStart.bind(this));
$editor.on('pointerup', this.onEditorTouchEnd.bind(this));
// $replaceLink.on('click', this.onReplaceHold.bind(this));
2021-01-22 21:20:31 +00:00
}
});
this.onSettingsClick = this.onSettingsClick.bind(this);
2021-01-28 10:27:18 +00:00
this.onSearchClick = this.onSearchClick.bind(this);
2021-03-02 19:33:26 +00:00
this.onReplaceClick = this.onReplaceClick.bind(this);
2021-01-22 21:20:31 +00:00
}
componentDidMount(){
2021-01-28 10:27:18 +00:00
const $$ = Dom7;
2021-03-02 19:33:26 +00:00
this.$replace = $$('#idx-replace-val');
2021-01-22 21:20:31 +00:00
}
onSettingsClick(e) {
if ( Device.phone ) {
f7.popup.open('.search-settings-popup');
2021-01-24 21:30:49 +00:00
} else f7.popover.open('#idx-search-settings', '#idx-btn-search-settings');
2021-01-22 21:20:31 +00:00
}
2021-01-28 10:27:18 +00:00
searchParams() {
let params = {
2021-01-28 16:23:28 +00:00
find: this.searchbar.query
2021-01-28 10:27:18 +00:00
};
2021-01-28 16:23:28 +00:00
if (searchOptions.usereplace) {
2021-01-28 16:23:28 +00:00
params.replace = this.$replace.val();
}
2021-01-28 16:23:28 +00:00
return params;
2021-01-28 10:27:18 +00:00
}
onSearchClick(action) {
2021-03-09 19:42:29 +00:00
if (this.searchbar && this.state.searchQuery) {
if (this.props.onSearchQuery) {
2021-01-28 16:23:28 +00:00
let params = this.searchParams();
2021-03-09 19:42:29 +00:00
params.find = this.state.searchQuery;
2021-01-29 07:01:02 +00:00
params.forward = action != SEARCH_BACKWARD;
2021-03-09 19:42:29 +00:00
// console.log(params);
2021-01-28 10:27:18 +00:00
this.props.onSearchQuery(params);
}
}
}
2021-03-02 19:33:26 +00:00
onReplaceClick() {
2021-03-09 19:42:29 +00:00
if (this.searchbar && this.state.searchQuery) {
if (this.props.onReplaceQuery) {
2021-03-02 19:33:26 +00:00
let params = this.searchParams();
2021-03-09 19:42:29 +00:00
params.find = this.state.searchQuery;
// console.log(params);
2021-03-02 19:33:26 +00:00
this.props.onReplaceQuery(params);
}
}
}
onReplaceAllClick() {
2021-03-09 19:42:29 +00:00
if (this.searchbar && this.state.searchQuery) {
if (this.props.onReplaceAllQuery) {
let params = this.searchParams();
2021-03-09 19:42:29 +00:00
params.find = this.state.searchQuery;
// console.log(params);
this.props.onReplaceAllQuery(params);
}
}
}
2021-01-29 07:01:02 +00:00
onSearchbarShow(isshowed, bar) {
if ( !isshowed ) {
2021-03-02 19:33:26 +00:00
this.$replace.val('');
2021-01-29 07:01:02 +00:00
}
}
onEditorTouchStart(e) {
this.startPoint = this.pointerPosition(e);
}
onEditorTouchEnd(e) {
const endPoint = this.pointerPosition(e);
if ( this.searchbar.enabled ) {
const distance = (this.startPoint.x === undefined || this.startPoint.y === undefined) ? 0 :
Math.sqrt((endPoint.x -= this.startPoint.x) * endPoint.x + (endPoint.y -= this.startPoint.y) * endPoint.y);
if ( distance < 1 ) {
this.searchbar.disable();
}
}
}
pointerPosition(e) {
let out = {x:0, y:0};
if ( e.type == 'touchstart' || e.type == 'touchend' ) {
const touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
out.x = touch.pageX;
out.y = touch.pageY;
} else
if ( e.type == 'mousedown' || e.type == 'mouseup' ) {
out.x = e.pageX;
out.y = e.pageY;
}
return out;
}
2021-03-02 19:33:26 +00:00
changeSearchQuery(value) {
this.setState({
searchQuery: value
});
}
changeReplaceQuery(value) {
this.setState({
replaceQuery: value
});
}
2021-01-22 21:20:31 +00:00
render() {
2021-01-28 10:27:18 +00:00
const usereplace = searchOptions.usereplace;
const hidden = {display: "none"};
2021-03-02 19:33:26 +00:00
const searchQuery = this.state.searchQuery;
const replaceQuery = this.state.replaceQuery;
const isIos = Device.ios;
2021-03-09 19:42:29 +00:00
// const _t = this.t('View.Settings', {returnObjects: true});
2021-03-09 19:42:29 +00:00
// console.log(this.state.searchQuery, this.state.replaceQuery);
2021-03-02 19:33:26 +00:00
if(this.searchbar && this.searchbar.enabled) {
usereplace ? this.searchbar.el.classList.add('replace') : this.searchbar.el.classList.remove('replace');
2021-03-09 19:42:29 +00:00
}
2021-03-02 19:33:26 +00:00
2021-01-22 21:20:31 +00:00
return (
<form className="searchbar">
{isIos ? <div className="searchbar-bg"></div> : null}
2021-01-22 21:20:31 +00:00
<div className="searchbar-inner">
<div className="buttons-row searchbar-inner__left">
2021-01-22 21:20:31 +00:00
<a id="idx-btn-search-settings" className="link icon-only" onClick={this.onSettingsClick}>
<i className="icon icon-settings" />
</a>
</div>
<div className="searchbar-inner__center">
<div className="searchbar-input-wrap">
<input placeholder="Search" type="search" value={searchQuery}
onChange={e => {this.changeSearchQuery(e.target.value)}} />
{isIos ? <i className="searchbar-icon" /> : null}
<span className="input-clear-button" />
</div>
<div className="searchbar-input-wrap" style={!usereplace ? hidden: null}>
<input placeholder="Replace" type="search" id="idx-replace-val" value={replaceQuery}
onChange={e => {this.changeReplaceQuery(e.target.value)}} />
{isIos ? <i className="searchbar-icon" /> : null}
<span className="input-clear-button" />
</div>
2021-01-22 21:20:31 +00:00
</div>
<div className="buttons-row searchbar-inner__right">
<div className="buttons-row buttons-row-replace">
<a id="replace-link" className={"link " + (searchQuery.trim().length ? "" : "disabled")} style={!usereplace ? hidden: null} onClick={() => this.onReplaceClick()}>Replace</a>
<a id="replace-all-link" className={"link " + (searchQuery.trim().length ? "" : "disabled")} style={!usereplace ? hidden: null} onClick={() => this.onReplaceAllClick()}>Replace All</a>
</div>
<div className="buttons-row">
<a className={"link icon-only prev " + (searchQuery.trim().length ? "" : "disabled")} onClick={() => this.onSearchClick(SEARCH_BACKWARD)}>
<i className="icon icon-prev" />
</a>
<a className={"link icon-only next " + (searchQuery.trim().length ? "" : "disabled")} onClick={() => this.onSearchClick(SEARCH_FORWARD)}>
<i className="icon icon-next" />
</a>
</div>
2021-01-22 21:20:31 +00:00
</div>
</div>
</form>
)
}
}
2021-01-28 16:23:28 +00:00
export {SearchView as default, SearchView, SearchSettingsView};