cablegates/static/js/pandora/Query.js

301 lines
12 KiB
JavaScript
Raw Permalink Normal View History

2011-07-29 18:37:11 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-05-25 19:42:45 +00:00
pandora.Query = (function() {
function constructFind(query) {
return /*encodeURI(*/query.conditions.map(function(condition) {
var ret;
if (condition.conditions) {
ret = '[' + constructFind(condition) + ']';
} else {
ret = condition.value !== ''
? condition.key + (condition.key ? ':' : '')
+ constructValue(condition.value, condition.operator)
: null;
}
return ret;
}).join(query.operator == '&' ? ',' : '|')/*)*/;
}
function constructValue(value, operator) {
operator = operator.replace('=', '^$');
if (operator.indexOf('$') > -1) {
value = operator.substr(0, operator.length - 1) + value + '$';
} else {
value = operator + value;
}
return value;
}
function everyCondition(conditions, key, operator) {
// if every condition has the given key and operator
// (excluding conditions where all subconditions match)
// returns true, otherwise false
return Ox.every(conditions, function(condition) {
return condition.key == key && condition.operator == operator;
});
}
2011-08-24 09:25:39 +00:00
function getGroupsData(fullQuery) {
// a group is selected if exactly one condition in an & query
// or every condition in an | query
// has the group id as key and "=" as operator
return pandora.user.ui.groups.map(function(key) {
var index = -1,
2011-08-24 09:25:39 +00:00
query = Ox.clone(fullQuery, true),
selected = [];
if (query.operator == '|') {
if (everyCondition(query.conditions, key, '=')) {
index = Ox.range(query.conditions.length);
selected = query.conditions.map(function(condition) {
return condition.value;
});
}
} else {
index = oneCondition(query.conditions, key, '=');
if (index > -1) {
selected = query.conditions[index].conditions
? query.conditions[index].conditions.map(function(condition) {
return condition.value;
})
: [query.conditions[index].value];
}
}
if (selected.length) {
if (Ox.isArray(index)) {
query = {conditions: [], operator: ''};
} else {
query.conditions.splice(index, 1);
if (query.conditions.length == 1) {
if (query.conditions[0].conditions) {
// unwrap single remaining bracketed query
query = {
conditions: query.conditions[0].conditions,
operator: query.conditions[0].operator
}
} else {
query.operator = '';
}
}
}
}
return {
index: index,
query: query,
selected: selected
};
});
}
function parseFind(str) {
2011-08-23 19:58:40 +00:00
// 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)
var conditions,
index, indices,
ret = {
find: {index: -1, key: '', value: ''},
groups: [], // {index, query, selected}
list: '',
query: {conditions: [], operator: ''}
},
subconditions = [];
2011-08-24 08:25:08 +00:00
if (str.length) {
// replace subconditions with placeholder,
// so we can later split by main operator
var counter = 0;
Ox.forEach(str, function(c, i) {
if (c == ']') {
counter--;
}
if (counter >= 1) {
subconditions[subconditions.length - 1] += c;
}
if (c == '[') {
(++counter == 1) && subconditions.push('');
}
});
2011-08-24 08:25:08 +00:00
subconditions.forEach(function(subcondition, i) {
str = str.replace(subcondition, i);
2011-08-24 08:25:08 +00:00
});
if (str.indexOf(',') > -1) {
ret.query.operator = '&';
} else if (str.indexOf('|') > -1) {
ret.query.operator = '|';
}
2011-08-24 08:25:08 +00:00
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(Ox.sub(condition, 1, -1))]).query;
2011-08-24 08:25:08 +00:00
} else {
kv = ((condition.indexOf(':') > -1 ? '' : ':') + condition).split(':');
ret = Ox.extend({key: kv[0]}, parseValue(kv[1]));
}
2011-08-24 08:25:08 +00:00
return ret;
});
2011-08-24 08:25:08 +00:00
// 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
2011-08-24 09:25:39 +00:00
// (and all other conditions are either list or groups)
2011-08-24 08:25:08 +00:00
// 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 {
// number of conditions that are not list or groups
2011-08-24 09:25:39 +00:00
conditions = ret.query.conditions.length
- (ret.list != '')
- ret.groups.filter(function(group) {
return group.index > -1;
}).length;
// indices of non-advanced find queries
2011-08-24 08:25:08 +00:00
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;
});
2011-08-24 09:25:39 +00:00
if (conditions > 0 || indices.length > 0) {
ret.find = (
conditions == 1 && indices.length == 1
&& !ret.query.conditions[indices[0]].conditions
) ? {
2011-08-24 08:25:08 +00:00
index: indices[0],
key: ret.query.conditions[indices[0]].key,
value: ret.query.conditions[indices[0]].value
2011-08-24 09:25:39 +00:00
} : {index: -1, key: 'advanced', value: ''}
2011-08-24 08:25:08 +00:00
}
2011-05-25 19:42:45 +00:00
}
}
ret.groups = getGroupsData(ret.query);
return ret;
2011-05-25 19:42:45 +00:00
}
function oneCondition(conditions, key, operator) {
// if exactly one condition has the given key and operator
// (including conditions where all subconditions match)
// returns the corresponding index, otherwise returns -1
var indices = Ox.map(conditions, function(condition, i) {
return (
condition.conditions
? everyCondition(condition.conditions, key, operator)
: condition.key == key && condition.operator == operator
) ? i : null;
2011-05-25 19:42:45 +00:00
});
return indices.length == 1 ? indices[0] : -1;
2011-05-25 19:42:45 +00:00
}
function parseValue(str) {
var value = {
value: decodeURI(str),
operator: ''
};
if (value.value[0] == '!') {
value.operator = '!';
value.value = value.value.substr(1);
}
if ('^<>'.indexOf(value.value[0]) > -1) {
value.operator += value.value[0];
value.value = value.value.substr(1);
}
if (value.value.substr(-1) == '$') {
value.operator += '$';
value.value = value.value.substr(0, value.value.length - 1);
}
value.operator = value.operator.replace('^$', '=');
return value;
}
return {
fromString: function(str) {
var data
2011-05-25 19:42:45 +00:00
query = Ox.unserialize(str.substr(1)),
sort = []
2011-05-25 19:42:45 +00:00
if ('find' in query) {
2011-08-24 08:25:08 +00:00
data = parseFind(query.find || '');
Ox.print(Ox.repeat('-', 120));
Ox.print('STATE', data);
Ox.print(Ox.repeat('-', 120));
!pandora.user.ui.lists[data.list] && pandora.UI.set(
['lists', data.list].join('|'), pandora.site.user.ui.lists['']
);
pandora.UI.set({list: data.list});
pandora.user.ui.find = data.find;
2011-08-24 09:25:39 +00:00
pandora.user.ui.groupsData = data.groups;
Ox.print("PUUGD", pandora.user.ui.groupsData);
pandora.user.ui.query = data.query;
2011-05-25 19:42:45 +00:00
}
if ('sort' in query) {
sort = query.sort.split(',');
2011-08-18 07:54:46 +00:00
pandora.UI.set(['lists', pandora.user.ui.list, 'sort'].join('|'), query.sort.split(',').map(function(v) {
2011-05-25 19:42:45 +00:00
var hasOperator = '+-'.indexOf(v[0]) > -1,
2011-08-18 07:54:46 +00:00
key = hasOperator ? v.substr(1) : v,
2011-05-25 19:42:45 +00:00
operator = hasOperator ? v[0]/*.replace('+', '')*/ : pandora.getSortOperator(key);
return {
key: key,
operator: operator
};
}));
}
if ('view' in query) {
2011-06-06 15:48:11 +00:00
pandora.UI.set(['lists', pandora.user.ui.list, 'listView'].join('|'), query.view);
2011-05-25 19:42:45 +00:00
}
},
/*
2011-05-25 19:42:45 +00:00
toObject: function(groupId) {
2011-06-06 15:48:11 +00:00
//Ox.print('tO', pandora.user.ui.findQuery.conditions)
2011-05-25 19:42:45 +00:00
// the inner $.merge() creates a clone
var conditions = $.merge(
2011-06-06 15:48:11 +00:00
$.merge([], pandora.user.ui.listQuery.conditions),
pandora.user.ui.findQuery.conditions
2011-05-25 19:42:45 +00:00
),
operator;
2011-06-06 15:48:11 +00:00
$.merge(conditions, pandora.user.queryGroups ? $.map(pandora.user.queryGroups, function(v, i) {
2011-05-25 19:42:45 +00:00
if (v.id != groupId && v.query.conditions.length) {
return v.query.conditions.length == 1 ?
v.query.conditions : v.query;
}
}) : []);
operator = conditions.length < 2 ? '' : ','; // fixme: should be &
2011-06-06 15:48:11 +00:00
//Ox.print('>>', groupId, pandora.user.ui.find, conditions);
2011-05-25 19:42:45 +00:00
return {
conditions: conditions,
operator: operator
};
},
*/
2011-05-25 19:42:45 +00:00
toString: function() {
2011-06-06 15:48:11 +00:00
//Ox.print('tS', pandora.user.ui.find)
var sort = pandora.user.ui.lists[pandora.user.ui.list].sort[0],
2011-05-25 19:42:45 +00:00
key = sort.key,
operator = sort.operator;
return '?' + Ox.serialize({
find: constructFind(pandora.user.ui.query),
2011-05-25 19:42:45 +00:00
sort: (operator == pandora.getSortOperator(key) ? '' : operator) + key,
2011-06-06 15:48:11 +00:00
view: pandora.user.ui.lists[pandora.user.ui.list].listView
2011-05-25 19:42:45 +00:00
});
},
updateGroups: function() {
2011-08-24 09:25:39 +00:00
pandora.user.ui.groupsData = getGroupsData(pandora.user.ui.query);
2011-05-25 19:42:45 +00:00
}
};
})();