cablegates/static/js/pandora/ui/findElement.js

126 lines
5.9 KiB
JavaScript
Raw Normal View History

2011-05-25 19:42:45 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=js
pandora.ui.findElement = function() {
var findKey = '',
findValue = '';
2011-06-06 15:48:11 +00:00
if (pandora.user.ui.findQuery.conditions.length == 1) {
findKey = pandora.user.ui.findQuery.conditions[0].key;
findValue = pandora.user.ui.findQuery.conditions[0].value;
2011-05-25 19:42:45 +00:00
}
var that = new Ox.FormElementGroup({
2011-06-06 15:48:11 +00:00
elements: $.merge(pandora.user.ui.list ? [
pandora.$ui.findListSelect = new Ox.Select({
2011-05-25 19:42:45 +00:00
items: [
2011-06-06 15:48:11 +00:00
{id: 'all', title: 'Find: All ' + pandora.site.itemName.plural},
2011-05-25 19:42:45 +00:00
{id: 'list', title: 'Find: This List'}
],
overlap: 'right',
type: 'image'
})
.bindEvent({
change: function(event, data) {
var key = data.selected[0].id;
2011-06-06 15:48:11 +00:00
pandora.$ui.findInput.options({
2011-05-25 19:42:45 +00:00
autocomplete: autocompleteFunction()
}).focus();
}
}),
] : [], [
2011-06-06 15:48:11 +00:00
pandora.$ui.findSelect = new Ox.Select({
2011-05-25 19:42:45 +00:00
id: 'select',
2011-06-06 15:48:11 +00:00
items: $.merge($.map(pandora.site.findKeys,
2011-05-30 13:46:57 +00:00
function(key, i) {
2011-05-25 19:42:45 +00:00
return {
id: key.id,
checked: key.id == findKey,
title: 'Find: ' + key.title
};
2011-05-30 13:46:57 +00:00
}), [{}, {
2011-05-25 19:42:45 +00:00
id: 'advanced',
title: 'Find: Advanced'
}]),
overlap: 'right',
width: 112
})
.bindEvent({
change: function(event, data) {
var key = data.selected[0].id;
if (key == 'advanced') {
2011-06-06 15:48:11 +00:00
pandora.$ui.filterDialog = pandora.ui.filterDialog().open();
2011-05-25 19:42:45 +00:00
} else {
2011-06-06 15:48:11 +00:00
if (!pandora.user.ui.findQuery.conditions.length) { // fixme: can this case happen at all?
pandora.user.ui.findQuery.conditions = [{key: key, value: '', operator: ''}];
2011-05-25 19:42:45 +00:00
} else {
2011-06-06 15:48:11 +00:00
pandora.user.ui.findQuery.conditions[0].key = key;
2011-05-25 19:42:45 +00:00
}
2011-06-06 15:48:11 +00:00
pandora.$ui.mainMenu.checkItem('findMenu_find_' + key);
pandora.$ui.findInput.options({
2011-05-25 19:42:45 +00:00
autocomplete: autocompleteFunction()
}).focus();
}
}
}),
2011-06-06 15:48:11 +00:00
pandora.$ui.findInput = new Ox.Input({
2011-05-25 19:42:45 +00:00
autocomplete: autocompleteFunction(),
autocompleteSelect: true,
autocompleteSelectHighlight: true,
autocompleteSelectSubmit: true,
clear: true,
id: 'input',
value: findValue,
width: 192
})
.bindEvent({
submit: function(event, data) {
2011-06-06 15:48:11 +00:00
var key = pandora.user.ui.findQuery.conditions.length ?
pandora.user.ui.findQuery.conditions[0].key : '';
if (pandora.user.ui.list && that.value()[0].id == 'all') {
$.each(pandora.$ui.folderList, function(k, $list) {
2011-05-25 19:42:45 +00:00
$list.options({selected: []});
});
pandora.UI.set({list: ''});
2011-06-06 15:48:11 +00:00
pandora.user.ui.listQuery = {conditions: [], operator: ''};
2011-05-25 19:42:45 +00:00
}
2011-06-06 15:48:11 +00:00
pandora.user.ui.findQuery.conditions = [{
2011-05-25 19:42:45 +00:00
key: key == 'all' ? '' : key,
value: data.value,
operator: ''
}];
pandora.URL.set(pandora.Query.toString());
}
})
]),
id: 'findElement'
})
.css({
float: 'right',
margin: '4px'
});
function autocompleteFunction() {
2011-06-06 15:48:11 +00:00
return pandora.user.ui.findQuery.conditions.length ? function(value, callback) {
2011-05-25 19:42:45 +00:00
var elementValue = that.value(),
2011-06-06 15:48:11 +00:00
key = elementValue[pandora.user.ui.list ? 1 : 0],
findKey = Ox.getObjectById(pandora.site.findKeys, key);
2011-05-25 19:42:45 +00:00
Ox.print('!!!!', key, findKey, 'autocomplete' in findKey && findKey.autocomplete)
value === '' && Ox.print('Warning: autocomplete function should never be called with empty value');
if ('autocomplete' in findKey && findKey.autocomplete) {
pandora.api.autocomplete({
key: key,
2011-06-06 15:48:11 +00:00
query: elementValue[0].id == 'list' ? pandora.user.ui.listQuery : {conditions: [], operator: ''},
2011-05-25 19:42:45 +00:00
range: [0, 20],
sort: [{
key: 'votes',
operator: '-'
}],
value: value
}, function(result) {
callback(result.data.items);
});
} else {
callback([]);
}
} : null;
}
return that;
};