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