Ox.Filter: make new conditions have same key and operator as previous condition; focus input

This commit is contained in:
rolux 2014-05-20 01:31:33 +02:00
parent 17120e6aec
commit af345b3514

View file

@ -290,11 +290,17 @@ Ox.Filter = function(options, self) {
function addCondition(pos, subpos, isGroup) {
subpos = Ox.isUndefined(subpos) ? -1 : subpos;
var key = self.options.findKeys[0],
var condition, key;
if (subpos == -1) {
condition = self.options.value.conditions[pos - 1]
} else {
condition = self.options.value.conditions[pos].conditions[subpos - 1];
}
key = Ox.getObjectById(self.options.findKeys, condition.key);
condition = {
key: key.id,
value: '',
operator: self.conditionOperators[key.type][0].id
operator: condition.operator,
value: ''
};
if (isGroup) {
Ox.Log('Form', 'isGroup', self.options.value.operator)
@ -308,7 +314,7 @@ Ox.Filter = function(options, self) {
} else {
self.options.value.conditions[pos].conditions.splice(subpos, 0, condition);
}
renderConditions();
renderConditions(pos, subpos);
if (!isUselessCondition(pos, subpos)) {
triggerChangeEvent();
}
@ -612,24 +618,37 @@ Ox.Filter = function(options, self) {
}
}
function renderConditions() {
function renderConditions(focusPos, focusSubpos) {
Ox.Log('Form', 'renderConditions', self.options.value)
var $conditions = [];
var $conditions = [], focusIndex;
while (self.$form.options('items').length > self.numberOfAdditionalFormItems) {
self.$form.removeItem(1);
}
self.options.value.conditions.forEach(function(condition, pos) {
if (!condition.conditions) {
$conditions.push(renderCondition(condition, pos));
if (pos == focusPos && focusSubpos == -1) {
focusIndex = $conditions.length - 1;
}
} else {
$conditions.push(renderGroup(condition, pos));
condition.conditions.forEach(function(subcondition, subpos) {
$conditions.push(renderCondition(subcondition, pos, subpos));
if (pos == focusPos && subpos == focusSubpos) {
focusIndex = $conditions.length - 1;
}
});
}
});
$conditions.forEach(function($condition, pos) {
var $input;
self.$form.addItem(1 + pos, $condition);
if (focusIndex == pos) {
$input = $condition.options('elements')[2];
if ($input.focusInput) {
$input.focusInput();
}
}
});
}