возможность сортировки выдачи поиска

This commit is contained in:
Alexey Kasyanchuk 2017-02-06 15:14:44 +03:00
parent cdc7a2ec6f
commit 5710289533
3 changed files with 115 additions and 21 deletions

View File

@ -253,19 +253,29 @@ io.on('connection', function(socket)
const index = navigation.index || 0; const index = navigation.index || 0;
const limit = navigation.limit || 10; const limit = navigation.limit || 10;
let search = {}; let args = [text, index, limit];
//mysqlPool.query('SELECT * FROM `torrents` WHERE `name` like \'%' + text + '%\' LIMIT ?,?', [index, limit], function (error, rows, fields) { const orderBy = navigation.orderBy;
sphinx.query('SELECT * FROM `torrents_index`,`torrents_index_delta` WHERE MATCH(?) ' + (safeSearch ? "and contentcategory != 'xxx'" : '') + ' LIMIT ?,?', [text, index, limit], function (error, rows, fields) { let order = '';
if(orderBy && orderBy.length > 0)
{
const orderDesc = navigation.orderDesc ? 'DESC' : 'ASC';
args.unshift(orderBy);
order = 'ORDER BY ?? ' + orderDesc;
}
let searchList = [];
//args.splice(orderBy && orderBy.length > 0 ? 1 : 0, 1);
//mysqlPool.query('SELECT * FROM `torrents` WHERE `name` like \'%' + text + '%\' ' + order + ' LIMIT ?,?', args, function (error, rows, fields) {
sphinx.query('SELECT * FROM `torrents_index`,`torrents_index_delta` WHERE MATCH(?) ' + (safeSearch ? "and contentcategory != 'xxx'" : '') + ' ' + order + ' LIMIT ?,?', args, function (error, rows, fields) {
if(!rows) { if(!rows) {
console.log(error)
callback(undefined) callback(undefined)
return; return;
} }
rows.forEach((row) => { rows.forEach((row) => {
search[row.hash] = baseRowData(row); searchList.push(baseRowData(row));
}); });
callback(Object.keys(search).map(function(key) { callback(searchList);
return search[key];
}));
}); });
}); });
@ -283,23 +293,38 @@ io.on('connection', function(socket)
const index = navigation.index || 0; const index = navigation.index || 0;
const limit = navigation.limit || 10; const limit = navigation.limit || 10;
let args = [text, index, limit];
const orderBy = navigation.orderBy;
let order = '';
if(orderBy && orderBy.length > 0)
{
const orderDesc = navigation.orderDesc ? 'DESC' : 'ASC';
args.unshift(orderBy);
order = 'ORDER BY ?? ' + orderDesc;
}
let search = {}; let search = {};
//mysqlPool.query('SELECT * FROM `files` inner join torrents on(torrents.hash = files.hash) WHERE files.path like \'%' + text + '%\' LIMIT ?,?', [index, limit], function (error, rows, fields) { let searchList = [];
sphinx.query('SELECT * FROM `files_index`,`files_index_delta` WHERE MATCH(?) ' + (safeSearch ? "and contentcategory != 'xxx'" : '') + ' LIMIT ?,?', [text, index, limit], function (error, rows, fields) { //args.splice(orderBy && orderBy.length > 0 ? 1 : 0, 1);
//mysqlPool.query('SELECT * FROM `files` inner join torrents on(torrents.hash = files.hash) WHERE files.path like \'%' + text + '%\' ' + order + ' LIMIT ?,?', args, function (error, rows, fields) {
sphinx.query('SELECT * FROM `files_index`,`files_index_delta` WHERE MATCH(?) ' + (safeSearch ? "and contentcategory != 'xxx'" : '') + ' ' + order + ' LIMIT ?,?', args, function (error, rows, fields) {
if(!rows) { if(!rows) {
console.log(error)
callback(undefined) callback(undefined)
return; return;
} }
rows.forEach((row) => { rows.forEach((row) => {
if(!(row.hash in search)) if(!(row.hash in search))
search[row.hash] = baseRowData(row); {
let torrent = baseRowData(row);
search[row.hash] = torrent;
searchList.push(torrent)
}
if(!search[row.hash].path) if(!search[row.hash].path)
search[row.hash].path = [] search[row.hash].path = []
search[row.hash].path.push(row.path); search[row.hash].path.push(row.path);
}); });
callback(Object.keys(search).map(function(key) { callback(searchList);
return search[key];
}));
}); });
}); });

View File

@ -14,7 +14,10 @@ export default class SearchResults extends Component {
(this.props.torrentsSearchResults && this.props.torrentsSearchResults.length > 0) (this.props.torrentsSearchResults && this.props.torrentsSearchResults.length > 0)
|| (this.props.filesSearchResults && this.props.filesSearchResults.length > 0) || (this.props.filesSearchResults && this.props.filesSearchResults.length > 0)
? ?
<Subheader inset={true}>Search results</Subheader> <div>
<Subheader inset={true}>Search results</Subheader>
<div className='w100p row center' style={{marginTop: '-16px'}}>{this.props.resultSelector}</div>
</div>
: :
null null
} }

View File

@ -9,6 +9,9 @@ import Checkbox from 'material-ui/Checkbox';
import Visibility from 'material-ui/svg-icons/action/visibility'; import Visibility from 'material-ui/svg-icons/action/visibility';
import VisibilityOff from 'material-ui/svg-icons/action/visibility-off'; import VisibilityOff from 'material-ui/svg-icons/action/visibility-off';
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';
import formatBytes from './format-bytes' import formatBytes from './format-bytes'
let session; let session;
@ -23,6 +26,8 @@ export default class Search extends Component {
safeSearchColor: 'rgb(0, 188, 212)', safeSearchColor: 'rgb(0, 188, 212)',
moreTorrentsIndicator: false, moreTorrentsIndicator: false,
moreFilesIndicator: false, moreFilesIndicator: false,
orderBy: null,
orderDesc: false,
} }
this.searchLimit = 10 this.searchLimit = 10
@ -35,10 +40,12 @@ export default class Search extends Component {
this.currentSearch = session.currentSearch; this.currentSearch = session.currentSearch;
this.searchValue = session.searchValue; this.searchValue = session.searchValue;
Object.assign(this.state, this.setSafeSearch(session.notSafeSearch)) Object.assign(this.state, this.setSafeSearch(session.notSafeSearch))
this.state.orderBy = session.orderBy;
this.state.orderDesc = session.orderDesc;
} }
} }
search() { search(oldSearch) {
this.setState({ this.setState({
searchingIndicator: true searchingIndicator: true
}); });
@ -48,9 +55,11 @@ export default class Search extends Component {
this.moreSearchFiles = true; this.moreSearchFiles = true;
this.currentSearch = this.searchValue; this.currentSearch = this.searchValue;
let queries = 2; let queries = 2;
window.torrentSocket.emit('searchTorrent', this.searchValue, { window.torrentSocket.emit('searchTorrent', oldSearch ? this.currentSearch : this.searchValue, {
limit: this.searchLimit, limit: this.searchLimit,
safeSearch: !this.notSafeSearch safeSearch: !this.notSafeSearch,
orderBy: this.state.orderBy,
orderDesc: this.state.orderDesc,
}, window.customLoader((torrents) => { }, window.customLoader((torrents) => {
if(torrents) { if(torrents) {
this.searchTorrents = torrents; this.searchTorrents = torrents;
@ -65,9 +74,11 @@ export default class Search extends Component {
this.forceUpdate(); this.forceUpdate();
} }
})); }));
window.torrentSocket.emit('searchFiles', this.searchValue, { window.torrentSocket.emit('searchFiles', oldSearch ? this.currentSearch : this.searchValue, {
limit: this.searchLimit, limit: this.searchLimit,
safeSearch: !this.notSafeSearch safeSearch: !this.notSafeSearch,
orderBy: this.state.orderBy,
orderDesc: this.state.orderDesc,
}, window.customLoader((torrents) => { }, window.customLoader((torrents) => {
if(torrents) { if(torrents) {
this.searchFiles = torrents; this.searchFiles = torrents;
@ -94,7 +105,9 @@ export default class Search extends Component {
window.torrentSocket.emit('searchTorrent', this.currentSearch, { window.torrentSocket.emit('searchTorrent', this.currentSearch, {
index: this.searchTorrents.length, index: this.searchTorrents.length,
limit: this.searchLimit, limit: this.searchLimit,
safeSearch: !this.notSafeSearch safeSearch: !this.notSafeSearch,
orderBy: this.state.orderBy,
orderDesc: this.state.orderDesc,
}, window.customLoader((torrents) => { }, window.customLoader((torrents) => {
if(torrents) { if(torrents) {
this.searchTorrents = this.searchTorrents.concat(torrents); this.searchTorrents = this.searchTorrents.concat(torrents);
@ -117,7 +130,9 @@ export default class Search extends Component {
window.torrentSocket.emit('searchFiles', this.currentSearch, { window.torrentSocket.emit('searchFiles', this.currentSearch, {
index: index, index: index,
limit: this.searchLimit, limit: this.searchLimit,
safeSearch: !this.notSafeSearch safeSearch: !this.notSafeSearch,
orderBy: this.state.orderBy,
orderDesc: this.state.orderDesc,
}, window.customLoader((torrents) => { }, window.customLoader((torrents) => {
if(torrents) { if(torrents) {
this.searchFiles = this.searchFiles.concat(torrents); this.searchFiles = this.searchFiles.concat(torrents);
@ -156,6 +171,8 @@ export default class Search extends Component {
currentSearch: this.currentSearch, currentSearch: this.currentSearch,
searchValue: this.searchValue, searchValue: this.searchValue,
notSafeSearch: this.notSafeSearch, notSafeSearch: this.notSafeSearch,
orderBy: this.state.orderBy,
orderDesc: this.state.orderDesc,
} }
} }
setSafeSearch(ch) { setSafeSearch(ch) {
@ -177,6 +194,16 @@ export default class Search extends Component {
}, },
}; };
const orderText = (text, field) => {
if(field !== this.state.orderBy)
return text;
if(this.state.orderDesc)
return text + ' ⇩'
else
return text + ' ⇧'
}
return ( return (
<div className="column w100p center"> <div className="column w100p center">
<div className='row inline w100p pad0-75' style={{maxWidth: '30em'}}> <div className='row inline w100p pad0-75' style={{maxWidth: '30em'}}>
@ -238,6 +265,45 @@ export default class Search extends Component {
torrentsSearchResults={this.searchTorrents} torrentsSearchResults={this.searchTorrents}
filesSearchResults={this.searchFiles} filesSearchResults={this.searchFiles}
resultSelector={
<SelectField
floatingLabelText="Sort by"
floatingLabelFixed={true}
value={this.state.orderBy}
onChange={(event, index, value) => {
if(value === 'none') {
this.setState({orderBy: null}, () => {
this.search(true)
})
return;
}
if(value === this.state.orderBy)
{
this.setState({orderDesc: !this.state.orderDesc}, () => {
this.search(true)
})
return;
}
this.setState({
orderBy: value,
orderDesc: (value === 'seeders' || value === 'completed' || value === 'added') ? true : this.state.orderDesc
}, () => {
this.search(true)
})
}}
>
<MenuItem value='none' primaryText={'None'} />
<MenuItem value='seeders' primaryText={orderText('Seeders', 'seeders')} />
<MenuItem value='name' primaryText={orderText('Name', 'name')} />
<MenuItem value='files' primaryText={orderText('Files', 'files')} />
<MenuItem value='size' primaryText={orderText('Size', 'size')} />
<MenuItem value='added' primaryText={orderText('Added date', 'added')} />
<MenuItem value='completed' primaryText={orderText('Completed', 'completed')} />
</SelectField>
}
moreTorrentsEnabled={this.moreSearchTorrents && !this.state.searchingIndicator} moreTorrentsEnabled={this.moreSearchTorrents && !this.state.searchingIndicator}
moreFilesEnabled={this.moreSearchFiles && !this.state.searchingIndicator} moreFilesEnabled={this.moreSearchFiles && !this.state.searchingIndicator}
onMoreTorrents={() => this.moreTorrents()} onMoreTorrents={() => this.moreTorrents()}