updates to url controller, filter and form elements

This commit is contained in:
rlx 2011-11-10 19:52:26 +00:00
parent 170fd84c80
commit 07c79ed7ac
7 changed files with 162 additions and 79 deletions

View file

@ -172,9 +172,12 @@ Ox.URL = function(options) {
function constructCondition(condition) { function constructCondition(condition) {
var key = condition.key == '*' ? '' : condition.key, var key = condition.key == '*' ? '' : condition.key,
operator = condition.operator, operator = condition.operator,
value = ( value;
Ox.isArray(condition.value) ? condition.value : [condition.value] value = (
).map(encodeValue).join(':'); Ox.isArray(condition.value) ? condition.value : [condition.value]
).map(function(value) {
return encodeValue(constructValue(value, condition.key));
}).join(',');
if (!key) { if (!key) {
operator = operator.replace('=', ''); operator = operator.replace('=', '');
} else if (operator.indexOf('^') > -1) { } else if (operator.indexOf('^') > -1) {
@ -267,6 +270,17 @@ Ox.URL = function(options) {
}).join('/'); }).join('/');
} }
function constructValue(str, key) {
var findKey = Ox.getObjectById(self.options.findKeys, key),
list = findKey.list,
type = Ox.isArray(findKey.type) ? findKey.type[0] : findKey.type,
value = str;
if (type == 'list') {
return list[value];
}
return value;
}
function decodeValue(str) { function decodeValue(str) {
return decodeURIComponent(str); return decodeURIComponent(str);
} }
@ -275,7 +289,7 @@ Ox.URL = function(options) {
// var chars = '/&|()=*:'; // var chars = '/&|()=*:';
var chars = '&|()=*', var chars = '&|()=*',
ret = ''; ret = '';
str.split('').forEach(function(char) { str.toString().split('').forEach(function(char) {
var index = chars.indexOf(char); var index = chars.indexOf(char);
ret += index > -1 ret += index > -1
? '%' + char.charCodeAt(0).toString(16).toUpperCase() ? '%' + char.charCodeAt(0).toString(16).toUpperCase()
@ -338,10 +352,12 @@ Ox.URL = function(options) {
condition.operator = condition.operator.replace('=', '^') condition.operator = condition.operator.replace('=', '^')
} }
} }
if (condition.value.indexOf(':') > -1) { if (condition.value.indexOf(',') > -1) {
condition.value = condition.value.split(':').map(decodeValue); condition.value = condition.value.split(',').map(function(value) {
return parseValue(decodeValue(value), condition.key);
});
} else { } else {
condition.value = decodeValue(condition.value); condition.value = parseValue(decodeValue(condition.value), condition.key);
} }
return condition; return condition;
} }
@ -429,6 +445,16 @@ Ox.URL = function(options) {
); );
} }
function parseTime(str) {
var split = str.split(':').reverse();
while (split.length > 3) {
split.pop();
}
return Ox.formatDuration(split.reduce(function(prev, curr, i) {
return prev + (parseFloat(curr) || 0) * Math.pow(60, i);
}, 0));
}
function parseURL(str, callback) { function parseURL(str, callback) {
// fixme: removing trailing slash makes it impossible to search for '/' // fixme: removing trailing slash makes it impossible to search for '/'
str = str.replace(/(^\/|\/$)/g, ''); str = str.replace(/(^\/|\/$)/g, '');
@ -589,6 +615,31 @@ Ox.URL = function(options) {
} }
} }
function parseValue(str, key) {
var findKey = Ox.getObjectById(self.options.findKeys, key),
list = findKey.list,
type = Ox.isArray(findKey.type) ? findKey.type[0] : findKey.type,
value = str;
if (type == 'boolean') {
value = ['', 'false'].indexOf(str) == -1;
} else if (type == 'date') {
value = Ox.formatDate(Ox.parseDate(str, true), '%F', true);
} else if (type == 'float') {
value = parseFloat(str) || 0;
} else if (type == 'integer') {
value = Math.round(str) || 0;
} else if (type == 'list') {
value = Math.max(list.map(function(value) {
return value.toLowerCase();
}).indexOf(str.toLowerCase()), 0);
} else if (type == 'time') {
value = parseTime(value);
} else if (type == 'year') {
value = Math.round(str) || 1970;
}
return value.toString();
}
function saveURL() { function saveURL() {
} }

View file

@ -61,12 +61,18 @@ Ox.Filter = function(options, self) {
{id: '!<', title: 'is not before'}, {id: '!<', title: 'is not before'},
{id: '>', title: 'is after'}, {id: '>', title: 'is after'},
{id: '!>', title: 'is not after'}, {id: '!>', title: 'is not after'},
{id: '=:', title: 'is between'}, {id: '=,', title: 'is between'},
{id: '!=:', title: 'is not between'} {id: '!=,', title: 'is not between'}
], ],
list: [ list: [
{id: '=', title: 'is'}, {id: '=', title: 'is'},
{id: '!=', title: 'is not'} {id: '!=', title: 'is not'},
{id: '<', title: 'is less than'},
{id: '!<', title: 'is not less than'},
{id: '>', title: 'is greater than'},
{id: '!>', title: 'is not greater than'},
{id: '=,', title: 'is between'},
{id: '!=,', title: 'is not between'}
], ],
number: [ number: [
{id: '=', title: 'is'}, {id: '=', title: 'is'},
@ -75,12 +81,8 @@ Ox.Filter = function(options, self) {
{id: '!<', title: 'is not less than'}, {id: '!<', title: 'is not less than'},
{id: '>', title: 'is greater than'}, {id: '>', title: 'is greater than'},
{id: '!>', title: 'is not greater than'}, {id: '!>', title: 'is not greater than'},
{id: '=:', title: 'is between'}, {id: '=,', title: 'is between'},
{id: '!=:', title: 'is not between'}/*, {id: '!=,', title: 'is not between'}
{id: '^', title: 'starts with'},
{id: '!^', title: 'does not start with'},
{id: '$', title: 'ends with'},
{id: '!$', title: 'does not end with'}*/
], ],
string: [ string: [
{id: '==', title: 'is'}, {id: '==', title: 'is'},
@ -95,24 +97,18 @@ Ox.Filter = function(options, self) {
text: [ text: [
{id: '=', title: 'contains'}, {id: '=', title: 'contains'},
{id: '!=', title: 'does not contain'} {id: '!=', title: 'does not contain'}
],
year: [
{id: '=', title: 'is'},
{id: '!=', title: 'is not'},
{id: '<', title: 'is before'},
{id: '!<', title: 'is not before'},
{id: '>', title: 'is after'},
{id: '!>', title: 'is not after'},
{id: '=:', title: 'is between'},
{id: '!=:', title: 'is not between'}
] ]
}; };
self.defaultValue = { self.defaultValue = {
boolean: 'true', boolean: 'true',
date: Ox.formatDate(new Date(), '%F'), date: Ox.formatDate(new Date(), '%F'),
number: 0, float: 0,
hue: 0,
integer: 0,
list: 0,
string: '', string: '',
text: '', text: '',
time: '00:00:00',
year: new Date().getFullYear() year: new Date().getFullYear()
}; };
self.operators = [ self.operators = [
@ -319,24 +315,26 @@ Ox.Filter = function(options, self) {
var condition = subpos == -1 var condition = subpos == -1
? self.options.query.conditions[pos] ? self.options.query.conditions[pos]
: self.options.query.conditions[pos].conditions[subpos], : self.options.query.conditions[pos].conditions[subpos],
oldOperator = condition.operator, oldFindKey = Ox.getObjectById(self.options.findKeys, condition.key),
oldType = Ox.getObjectById(self.options.findKeys, condition.key).type, newFindKey = Ox.getObjectById(self.options.findKeys, key),
newType = Ox.getObjectById(self.options.findKeys, key).type, oldConditionType = getConditionType(oldFindKey.type),
oldConditionType = getConditionType(oldType), newConditionType = getConditionType(newFindKey.type),
newConditionType = getConditionType(newType),
changeConditionType = oldConditionType != newConditionType, changeConditionType = oldConditionType != newConditionType,
changeConditionFormat = !Ox.isEqual(oldFindKey.format, newFindKey.format),
wasUselessCondition = isUselessCondition(pos, subpos); wasUselessCondition = isUselessCondition(pos, subpos);
Ox.Log('Form', 'old new', oldConditionType, newConditionType) Ox.Log('Form', 'old new', oldConditionType, newConditionType)
condition.key = key; condition.key = key;
if (changeConditionType) { if (changeConditionType || changeConditionFormat) {
if (Ox.getPositionById(self.conditionOperators[newConditionType], condition.operator) == -1) {
condition.operator = self.conditionOperators[newConditionType][0].id;
}
if ( if (
['string', 'text'].indexOf(oldConditionType) == -1 ['string', 'text'].indexOf(oldConditionType) == -1
|| ['string', 'text'].indexOf(newConditionType) == -1 || ['string', 'text'].indexOf(newConditionType) == -1
) { ) {
condition.value = self.defaultValue[newType]; condition.value = self.defaultValue[newFindKey.type];
} }
renderConditions(); renderConditions();
//self.$conditions[pos].replaceElement(1, constructConditionOperator(pos, oldOperator));
} }
if (!(wasUselessCondition && isUselessCondition(pos, subpos))) { if (!(wasUselessCondition && isUselessCondition(pos, subpos))) {
triggerChangeEvent(); triggerChangeEvent();
@ -349,13 +347,13 @@ Ox.Filter = function(options, self) {
var condition = subpos == -1 var condition = subpos == -1
? self.options.query.conditions[pos] ? self.options.query.conditions[pos]
: self.options.query.conditions[pos].conditions[subpos], : self.options.query.conditions[pos].conditions[subpos],
isBetween = operator.indexOf(':') > -1, isBetween = operator.indexOf(',') > -1,
wasBetween = Ox.isArray(condition.value), wasBetween = Ox.isArray(condition.value),
wasUselessCondition = isUselessCondition(pos, subpos); wasUselessCondition = isUselessCondition(pos, subpos);
Ox.Log('FILTER', 'chCoOp', 'iB/wB', isBetween, wasBetween) Ox.Log('FILTER', 'chCoOp', 'iB/wB', isBetween, wasBetween)
condition.operator = operator; condition.operator = operator;
if (isBetween && !wasBetween) { if (isBetween && !wasBetween) {
condition.operator = condition.operator.replace(':', ''); condition.operator = condition.operator.replace(',', '');
condition.value = [condition.value, condition.value] condition.value = [condition.value, condition.value]
renderConditions(); renderConditions();
} else if (!isBetween && wasBetween) { } else if (!isBetween && wasBetween) {
@ -368,10 +366,10 @@ Ox.Filter = function(options, self) {
} }
function changeConditionValue(pos, subpos, value) { function changeConditionValue(pos, subpos, value) {
Ox.Log('FILTER', 'cCV', pos, subpos, value);
var condition = subpos == -1 var condition = subpos == -1
? self.options.query.conditions[pos] ? self.options.query.conditions[pos]
: self.options.query.conditions[pos].conditions[subpos]; : self.options.query.conditions[pos].conditions[subpos];
// fixme: change to number if needed
condition.value = value; condition.value = value;
triggerChangeEvent(); triggerChangeEvent();
} }
@ -391,7 +389,7 @@ Ox.Filter = function(options, self) {
function getConditionType(type) { function getConditionType(type) {
type = Ox.isArray(type) ? type[0] : type; type = Ox.isArray(type) ? type[0] : type;
if (['float', 'hue', 'integer', 'year'].indexOf(type) > -1) { if (['float', 'hue', 'integer', 'time', 'year'].indexOf(type) > -1) {
type = 'number'; type = 'number';
} }
return type; return type;
@ -543,7 +541,7 @@ Ox.Filter = function(options, self) {
return { return {
// fixme: should be "selected", not "checked" // fixme: should be "selected", not "checked"
checked: Ox.isArray(condition.value) checked: Ox.isArray(condition.value)
? operator.id == condition.operator + ':' ? operator.id == condition.operator + ','
: operator.id == condition.operator, : operator.id == condition.operator,
id: operator.id, id: operator.id,
title: operator.title title: operator.title
@ -578,6 +576,7 @@ Ox.Filter = function(options, self) {
}) })
).bindEvent({ ).bindEvent({
change: function(data) { change: function(data) {
Ox.Log('FILTER', 'change event', data)
var $element = data._element.parent(); var $element = data._element.parent();
changeConditionValue( changeConditionValue(
$element.data('position'), $element.data('position'),
@ -657,9 +656,27 @@ Ox.Filter = function(options, self) {
findKey = Ox.getObjectById(self.options.findKeys, condition.key), findKey = Ox.getObjectById(self.options.findKeys, condition.key),
isArray = Ox.isArray(condition.value), isArray = Ox.isArray(condition.value),
isHue, isHue,
// FIXME: always use 'int'
type = findKey.type == 'integer' ? 'int' : findKey.type, type = findKey.type == 'integer' ? 'int' : findKey.type,
value = !isArray ? condition.value : condition.value[index],
formatArgs, formatType, title; formatArgs, formatType, title;
if (findKey.format) { if (type == 'boolean') {
$input = Ox.Select({
items: [
{id: 'true', title: 'true', checked: value == 'true'},
{id: 'false', title: 'false', checked: value == 'false'}
],
width: 288
});
} else if (type == 'list') {
Ox.Log('FILTER', findKey, condition)
$input = Ox.Select({
items: findKey.list.map(function(v, i) {
return {id: i, title: v, checked: i == value}
}),
width: !isArray ? 288 : 128
});
} else if (findKey.format) {
formatArgs = findKey.format.args formatArgs = findKey.format.args
formatType = findKey.format.type; formatType = findKey.format.type;
if (formatType == 'color') { if (formatType == 'color') {
@ -681,20 +698,20 @@ Ox.Filter = function(options, self) {
}); });
} else if (formatType == 'date') { } else if (formatType == 'date') {
$input = Ox.DateInput(!isArray ? { $input = Ox.DateInput(!isArray ? {
value: condition.value, value: value,
width: {day: 66, month: 66, year: 140} width: {day: 66, month: 66, year: 140}
} : { } : {
value: condition.value[index], value: value,
width: {day: 32, month: 32, year: 48} width: {day: 32, month: 32, year: 48}
}); });
} else if (formatType == 'duration') { } else if (formatType == 'duration') {
$input = Ox.TimeInput(!isArray ? { $input = Ox.TimeInput(!isArray ? {
seconds: true, seconds: true,
value: '00:00:00', value: value,
width: {hours: 91, minutes: 91, seconds: 90} width: {hours: 91, minutes: 91, seconds: 90}
} : { } : {
seconds: true, seconds: true,
value: '00:00:00', value: value,
width: {hours: 38, minutes: 37, seconds: 37} width: {hours: 38, minutes: 37, seconds: 37}
}); });
} else if ([ } else if ([
@ -705,13 +722,13 @@ Ox.Filter = function(options, self) {
elements: [ elements: [
Ox.Input({ Ox.Input({
type: type, type: type,
value: !isArray ? condition.value : condition.value[index], value: value,
width: !isArray ? 240 : 80 width: !isArray ? 240 : 80
}), }),
formatType == 'value' ? Ox.Select({ formatType == 'value' ? Ox.Select({
overlap: 'left', overlap: 'left',
items: ['K', 'M', 'G', 'T'].map(function(prefix) { items: ['K', 'M', 'G', 'T'].map(function(prefix, i) {
return {id: prefix + title, title: prefix + title}; return {id: Math.pow(1000, i + 1), title: prefix + title};
}), }),
width: 48 width: 48
}) : Ox.Label({ }) : Ox.Label({
@ -722,25 +739,21 @@ Ox.Filter = function(options, self) {
}) })
], ],
float: 'right', float: 'right',
joinValues: function(values) {
Ox.print(values, '?????')
return formatType == 'value'
? values[0] * values[1]
: values[0];
},
width: !isArray ? 288 : 128 width: !isArray ? 288 : 128
}) })
} }
} else { } else {
if (type == 'boolean') { $input = Ox.Input({
$input = Ox.Select({ type: type,
items: [ value: value,
{id: 'true', title: 'true'}, width: !isArray ? 288 : 128
{id: 'false', title: 'false'} });
],
width: 288
});
} else {
$input = Ox.Input({
type: type,
value: !isArray ? condition.value : condition.value[index],
width: !isArray ? 288 : 128
});
}
} }
return $input; return $input;
} }

View file

@ -24,6 +24,7 @@ Ox.FormElementGroup = function(options, self) {
id: '', id: '',
elements: [], elements: [],
float: 'left', float: 'left',
joinValues: null,
separators: [], separators: [],
width: 0 width: 0
}) })
@ -32,7 +33,7 @@ Ox.FormElementGroup = function(options, self) {
( (
self.options.float == 'left' ? self.options.float == 'left' ?
self.options.elements : self.options.elements.reverse() self.options.elements : Ox.clone(self.options.elements).reverse()
).forEach(function($element, i) { ).forEach(function($element, i) {
$element.css({ $element.css({
float: self.options.float // fixme: make this a class float: self.options.float // fixme: make this a class
@ -41,6 +42,9 @@ Ox.FormElementGroup = function(options, self) {
autovalidate: function(data) { autovalidate: function(data) {
that.triggerEvent({autovalidate: data}); that.triggerEvent({autovalidate: data});
}, },
change: function(data) {
that.triggerEvent({change: {value: that.value()}});
},
validate: function(data) { validate: function(data) {
that.triggerEvent({validate: data}); that.triggerEvent({validate: data});
} }
@ -78,13 +82,16 @@ Ox.FormElementGroup = function(options, self) {
}; };
that.value = function() { that.value = function() {
return self.options.elements.map(function(element) { var values = self.options.elements.map(function(element) {
var ret = null; var ret = null;
['checked', 'selected', 'value'].forEach(function(v) { ['checked', 'selected', 'value'].forEach(function(v) {
element[v] && (ret = element[v]()); element[v] && (ret = element[v]());
}); });
return ret; return ret;
}); });
return self.options.joinValues
? self.options.joinValues(values)
: values;
}; };
return that; return that;

View file

@ -137,13 +137,16 @@ Ox.InputGroup = function(options, self) {
}; };
that.value = function() { that.value = function() {
return self.options.inputs.map(function(input) { var values = self.options.inputs.map(function(input) {
var ret = null; var ret = null;
['checked', 'selected', 'value'].forEach(function(v) { ['checked', 'selected', 'value'].forEach(function(v) {
input[v] && (ret = input[v]()); input[v] && (ret = input[v]());
}); });
return ret; return ret;
}); });
return self.options.joinValues
? self.options.joinValues(values)
: values;
}; };
return that; return that;

View file

@ -113,6 +113,10 @@ Ox.TimeInput = function(options, self) {
] : [], self.options.ampm ? [ ] : [], self.options.ampm ? [
self.$input.ampm self.$input.ampm
] : []), ] : []),
joinValues: function(values) {
setValue();
return self.options.value;
},
separators: Ox.merge([ separators: Ox.merge([
{title: ':', width: 8}, {title: ':', width: 8},
], self.options.seconds ? [ ], self.options.seconds ? [
@ -125,7 +129,7 @@ Ox.TimeInput = function(options, self) {
width: 0 width: 0
//width: self.options.width || 128 //width: self.options.width || 128
}), self) }), self)
.bindEvent('change', setValue); /*.bindEvent('change', setValue)*/;
setValue(); setValue();

View file

@ -278,16 +278,20 @@ Ox.parseDate = function(str, utc) {
var date = new Date(0), var date = new Date(0),
defaults = [, 1, 1, 0, 0, 0], defaults = [, 1, 1, 0, 0, 0],
values = /(-?\d+)-?(\d+)?-?(\d+)? ?(\d+)?:?(\d+)?:?(\d+)?/.exec(str); values = /(-?\d+)-?(\d+)?-?(\d+)? ?(\d+)?:?(\d+)?:?(\d+)?/.exec(str);
values.shift(); if (values) {
values = values.map(function(v, i) { values.shift();
return v || defaults[i]; values = values.map(function(v, i) {
}); return v || defaults[i];
values[1]--; });
[ values[1]--;
'FullYear', 'Month', 'Date', 'Hours', 'Minutes', 'Seconds' [
].forEach(function(part, i) { 'FullYear', 'Month', 'Date', 'Hours', 'Minutes', 'Seconds'
Ox['set' + part](date, values[i], utc); ].forEach(function(part, i) {
}); Ox['set' + part](date, values[i], utc);
});
} else {
date = null;
}
return date; return date;
}; };

View file

@ -548,6 +548,7 @@ Ox.formatValue = function(num, str, bin) {
/*@ /*@
Ox.formatUnit <f> Formats a number with a unit Ox.formatUnit <f> Formats a number with a unit
@*/ @*/
Ox.formatUnit = function(num, str) { Ox.formatUnit = function(num, str, dec) {
return Ox.formatNumber(num, 3) + ' ' + str; dec = Ox.isUndefined(dec) ? 3 : dec;
return Ox.formatNumber(num, dec) + (str == '%' ? '' : ' ') + str;
}; };