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