oxjs/source/Ox.UI/js/Core/Ox.App.js

130 lines
3.9 KiB
JavaScript
Raw Normal View History

2011-04-23 16:45:50 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=js
2011-04-22 22:03:10 +00:00
/*
============================================================================
Application
============================================================================
*/
2011-04-23 17:28:21 +00:00
// fixme: get rid of launch, fire load event
2011-04-22 22:03:10 +00:00
Ox.App = (function() {
/***
Ox.App
Basic application instance that communicates with a JSON API.
2011-04-23 18:02:41 +00:00
The JSON API must support at least the following actions:
2011-04-22 22:03:10 +00:00
api returns all api methods
2011-04-23 18:02:41 +00:00
init returns data (site, user, ...)
2011-04-22 22:03:10 +00:00
Options
timeout API timeout in msec
type 'GET' or 'POST'
url URL of the API
Methods
api[action] make a request
api.cancel cancel a request
options get or set options
2011-04-23 17:28:21 +00:00
Events
load app loaded
2011-04-22 22:03:10 +00:00
***/
return function(options) {
options = options || {};
2011-04-23 17:28:21 +00:00
var self = {
options: Ox.extend({
timeout: 60000,
type: 'POST',
url: '/api/',
}, options || {}),
time: new Date()
},
2011-04-23 18:02:41 +00:00
that = new Ox.Element({}, self);
2011-04-22 22:03:10 +00:00
2011-04-23 17:28:21 +00:00
that.api = {
api: function(callback) {
Ox.Request.send({
url: self.options.url,
data: {
action: 'api'
},
callback: callback
});
},
cancel: function(id) {
Ox.Request.cancel(id);
}
};
2011-04-22 22:03:10 +00:00
2011-04-23 17:28:21 +00:00
$.ajaxSetup({
timeout: self.options.timeout,
type: self.options.type,
url: self.options.url
});
that.api.api(function(result) {
Ox.forEach(result.data.actions, function(val, key) {
that.api[key] = function(data, callback) {
if (arguments.length == 1 && Ox.isFunction(data)) {
callback = data;
data = {};
}
return Ox.Request.send($.extend({
url: self.options.url,
data: {
action: key,
data: JSON.stringify(data)
},
callback: callback
}, !val.cache ? {age: 0}: {}));
};
2011-04-22 22:03:10 +00:00
});
2011-04-23 17:28:21 +00:00
that.api.init(getUserData(), function(result) {
//Ox.UI.hideLoadingScreen();
2011-04-23 17:28:21 +00:00
that.triggerEvent({
load: result.data
});
});
});
2011-04-22 22:03:10 +00:00
function getUserData() {
return {
document: {
referrer: document.referrer
},
history: {
length: history.length
},
navigator: {
cookieEnabled: navigator.cookieEnabled,
plugins: $.map(navigator.plugins, function(plugin, i) {
return plugin.name;
}),
userAgent: navigator.userAgent
},
screen: screen,
time: (+new Date() - self.time) / 1000,
window: {
innerHeight: window.innerHeight,
innerWidth: window.innerWidth,
outerHeight: window.outerHeight,
outerWidth: window.outerWidth,
screenLeft: window.screenLeft,
screenTop: window.screenTop
}
};
}
self.change = function(key, value) {
};
that.options = function() {
return Ox.getset(self.options, Array.prototype.slice.call(arguments), self.change, that);
};
return that;
};
}());