update URL controller
This commit is contained in:
parent
b520654e31
commit
10328fc14b
1 changed files with 52 additions and 58 deletions
|
@ -207,16 +207,6 @@ Ox.URL <f> URL controller
|
|||
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': {
|
||||
hash: {
|
||||
anchor: 'a',
|
||||
|
@ -225,6 +215,18 @@ Ox.URL <f> URL controller
|
|||
{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>
|
||||
|
@ -256,17 +258,17 @@ Ox.URL <f> URL controller
|
|||
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)']); })
|
||||
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']); })
|
||||
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]
|
||||
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"
|
||||
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: ''}
|
||||
(comparison operator) or {conditions: [], operator: ''} (logical
|
||||
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
|
||||
evaluated as JSON, and if that fails interpreted as a string. So strings
|
||||
(outside arrays or objects) work without quotes as well, unless their
|
||||
value is valid JSON, like 'true' or '1'.
|
||||
hash Anchor and/or query, like 'a' or '?k=v' or 'a?k=v&l=w'.
|
||||
hash Anchor and/or query, like 'a' or '?k=v' or 'a?k=v&l=w' or
|
||||
'?a=[1,2]&b=true&f=1.2&i=3&o={"k":"v"}&s="foo"'. Values are evaluated as
|
||||
JSON, and if that fails interpreted as a string. So strings (outside
|
||||
arrays or objects) work without quotes as well, unless their value is
|
||||
valid JSON, like 'true' or '1'.
|
||||
|
||||
String Key Value Operator
|
||||
v * v = any text or string contains or any number is
|
||||
|
@ -433,14 +435,13 @@ Ox.URL = function(options) {
|
|||
}
|
||||
|
||||
function constructHash(hash) {
|
||||
return (hash.anchor || '')
|
||||
+ hash.query ? '?' + hash.query.map(function(query) {
|
||||
return encodeValue(query.key) + '=' + encodeValue(query.value);
|
||||
}) : '';
|
||||
}
|
||||
|
||||
function constructJSON(json) {
|
||||
return Ox.serialize(json, true);
|
||||
var obj = {};
|
||||
if (hash.query) {
|
||||
hash.query.forEach(function(condition) {
|
||||
obj[condition.key] = condition.value;
|
||||
});
|
||||
}
|
||||
return (hash.anchor || '') + hash.query ? '?' + Ox.serialize(obj) : '';
|
||||
}
|
||||
|
||||
function constructLocation(location) {
|
||||
|
@ -510,15 +511,15 @@ Ox.URL = function(options) {
|
|||
return type == 'enum' ? values[value] : value;
|
||||
}
|
||||
|
||||
function decodeValue(str) {
|
||||
return decodeURIComponent(str);
|
||||
function decodeValue(value) {
|
||||
return decodeURIComponent(value);
|
||||
}
|
||||
|
||||
function encodeValue(str) {
|
||||
function encodeValue(value) {
|
||||
// var chars = '/&|()=*:';
|
||||
var chars = '&|()=*?#',
|
||||
var chars = '&|()=*',
|
||||
ret = '';
|
||||
str.toString().split('').forEach(function(char) {
|
||||
value.toString().split('').forEach(function(char) {
|
||||
var index = chars.indexOf(char);
|
||||
ret += index > -1
|
||||
? '%' + char.charCodeAt(0).toString(16).toUpperCase()
|
||||
|
@ -554,7 +555,7 @@ Ox.URL = function(options) {
|
|||
operators = ['!==', '==', '!=', '=', '!<', '<', '!>', '>'],
|
||||
split;
|
||||
Ox.forEach(operators, function(operator) {
|
||||
if (str.indexOf(operator) > - 1) {
|
||||
if (str.indexOf(operator) > -1) {
|
||||
split = str.split(operator);
|
||||
condition = {
|
||||
key: split.shift(),
|
||||
|
@ -592,6 +593,7 @@ Ox.URL = function(options) {
|
|||
} else {
|
||||
condition.value = parseValue(decodeValue(condition.value), condition.key);
|
||||
}
|
||||
Ox.Log('Core', 'PARSE COND', str, condition);
|
||||
return condition;
|
||||
}
|
||||
|
||||
|
@ -645,24 +647,21 @@ Ox.URL = function(options) {
|
|||
}
|
||||
|
||||
function parseHash(str) {
|
||||
var split = str.split('?');
|
||||
return Ox.extend({
|
||||
anchor: decodeValue(split[0])
|
||||
}, split[1] ? {
|
||||
query: split[1].split('&').filter(function(kv) {
|
||||
return kv.indexOf('=') > -1;
|
||||
}).map(function(kv) {
|
||||
var split = kv.split('=');
|
||||
return {
|
||||
key: decodeValue(split[0]),
|
||||
value: decodeValue(split[1])
|
||||
};
|
||||
})
|
||||
} : {});
|
||||
}
|
||||
|
||||
function parseJSON(str) {
|
||||
return Ox.unserialize(str, true);
|
||||
var hash = {},
|
||||
split = str.split('?');
|
||||
if (split[0]) {
|
||||
hash.anchor = decodeValue(split[0]);
|
||||
}
|
||||
if (split[1]) {
|
||||
Ox.forEach(Ox.unserialize(split[1], true), function(value, key) {
|
||||
hash.query = (hash.query || [])
|
||||
hash.query.push({
|
||||
key: key,
|
||||
value: value
|
||||
});
|
||||
});
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
function parseLocation(str) {
|
||||
|
@ -699,14 +698,9 @@ Ox.URL = function(options) {
|
|||
|
||||
function parseURL(str, callback) {
|
||||
// fixme: removing trailing slash makes it impossible to search for '/'
|
||||
var hashSplit = str.split('#'),
|
||||
jsonSplit = hashSplit[0].split('?'),
|
||||
parts = jsonSplit[0].replace(/(^\/|\/$)/g, '').split('/'),
|
||||
state = Ox.extend(
|
||||
{},
|
||||
hashSplit.length == 2 ? {hash: parseHash(hashSplit[1])} : {},
|
||||
jsonSplit.length == 2 ? {json: parseJSON(jsonSplit[1])} : {}
|
||||
);
|
||||
var split = str.split('#'),
|
||||
parts = split.shift().replace(/(^\/|\/$)/g, '').split('/'),
|
||||
state = split.length ? {hash: parseHash(split.join('#'))} : {};
|
||||
if (parts[0] == '') {
|
||||
// empty URL
|
||||
callback(state);
|
||||
|
|
Loading…
Reference in a new issue