pandora/static/js/pandora/findElement.js

148 lines
6.7 KiB
JavaScript
Raw Normal View History

2011-07-29 18:37:11 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-11-05 17:04:10 +00:00
'use strict';
2011-05-25 19:42:45 +00:00
pandora.ui.findElement = function() {
var findIndex = pandora.user.ui._findState.index,
findKey = pandora.user.ui._findState.key,
findValue = pandora.user.ui._findState.value;
2011-06-19 17:49:25 +00:00
var that = Ox.FormElementGroup({
2011-09-28 00:10:26 +00:00
elements: Ox.merge(pandora.user.ui._list ? [
2011-06-19 17:49:25 +00:00
pandora.$ui.findListSelect = 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},
{id: 'list', title: 'Find: This List', checked: true}
2011-05-25 19:42:45 +00:00
],
overlap: 'right',
type: 'image'
})
.bindEvent({
2011-09-17 17:40:15 +00:00
change: function(data) {
2011-06-06 15:48:11 +00:00
pandora.$ui.findInput.options({
2011-05-25 19:42:45 +00:00
autocomplete: autocompleteFunction()
2011-12-18 09:44:31 +00:00
}).focusInput(true);
2011-05-25 19:42:45 +00:00
}
}),
] : [], [
2011-06-19 17:49:25 +00:00
pandora.$ui.findSelect = Ox.Select({
2011-05-25 19:42:45 +00:00
id: 'select',
items: Ox.merge(
Ox.map(pandora.site.findKeys, function(key, i) {
return !key.capability
|| pandora.site.capabilities[key.capability][pandora.user.level]
? {
id: key.id,
title: 'Find: ' + key.title,
} : null;
}),
[{}, {
id: 'advanced',
title: 'Find: Advanced',
checked: findKey == 'advanced'
}]
),
2011-05-25 19:42:45 +00:00
overlap: 'right',
2011-12-22 07:27:48 +00:00
value: findKey,
2011-05-25 19:42:45 +00:00
width: 112
})
.bindEvent({
2011-09-17 17:40:15 +00:00
change: function(data) {
2011-12-21 15:34:28 +00:00
if (data.value == 'advanced') {
that.update();
2011-11-10 22:48:32 +00:00
pandora.$ui.filterDialog = pandora.ui.filterDialog().open();
2011-05-25 19:42:45 +00:00
} else {
Ox.Log('FIND', 'select change', pandora.$ui.findInput.value())
2011-06-06 15:48:11 +00:00
pandora.$ui.findInput.options({
2011-08-24 08:25:08 +00:00
autocomplete: autocompleteFunction(),
placeholder: ''
2011-12-18 09:44:31 +00:00
}).focusInput(true);
2011-05-25 19:42:45 +00:00
}
}
}),
2011-06-19 17:49:25 +00:00
pandora.$ui.findInput = Ox.Input({
2011-05-25 19:42:45 +00:00
autocomplete: autocompleteFunction(),
autocompleteSelect: true,
autocompleteSelectHighlight: true,
autocompleteSelectMaxWidth: 256,
2011-05-25 19:42:45 +00:00
autocompleteSelectSubmit: true,
clear: true,
id: 'input',
2011-11-04 12:13:04 +00:00
placeholder: findKey == 'advanced' ? 'Edit Query...' : '',
2011-05-25 19:42:45 +00:00
value: findValue,
width: 192
})
.bindEvent({
focus: function(data) {
2011-08-24 08:25:08 +00:00
if (pandora.$ui.findSelect.value() == 'advanced') {
2011-11-04 12:13:04 +00:00
pandora.$ui.findInput.blurInput();
2011-11-10 22:48:32 +00:00
pandora.$ui.filterDialog = pandora.ui.filterDialog().open();
}
},
submit: function(data) {
2011-09-28 00:10:26 +00:00
var findInList = pandora.user.ui._list
&& pandora.$ui.findListSelect.value() == 'list',
key = pandora.$ui.findSelect.value(),
conditions = Ox.merge(
findInList ? [{
key: 'list',
2011-09-28 00:10:26 +00:00
value: pandora.user.ui._list,
operator: '=='
}] : [],
data.value ? [{
key: key,
value: data.value,
operator: '='
}] : []
);
pandora.UI.set('find', {conditions: conditions, operator: '&'});
2011-05-25 19:42:45 +00:00
}
})
]),
id: 'findElement'
})
.css({
float: 'right',
margin: '4px'
});
function autocompleteFunction() {
return pandora.user.ui.find.conditions.length ? function(value, callback) {
2011-05-25 19:42:45 +00:00
var elementValue = that.value(),
2011-09-28 00:10:26 +00:00
key = elementValue[pandora.user.ui._list ? 1 : 0],
2011-06-06 15:48:11 +00:00
findKey = Ox.getObjectById(pandora.site.findKeys, key);
2011-11-04 15:54:42 +00:00
value === '' && Ox.Log('', 'Warning: autocomplete function should never be called with empty value');
2011-09-27 14:14:40 +00:00
if (findKey.autocomplete) {
Ox.Log('AUTO', '_list/select', pandora.user.ui._list, pandora.$ui.findListSelect.value())
2011-05-25 19:42:45 +00:00
pandora.api.autocomplete({
key: key,
2011-09-27 14:14:40 +00:00
query: {
2011-10-25 14:27:38 +00:00
conditions: pandora.user.ui._list
&& pandora.$ui.findListSelect.value() == 'list'
2011-09-28 00:10:26 +00:00
? [{key: 'list', value: pandora.user.ui._list, operator: '=='}] : [],
2011-09-27 14:14:40 +00:00
operator: '&'
},
2011-05-25 19:42:45 +00:00
range: [0, 20],
sort: [{key: 'votes', operator: '-'}],
2011-05-25 19:42:45 +00:00
value: value
}, function(result) {
callback(result.data.items);
});
} else {
2011-10-29 17:46:46 +00:00
callback([]);
2011-05-25 19:42:45 +00:00
}
} : null;
}
that.update = function() {
var findState = pandora.user.ui._findState;
pandora.$ui.findSelect.value(findState.key);
pandora.$ui.findInput.options(
findState.key == 'advanced'
? {placeholder: 'Edit Query...', value: ''}
: {
autocomplete: autocompleteFunction(),
placeholder: '',
value: findState.value
}
);
};
2011-05-25 19:42:45 +00:00
return that;
};