Clean up interface.js a bit.

This commit is contained in:
Michael Jumper
2012-11-09 11:00:00 -08:00
parent 95a61ceb14
commit 1a3801c2f7

View File

@@ -107,157 +107,91 @@ GuacamoleUI.toggleKeyboard = function() {
}; };
// Constant UI initialization and behavior // If Node.classList is supported, implement addClass/removeClass using that
(function() { if (Node.classList) {
// Cache error image (might not be available when error occurs) GuacamoleUI.addClass = function(element, classname) {
var guacErrorImage = new Image(); element.classList.add(classname);
guacErrorImage.src = "images/noguacamole-logo-24.png";
// Function for adding a class to an element
var addClass;
// Function for removing a class from an element
var removeClass;
// If Node.classList is supported, implement addClass/removeClass using that
if (Node.classList) {
addClass = function(element, classname) {
element.classList.add(classname);
};
removeClass = function(element, classname) {
element.classList.remove(classname);
};
}
// Otherwise, implement own
else {
addClass = function(element, classname) {
// Simply add new class
element.className += " " + classname;
};
removeClass = function(element, classname) {
// Filter out classes with given name
element.className = element.className.replace(/([^ ]+)[ ]*/g,
function(match, testClassname, spaces, offset, string) {
// If same class, remove
if (testClassname == classname)
return "";
// Otherwise, allow
return match;
}
);
};
}
GuacamoleUI.hideStatus = function() {
removeClass(document.body, "guac-error");
GuacamoleUI.containers.state.style.visibility = "hidden";
GuacamoleUI.display.style.opacity = "1";
}; };
GuacamoleUI.showStatus = function(text) { GuacamoleUI.removeClass = function(element, classname) {
removeClass(document.body, "guac-error"); element.classList.remove(classname);
GuacamoleUI.containers.state.style.visibility = "visible";
GuacamoleUI.state.textContent = text;
GuacamoleUI.display.style.opacity = "1";
}; };
GuacamoleUI.showError = function(error) { }
addClass(document.body, "guac-error");
GuacamoleUI.state.textContent = error; // Otherwise, implement own
GuacamoleUI.display.style.opacity = "0.1"; else {
GuacamoleUI.addClass = function(element, classname) {
// Simply add new class
element.className += " " + classname;
}; };
GuacamoleUI.removeClass = function(element, classname) {
// Detect three-finger tap // Filter out classes with given name
GuacamoleUI.display.addEventListener('touchstart', function(e) { element.className = element.className.replace(/([^ ]+)[ ]*/g,
function(match, testClassname, spaces, offset, string) {
// If three touches, toggle keyboard
if (e.touches.length == 3)
GuacamoleUI.toggleKeyboard();
}, true);
function positionCentered(element) { // If same class, remove
element.style.left = if (testClassname == classname)
((GuacamoleUI.viewport.offsetWidth - element.offsetWidth) / 2 return "";
+ window.pageXOffset)
+ "px";
element.style.top = // Otherwise, allow
((GuacamoleUI.viewport.offsetHeight - element.offsetHeight) / 2 return match;
+ window.pageYOffset)
+ "px"; }
} );
// Reconnect button
GuacamoleUI.buttons.reconnect.onclick = function() {
window.location.reload();
}; };
}
// Query audio support
if (!GuacamoleUI.sessionState.getProperty("disable-sound"))
(function () {
var probably_supported = [];
var maybe_supported = [];
// Build array of supported audio formats GuacamoleUI.hideStatus = function() {
[ GuacamoleUI.removeClass(document.body, "guac-error");
'audio/ogg; codecs="vorbis"', GuacamoleUI.containers.state.style.visibility = "hidden";
'audio/mp4; codecs="mp4a.40.5"', GuacamoleUI.display.style.opacity = "1";
'audio/mpeg; codecs="mp3"', };
'audio/webm; codecs="vorbis"',
'audio/wav; codecs=1'
].forEach(function(mimetype) {
var audio = new Audio(); GuacamoleUI.showStatus = function(text) {
var support_level = audio.canPlayType(mimetype); GuacamoleUI.removeClass(document.body, "guac-error");
GuacamoleUI.containers.state.style.visibility = "visible";
GuacamoleUI.state.textContent = text;
GuacamoleUI.display.style.opacity = "1";
};
// Trim semicolon and trailer GuacamoleUI.showError = function(error) {
var semicolon = mimetype.indexOf(";"); GuacamoleUI.addClass(document.body, "guac-error");
if (semicolon != -1) GuacamoleUI.state.textContent = error;
mimetype = mimetype.substring(0, semicolon); GuacamoleUI.display.style.opacity = "0.1";
};
// Partition by probably/maybe // Reconnect button
if (support_level == "probably") GuacamoleUI.buttons.reconnect.onclick = function() {
probably_supported.push(mimetype); window.location.reload();
else if (support_level == "maybe") };
maybe_supported.push(mimetype);
}); // Query audio support
if (!GuacamoleUI.sessionState.getProperty("disable-sound"))
Array.prototype.push.apply(GuacamoleUI.supportedAudio, probably_supported);
Array.prototype.push.apply(GuacamoleUI.supportedAudio, maybe_supported);
})();
// Query video support
(function () { (function () {
var probably_supported = []; var probably_supported = [];
var maybe_supported = []; var maybe_supported = [];
// Build array of supported video formats // Build array of supported audio formats
[ [
'video/ogg; codecs="theora, vorbis"', 'audio/ogg; codecs="vorbis"',
'video/mp4; codecs="avc1.4D401E, mp4a.40.5"', 'audio/mp4; codecs="mp4a.40.5"',
'video/webm; codecs="vp8.0, vorbis"' 'audio/mpeg; codecs="mp3"',
'audio/webm; codecs="vorbis"',
'audio/wav; codecs=1'
].forEach(function(mimetype) { ].forEach(function(mimetype) {
var video = document.createElement("video"); var audio = new Audio();
var support_level = video.canPlayType(mimetype); var support_level = audio.canPlayType(mimetype);
// Trim semicolon and trailer // Trim semicolon and trailer
var semicolon = mimetype.indexOf(";"); var semicolon = mimetype.indexOf(";");
@@ -272,10 +206,40 @@ GuacamoleUI.toggleKeyboard = function() {
}); });
Array.prototype.push.apply(GuacamoleUI.supportedVideo, probably_supported); Array.prototype.push.apply(GuacamoleUI.supportedAudio, probably_supported);
Array.prototype.push.apply(GuacamoleUI.supportedVideo, maybe_supported); Array.prototype.push.apply(GuacamoleUI.supportedAudio, maybe_supported);
})(); })();
// Query video support
(function () {
var probably_supported = [];
var maybe_supported = [];
// Build array of supported video formats
[
'video/ogg; codecs="theora, vorbis"',
'video/mp4; codecs="avc1.4D401E, mp4a.40.5"',
'video/webm; codecs="vp8.0, vorbis"'
].forEach(function(mimetype) {
var video = document.createElement("video");
var support_level = video.canPlayType(mimetype);
// Trim semicolon and trailer
var semicolon = mimetype.indexOf(";");
if (semicolon != -1)
mimetype = mimetype.substring(0, semicolon);
// Partition by probably/maybe
if (support_level == "probably")
probably_supported.push(mimetype);
else if (support_level == "maybe")
maybe_supported.push(mimetype);
});
Array.prototype.push.apply(GuacamoleUI.supportedVideo, probably_supported);
Array.prototype.push.apply(GuacamoleUI.supportedVideo, maybe_supported);
})(); })();
// Tie UI events / behavior to a specific Guacamole client // Tie UI events / behavior to a specific Guacamole client