1
0
Fork 0
forked from 0x2620/oxjs

update Ox.URL, add Ox.Event

This commit is contained in:
rlx 2011-09-23 10:43:57 +00:00
commit f2c8644001
6 changed files with 166 additions and 33 deletions

View file

@ -157,6 +157,8 @@ Ox.URL = function(options) {
Ox.print('Ox.URL options', self.options)
self.previousURL = '';
function constructCondition(condition) {
var key = condition.key == '*' ? '' : condition.key,
operator = condition.operator,
@ -200,13 +202,15 @@ Ox.URL = function(options) {
}
function constructSort(sort, state) {
var sortKeys = self.options.sortKeys[state.type][
!state.item ? 'list' : 'item'
][state.view];
return sort.map(function(sort) {
return (
sort.operator == Ox.getObjectById(self.options.sortKeys[state.type][
!state.item ? 'list' : 'item'
][state.view], sort.key).operator ? '' : sort.operator
Ox.getObjectById(sortKeys, sort.key).operator == sort.operator
? '' : sort.operator
) + sort.key;
}).join(',')
}).join(',');
}
function constructSpan(span, state) {
@ -233,7 +237,7 @@ Ox.URL = function(options) {
if (state.item) {
parts.push(state.item);
}
if (self.options.views[state.type][
if (state.type && self.options.views[state.type][
state.item ? 'item' : 'list'
].indexOf(state.view) > -1) {
parts.push(state.view);
@ -256,7 +260,8 @@ Ox.URL = function(options) {
}
function encodeValue(str) {
var chars = '/&|()=*:';
// var chars = '/&|()=*:';
var chars = '&|()=*';
ret = '';
str.split('').forEach(function(char) {
var index = chars.indexOf(char);
@ -320,7 +325,7 @@ Ox.URL = function(options) {
}
}
if (condition.value.indexOf(':') > -1) {
condition.value = condition.value.split(':').map(decodeValue);
condition.value = condition.value.split(':').map(decodeValue).join(':');
} else {
condition.value = decodeValue(condition.value);
}
@ -410,12 +415,14 @@ Ox.URL = function(options) {
}
function parseURL(str, callback) {
Ox.print('pU', str)
var parts = str.substr(1).split('/'),
state = {};
if (parts.length == 0) {
state.page = '';
if (parts[0] == '') {
// empty URL
callback(state);
} else if (self.options.pages.indexOf(parts[0]) > -1) {
// page
state.page = parts[0];
callback(state);
} else {
@ -428,6 +435,7 @@ Ox.URL = function(options) {
state.type = self.options.types[0];
}
if (parts.length) {
Ox.print('ST', state.type, self.options.views)
if (self.options.views[state.type].list.indexOf(parts[0]) > -1) {
// list view
state.item = '';
@ -561,6 +569,12 @@ Ox.URL = function(options) {
}
}
function saveURL() {
self.previousURL = document.location.pathname
+ document.location.search
+ document.location.hash;
}
that._construct = function(state) {
return constructURL(state);
};
@ -571,25 +585,50 @@ Ox.URL = function(options) {
+ document.location.search
+ document.location.hash,
callback = arguments[arguments.length - 1];
Ox.print(str, callback)
parseURL(str, callback);
parseURL(str, function(state) {
that.replace(constructURL(state));
callback(state);
});
return that;
}
/*@
pop <f> Sets the URL to the previous URL
@*/
that.pop = function() {
history.pushState(self.previousURL);
return that;
};
that.push = function(url) {
/*@
push <f> Pushes a new URL
(url, callback) -> <o> URL controller
(state) -> <o> URL controller
url <s> New URL
callback <f> callback function
state <o> New state
state <o> State to construct the new URL from
@*/
that.push = function(url, callback) {
url = callback ? url : constructURL(url);
saveURL();
Ox.print('PUSH', url);
history.pushState({}, '', url);
callback && parseURL(url, callback);
return that;
};
that.replace = function(url) {
saveURL();
history.replaceState({}, '', url);
return that;
};
that.update = function(state) {
// pushes a new URL, constructed from state
// state can have type, item, view, span, sort, find
that.push(constructURL(state));
return that;
};
return that;