some query fixes

This commit is contained in:
rlx 2011-08-24 08:25:08 +00:00
parent cc4df21897
commit ab3098f847
4 changed files with 75 additions and 88 deletions

View file

@ -89,7 +89,6 @@ pandora.Query = (function() {
function parseFind(str) {
// takes a find query string, returns useful information about the application's state
// (selected lists, find input key/value (and index of the corresponding condition), query object)
str = str || '';
var conditions,
index, indices,
ret = {
@ -98,61 +97,65 @@ pandora.Query = (function() {
query: {conditions: [], operator: ''}
},
subconditions = str.match(/\[.*?\]/g) || [];
// replace subconditions with placeholder,
// so we can later split by main operator
subconditions.forEach(function(subcondition, i) {
subconditions[i] = subcondition.substr(1, subcondition.length - 2);
str = str.replace(subconditions[i], i);
});
if (str.indexOf(',') > -1) {
ret.query.operator = '&';
} else if (str.indexOf('|') > -1) {
ret.query.operator = '|';
}
ret.query.conditions = (
ret.query.operator == '' ? [str] : str.split(ret.query.operator == '&' ? ',' : '|')
).map(function(condition, i) {
var kv, ret;
if (condition[0] == '[') {
// re-insert subcondition
ret = parseFind(subconditions[parseInt(condition.substr(1, condition.length - 2))]).query;
} else {
kv = ((condition.indexOf(':') > -1 ? '' : ':') + condition).split(':');
ret = Ox.extend({key: kv[0]}, parseValue(kv[1]));
if (str.length) {
// replace subconditions with placeholder,
// so we can later split by main operator
subconditions.forEach(function(subcondition, i) {
subconditions[i] = subcondition.substr(1, subcondition.length - 2);
str = str.replace(subconditions[i], i);
});
if (str.indexOf(',') > -1) {
ret.query.operator = '&';
} else if (str.indexOf('|') > -1) {
ret.query.operator = '|';
}
return ret;
});
// a list is selected if exactly one condition in an & query
// has "list" as key and "" as operator
if (ret.query.operator != '|') {
index = oneCondition(ret.query.conditions, 'list', '');
if (index > -1 && !ret.query.conditions[index].conditions) {
ret.list = ret.query.conditions[index].value;
}
}
// find is populated if exactly one condition in an & query
// has a findKey as key and "" as operator
// or if all conditions in an | query have the same group id as key
if (ret.query.operator == '|') {
ret.find = {index: -1, key: 'advanced', value: ''};
Ox.map(pandora.user.ui.groups, function(key) {
if (everyCondition(ret.query.conditions, key, '=')) {
ret.find.key = '';
return false;
ret.query.conditions = (
ret.query.operator == '' ? [str] : str.split(ret.query.operator == '&' ? ',' : '|')
).map(function(condition, i) {
var kv, ret;
if (condition[0] == '[') {
// re-insert subcondition
ret = parseFind(subconditions[parseInt(condition.substr(1, condition.length - 2))]).query;
} else {
kv = ((condition.indexOf(':') > -1 ? '' : ':') + condition).split(':');
ret = Ox.extend({key: kv[0]}, parseValue(kv[1]));
}
return ret;
});
} else {
indices = Ox.map(pandora.site.findKeys, function(findKey) {
var key = findKey.id == 'all' ? '' : findKey.id,
index = oneCondition(ret.query.conditions, key, '');
return index > -1 ? index : null;
});
if (indices.length) {
ret.find = indices.length == 1 && !ret.query.conditions[indices[0]].conditions ? {
index: indices[0],
key: ret.query.conditions[indices[0]].key,
value: ret.query.conditions[indices[0]].value
} : {index: -1, key: 'advanced', value: ''}
// a list is selected if exactly one condition in an & query
// has "list" as key and "" as operator
if (ret.query.operator != '|') {
index = oneCondition(ret.query.conditions, 'list', '');
if (index > -1 && !ret.query.conditions[index].conditions) {
ret.list = ret.query.conditions[index].value;
}
}
// find is populated if exactly one condition in an & query
// has a findKey as key and "" as operator
// or if all conditions in an | query have the same group id as key
if (ret.query.operator == '|') {
ret.find = {index: -1, key: 'advanced', value: ''};
Ox.map(pandora.user.ui.groups, function(key) {
if (everyCondition(ret.query.conditions, key, '=')) {
ret.find.key = '';
return false;
}
});
} else {
indices = Ox.map(pandora.site.findKeys, function(findKey) {
var key = findKey.id == 'all' ? '' : findKey.id,
index = oneCondition(ret.query.conditions, key, '');
return index > -1 ? index : null;
});
Ox.print('INDICES', indices, indices.length == 1 && !ret.query.conditions[indices[0]].conditions)
if (indices.length) {
ret.find = indices.length == 1 && !ret.query.conditions[indices[0]].conditions ? {
index: indices[0],
key: ret.query.conditions[indices[0]].key,
value: ret.query.conditions[indices[0]].value
} : {index: -1, key: 'advanced', value: ''}
}
Ox.print('FIND', ret.find)
}
}
return ret;
@ -200,7 +203,7 @@ pandora.Query = (function() {
query = Ox.unserialize(str.substr(1)),
sort = []
if ('find' in query) {
data = parseFind(query.find);
data = parseFind(query.find || '');
Ox.print(Ox.repeat('-', 120));
Ox.print('STATE', data);
Ox.print(Ox.repeat('-', 120));

View file

@ -58,8 +58,8 @@ pandora.ui.browser = function() {
};
},
items: function(data, callback) {
pandora.api.find($.extend(data, {
query: pandora.Query.toObject()
pandora.api.find(Ox.extend(data, {
query: pandora.user.ui.query
}), callback);
},
keys: ['director', 'id', 'posterRatio', 'title', 'year'],

View file

@ -46,10 +46,12 @@ pandora.ui.findElement = function() {
var key = data.selected[0].id;
if (key == 'advanced') {
pandora.$ui.filterDialog = pandora.ui.filterDialog().open();
pandora.$ui.findInput.options({placeholder: 'Edit...'})
} else {
pandora.$ui.mainMenu.checkItem('findMenu_find_' + key);
pandora.$ui.findInput.options({
autocomplete: autocompleteFunction()
autocomplete: autocompleteFunction(),
placeholder: ''
}).focus();
}
}
@ -67,7 +69,7 @@ pandora.ui.findElement = function() {
})
.bindEvent({
focus: function(data) {
if (findKey == 'advanced') {
if (pandora.$ui.findSelect.value() == 'advanced') {
pandora.$ui.filterDialog = pandora.ui.filterDialog().open();
}
},

View file

@ -65,7 +65,8 @@ pandora.ui.group = function(id) {
} else {
if (index == -1) {
index = pandora.user.ui.query.conditions.length;
pandora.user.ui.query.operator = '&'
pandora.user.ui.query.operator = index ? '&' : '';
Ox.print('$$$$$$$$$$$$$$$$$$$', index, pandora.user.ui.query.operator)
}
if (conditions.length == 0) {
pandora.user.ui.query.conditions.splice(index, 1);
@ -82,7 +83,6 @@ pandora.ui.group = function(id) {
}
}
pandora.Query.updateGroups();
Ox.print('---------', pandora.user.ui.query, pandora.user.ui.groupsData)
pandora.URL.push(pandora.Query.toString());
pandora.reloadGroups(i);
}
@ -104,55 +104,37 @@ pandora.ui.group = function(id) {
i_ = pandora.user.ui.groups.indexOf(id_);
if (i_ == -1) {
// new group was not part of old group set
if (pandora.user.queryGroups[i].query.conditions.length) {
if (pandora.user.ui.groupsData[i].selected.length) {
// if group with selection gets replaced, reload
pandora.user.queryGroups[i].query.conditions = [];
pandora.user.ui.query.conditions.splice(pandora.user.ui.groupsData[i].index, 1);
pandora.Query.updateGroups();
pandora.URL.push(pandora.Query.toString());
pandora.reloadGroups(i);
}
pandora.user.queryGroups[i] = getGroupObject(id_);
pandora.user.ui.groups[i] = id_;
pandora.UI.set({groups: pandora.user.ui.groups});
replaceGroup(i, id_);
} else {
// swap two existing groups
var group = $.extend({}, pandora.user.queryGroups[i]);
pandora.user.queryGroups[i] = pandora.user.queryGroups[i_];
pandora.user.queryGroups[i_] = group;
var groupsData = Ox.clone(pandora.user.ui.groupsData[i]);
pandora.user.ui.groupsData[i] = pandora.user.ui.groupsData[i_];
pandora.user.ui.groupsData[i_] = groupsData;
pandora.user.ui.groups[i] = id_;
pandora.user.ui.groups[i_] = id;
pandora.UI.set({groups: pandora.user.ui.groups});
replaceGroup(i, id_, pandora.user.queryGroups[i].query);
replaceGroup(i_, id, pandora.user.queryGroups[i_].query);
replaceGroup(i, id_);
replaceGroup(i_, id);
}
function replaceGroup(i, id, query) {
// if query is passed, selected items will be derived from it
var isOuter = i % 4 == 0;
pandora.$ui[isOuter ? 'browser' : 'groupsInnerPanel'].replaceElement(
isOuter ? i / 2 : i - 1,
pandora.$ui.groups[i] = pandora.ui.group(id, query)
pandora.$ui.groups[i] = pandora.ui.group(id)
);
}
})
.appendTo(that.$bar.$element);
if (!query) {
// if query is set, group object has already been taken care of
pandora.user.queryGroups[i] = getGroupObject(id);
}
function getGroupObject(id) {
var i = pandora.user.ui.groups.indexOf(id),
title = Ox.getObjectById(pandora.site.groups, id).title,
width = pandora.getGroupWidth(i, panelWidth);
return {
id: id,
element: that,
query: {
conditions: [],
operator: '|'
},
size: width.list,
title: title
};
}
return that;
};