updating Ox.App

This commit is contained in:
rolux 2011-04-23 19:28:21 +02:00
parent d8d14cffe7
commit 6c0d7cd3ca
2 changed files with 69 additions and 115 deletions

View file

@ -5,7 +5,7 @@ Application
============================================================================ ============================================================================
*/ */
// fixme: get rid og launch, fire load event // fixme: get rid of launch, fire load event
Ox.App = (function() { Ox.App = (function() {
@ -22,42 +22,75 @@ Ox.App = (function() {
Methods Methods
api[action] make a request api[action] make a request
api.cancel cancel a request api.cancel cancel a request
launch launch the App
options get or set options options get or set options
Events
load app loaded
***/ ***/
return function(options) { return function(options) {
options = options || {}; options = options || {};
var self = {}, var self = {
that = this; options: Ox.extend({
timeout: 60000,
type: 'POST',
url: '/api/',
}, options || {}),
time: new Date()
},
that = Ox.Element();
self.time = +new Date(); that.api = {
api: function(callback) {
Ox.Request.send({
url: self.options.url,
data: {
action: 'api'
},
callback: callback
});
},
cancel: function(id) {
Ox.Request.cancel(id);
}
};
self.options = $.extend({ $.ajaxSetup({
timeout: 60000, timeout: self.options.timeout,
type: 'POST', type: self.options.type,
url: '/api/', url: self.options.url
}, options); });
that.$element = new Ox.Element('body'); that.api.api(function(result) {
Ox.forEach(result.data.actions, function(val, key) {
function getUserAgent() { that.api[key] = function(data, callback) {
var userAgent = ''; if (arguments.length == 1 && Ox.isFunction(data)) {
Ox.forEach(['Chrome', 'Firefox', 'Internet Explorer', 'Opera', 'Safari'], function(v) { callback = data;
if (navigator.userAgent.indexOf(v) > -1) { data = {};
userAgent = v; }
return false; return Ox.Request.send($.extend({
} url: self.options.url,
data: {
action: key,
data: JSON.stringify(data)
},
callback: callback
}, !val.cache ? {age: 0}: {}));
};
}); });
if (!userAgent && $.browser.mozilla) { that.api.init(getUserData(), function(result) {
userAgent = 'Firefox'; var $div = Ox.UI.$body.find('div');
} Ox.UI.$body.find('img').remove();
if (!userAgent && $.browser.webkit) { $div.animate({
userAgent = 'Chrome'; opacity: 0
} }, 1000, function() {
return userAgent; $div.remove();
} });
that.triggerEvent({
load: result.data
});
});
});
function getUserData() { function getUserData() {
return { return {
@ -91,81 +124,10 @@ Ox.App = (function() {
}; };
that.api = {
api: function(callback) {
Ox.Request.send({
url: self.options.url,
data: {
action: 'api'
},
callback: callback
});
},
cancel: function(id) {
Ox.Request.cancel(id);
}
};
that.bindEvent = function() { that.bindEvent = function() {
}; };
// fixme: use $.when()
that.launch = function(callback) {
var time = +new Date(),
userAgent = getUserAgent(),
userAgents = ['Chrome', 'Firefox', 'Opera', 'Safari'];
$.ajaxSetup({
timeout: self.options.timeour,
type: self.options.type,
url: self.options.url
});
userAgents.indexOf(userAgent) > -1 ? start() : stop();
function start() {
// fixme: rename config to site?
var counter = 0, config, user;
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}: {}));
};
});
that.api.init(getUserData(), function(result) {
config = result.data.config;
user = result.data.user;
// fixme: not generic
document.title = config.site.name;
$(function() {
var $div = Ox.UI.$body.find('div');
Ox.UI.$body.find('img').remove();
$div.animate({
opacity: 0
}, 1000, function() {
$div.remove();
});
// fixme: not generic enough, just pass data
callback({config: config, user: user});
});
});
});
}
function stop() {
that.request.send(self.options.init, getUserData(), function() {});
}
return that;
};
that.options = function() { that.options = function() {
return Ox.getset(self.options, Array.prototype.slice.call(arguments), self.change, that); return Ox.getset(self.options, Array.prototype.slice.call(arguments), self.change, that);
}; };

View file

@ -32,9 +32,9 @@ Provides function Ox.UI([options], callback) that fires when
} }
}, },
oxUIOptions = { oxUIOptions = {
// 'icon', 'console' or 'none' app: false,
display: 'none', display: 'none', // 'icon', 'console' or 'none'
theme: 'classic' theme: 'classic' // 'classic' or 'modern
}; };
files.forEach(function(file, i) { files.forEach(function(file, i) {
@ -161,7 +161,9 @@ Provides function Ox.UI([options], callback) that fires when
'Consolas', 'Lucida Console' 'Consolas', 'Lucida Console'
].join(', '), ].join(', '),
fontSize: '12px', fontSize: '12px',
color: 'rgb(240, 240, 240)' color: 'rgb(' + (
oxUIOptions.theme == 'classic' ? '16, 16, 16' : '240, 240 240'
) + ')'
}) })
.html(str) .html(str)
.appendTo(element); .appendTo(element);
@ -376,7 +378,7 @@ Provides function Ox.UI([options], callback) that fires when
$.when.apply(null, promises) $.when.apply(null, promises)
.done(function() { .done(function() {
var $div, error = $('.error').length; var $div, error = $('.error').length;
if (!error) { if (!error && !oxUIOptions.app) {
$div = $('div'); $div = $('div');
$('img').remove(); $('img').remove();
$div.animate({ $div.animate({
@ -441,7 +443,7 @@ Provides function Ox.UI([options], callback) that fires when
}; };
Ox.UI.elements = {}; Ox.UI.elements = {};
Ox.UI.DEFAULT_THEME = 'classic'; Ox.UI.DEFAULT_THEME = 'classic'; // fixme: needed?
Ox.UI.DIMENSIONS = { Ox.UI.DIMENSIONS = {
horizontal: ['width', 'height'], horizontal: ['width', 'height'],
vertical: ['height', 'width'] vertical: ['height', 'width']
@ -456,23 +458,13 @@ Provides function Ox.UI([options], callback) that fires when
filename = 'symbolRight.svg'; filename = 'symbolRight.svg';
} }
return Ox.UI.PATH + filename.split('.').pop() + return Ox.UI.PATH + filename.split('.').pop() +
'/ox.ui.' + Ox.UI.theme() + '/' + filename; '/ox.ui.' + Ox.Theme() + '/' + filename;
}; };
Ox.UI.IMAGE_CACHE = []; Ox.UI.IMAGE_CACHE = [];
Ox.UI.PATH = $('script[src*="OxUI.js"]') Ox.UI.PATH = $('script[src*="OxUI.js"]')
.attr('src').replace('js/OxUI.js', ''); .attr('src').replace('js/OxUI.js', '');
Ox.UI.SCROLLBAR_SIZE = $.browser.mozilla ? 16 : 12; Ox.UI.SCROLLBAR_SIZE = $.browser.mozilla ? 16 : 12;
// fixme: the follwing should be deprecated // fixme: the follwing should be deprecated
Ox.UI.theme = function() {
var theme;
Ox.forEach(Ox.UI.$body.attr('class').split(' '), function(v) {
if (Ox.startsWith(v, 'OxTheme')) {
theme = v.replace('OxTheme', '').toLowerCase();
return false;
}
});
return theme || Ox.UI.DEFAULT_THEME; // fixme: shouldn't be neccessary
};
Ox.UI.getBarSize = function(size) { Ox.UI.getBarSize = function(size) {
var sizes = { var sizes = {
small: 20, small: 20,