JSDoc + namespace, some cleanup.

This commit is contained in:
Michael Jumper
2011-07-05 22:13:45 -07:00
parent bd477daa2c
commit 3e65c46204
6 changed files with 82 additions and 32 deletions

View File

@@ -16,7 +16,10 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
*/ */
function GuacamoleClient(display, tunnel) { // Guacamole namespace
var Guacamole = Guacamole || {};
Guacamole.Client = function(display, tunnel) {
var guac_client = this; var guac_client = this;
@@ -150,7 +153,7 @@ function GuacamoleClient(display, tunnel) {
// Create buffer if necessary // Create buffer if necessary
if (buffer == null) { if (buffer == null) {
buffer = new Layer(0, 0); buffer = new Guacamole.Layer(0, 0);
buffer.setAutosize(1); buffer.setAutosize(1);
buffers[index] = buffer; buffers[index] = buffer;
} }
@@ -165,7 +168,7 @@ function GuacamoleClient(display, tunnel) {
if (layer == null) { if (layer == null) {
// Add new layer // Add new layer
layer = new Layer(displayWidth, displayHeight); layer = new Guacamole.Layer(displayWidth, displayHeight);
layers[index] = layer; layers[index] = layer;
// (Re)-add existing layers in order // (Re)-add existing layers in order
@@ -174,18 +177,18 @@ function GuacamoleClient(display, tunnel) {
// If already present, remove // If already present, remove
if (layers[i].parentNode === display) if (layers[i].parentNode === display)
display.removeChild(layers[i]); display.removeChild(layers[i].getCanvas());
// Add to end // Add to end
display.appendChild(layers[i]); display.appendChild(layers[i].getCanvas());
} }
} }
// Add cursor layer last // Add cursor layer last
if (cursor != null) { if (cursor != null) {
if (cursor.parentNode === display) if (cursor.parentNode === display)
display.removeChild(cursor); display.removeChild(cursor.getCanvas());
display.appendChild(cursor); display.appendChild(cursor.getCanvas());
} }
} }
@@ -284,8 +287,8 @@ function GuacamoleClient(display, tunnel) {
var data = parameters[2]; var data = parameters[2];
if (cursor == null) { if (cursor == null) {
cursor = new Layer(displayWidth, displayHeight); cursor = new Guacamole.Layer(displayWidth, displayHeight);
display.appendChild(cursor); display.appendChild(cursor.getCanvas());
} }
// Start cursor image load // Start cursor image load

View File

@@ -17,7 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
function GuacamoleKeyboard(element) { // Guacamole namespace
var Guacamole = Guacamole || {};
Guacamole.Keyboard = function(element) {
var guac_keyboard = this; var guac_keyboard = this;

View File

@@ -17,12 +17,40 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
function Layer(width, height) { // Guacamole namespace
var Guacamole = Guacamole || {};
/**
* Abstract ordered drawing surface. Each Layer contains a canvas element and
* provides simple drawing instructions for drawing to that canvas element,
* however unlike the canvas element itself, drawing operations on a Layer are
* guaranteed to run in order, even if such an operation must wait for an image
* to load before completing.
*
* @constructor
*
* @param {Number} width The width of the Layer, in pixels. The canvas element
* backing this Layer will be given this width.
*
* @param {Number} height The height of the Layer, in pixels. The canvas element
* backing this Layer will be given this height.
*/
Guacamole.Layer = function(width, height) {
var layer = this;
// Off-screen buffer // Off-screen buffer
var display = document.createElement("canvas"); var display = document.createElement("canvas");
var displayContext = display.getContext("2d"); var displayContext = display.getContext("2d");
/**
* Returns the canvas element backing this Layer.
* @returns {Element} The canvas element backing this Layer.
*/
this.getCanvas = function() {
return display;
};
function resize(newWidth, newHeight) { function resize(newWidth, newHeight) {
display.style.position = "absolute"; display.style.position = "absolute";
display.style.left = "0px"; display.style.left = "0px";
@@ -35,7 +63,15 @@ function Layer(width, height) {
height = newHeight; height = newHeight;
} }
display.resize = function(newWidth, newHeight) { /**
* Changes the size of this Layer to the given width and height. Resizing
* is only attempted if the new size provided is actually different from
* the current size.
*
* @param {Number} newWidth The new width to assign to this Layer.
* @param {Number} newHeight The new height to assign to this Layer.
*/
this.resize = function(newWidth, newHeight) {
if (newWidth != width || newHeight != height) if (newWidth != width || newHeight != height)
resize(newWidth, newHeight); resize(newWidth, newHeight);
}; };
@@ -87,14 +123,14 @@ function Layer(width, height) {
} }
display.setAutosize = function(flag) { this.setAutosize = function(flag) {
autosize = flag; autosize = flag;
}; };
function reserveJob(handler) { function reserveJob(handler) {
// If no pending updates, just call (if available) and exit // If no pending updates, just call (if available) and exit
if (display.isReady() && handler != null) { if (layer.isReady() && handler != null) {
handler(); handler();
return null; return null;
} }
@@ -118,12 +154,12 @@ function Layer(width, height) {
} }
display.isReady = function() { this.isReady = function() {
return updates.length == 0; return updates.length == 0;
}; };
display.drawImage = function(x, y, image) { this.drawImage = function(x, y, image) {
reserveJob(function() { reserveJob(function() {
if (autosize != 0) fitRect(x, y, image.width, image.height); if (autosize != 0) fitRect(x, y, image.width, image.height);
displayContext.drawImage(image, x, y); displayContext.drawImage(image, x, y);
@@ -131,7 +167,7 @@ function Layer(width, height) {
}; };
display.draw = function(x, y, url) { this.draw = function(x, y, url) {
var update = reserveJob(null); var update = reserveJob(null);
var image = new Image(); var image = new Image();
@@ -151,14 +187,18 @@ function Layer(width, height) {
}; };
// Run arbitrary function as soon as currently pending operations complete. /**
// Future operations will not block this function from being called (unlike * Run an arbitrary function as soon as currently pending operations
// the ready handler, which requires no pending updates) * are complete.
display.sync = function(handler) { *
* @param {function} handler The function to call once all currently
* pending operations are complete.
*/
this.sync = function(handler) {
reserveJob(handler); reserveJob(handler);
}; };
display.copyRect = function(srcLayer, srcx, srcy, w, h, x, y) { this.copyRect = function(srcLayer, srcx, srcy, w, h, x, y) {
function doCopyRect() { function doCopyRect() {
if (autosize != 0) fitRect(x, y, w, h); if (autosize != 0) fitRect(x, y, w, h);
@@ -167,7 +207,7 @@ function Layer(width, height) {
// If we ARE the source layer, no need to sync. // If we ARE the source layer, no need to sync.
// Syncing would result in deadlock. // Syncing would result in deadlock.
if (display === srcLayer) if (layer === srcLayer)
reserveJob(doCopyRect); reserveJob(doCopyRect);
// Otherwise synchronize copy operation with source layer // Otherwise synchronize copy operation with source layer
@@ -186,14 +226,14 @@ function Layer(width, height) {
}; };
display.clearRect = function(x, y, w, h) { this.clearRect = function(x, y, w, h) {
reserveJob(function() { reserveJob(function() {
if (autosize != 0) fitRect(x, y, w, h); if (autosize != 0) fitRect(x, y, w, h);
displayContext.clearRect(x, y, w, h); displayContext.clearRect(x, y, w, h);
}); });
}; };
display.filter = function(filter) { this.filter = function(filter) {
reserveJob(function() { reserveJob(function() {
var imageData = displayContext.getImageData(0, 0, width, height); var imageData = displayContext.getImageData(0, 0, width, height);
filter(imageData.data, width, height); filter(imageData.data, width, height);
@@ -220,13 +260,11 @@ function Layer(width, height) {
0xF: "lighter" 0xF: "lighter"
}; };
display.setChannelMask = function(mask) { this.setChannelMask = function(mask) {
reserveJob(function() { reserveJob(function() {
displayContext.globalCompositeOperation = compositeOperation[mask]; displayContext.globalCompositeOperation = compositeOperation[mask];
}); });
}; };
return display;
} }

View File

@@ -17,8 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
// Guacamole namespace
var Guacamole = Guacamole || {};
function GuacamoleMouse(element) { Guacamole.Mouse = function(element) {
var guac_mouse = this; var guac_mouse = this;

View File

@@ -17,10 +17,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
// Guacamole namespace
var Guacamole = Guacamole || {};
function GuacamoleOnScreenKeyboard(url) { Guacamole.OnScreenKeyboard = function(url) {
var tabIndex = 1;
var allKeys = new Array(); var allKeys = new Array();
var modifierState = new function() {}; var modifierState = new function() {};

View File

@@ -16,7 +16,10 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
*/ */
function GuacamoleHTTPTunnel(tunnelURL) { // Guacamole namespace
var Guacamole = Guacamole || {};
Guacamole.HTTPTunnel = function(tunnelURL) {
var tunnel = this; var tunnel = this;