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

108 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2011-05-16 08:24:46 +00:00
/*@
2012-05-31 10:32:54 +00:00
Ox.App <f> Basic application instance that communicates with a JSON API
2011-05-16 08:24:46 +00:00
options <o> Options object
name <s> App name
timeout <n> Request timeout
type <s> HTTP Request type, i.e. 'GET' or 'POST'
url <s> JSON API URL
([options]) -> <o> App object
load <!> App loaded
2011-05-16 08:24:46 +00:00
@*/
2011-04-22 22:03:10 +00:00
Ox.App = function(options) {
2011-04-22 22:03:10 +00:00
2011-09-17 11:49:29 +00:00
var self = {
options: Ox.extend({
2011-12-30 15:06:55 +00:00
name: 'App',
2014-05-10 13:08:08 +00:00
socket: '',
2011-09-17 11:49:29 +00:00
timeout: 60000,
type: 'POST',
url: '/api/'
2011-09-17 11:49:29 +00:00
}, options || {}),
time: new Date()
},
that = Ox.Element({}, Ox.extend({}, self));
2011-04-22 22:03:10 +00:00
//@ api <o> API endpoint
2011-12-21 15:40:06 +00:00
that.api = Ox.API({
2011-09-17 11:49:29 +00:00
type: self.options.type,
timeout: self.options.timeout,
2011-09-17 11:49:29 +00:00
url: self.options.url
}, function() {
2011-09-17 11:49:29 +00:00
that.api.init(getUserData(), function(result) {
that.triggerEvent({load: result.data});
2011-04-23 17:28:21 +00:00
});
2011-09-17 11:49:29 +00:00
});
2011-04-22 22:03:10 +00:00
2014-05-10 13:08:08 +00:00
self.options.socket && connectSocket();
2012-06-17 22:38:26 +00:00
//@ localStorage <f> Ox.localStorage instance
2011-12-30 15:06:55 +00:00
that.localStorage = Ox.localStorage(self.options.name);
2014-05-10 13:08:08 +00:00
function connectSocket() {
that.socket = new WebSocket(self.options.socket);
that.socket.onopen = function(event) {
that.triggerEvent('open', event);
};
that.socket.onmessage = function(event) {
var data = JSON.parse(event.data);
that.triggerEvent(data[0], data[1]);
};
that.socket.onerror = function(event) {
that.triggerEvent('error', event);
2014-05-11 14:39:20 +00:00
that.socket.close();
2014-05-10 13:08:08 +00:00
};
that.socket.onclose = function(event) {
that.triggerEvent('close', event);
setTimeout(connectSocket, 1000);
};
}
2011-09-17 11:49:29 +00:00
function getUserData() {
return {
document: {referrer: document.referrer},
history: {length: history.length},
location: {href: location.href},
2011-09-17 11:49:29 +00:00
navigator: {
cookieEnabled: navigator.cookieEnabled,
2013-12-01 13:57:52 +00:00
plugins: Ox.slice(navigator.plugins).map(function(plugin) {
2011-09-17 11:49:29 +00:00
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
}
2011-04-22 22:03:10 +00:00
};
2011-09-17 11:49:29 +00:00
}
2011-04-22 22:03:10 +00:00
function update() {
// ...
}
2011-09-17 11:49:29 +00:00
/*@
options <f> Gets or sets options (see Ox.getset)
() -> <o> All options
(key) -> <*> The value of option[key]
(key, value) -> <o> Sets one option, returns App object
({key: value, ...}) -> <o> Sets multiple options, returns App object
key <s> The name of the option
value <*> The value of the option
2011-09-17 11:49:29 +00:00
@*/
that.options = function() {
return Ox.getset(self.options, arguments, update, that);
2011-09-17 11:49:29 +00:00
};
2011-04-22 22:03:10 +00:00
2011-09-17 11:49:29 +00:00
return that;
2011-04-22 22:03:10 +00:00
};