Ox.Filter: options.query -> options.value, handle value update

This commit is contained in:
rolux 2014-02-17 11:26:33 +05:30
parent 15444fe5a9
commit 3ce2852a99

View file

@ -7,14 +7,14 @@ Ox.Filter <f> Filter Object
list <o> list object
sort <s> List sort
view <s> List view
query <o> query object
sortKeys <a|[]> keys to sort by
value <o> query object
conditions <[o]> Conditions (array of {key, value, operator})
operator <s> Operator ('&' or '|')
limit <o> Limit
key <s> Limit key
sort <s> Limit sort
value <n> Limit value
sortKeys <a|[]> keys to sort by
viewKeys <a|[]> visible keys
self <o> Shared private variable
([options[, self]]) -> <o:Ox.Element> Filter Object
@ -28,29 +28,21 @@ Ox.Filter = function(options, self) {
.defaults({
findKeys: [],
list: null,
query: {
sortKeys: [],
viewKeys: [],
value: {
conditions: [],
operator: '&'
},
sortKeys: [],
viewKeys: []
}
})
.options(options || {})
.update({
query: function() {
// ...
value: function() {
setValue();
renderConditions();
}
});
// fixme: this should not happen, but some lists
// have their query set to {} or their query operator set to ''
if (Ox.isEmpty(self.options.query)) {
self.options.query = {conditions: [], operator: '&'};
} else if (self.options.query.operator == '') {
self.options.query.operator = '&';
}
Ox.Log('Form', 'Ox.Filter self.options', self.options);
self.conditionOperators = {
boolean: [
{id: '=', title: Ox._('is')},
@ -133,15 +125,7 @@ Ox.Filter = function(options, self) {
{id: '|', title: Ox._('any')}
];
if (!self.options.query.conditions.length) {
self.options.query.conditions = [{
key: self.options.findKeys[0].id,
value: '',
operator: self.conditionOperators[
getConditionType(self.options.findKeys[0].type)
][0].id
}];
}
setValue();
self.$operator = Ox.FormElementGroup({
elements: [
@ -154,7 +138,7 @@ Ox.Filter = function(options, self) {
elements: [
Ox.Select({
items: self.operators,
value: self.options.query.operator,
value: self.options.value.operator,
width: 48
})
.bindEvent({
@ -175,7 +159,7 @@ Ox.Filter = function(options, self) {
self.$save = Ox.InputGroup({
inputs: [
self.$foo = Ox.Checkbox({
Ox.Checkbox({
width: 16
}),
Ox.Input({
@ -295,6 +279,7 @@ Ox.Filter = function(options, self) {
self.$form = Ox.Form({
items: self.$items
}).appendTo(that);
renderConditions();
//that.$element = self.$form.$element;
@ -307,16 +292,16 @@ Ox.Filter = function(options, self) {
operator: self.conditionOperators[key.type][0].id
};
if (isGroup) {
Ox.Log('Form', 'isGroup', self.options.query.operator)
Ox.Log('Form', 'isGroup', self.options.value.operator)
condition = {
conditions: [condition],
operator: self.options.query.operator == '&' ? '|' : '&'
operator: self.options.value.operator == '&' ? '|' : '&'
};
}
if (subpos == -1) {
self.options.query.conditions.splice(pos, 0, condition);
self.options.value.conditions.splice(pos, 0, condition);
} else {
self.options.query.conditions[pos].conditions.splice(subpos, 0, condition);
self.options.value.conditions[pos].conditions.splice(subpos, 0, condition);
}
renderConditions();
if (!isUselessCondition(pos, subpos)) {
@ -328,8 +313,8 @@ Ox.Filter = function(options, self) {
subpos = Ox.isUndefined(subpos) ? -1 : subpos;
Ox.Log('Form', 'changeConditionKey', pos, subpos, key);
var condition = subpos == -1
? self.options.query.conditions[pos]
: self.options.query.conditions[pos].conditions[subpos],
? self.options.value.conditions[pos]
: self.options.value.conditions[pos].conditions[subpos],
oldFindKey = Ox.getObjectById(self.options.findKeys, condition.key),
newFindKey = Ox.getObjectById(self.options.findKeys, key),
oldConditionType = getConditionType(oldFindKey.type),
@ -358,10 +343,10 @@ Ox.Filter = function(options, self) {
function changeConditionOperator(pos, subpos, operator) {
subpos = Ox.isUndefined(subpos) ? -1 : subpos;
Ox.Log('FILTER', 'chCoOp', 'query', self.options.query)
Ox.Log('FILTER', 'chCoOp', 'query', self.options.value)
var condition = subpos == -1
? self.options.query.conditions[pos]
: self.options.query.conditions[pos].conditions[subpos],
? self.options.value.conditions[pos]
: self.options.value.conditions[pos].conditions[subpos],
isBetween = operator.indexOf(',') > -1,
wasBetween = Ox.isArray(condition.value),
wasUselessCondition = isUselessCondition(pos, subpos);
@ -383,28 +368,28 @@ Ox.Filter = function(options, self) {
function changeConditionValue(pos, subpos, value) {
Ox.Log('FILTER', 'cCV', pos, subpos, value);
var condition = subpos == -1
? self.options.query.conditions[pos]
: self.options.query.conditions[pos].conditions[subpos];
? self.options.value.conditions[pos]
: self.options.value.conditions[pos].conditions[subpos];
condition.value = value;
triggerChangeEvent();
}
function changeGroupOperator(pos, value) {
self.options.query.conditions[pos].operator = value;
self.options.value.conditions[pos].operator = value;
triggerChangeEvent();
}
function changeOperator(data) {
var hasGroups = false;
self.options.query.operator = data.value;
Ox.forEach(self.options.query.conditions, function(condition) {
self.options.value.operator = data.value;
Ox.forEach(self.options.value.conditions, function(condition) {
if (condition.conditions) {
hasGroups = true;
return false; // break
}
});
hasGroups && renderConditions();
self.options.query.conditions.length > 1 && triggerChangeEvent();
self.options.value.conditions.length > 1 && triggerChangeEvent();
}
function getConditionType(type) {
@ -418,16 +403,16 @@ Ox.Filter = function(options, self) {
function isUselessCondition(pos, subpos) {
subpos = Ox.isUndefined(subpos) ? -1 : subpos;
var conditions = subpos == -1
? self.options.query.conditions[pos].conditions
|| [self.options.query.conditions[pos]]
: [self.options.query.conditions[pos].conditions[subpos]],
? self.options.value.conditions[pos].conditions
|| [self.options.value.conditions[pos]]
: [self.options.value.conditions[pos].conditions[subpos]],
isUseless = false;
Ox.forEach(conditions, function(condition) {
isUseless = ['string', 'text'].indexOf(getConditionType(
Ox.getObjectById(self.options.findKeys, condition.key).type
)) > -1
&& (
self.options.query.operator == '&' ? ['', '^', '$'] : ['!', '!^', '!$']
self.options.value.operator == '&' ? ['', '^', '$'] : ['!', '!^', '!$']
).indexOf(condition.operator) > -1
&& condition.value === '';
if (!isUseless) {
@ -440,10 +425,10 @@ Ox.Filter = function(options, self) {
function removeCondition(pos, subpos) {
subpos = Ox.isUndefined(subpos) ? -1 : subpos;
var wasUselessCondition = isUselessCondition(pos, subpos);
if (subpos == -1 || self.options.query.conditions[pos].conditions.length == 1) {
self.options.query.conditions.splice(pos, 1);
if (subpos == -1 || self.options.value.conditions[pos].conditions.length == 1) {
self.options.value.conditions.splice(pos, 1);
} else {
self.options.query.conditions[pos].conditions.splice(subpos, 1);
self.options.value.conditions[pos].conditions.splice(subpos, 1);
}
renderConditions();
if (!wasUselessCondition) {
@ -453,12 +438,12 @@ Ox.Filter = function(options, self) {
function renderButtons(pos, subpos) {
subpos = Ox.isUndefined(subpos) ? -1 : subpos;
var isGroup = subpos == -1 && self.options.query.conditions[pos].conditions;
var isGroup = subpos == -1 && self.options.value.conditions[pos].conditions;
return [].concat([
Ox.Button({
id: 'remove',
title: self.options.query.conditions.length == 1 ? 'close' : 'remove',
tooltip: self.options.query.conditions.length == 1 ? Ox._('Reset this condition')
title: self.options.value.conditions.length == 1 ? 'close' : 'remove',
tooltip: self.options.value.conditions.length == 1 ? Ox._('Reset this condition')
: isGroup ? Ox._('Remove this group of conditions')
: Ox._('Remove this condition'),
type: 'image'
@ -467,9 +452,9 @@ Ox.Filter = function(options, self) {
.bindEvent({
click: function(data) {
var key;
if (self.options.query.conditions.length == 1) {
if (self.options.value.conditions.length == 1) {
key = self.options.findKeys[0];
self.options.query.conditions = [{
self.options.value.conditions = [{
key: key.id,
value: '',
operator: self.conditionOperators[key.type][0].id
@ -533,8 +518,8 @@ Ox.Filter = function(options, self) {
function renderCondition(condition, pos, subpos) {
subpos = Ox.isUndefined(subpos) ? -1 : subpos;
var condition = subpos == -1
? self.options.query.conditions[pos]
: self.options.query.conditions[pos].conditions[subpos];
? self.options.value.conditions[pos]
: self.options.value.conditions[pos].conditions[subpos];
Ox.Log('Form', 'renderCondition', condition, pos, subpos)
return Ox.FormElementGroup({
elements: [
@ -618,12 +603,12 @@ Ox.Filter = function(options, self) {
}
function renderConditions() {
Ox.Log('Form', 'renderConditions', self.options.query)
Ox.Log('Form', 'renderConditions', self.options.value)
var $conditions = [];
while (self.$form.options('items').length > self.numberOfAdditionalFormItems) {
self.$form.removeItem(1);
}
self.options.query.conditions.forEach(function(condition, pos) {
self.options.value.conditions.forEach(function(condition, pos) {
if (!condition.conditions) {
$conditions.push(renderCondition(condition, pos));
} else {
@ -643,7 +628,7 @@ Ox.Filter = function(options, self) {
var $condition = Ox.FormElementGroup({
elements: [
Ox.Label({
title: self.options.query.operator == '&'
title: self.options.value.operator == '&'
? (pos == 0 ? 'Both' : 'and')
: (pos == 0 ? 'Either': 'or'),
overlap: 'right',
@ -653,7 +638,7 @@ Ox.Filter = function(options, self) {
elements: [
Ox.Select({
items: self.operators,
value: self.options.query.operator == '&' ? '|' : '&',
value: self.options.value.operator == '&' ? '|' : '&',
width: 48
})
.bindEvent({
@ -823,16 +808,37 @@ Ox.Filter = function(options, self) {
return $input;
}
function setValue() {
// fixme: this should not happen, but some lists
// have their query set to {} or their query operator set to ''
if (Ox.isEmpty(self.options.value)) {
self.options.value = {conditions: [], operator: '&'};
} else if (self.options.value.operator == '') {
self.options.value.operator = '&';
}
Ox.Log('Form', 'Ox.Filter self.options', self.options);
// end fixme
if (!self.options.value.conditions.length) {
self.options.value.conditions = [{
key: self.options.findKeys[0].id,
value: '',
operator: self.conditionOperators[
getConditionType(self.options.findKeys[0].type)
][0].id
}];
}
}
function triggerChangeEvent() {
var query = Ox.clone(self.options.query, true);
var value = Ox.clone(self.options.value, true);
/*
// FIXME: doesn't work for nested conditions
query.conditions.forEach(function(condition) {
value.conditions.forEach(function(condition) {
// Ox.print('CO', condition.operator)
condition.operator = condition.operator.replace(':', '');
});
*/
that.triggerEvent('change', {query: query});
that.triggerEvent('change', {value: value});
}
/*@
@ -845,13 +851,17 @@ Ox.Filter = function(options, self) {
return {
save: value[0],
name: value[1],
query: self.options.query
query: self.options.value
};
}
};
that.value = function() {
// ...
if (arguments.length == 0) {
return self.options.value;
} else {
return that.options({value: arguments[0]});
}
};
return that;