Ox.URL: support findKeys per type

This commit is contained in:
j 2016-10-14 14:12:01 +02:00
parent 67302e1a3e
commit 927678e6be

View file

@ -4,9 +4,10 @@
Ox.URL <f> URL controller
(options) -> <o> URL controller
options <o> Options object
findKeys <[o]> Find keys
id <s> Find key id
type <s> Value type (like "string" or "integer")
findKeys <o> Find keys for all types
typeA <o> Find keys for this type
id <s> Find key id
type <s> Value type (like "string" or "integer")
getHash <f> Tests if a hash is valid
May modify the state's hash property
(state, callback) -> <u> undefined
@ -63,10 +64,16 @@ Ox.URL <f> URL controller
item <[s]> Item views for this type
<script>
Ox.test.url = Ox.URL({
findKeys: [
{id: 'name', type: 'string'},
{id: 'population', type: 'integer'}
],
findKeys: {
countries: [
{id: 'name', type: 'string'},
{id: 'population', type: 'integer'}
],
cities: [
{id: 'name', type: 'string'},
{id: 'population', type: 'integer'}
]
},
getHash: function(state, callback) {
if (state.hash) {
if (state.hash.anchor == 'invalid') {
@ -412,7 +419,7 @@ Ox.URL = function(options) {
self.options = Ox.extend({
// fixme: find keys are also per type/list|item/view
// since one can search for layer properties in some item views
findKeys: [],
findKeys: {},
getHash: Ox.noop,
getItem: Ox.noop,
getPart: Ox.noop,
@ -425,11 +432,13 @@ Ox.URL = function(options) {
views: {}
}, options);
if (Ox.every(self.options.findKeys, function(findKey) {
return findKey.id != '*';
})) {
self.options.findKeys.push({id: '*', type: 'string'});
}
Ox.forEach(self.options.findKeys, function(findKeys) {
if (Ox.every(findKeys, function(findKey) {
return findKey.id != '*';
})) {
findKeys.push({id: '*', type: 'string'});
}
});
self.previousTitle = '';
self.previousURL = '';
@ -441,13 +450,13 @@ Ox.URL = function(options) {
+ document.location.hash;
});
function constructCondition(condition) {
function constructCondition(condition, state) {
var key = condition.key == '*' ? '' : condition.key,
operator = condition.operator,
value = (
Ox.isArray(condition.value) ? condition.value : [condition.value]
).map(function(value) {
return encodeValue(constructValue(value, condition.key));
return encodeValue(constructValue(value, condition.key, state));
}).join(',');
if (!key) {
operator = operator.replace('=', '');
@ -469,11 +478,11 @@ Ox.URL = function(options) {
return Ox.formatDuration(duration, 3).replace(/\.000$/, '');
}
function constructFind(find) {
function constructFind(find, state) {
return find.conditions.map(function(condition) {
return condition.conditions
? '(' + constructFind(condition) + ')'
: constructCondition(condition);
? '(' + constructFind(condition, state) + ')'
: constructCondition(condition, state);
}).join(find.operator);
}
@ -560,7 +569,7 @@ Ox.URL = function(options) {
parts.push(constructSpan(state.span, state));
}
if (state.find) {
parts.push(constructFind(state.find));
parts.push(constructFind(state.find, state));
}
}
return '/' + Ox.filter(parts).join('/') + (
@ -568,8 +577,8 @@ Ox.URL = function(options) {
);
}
function constructValue(str, key) {
var findKey = Ox.getObjectById(self.options.findKeys, key),
function constructValue(str, key, state) {
var findKey = Ox.getObjectById(self.options.findKeys[state.type], key),
type = Ox.isArray(findKey.type) ? findKey.type[0] : findKey.type,
value = str,
values = findKey.values;
@ -623,7 +632,7 @@ Ox.URL = function(options) {
: '';
}
function parseCondition(str) {
function parseCondition(str, state) {
Ox.Log('Core', 'PARSE COND', str)
var condition = {},
operators = ['!==', '==', '!=', '=', '!<', '<', '!>', '>'],
@ -642,7 +651,7 @@ Ox.URL = function(options) {
});
if (
!condition.operator
|| Ox.getIndexById(self.options.findKeys, condition.key) == -1
|| Ox.getIndexById(self.options.findKeys[state.type], condition.key) == -1
) {
// missing operator or unknown key
condition = {key: '*', value: str, operator: '='};
@ -658,15 +667,15 @@ Ox.URL = function(options) {
}
if (
['date', 'enum', 'float', 'integer', 'time', 'year'].indexOf(
Ox.getObjectById(self.options.findKeys, condition.key).type
Ox.getObjectById(self.options.findKeys[state.type], condition.key).type
) > -1
&& condition.value.indexOf(',') > -1
) {
condition.value = condition.value.split(',').map(function(value) {
return parseValue(decodeValue(value), condition.key);
return parseValue(decodeValue(value), condition.key, state);
});
} else {
condition.value = parseValue(decodeValue(condition.value), condition.key);
condition.value = parseValue(decodeValue(condition.value), condition.key, state);
}
Ox.Log('Core', 'PARSE COND', str, condition);
return condition;
@ -680,7 +689,7 @@ Ox.URL = function(options) {
return Ox.parseDuration(str);
}
function parseFind(str) {
function parseFind(str, state) {
str = (str || '').replace(/%7C/g, '|');
var counter = 0,
find = {conditions: [], operator: '&'},
@ -716,8 +725,8 @@ Ox.URL = function(options) {
return subconditions[parseInt(arguments[1])];
});
return condition[0] == '('
? parseFind(condition.slice(1, -1))
: parseCondition(condition);
? parseFind(condition.slice(1, -1), state)
: parseCondition(condition, state);
});
}
return find;
@ -767,6 +776,9 @@ Ox.URL = function(options) {
function parseSpan(str, type) {
var split = str.split(',');
if (type == 'string') {
return str;
}
if (split.length == 4) {
split = [split[0] + ',' + split[1], split[2] + ',' + split[3]];
}
@ -976,7 +988,7 @@ Ox.URL = function(options) {
}
if (parts.length) {
// find
state.find = parseFind(parts.join('/'));
state.find = parseFind(parts.join('/'), state);
}
getHash();
}
@ -991,8 +1003,8 @@ Ox.URL = function(options) {
}
}
function parseValue(str, key) {
var findKey = Ox.getObjectById(self.options.findKeys, key),
function parseValue(str, key, state) {
var findKey = Ox.getObjectById(self.options.findKeys[state.type], key),
type = Ox.isArray(findKey.type) ? findKey.type[0] : findKey.type,
value = str,
values = findKey.values;