diff --git a/index.js b/index.js index 993b9c9..c519033 100644 --- a/index.js +++ b/index.js @@ -253,19 +253,29 @@ io.on('connection', function(socket) const index = navigation.index || 0; const limit = navigation.limit || 10; - let search = {}; - //mysqlPool.query('SELECT * FROM `torrents` WHERE `name` like \'%' + text + '%\' LIMIT ?,?', [index, limit], function (error, rows, fields) { - sphinx.query('SELECT * FROM `torrents_index`,`torrents_index_delta` WHERE MATCH(?) ' + (safeSearch ? "and contentcategory != 'xxx'" : '') + ' LIMIT ?,?', [text, index, limit], function (error, rows, fields) { + 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 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) { + console.log(error) callback(undefined) return; } rows.forEach((row) => { - search[row.hash] = baseRowData(row); + searchList.push(baseRowData(row)); }); - callback(Object.keys(search).map(function(key) { - return search[key]; - })); + callback(searchList); }); }); @@ -283,23 +293,38 @@ io.on('connection', function(socket) const index = navigation.index || 0; 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 = {}; - //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) { - sphinx.query('SELECT * FROM `files_index`,`files_index_delta` WHERE MATCH(?) ' + (safeSearch ? "and contentcategory != 'xxx'" : '') + ' LIMIT ?,?', [text, index, limit], function (error, rows, fields) { + let searchList = []; + //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) { + console.log(error) callback(undefined) return; } rows.forEach((row) => { 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) search[row.hash].path = [] search[row.hash].path.push(row.path); }); - callback(Object.keys(search).map(function(key) { - return search[key]; - })); + callback(searchList); }); }); diff --git a/src/search-results.js b/src/search-results.js index 5990803..8394795 100644 --- a/src/search-results.js +++ b/src/search-results.js @@ -14,7 +14,10 @@ export default class SearchResults extends Component { (this.props.torrentsSearchResults && this.props.torrentsSearchResults.length > 0) || (this.props.filesSearchResults && this.props.filesSearchResults.length > 0) ? - Search results +
+ Search results +
{this.props.resultSelector}
+
: null } diff --git a/src/search.js b/src/search.js index f9f6b18..ba98263 100644 --- a/src/search.js +++ b/src/search.js @@ -9,6 +9,9 @@ import Checkbox from 'material-ui/Checkbox'; import Visibility from 'material-ui/svg-icons/action/visibility'; 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' let session; @@ -23,6 +26,8 @@ export default class Search extends Component { safeSearchColor: 'rgb(0, 188, 212)', moreTorrentsIndicator: false, moreFilesIndicator: false, + orderBy: null, + orderDesc: false, } this.searchLimit = 10 @@ -35,10 +40,12 @@ export default class Search extends Component { this.currentSearch = session.currentSearch; this.searchValue = session.searchValue; Object.assign(this.state, this.setSafeSearch(session.notSafeSearch)) + this.state.orderBy = session.orderBy; + this.state.orderDesc = session.orderDesc; } } - search() { + search(oldSearch) { this.setState({ searchingIndicator: true }); @@ -48,9 +55,11 @@ export default class Search extends Component { this.moreSearchFiles = true; this.currentSearch = this.searchValue; let queries = 2; - window.torrentSocket.emit('searchTorrent', this.searchValue, { + window.torrentSocket.emit('searchTorrent', oldSearch ? this.currentSearch : this.searchValue, { limit: this.searchLimit, - safeSearch: !this.notSafeSearch + safeSearch: !this.notSafeSearch, + orderBy: this.state.orderBy, + orderDesc: this.state.orderDesc, }, window.customLoader((torrents) => { if(torrents) { this.searchTorrents = torrents; @@ -65,9 +74,11 @@ export default class Search extends Component { this.forceUpdate(); } })); - window.torrentSocket.emit('searchFiles', this.searchValue, { + window.torrentSocket.emit('searchFiles', oldSearch ? this.currentSearch : this.searchValue, { limit: this.searchLimit, - safeSearch: !this.notSafeSearch + safeSearch: !this.notSafeSearch, + orderBy: this.state.orderBy, + orderDesc: this.state.orderDesc, }, window.customLoader((torrents) => { if(torrents) { this.searchFiles = torrents; @@ -94,7 +105,9 @@ export default class Search extends Component { window.torrentSocket.emit('searchTorrent', this.currentSearch, { index: this.searchTorrents.length, limit: this.searchLimit, - safeSearch: !this.notSafeSearch + safeSearch: !this.notSafeSearch, + orderBy: this.state.orderBy, + orderDesc: this.state.orderDesc, }, window.customLoader((torrents) => { if(torrents) { this.searchTorrents = this.searchTorrents.concat(torrents); @@ -117,7 +130,9 @@ export default class Search extends Component { window.torrentSocket.emit('searchFiles', this.currentSearch, { index: index, limit: this.searchLimit, - safeSearch: !this.notSafeSearch + safeSearch: !this.notSafeSearch, + orderBy: this.state.orderBy, + orderDesc: this.state.orderDesc, }, window.customLoader((torrents) => { if(torrents) { this.searchFiles = this.searchFiles.concat(torrents); @@ -156,6 +171,8 @@ export default class Search extends Component { currentSearch: this.currentSearch, searchValue: this.searchValue, notSafeSearch: this.notSafeSearch, + orderBy: this.state.orderBy, + orderDesc: this.state.orderDesc, } } 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 (
@@ -238,6 +265,45 @@ export default class Search extends Component { torrentsSearchResults={this.searchTorrents} filesSearchResults={this.searchFiles} + resultSelector={ + { + 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) + }) + }} + > + + + + + + + + + } + moreTorrentsEnabled={this.moreSearchTorrents && !this.state.searchingIndicator} moreFilesEnabled={this.moreSearchFiles && !this.state.searchingIndicator} onMoreTorrents={() => this.moreTorrents()}