update URL controller

This commit is contained in:
rolux 2013-02-18 14:59:15 +05:30
parent b520654e31
commit 10328fc14b

View file

@ -207,16 +207,6 @@ Ox.URL <f> URL controller
operator: '&' operator: '&'
} }
}, },
'/?a=[1,2]&b=true&n=1.2&o={"k":"v"}&s1="foo"&s2=bar': {
json: {
a: [1, 2],
b: true,
n: 1.2,
o: {k: 'v'},
s1: 'foo',
s2: 'bar'
}
},
'/#a?k=v&l=w': { '/#a?k=v&l=w': {
hash: { hash: {
anchor: 'a', anchor: 'a',
@ -225,6 +215,18 @@ Ox.URL <f> URL controller
{key: 'l', value: 'w'} {key: 'l', value: 'w'}
] ]
} }
},
'/#?a=[1,2]&b=true&n=1.2&o={"k":"v"}&s1="foo"&s2=bar': {
hash: {
query: [
{key: 'a', value: [1, 2]},
{key: 'b', value: true},
{key: 'n', value: 1.2},
{key: 'o', value: {k: 'v'}},
{key: 's1', value: 'foo'},
{key: 's2', value: 'bar'}
]
}
} }
}; };
</script> </script>
@ -256,17 +258,17 @@ Ox.URL <f> URL controller
true true
> !!Ox.test.url.parse('/population>0&(name=a*|name=*z)', function(o) { Ox.test(o, Ox.test.result['/population>0&(name=a*|name=*z)']); }) > !!Ox.test.url.parse('/population>0&(name=a*|name=*z)', function(o) { Ox.test(o, Ox.test.result['/population>0&(name=a*|name=*z)']); })
true true
> !!Ox.test.url.parse('/?a=[1,2]&b=true&n=1.2&o={"k":"v"}&s1="foo"&s2=bar', function(o) { Ox.test(o, Ox.test.result['/?a=[1,2]&b=true&n=1.2&o={"k":"v"}&s1="foo"&s2=bar']); })
true
> !!Ox.test.url.parse('/#a?k=v&l=w', function(o) { Ox.test(o, Ox.test.result['/#a?k=v&l=w']); }) > !!Ox.test.url.parse('/#a?k=v&l=w', function(o) { Ox.test(o, Ox.test.result['/#a?k=v&l=w']); })
true true
> !!Ox.test.url.parse('/#?a=[1,2]&b=true&n=1.2&o={"k":"v"}&s1="foo"&s2=bar', function(o) { Ox.test(o, Ox.test.result['/#?a=[1,2]&b=true&n=1.2&o={"k":"v"}&s1="foo"&s2=bar']); })
true
@*/ @*/
/* /*
example.com[/page][#hash] example.com[/page][#hash]
or or
example.com[/type][/item][/view][/span][/sort][/find][?json][#hash] example.com[/type][/item][/view][/span][/sort][/find][#hash]
page Special page, like "about" or "contact" page Special page, like "about" or "contact"
type Section a.k.a. item type, like "movies", "edits", "texts" etc. type Section a.k.a. item type, like "movies", "edits", "texts" etc.
@ -285,11 +287,11 @@ find Query, like a=x or a=x&b=y or a=x&(b=y|c=z). A query object has the form
object has the form {key: '', value: '' or ['', ''], operator: ''} object has the form {key: '', value: '' or ['', ''], operator: ''}
(comparison operator) or {conditions: [], operator: ''} (logical (comparison operator) or {conditions: [], operator: ''} (logical
operator). Condition strings can be more than just "k=v", see below. operator). Condition strings can be more than just "k=v", see below.
json Query, like 'a=[1,2]&b=true&f=1.2&i=3&o={"k":"v"}&s="foo"'. Values are hash Anchor and/or query, like 'a' or '?k=v' or 'a?k=v&l=w' or
evaluated as JSON, and if that fails interpreted as a string. So strings '?a=[1,2]&b=true&f=1.2&i=3&o={"k":"v"}&s="foo"'. Values are evaluated as
(outside arrays or objects) work without quotes as well, unless their JSON, and if that fails interpreted as a string. So strings (outside
value is valid JSON, like 'true' or '1'. arrays or objects) work without quotes as well, unless their value is
hash Anchor and/or query, like 'a' or '?k=v' or 'a?k=v&l=w'. valid JSON, like 'true' or '1'.
String Key Value Operator String Key Value Operator
v * v = any text or string contains or any number is v * v = any text or string contains or any number is
@ -433,14 +435,13 @@ Ox.URL = function(options) {
} }
function constructHash(hash) { function constructHash(hash) {
return (hash.anchor || '') var obj = {};
+ hash.query ? '?' + hash.query.map(function(query) { if (hash.query) {
return encodeValue(query.key) + '=' + encodeValue(query.value); hash.query.forEach(function(condition) {
}) : ''; obj[condition.key] = condition.value;
} });
}
function constructJSON(json) { return (hash.anchor || '') + hash.query ? '?' + Ox.serialize(obj) : '';
return Ox.serialize(json, true);
} }
function constructLocation(location) { function constructLocation(location) {
@ -510,15 +511,15 @@ Ox.URL = function(options) {
return type == 'enum' ? values[value] : value; return type == 'enum' ? values[value] : value;
} }
function decodeValue(str) { function decodeValue(value) {
return decodeURIComponent(str); return decodeURIComponent(value);
} }
function encodeValue(str) { function encodeValue(value) {
// var chars = '/&|()=*:'; // var chars = '/&|()=*:';
var chars = '&|()=*?#', var chars = '&|()=*',
ret = ''; ret = '';
str.toString().split('').forEach(function(char) { value.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()
@ -554,7 +555,7 @@ Ox.URL = function(options) {
operators = ['!==', '==', '!=', '=', '!<', '<', '!>', '>'], operators = ['!==', '==', '!=', '=', '!<', '<', '!>', '>'],
split; split;
Ox.forEach(operators, function(operator) { Ox.forEach(operators, function(operator) {
if (str.indexOf(operator) > - 1) { if (str.indexOf(operator) > -1) {
split = str.split(operator); split = str.split(operator);
condition = { condition = {
key: split.shift(), key: split.shift(),
@ -592,6 +593,7 @@ Ox.URL = function(options) {
} else { } else {
condition.value = parseValue(decodeValue(condition.value), condition.key); condition.value = parseValue(decodeValue(condition.value), condition.key);
} }
Ox.Log('Core', 'PARSE COND', str, condition);
return condition; return condition;
} }
@ -645,24 +647,21 @@ Ox.URL = function(options) {
} }
function parseHash(str) { function parseHash(str) {
var split = str.split('?'); var hash = {},
return Ox.extend({ split = str.split('?');
anchor: decodeValue(split[0]) if (split[0]) {
}, split[1] ? { hash.anchor = decodeValue(split[0]);
query: split[1].split('&').filter(function(kv) { }
return kv.indexOf('=') > -1; if (split[1]) {
}).map(function(kv) { Ox.forEach(Ox.unserialize(split[1], true), function(value, key) {
var split = kv.split('='); hash.query = (hash.query || [])
return { hash.query.push({
key: decodeValue(split[0]), key: key,
value: decodeValue(split[1]) value: value
}; });
}) });
} : {}); }
} return hash;
function parseJSON(str) {
return Ox.unserialize(str, true);
} }
function parseLocation(str) { function parseLocation(str) {
@ -699,14 +698,9 @@ Ox.URL = function(options) {
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 '/'
var hashSplit = str.split('#'), var split = str.split('#'),
jsonSplit = hashSplit[0].split('?'), parts = split.shift().replace(/(^\/|\/$)/g, '').split('/'),
parts = jsonSplit[0].replace(/(^\/|\/$)/g, '').split('/'), state = split.length ? {hash: parseHash(split.join('#'))} : {};
state = Ox.extend(
{},
hashSplit.length == 2 ? {hash: parseHash(hashSplit[1])} : {},
jsonSplit.length == 2 ? {json: parseJSON(jsonSplit[1])} : {}
);
if (parts[0] == '') { if (parts[0] == '') {
// empty URL // empty URL
callback(state); callback(state);