getItem: check for secondary id match, then only look for exact title or original title match

This commit is contained in:
rolux 2013-03-06 10:02:45 +00:00
parent b802dfa296
commit 0372eff998

View file

@ -714,38 +714,61 @@ pandora.getInfoHeight = function(includeHidden) {
pandora.getItem = function(state, str, callback) { pandora.getItem = function(state, str, callback) {
if (state.type == pandora.site.itemName.plural.toLowerCase()) { if (state.type == pandora.site.itemName.plural.toLowerCase()) {
var sortKey = Ox.getObjectById(pandora.site.itemKeys, 'votes') var secondaryId = pandora.site.itemKeys.filter(function(key) {
? 'votes' return key.secondaryId;
: 'timesaccessed'; }).map(function(key) {
return key.id;
})[0],
sortKey = Ox.getObjectById(pandora.site.itemKeys, 'votes')
? 'votes'
: 'timesaccessed';
Ox.getObjectById(pandora.site.itemKeys, 'alt')
pandora.api.get({id: str, keys: ['id']}, function(result) { pandora.api.get({id: str, keys: ['id']}, function(result) {
if (result.status.code == 200) { if (result.status.code == 200) {
state.item = result.data.id; state.item = result.data.id;
callback(); callback();
} else { } else {
pandora.api.find({ (secondaryId ? pandora.api.find : Ox.noop)({
query: { query: {
conditions: [{key: 'title', value: str, operator: '='}], conditions: [{key: secondaryId, value: str, operator: '=='}],
operator: '&' operator: '&'
}, },
sort: [{key: sortKey, operator: ''}], sort: [{key: sortKey, operator: '-'}],
range: [0, 100], range: [0, 1],
keys: ['id', 'title', sortKey] keys: ['id']
}, function(result) { }, function(result) {
if (result.data.items.length) { if (result && result.data.items.length) {
var items = Ox.filter(Ox.map(result.data.items, function(item) { state.item = result.data.items[0].id;
// test if exact match or word match callback();
var sort = new RegExp('^' + str + '$', 'i').test(item.title) ? 2000000 } else {
: new RegExp('\\b' + str + '\\b', 'i').test(item.title) ? 1000000 : 0; pandora.api.find({
return sort ? {id: item.id, sort: sort + (parseInt(item[sortKey]) || 0)} : null; query: {
// fixme: remove the (...|| 0) check once the backend sends correct data conditions: [{key: 'title', value: str, operator: '=='}],
})); operator: '&'
if (items.length) { },
state.item = items.sort(function(a, b) { sort: [{key: sortKey, operator: '-'}],
return b.sort - a.sort; range: [0, 100],
})[0].id; keys: ['id', 'title', sortKey]
} }, function(result) {
if (result.data.items.length) {
var regexp = new RegExp('^' + Ox.escapeRegExp(str) + '$', 'i'),
items = result.data.items.map(function(item) {
return {
id: item.id,
// prefer title match over originalTitle match
sort: (item.title == str ? 1000000 : 0)
+ (parseInt(item[sortKey]) || 0)
// fixme: remove the (...|| 0) check
// once the backend sends correct data
};
});
state.item = items.sort(function(a, b) {
return b.sort - a.sort;
})[0].id;
}
callback();
});
} }
callback();
}); });
} }
}); });