oxjs/source/Ox.UI/Ox.UI.js

449 lines
15 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2011-04-25 09:12:02 +00:00
Ox.load.UI = function(options, callback) {
options = Ox.extend({
hideScreen: true,
2014-09-23 21:15:35 +00:00
loadCSS: true,
2011-04-25 09:12:02 +00:00
showScreen: false,
2013-02-10 11:45:37 +00:00
theme: 'oxlight'
2011-04-25 09:12:02 +00:00
}, options);
var browsers = [
2011-09-03 02:12:44 +00:00
{
name: 'Chrome Frame',
2012-05-26 15:48:19 +00:00
url: 'http://www.google.com/chromeframe/'
2011-09-03 02:12:44 +00:00
},
2011-04-25 09:12:02 +00:00
{
name: 'Chrome',
regexp: /Chrome\/(\d+)\./,
2011-04-25 09:12:02 +00:00
url: 'http://www.google.com/chrome/',
version: 10
},
{
name: 'Firefox',
regexp: /Firefox\/(\d+)\./,
url: 'http://www.mozilla.org/firefox/',
version: 4
},
{
name: 'Safari',
regexp: /Version\/(\d+).*? Safari/,
url: 'http://www.apple.com/safari/',
version: 5
2011-09-03 02:12:44 +00:00
},
{
name: 'WebKit',
regexp: /AppleWebKit\/(\d+)\./,
version: 534
},
{
name: 'Googlebot',
regexp: /Googlebot\/(\d+)\./,
version: 2
},
2011-09-03 02:12:44 +00:00
{
name: 'Internet Explorer',
url: 'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
version: 9
2011-04-25 09:12:02 +00:00
}
],
2011-04-25 13:38:47 +00:00
browserSupported = false,
2012-12-28 17:21:26 +00:00
colors = {
marker: {
'#000000': 'videoMarkerBorder',
'#FFFFFF': 'videoMarkerBackground'
},
symbol: {
'#FF0000': 'symbolWarningColor'
}
},
images = {},
2011-09-03 02:12:44 +00:00
isInternetExplorer = /MSIE/.test(navigator.userAgent),
2012-12-28 17:21:26 +00:00
loadingInterval,
themes = {};
2011-04-25 09:12:02 +00:00
browsers.forEach(function(browser) {
2011-09-03 02:12:44 +00:00
var match = browser.regexp && browser.regexp.exec(navigator.userAgent);
if (match && match[1] >= browser.version) {
2011-04-25 09:12:02 +00:00
browserSupported = true;
}
});
Ox.documentReady(function() {
2012-04-15 12:17:19 +00:00
Ox.$('body').addClass(
2013-02-10 11:45:37 +00:00
'OxTheme' + Ox.toTitleCase(options.theme || 'oxlight')
2012-04-15 12:17:19 +00:00
);
2011-04-25 09:12:02 +00:00
options.showScreen && showScreen();
});
loadFiles();
function showScreen() {
2012-04-15 12:17:19 +00:00
var body = Ox.$('body'),
2011-04-25 09:12:02 +00:00
css = {
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0,
margin: 'auto',
MozUserSelect: 'none',
WebkitUserSelect: 'none'
},
2012-04-15 12:17:19 +00:00
div = Ox.$('<div>')
2011-04-25 09:12:02 +00:00
.addClass('OxLoadingScreen')
.css({
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0,
padding: '4px',
background: 'rgb(' + (
2013-02-10 11:45:37 +00:00
options.theme == 'oxlight' ? '240, 240, 240'
: options.theme == 'oxmedium' ? '144, 144, 144'
: '16, 16, 16'
) + ')',
2011-04-25 09:12:02 +00:00
opacity: 1,
zIndex: 1000
})
.appendTo(body);
2012-12-29 16:54:58 +00:00
browserSupported ? showIcon() : showWarning();
2011-04-25 09:12:02 +00:00
function showIcon() {
2011-04-25 13:26:17 +00:00
/*
// SVG transform performs worse than CSS transform
var src = Ox.PATH + 'Ox.UI/themes/' + options.theme + '/svg/symbolLoadingAnimated.svg'
2012-06-17 13:37:21 +00:00
Ox.getFile(src, function() {
2012-04-15 12:17:19 +00:00
Ox.$('<img>')
2011-04-25 09:12:02 +00:00
.attr({
src: src
})
.css(Ox.extend({
width: '32px',
height: '32px'
2011-04-25 09:12:02 +00:00
}, css))
2012-05-28 14:06:22 +00:00
.on({
2012-04-15 12:17:19 +00:00
mousedown: function(e) {
e.preventDefault();
}
2011-04-25 09:12:02 +00:00
})
.appendTo(div);
});
2011-04-25 13:26:17 +00:00
*/
2011-04-25 13:38:47 +00:00
var deg = 0,
element,
src = Ox.PATH + 'Ox.UI/themes/' + options.theme + '/svg/symbolLoading.svg'
2012-06-17 13:37:21 +00:00
Ox.getFile(src, function() {
2012-04-15 12:17:19 +00:00
element = Ox.$('<img>')
.attr({
src: src
})
2011-04-25 13:26:17 +00:00
.css(Ox.extend({
width: '32px',
height: '32px'
2011-04-25 13:26:17 +00:00
}, css))
2012-05-28 14:06:22 +00:00
.on({
2012-04-15 12:17:19 +00:00
mousedown: function(e) {
e.preventDefault()
}
})
.appendTo(div);
2011-04-25 13:38:47 +00:00
loadingInterval = setInterval(function() {
deg = (deg + 30) % 360;
element.css({
MozTransform: 'rotate(' + deg + 'deg)',
2012-05-24 17:13:25 +00:00
OTransform: 'rotate(' + deg + 'deg)',
WebkitTransform: 'rotate(' + deg + 'deg)',
transform: 'rotate(' + deg + 'deg)'
});
}, 83);
});
2011-04-25 09:12:02 +00:00
}
function showWarning() {
2011-09-03 02:12:44 +00:00
var counter = 0;
/*
2011-04-25 09:12:02 +00:00
message = 'Browser not supported, use ' + browsers.map(function(browser, i) {
return browser.name + (
i == browsers.length - 1 ? '.' :
i == browsers.length - 2 ? ' or' : ','
);
}).join(' ');
2011-09-03 02:12:44 +00:00
*/
2011-04-25 09:12:02 +00:00
div.addClass('OxError');
browsers = browsers.filter(function(browser) {
return browser.url;
});
2012-12-29 16:54:58 +00:00
isInternetExplorer ? browsers.pop() : browsers.shift();
2011-04-25 09:12:02 +00:00
browsers.forEach(function(browser) {
2011-09-03 02:12:44 +00:00
browser.src = Ox.PATH + 'Ox.UI/png/browser' + browser.name.replace(' ', '') + '128.png';
2012-06-17 13:37:21 +00:00
Ox.getFile(browser.src, function() {
2011-04-25 09:12:02 +00:00
++counter == browsers.length && showIcons();
});
});
function showIcons() {
2012-04-15 12:17:19 +00:00
var box = Ox.$('<div>')
2011-04-25 09:12:02 +00:00
.css(Ox.extend({
width: (browsers.length * 72) + 'px',
height: '72px'
}, css))
.appendTo(div);
browsers.forEach(function(browser, i) {
2012-04-15 12:17:19 +00:00
var link = Ox.$('<a>')
2011-04-25 09:12:02 +00:00
.attr({
href: browser.url,
2011-10-08 17:19:18 +00:00
title: (browser.name == 'Chrome Frame' ? 'Install' : 'Download')
+ ' ' + browser.name
2011-04-25 09:12:02 +00:00
})
.css({
position: 'absolute',
left: (i * 72) + 'px',
width: '72px',
2012-05-26 15:48:19 +00:00
height: '72px'
2011-04-25 09:12:02 +00:00
})
.appendTo(box);
2012-04-15 12:17:19 +00:00
Ox.$('<img>')
2011-04-25 09:12:02 +00:00
.attr({
src: browser.src
})
.css(Ox.extend({
width: '64px',
height: '64px',
border: 0,
cursor: 'pointer'
}, css))
2012-05-28 14:06:22 +00:00
.on({
2012-04-15 12:17:19 +00:00
mousedown: function(e) {
e.preventDefault();
}
2011-04-25 09:12:02 +00:00
})
.appendTo(link);
});
}
}
}
function loadFiles() {
Ox.getFile(Ox.PATH + 'Ox.UI/jquery/jquery.js?' + Ox.VERSION, function() {
2011-04-25 09:12:02 +00:00
initUI();
2012-12-28 17:21:26 +00:00
Ox.getJSON(Ox.UI.PATH + 'json/Ox.UI.json?' + Ox.VERSION, function(data) {
2014-09-23 21:15:35 +00:00
var counter = 0, length;
if (!options.loadCSS) {
data.files = data.files.filter(function(file) {
return !Ox.endsWith(file, '.css');
});
}
length = data.files.length;
2012-12-28 17:21:26 +00:00
images = data.images;
data.files.forEach(function(file) {
if (/\.jsonc$/.test(file)) {
Ox.getJSONC(Ox.PATH + file + '?' + Ox.VERSION, function(data) {
var theme = /\/themes\/(\w+)\/json\//.exec(file)[1];
themes[theme] = data;
2012-12-28 17:21:26 +00:00
++counter == length && initThemes();
});
} else {
Ox.getFile(Ox.PATH + file + '?' + Ox.VERSION, function() {
2012-12-28 17:21:26 +00:00
++counter == length && initThemes();
});
}
2011-04-25 10:07:09 +00:00
});
2011-08-09 17:00:39 +00:00
});
2011-04-25 09:12:02 +00:00
});
2012-12-28 17:21:26 +00:00
}
function initThemes() {
Ox.Theme.getThemes = function() {
return Object.keys(themes);
};
Ox.Theme.getThemeData = function(theme) {
return themes[theme || Ox.Theme()];
};
Ox.documentReady(function() {
2012-12-28 17:21:26 +00:00
if (options.showScreen && options.hideScreen) {
Ox.UI.hideLoadingScreen();
}
callback(browserSupported);
});
2011-04-25 09:12:02 +00:00
}
function initUI() {
//@ UI
2012-06-18 08:54:39 +00:00
//@ Ox.UI <o> Collection of UI methods and properties
2011-04-25 09:12:02 +00:00
Ox.UI = {};
/*@
Ox.UI.ready <f> queue callback to be called once UI is ready
(callback) -> <u> call callback later
@*/
2011-04-25 09:12:02 +00:00
Ox.UI.ready = (function() {
var callbacks = [];
Ox.documentReady(function() {
// FIXME: use Ox.$foo everywhere!
Ox.body = Ox.$body = Ox.UI.$body = $('body');
Ox.document = Ox.$document = Ox.UI.$document = $(document);
Ox.head = Ox.$head = Ox.UI.$head = $('head');
Ox.window = Ox.$window = Ox.UI.$window = $(window);
// fixme: is this the right place to do this?
2012-05-28 14:06:22 +00:00
$.browser.mozilla && Ox.$document.on('dragstart', function() {
return false;
});
2011-04-25 09:12:02 +00:00
callbacks.forEach(function(callback) {
callback();
});
2011-11-05 16:46:53 +00:00
//delete callbacks;
2011-04-25 09:12:02 +00:00
});
return function(callback) {
2012-06-20 16:30:35 +00:00
if (Ox.$window) {
2011-04-25 09:12:02 +00:00
callback();
} else {
2012-12-29 16:54:58 +00:00
callbacks.push(callback);
2011-04-25 09:12:02 +00:00
}
}
}());
//@ Ox.UI.DIMENSIONS <o> horizontal, vertical dimensions
2011-04-25 09:12:02 +00:00
Ox.UI.DIMENSIONS = {
horizontal: ['width', 'height'],
vertical: ['height', 'width']
};
//@ Ox.UI.EDGES <o> horizontal, vertical edges
2011-04-25 09:12:02 +00:00
Ox.UI.EDGES = {
horizontal: ['left', 'right', 'top', 'bottom'],
vertical: ['top', 'bottom', 'left', 'right']
};
2014-08-21 09:55:52 +00:00
//@ Ox.UI.elements <o> reference to all UI element instances
2014-09-22 12:26:56 +00:00
Ox.elements = Ox.$elements = Ox.UI.elements = {};
/*@
2012-12-28 17:21:26 +00:00
Ox.UI.getImageData <f> Returns image properties
(url) -> <s> Image Name
@*/
2012-12-28 17:21:26 +00:00
Ox.UI.getImageData = Ox.cache(function(url) {
var str = 'data:image/svg+xml;base64,';
return Ox.startsWith(url, str)
? JSON.parse(atob(url.split(',')[1]).match(/<!--(.+?)-->/)[1])
: null;
});
/*@
Ox.UI.getImageURL <f> Returns an image URL
2012-12-28 17:21:26 +00:00
(name[, color[, theme]]) -> <s> Image URL
name <s> Image name
color <s|[n]> Color name or RGB values
theme <s> Theme name
@*/
2012-12-29 14:57:01 +00:00
Ox.UI.getImageURL = Ox.cache(function(name, color, theme) {
2012-12-28 17:21:26 +00:00
var colorName,
image = images[name],
themeData,
type = Ox.toDashes(name).split('-')[0];
color = color || 'default';
2011-08-09 17:00:39 +00:00
theme = theme || Ox.Theme();
2012-12-28 17:21:26 +00:00
themeData = Ox.Theme.getThemeData(theme);
2013-01-31 13:16:01 +00:00
if (type == 'symbol') {
2012-12-28 17:21:26 +00:00
if (Ox.isString(color)) {
colorName = color;
color = themeData[
'symbol' + color[0].toUpperCase() + color.slice(1) + 'Color'
];
2012-12-28 17:21:26 +00:00
}
image = image.replace(/#808080/g, '#' + Ox.toHex(color));
2011-04-25 09:12:02 +00:00
}
2012-12-28 17:21:26 +00:00
Ox.forEach(colors[type], function(name, hex) {
image = image.replace(
new RegExp(hex, 'g'),
2013-01-31 13:22:01 +00:00
'$' + Ox.toHex(themeData[name])
);
2012-12-28 17:21:26 +00:00
});
2013-01-31 13:24:27 +00:00
image = image.replace(/\$/g, '#');
2012-12-28 17:21:26 +00:00
return 'data:image/svg+xml;base64,' + btoa(
image + '<!--' + JSON.stringify(Ox.extend(color ? {
color: colorName
} : {}, {
name: name, theme: theme
})) + '-->'
);
2012-12-29 14:57:01 +00:00
}, {
key: function(args) {
args[1] = args[1] || 'default';
args[2] = args[2] || Ox.Theme();
return JSON.stringify(args);
}
});
// ...
Ox.getElement = Ox.getOxElement = Ox.UI.getOxElement = function(element) {
return Ox.elements[$(element).data('oxid')];
};
/*@
Ox.UI.hideLoadingScreen <f> hide loading screen
() -> <u> hide loading screen
@*/
2011-04-25 11:08:06 +00:00
Ox.UI.hideLoadingScreen = function() {
var $div = $('.OxLoadingScreen'),
error = $div.is('.OxError');
//$div.find('img').remove();
$div.animate({
opacity: error ? 0.9 : 0
}, 1000, function() {
if (error) {
2013-12-06 20:43:00 +00:00
$div.on({
click: function() {
$div.remove();
}
2011-08-24 21:05:39 +00:00
});
2011-04-25 11:08:06 +00:00
} else {
2011-04-25 13:38:47 +00:00
clearInterval(loadingInterval);
2011-04-25 11:08:06 +00:00
$div.remove();
}
});
};
2012-05-22 14:08:09 +00:00
/*@
2012-06-12 16:09:25 +00:00
Ox.UI.isElement <f> Checks if an object is an Ox.Element
(obj) -> <b> True if object is an Ox.Element
2012-05-22 14:08:09 +00:00
@*/
2012-06-12 16:09:25 +00:00
Ox.UI.isElement = function(object) {
return Ox.isObject(object) && 'oxid' in object;
2012-05-22 14:08:09 +00:00
};
// ...
Ox.isOxElement = Ox.UI.isOxElement = function(element) {
return !!$(element).data('oxid');
};
//@ Ox.UI.PATH <str> Path of Ox.UI
Ox.UI.PATH = Ox.PATH + 'Ox.UI/';
//@ Ox.UI.SCOLLBAR_SIZE <str> size of scrollbar
2012-12-10 16:44:10 +00:00
Ox.UI.SCROLLBAR_SIZE = $.browser.webkit ? 8 : (function() {
2013-12-06 21:15:04 +00:00
var inner = Ox.$('<p>').css({
height: '200px',
width: '100%'
}),
2013-12-06 21:15:04 +00:00
outer = Ox.$('<div>').css({
height: '150px',
left: 0,
overflow: 'hidden',
position: 'absolute',
top: 0,
visibility: 'hidden',
width: '200px'
}).append(inner).appendTo($('body')),
2013-12-06 21:15:04 +00:00
width = inner[0].offsetWidth;
outer.css({overflow: 'scroll'});
2012-06-28 10:34:18 +00:00
width = 1 + width - (inner[0].offsetWidth == width
? outer[0].clientWidth : inner[0].offsetWidth);
outer.remove();
return width;
})();
2011-04-25 09:12:02 +00:00
}
2011-10-08 09:10:01 +00:00
};