1
0
Fork 0
forked from 0x2620/oxjs

updates to url controller, filter and form elements

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

View file

@ -172,9 +172,12 @@ Ox.URL = function(options) {
function constructCondition(condition) {
var key = condition.key == '*' ? '' : condition.key,
operator = condition.operator,
value = (
Ox.isArray(condition.value) ? condition.value : [condition.value]
).map(encodeValue).join(':');
value;
value = (
Ox.isArray(condition.value) ? condition.value : [condition.value]
).map(function(value) {
return encodeValue(constructValue(value, condition.key));
}).join(',');
if (!key) {
operator = operator.replace('=', '');
} else if (operator.indexOf('^') > -1) {
@ -267,6 +270,17 @@ Ox.URL = function(options) {
}).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) {
return decodeURIComponent(str);
}
@ -275,7 +289,7 @@ Ox.URL = function(options) {
// var chars = '/&|()=*:';
var chars = '&|()=*',
ret = '';
str.split('').forEach(function(char) {
str.toString().split('').forEach(function(char) {
var index = chars.indexOf(char);
ret += index > -1
? '%' + char.charCodeAt(0).toString(16).toUpperCase()
@ -338,10 +352,12 @@ Ox.URL = function(options) {
condition.operator = condition.operator.replace('=', '^')
}
}
if (condition.value.indexOf(':') > -1) {
condition.value = condition.value.split(':').map(decodeValue);
if (condition.value.indexOf(',') > -1) {
condition.value = condition.value.split(',').map(function(value) {
return parseValue(decodeValue(value), condition.key);
});
} else {
condition.value = decodeValue(condition.value);
condition.value = parseValue(decodeValue(condition.value), condition.key);
}
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) {
// fixme: removing trailing slash makes it impossible to search for '/'
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() {
}