oxjs/index.js

525 lines
20 KiB
JavaScript
Raw Normal View History

2012-04-06 23:42:54 +00:00
Ox.load(function() {
var app = {
$ui: {},
2012-04-08 12:48:07 +00:00
animate: function(callback) {
2012-04-06 23:42:54 +00:00
var home = !app.url.get();
2012-04-08 12:48:07 +00:00
app.state.animating = true;
2012-04-06 23:42:54 +00:00
if (home) {
app.$ui.logo.attr({
src: app.getSRC('logo')
});
app.$ui.screen.show();
app.$ui.label.show();
app.$ui.menu.options({value: ''}).show();
} else {
app.$ui.menu.options({value: app.user('url').split('/')[0]});
}
2012-04-08 12:48:07 +00:00
app.$ui.panel.find('.OxButtonGroup').css({opacity: 0});
2012-04-06 23:42:54 +00:00
[
'screen', 'logo', 'label', 'menu', 'switch'
].forEach(function(element) {
app.$ui[element].stop().animate(
app.getCSS(element),
1000,
element == 'screen' ? function() {
if (!home) {
app.$ui.logo.attr({
src: app.getSRC('logo')
});
app.$ui.panel.find('.OxButtonGroup').css({opacity: 1});
app.$ui.screen.hide();
2012-04-07 14:42:57 +00:00
app.$ui.label.hide();
2012-04-06 23:42:54 +00:00
app.$ui.menu.hide().options({value: ''});
}
2012-04-08 12:48:07 +00:00
app.state.animating = false;
callback();
2012-04-06 23:42:54 +00:00
} : void 0
);
});
},
2012-04-07 14:42:57 +00:00
data: {
2012-04-08 12:48:07 +00:00
pages: [
2012-04-07 14:42:57 +00:00
{id: 'about', title: 'About'},
{id: 'readme', title: 'Readme'},
{id: 'examples', title: 'Examples'},
{id: 'doc', title: 'Documentation'},
{id: 'downloads', title: 'Downloads'},
{id: 'development', title: 'Development'}
]
},
2012-04-06 23:42:54 +00:00
getCSS: function(element) {
var css = {},
home = !app.url.get(),
center = Math.floor(window.innerWidth / 2),
middle = Math.floor(window.innerHeight / 2);
if (element == 'label') {
css = home ? {
top: middle + 16 + 'px',
left: center - 128 + 'px',
width: '240px',
2012-04-08 12:48:07 +00:00
height: '14px',
2012-04-06 23:42:54 +00:00
fontSize: '11px',
opacity: 1
} : {
top: '26px',
left: '4px',
width: '32px',
2012-04-08 12:48:07 +00:00
height: '4px',
2012-04-06 23:42:54 +00:00
fontSize: '1px',
opacity: 0
}
} else if (element == 'loading') {
css.top = middle + 40 + 'px'
} else if (element == 'logo') {
2012-04-08 12:48:07 +00:00
css = home || !app.state.loaded ? {
2012-04-06 23:42:54 +00:00
left: center - 128 + 'px',
top: middle - 128 + 'px',
width: '256px',
height: '128px'
} : {
top: '8px',
left: '8px',
width: '32px',
height: '16px'
};
} else if (element == 'menu') {
css = home ? {
top: middle + 48 + 'px',
left: Math.ceil(center - app.$ui.menu.width() / 2) + 'px'
} : {
top: '8px',
left: Math.ceil(center - app.$ui.menu.width() / 2) + 'px'
};
} else if (element == 'screen') {
css.opacity = home ? 1 : 0;
} else if (element == 'switch') {
css = home ? {
top: middle + 80 + 'px',
right: Math.floor(center - app.$ui.switch.width() / 2) + 'px'
} : {
top: '8px',
right: '8px'
};
} else if (element == 'warning') {
css = {
left: center - 128 + 'px',
top: middle + 16 + 'px',
};
2012-04-06 23:42:54 +00:00
}
return css;
},
getSRC: function(element) {
var src,
home = !app.url.get(),
theme = app.user('theme');
if (element == 'icon') {
src = 'source/Ox.UI/themes/' + theme
+ '/png/icon16.png';
} else if (element == 'loading') {
src = 'source/Ox.UI/themes/' + theme
+ '/svg/symbolLoadingAnimated.svg'
} else if (element == 'logo') {
2012-04-08 12:48:07 +00:00
src = 'source/Ox.UI/themes/' + theme + '/png/logo'
+ (home || !app.state.loaded ? 128 : 16) + '.png'
2012-04-06 23:42:54 +00:00
}
return src;
},
2012-04-08 12:48:07 +00:00
history: {},
2012-04-06 23:42:54 +00:00
init: function() {
app.loadScreen(function() {
app.loadData(function() {
Ox.load({UI: {theme: app.theme}}, app.load);
});
});
},
load: function(browserSupported) {
2012-04-06 23:42:54 +00:00
var url = app.url.get();
if (url) {
app.user({url: url})
} else if (!app.user('url')) {
app.user({url: 'about'});
}
app.$ui.panel = app.ui.panel()
2012-04-08 12:48:07 +00:00
.select(app.url.parse(app.user('url')).page)
2012-04-06 23:42:54 +00:00
.appendTo(Ox.$body);
2012-04-07 14:42:57 +00:00
// jqueryfy so that we can animate
2012-04-06 23:42:54 +00:00
['screen', 'logo', 'loading'].forEach(function(element) {
app.$ui[element] = $('.' + element);
});
app.$ui.loading.animate({opacity: 0}, 500, function() {
app.$ui.loading.remove();
});
if (!browserSupported) {
app.$ui.warning = app.ui.warning()
.css(app.getCSS('warning'))
.appendTo(Ox.$body);
app.$ui.logo
.css({cursor: 'pointer'})
.one({
click: function() {
app.$ui.warning.remove();
app.load(true);
}
});
} else if (!url) {
2012-04-06 23:42:54 +00:00
app.$ui.logo
.css({cursor: 'pointer'})
.bind({
2012-04-08 12:48:07 +00:00
click: app.toggle
2012-04-06 23:42:54 +00:00
});
['label', 'menu', 'switch'].forEach(function(element) {
app.$ui[element] = app.ui[element]()
.css({opacity: 0})
.appendTo(Ox.$body);
app.$ui[element]
.css(app.getCSS(element))
.animate({opacity: 1}, 500);
});
} else {
['label', 'menu', 'switch'].forEach(function(element) {
app.$ui[element] = app.ui[element]().appendTo(Ox.$body);
2012-04-08 12:48:07 +00:00
app.$ui[element].css(app.getCSS(element)).hide();
2012-04-06 23:42:54 +00:00
});
app.$ui.screen.animate({opacity: 0}, 500, function() {
app.$ui.screen.hide();
});
app.$ui.logo.animate({opacity: 0}, 500, function() {
app.$ui.logo
.attr({src: app.getSRC('logo')})
.css(app.getCSS('logo'))
.animate({opacity: 1}, 500, function() {
app.$ui.logo
.css({cursor: 'pointer'})
.bind({
2012-04-08 12:48:07 +00:00
click: app.toggle
2012-04-06 23:42:54 +00:00
});
});
2012-04-08 12:48:07 +00:00
app.$ui.switch.css({opacity: 0}).show().animate({
opacity: 1
}, 500);
2012-04-06 23:42:54 +00:00
});
}
Ox.$window.bind({
hashchange: app.urlchange
});
2012-04-08 12:48:07 +00:00
app.state.loaded = true;
2012-04-06 23:42:54 +00:00
},
loadData: function(callback) {
Ox.getJSON('index.json', function(data) {
2012-04-07 14:42:57 +00:00
app.data = Ox.extend(app.data, data);
2012-04-06 23:42:54 +00:00
callback();
});
},
loadScreen: function(callback) {
app.setTheme(app.user('theme') || 'classic');
app.$ui.screen = app.ui.screen();
app.$ui.loading = app.ui.loading();
app.$ui.logo = app.ui.logo()
.bindOnce({
load: function() {
Ox.$('body')
.append(app.$ui.screen)
.append(app.$ui.logo)
.append(app.$ui.loading);
callback();
}
});
Ox.$(window).bind({
resize: app.resize
});
},
re: {
code: [
new RegExp(
'<span class="OxIdentifier">Ox</span>'
+ '(<span class="OxOperator">\.</span>'
+ '<span class="OxIdentifier">UI</span>)?'
+ '<span class="OxOperator">\.</span>'
+ '<span class="OxIdentifier">([\\$\\w]+)<\/span>',
'g'
),
function (str) {
return '<a href="#doc/' + Ox.stripTags(str)
+ '" class="doclink">' + str + '</a>';
}
],
comment: [
/\b(Ox\.\w+)\b/g,
'<a href="#doc/$1" class="OxMonospace doclink">$1</a>'
2012-04-07 14:42:57 +00:00
],
version: [
'{version}',
function() {
return (
/:\/\/oxjs.org\//.test(window.location.href)
? 'The latest version is'
: 'You\'re currently running version'
) + ' <code>' + app.data.version + '.</code>'
}
2012-04-06 23:42:54 +00:00
]
},
resize: function() {
[
'logo', 'loading', 'label', 'menu', 'switch'
].forEach(function(element) {
app.$ui[element] && app.$ui[element].css(app.getCSS(element));
});
},
setTheme: function(theme) {
app.user({theme: theme});
(Ox.$('#icon') || Ox.$('<link>').attr({
id: 'icon',
rel: 'shortcut icon',
type: 'image/png'
}).appendTo(Ox.$('head'))).attr({
href: app.getSRC('icon')
});
2012-04-07 14:42:57 +00:00
app.$ui.logo && app.$ui.logo
.attr({src: app.getSRC('logo')})
.css(app.getCSS('logo'));
2012-04-06 23:42:54 +00:00
Ox.Theme
? Ox.Theme(theme)
: Ox.$('body').addClass('OxTheme' + Ox.toTitleCase(theme)
);
},
2012-04-08 12:48:07 +00:00
state: {
animating: false,
loaded: false
},
toggle: function() {
!app.state.animating && app.url.set(
app.url.get() ? '' : app.user('url')
);
},
2012-04-06 23:42:54 +00:00
ui: {
doc: function() {
return Ox.DocPanel({
files: app.data.documentation,
getModule: function(item) {
var file = item.file.replace(/^dev\//, '');
return file.split('/')[0];
},
getSection: function(item) {
var file = item.file.replace(/^dev\//, '');
return item.section || file.split('/')[2].split('.')[0];
},
path: 'dev/',
replace: [app.re.code],
});
},
examples: function() {
return Ox.ExamplePanel({
examples: app.data.examples,
keywords: /\b(Ox\.[\w]+)\b/g,
path: 'examples/',
replaceCode: [app.re.code],
replaceComment: [app.re.comment],
selected: ''
})
.bindEvent({
select: function(data) {
2012-04-08 12:48:07 +00:00
app.url.set(
2012-04-06 23:42:54 +00:00
'examples' + (data.id ? '/' + data.id : '')
)
}
});
},
label: function() {
return Ox.Label({
textAlign: 'center',
title: 'A JavaScript Library for Web Applications',
width: 256
})
.addClass('label animate')
2012-04-08 12:48:07 +00:00
.bind({
click: app.toggle
2012-04-06 23:42:54 +00:00
});
},
loading: function() {
return Ox.$('<img>')
.addClass('loading')
.attr({src: app.getSRC('loading')})
.css(app.getCSS('loading'));
},
logo: function() {
return Ox.$('<img>')
.addClass('logo animate')
.attr({src: app.getSRC('logo')})
.css(app.getCSS('logo'));
},
menu: function() {
return Ox.ButtonGroup({
2012-04-08 12:48:07 +00:00
buttons: app.data.pages,
2012-04-06 23:42:54 +00:00
min: 0,
selectable: true,
})
.addClass('menu animate')
.bindEvent({
change: function(data) {
data.value && app.url.set(data.value);
}
})
},
2012-04-07 14:42:57 +00:00
page: function(page, replace) {
2012-04-06 23:42:54 +00:00
var $element = Ox.Container();
Ox.get('readme/html/_' + page + '.html', function(html) {
$('<div>')
.addClass('OxSerif page')
2012-04-07 14:42:57 +00:00
.html(html.replace(app.re.version[0], app.re.version[1]))
2012-04-06 23:42:54 +00:00
.appendTo($element);
});
return $element;
},
panel: function() {
return Ox.TabPanel({
content: function(id) {
2012-04-08 12:48:07 +00:00
return app.$ui[id] = app.ui[id] ? app.ui[id]() : app.ui.page(id);
2012-04-06 23:42:54 +00:00
},
size: 32,
2012-04-08 12:48:07 +00:00
tabs: app.data.pages
2012-04-06 23:42:54 +00:00
})
.bindEvent({
change: function(data) {
2012-04-08 12:48:07 +00:00
app.state.loaded && app.url.set(data.selected);
2012-04-06 23:42:54 +00:00
}
});
},
readme: function() {
2012-04-07 14:42:57 +00:00
var $list = Ox.Container().css({overflowY: 'scroll'}),
$text = Ox.Container().addClass('OxSerif text'),
2012-04-08 12:48:07 +00:00
self = {},
that = Ox.SplitPanel({
2012-04-07 14:42:57 +00:00
elements: [
{element: $list, size: 256},
{element: $text}
],
orientation: 'horizontal'
2012-04-08 12:48:07 +00:00
}, self)
2012-04-07 14:42:57 +00:00
.addClass('readme');
app.data.readme.forEach(function(item, i) {
var $item = $('<div>')
.addClass('item')
2012-04-08 12:48:07 +00:00
.attr({id: 'readme_' + item.id})
2012-04-07 14:42:57 +00:00
.css({
width: 224 - Ox.UI.SCROLLBAR_SIZE + 'px'
})
.bind({
click: function(e) {
2012-04-08 12:48:07 +00:00
if (!$(this).is('.selected')) {
selectItem(item.id);
2012-04-07 14:42:57 +00:00
} else if (e.metaKey) {
2012-04-08 12:48:07 +00:00
selectItem();
2012-04-07 14:42:57 +00:00
}
}
})
.appendTo($list);
$('<div>')
.addClass('OxSerif title')
.html(item.title)
.appendTo($item);
$('<div>')
.addClass('OxSerif OxLight date')
.html(Ox.formatDate(item.date, '%B %e, %Y', true))
.appendTo($item);
});
2012-04-08 12:48:07 +00:00
selectItem(self.options.selected);
function selectItem(id) {
2012-04-07 14:42:57 +00:00
$('.readme .item.selected').removeClass('selected');
2012-04-08 12:48:07 +00:00
id && $('#readme_' + id).addClass('selected');
Ox.get('readme/html/' + (id || '_readme') + '.html', function(html) {
$text.html(html);
$text.find('.code').each(function() {
var $this = $(this);
$this.replaceWith(
Ox.SyntaxHighlighter({
source: $this.text()
})
.attr({id: $this.attr('id')})
);
2012-04-07 14:42:57 +00:00
});
2012-04-08 12:48:07 +00:00
});
app.url.set('readme' + (id ? '/' + id : ''));
}
self.setOption = function(key, value) {
if (key == 'selected') {
selectItem(value);
2012-04-07 14:42:57 +00:00
}
}
2012-04-08 12:48:07 +00:00
return that;
2012-04-06 23:42:54 +00:00
},
screen: function() {
return Ox.$('<div>').addClass('screen animate');
},
switch: function() {
return Ox.ButtonGroup({
buttons: [
{id: 'classic', title: 'Light'},
{id: 'modern', title: 'Dark'}
],
selectable: true,
value: app.user('theme')
})
.addClass('switch animate')
.bindEvent({
change: function(data) {
app.setTheme(data.value);
}
});
},
warning: function() {
return $('<div>')
.addClass('warning')
.html(
'Aw, snap! This website requires an up-to-date, HTML5-compliant web browser. '
+ 'It should work fine in current versions of Chrome, Firefox and Safari, '
+ 'or Internet Explorer with Chrome Frame installed. '
+ 'To proceed at your own risk, click on the logo above.'
);
2012-04-06 23:42:54 +00:00
}
},
url: {
get: function() {
return window.location.hash.substr(1);
},
2012-04-08 12:48:07 +00:00
parse: function(url) {
var split = (
url === void 0 ? app.url.get() : url
).split('/');
return {page: split[0], item: split[1] || ''};
2012-04-06 23:42:54 +00:00
},
2012-04-08 12:48:07 +00:00
set: function(url) {
window.location.hash = '#' + url;
2012-04-06 23:42:54 +00:00
return app;
}
},
urlchange: function() {
var url = app.url.get(),
2012-04-08 12:48:07 +00:00
current = app.url.parse(url);
2012-04-06 23:42:54 +00:00
url && app.user({url: url});
2012-04-08 12:48:07 +00:00
Ox.print(JSON.stringify({
'current': current,
'history': app.history,
'user.url': app.user('url')
}, null, ' '));
if (current.page != app.history.page) {
app.$ui.panel.select(current.page);
}
if (Ox.in(['readme', 'examples', 'doc'], current.page)) {
Ox.print('cp',current.page, current.item)
app.$ui[current.page].options({selected: current.item});
2012-04-06 23:42:54 +00:00
}
2012-04-08 12:48:07 +00:00
(!current.page || !app.history.page ? app.animate : Ox.void)(function() {
//app.url.replace(url);
app.history = current;
Ox.print('CALLBACK, HISTORY', app.history)
2012-04-06 23:42:54 +00:00
});
},
2012-04-08 12:48:07 +00:00
user: Ox.localStorage('OxJS')
2012-04-06 23:42:54 +00:00
};
app.init();
window.oxjs = app;
});