Ox.URL: add getPart option (page sections)

This commit is contained in:
rlx 2013-07-11 15:13:10 +00:00
parent 7824a55eb0
commit a2fb27d469

View file

@ -19,6 +19,12 @@ Ox.URL <f> URL controller
state <o> State object
string <s> The string to be tested
callback <f> callback function
getPart <f> Tests if a string matches a part (page section)
May modify the state's part property
(state, string, callback) -> <u> undefined
state <o> State object
string <s> The string to be tested
callback <f> callback function
getSpan <f> Tests if a string matches a span
May modify the state's view and span properties
(state, string, callback) -> <u> undefined
@ -298,7 +304,8 @@ example.com[/page][#hash]
or
example.com[/type][/item][/view][/span][/sort][/find][#hash]
page Special page, like "about" or "contact"
page Special page, like "about" or "contact".
part Part (section) of a page, like in "help/accounts".
type Section a.k.a. item type, like "movies", "edits", "texts" etc.
item Item id or title, like in '/movies/0060304', '/movies/inception' or
'texts/ABC'. Testing this is asynchonous.
@ -402,6 +409,7 @@ Ox.URL = function(options) {
findKeys: [],
getHash: null,
getItem: null,
getPart: null,
getSpan: null,
pages: [],
spanType: {},
@ -516,6 +524,9 @@ Ox.URL = function(options) {
var parts = [];
if (state.page) {
parts.push(state.page);
if (state.part) {
parts.push(state.part);
}
} else {
if (self.options.types.indexOf(state.type) > 0) {
parts.push(state.type);
@ -581,6 +592,7 @@ Ox.URL = function(options) {
canBeLocation = types.indexOf('location') > -1,
canBeNumber = types.indexOf('number') > -1,
length = str.split(',').length;
// !/^\d{7}$/.test(str) avoids matching imdb ids
return canBeDate && /\d-/.test(str) ? 'date'
: canBeDuration && /:/.test(str) ? 'duration'
: canBeLocation && length == 4 ? 'location'
@ -590,7 +602,6 @@ Ox.URL = function(options) {
: canBeLocation && length == 2 ? 'location'
: canBeNumber && /^\d+$/.test(str) ? 'number'
: '';
// !/^\d{7}$/.test(str) avoids matching imdb ids
}
function parseCondition(str) {
@ -759,7 +770,13 @@ Ox.URL = function(options) {
} else if (self.options.pages.indexOf(parts[0]) > -1) {
// page
state.page = parts[0];
getHash();
parts.shift();
if (parts.length) {
// may modify state.part
self.options.getPart(state, parts[0], getHash);
} else {
getHash();
}
} else {
if (self.options.types.indexOf(parts[0]) > -1) {
// type
@ -906,14 +923,10 @@ Ox.URL = function(options) {
getHash();
}
function getHash() {
if (self.options.getHash) {
self.options.getHash(state, function() {
// may have modified state.hash
callback(state);
});
} else {
self.options.getHash(state, function() {
// may have modified state.hash
callback(state);
}
});
}
}