splunk-sdk
Version:
SDK for usage with the Splunk REST API
2,068 lines (1,660 loc) • 194 kB
JavaScript
/**
* Includes code from the jgatt library
* Copyright (c) 2011 Jason Gatt
* Dual licensed under the MIT and GPL licenses
*/
(function() {
var jg_global = require('./jg_global');
var jg_namespace = jg_global.jg_namespace;
var jg_import = jg_global.jg_import;
var jg_extend = jg_global.jg_extend;
var jg_static = jg_global.jg_static;
var jg_mixin = jg_global.jg_mixin;
var jg_has_mixin = jg_global.jg_has_mixin;
var jg_delegate = jg_global.jg_delegate;
module.exports = jg_global;
/***** ONLY CHANGE THINGS UNDER THIS LINE *****/
jg_namespace("jgatt.geom", function()
{
this.Point = jg_extend(Object, function(Point, base)
{
// Public Properties
this.x = 0;
this.y = 0;
// Constructor
this.constructor = function(x, y)
{
this.x = (x !== undefined) ? x : 0;
this.y = (y !== undefined) ? y : 0;
};
// Public Methods
this.length = function()
{
return Math.sqrt(this.x * this.x + this.y * this.y);
};
this.hasNaN = function()
{
return (isNaN(this.x) ||
isNaN(this.y));
};
this.hasInfinity = function()
{
return ((this.x == Infinity) || (this.x == -Infinity) ||
(this.y == Infinity) || (this.y == -Infinity));
};
this.hasPositiveInfinity = function()
{
return ((this.x == Infinity) ||
(this.y == Infinity));
};
this.hasNegativeInfinity = function()
{
return ((this.x == -Infinity) ||
(this.y == -Infinity));
};
this.isFinite = function()
{
return (((this.x - this.x) === 0) &&
((this.y - this.y) === 0));
};
this.clone = function()
{
return new Point(this.x, this.y);
};
this.equals = function(point)
{
return ((this.x === point.x) && (this.y === point.y));
};
this.toString = function()
{
return "(x=" + this.x + ", y=" + this.y + ")";
};
});
});
jg_namespace("jgatt.geom", function()
{
var Point = jg_import("jgatt.geom.Point");
this.Matrix = jg_extend(Object, function(Matrix, base)
{
// Public Properties
this.a = 1;
this.b = 0;
this.c = 0;
this.d = 1;
this.tx = 0;
this.ty = 0;
// Constructor
this.constructor = function(a, b, c, d, tx, ty)
{
this.a = (a !== undefined) ? a : 1;
this.b = (b !== undefined) ? b : 0;
this.c = (c !== undefined) ? c : 0;
this.d = (d !== undefined) ? d : 1;
this.tx = (tx !== undefined) ? tx : 0;
this.ty = (ty !== undefined) ? ty : 0;
};
// Public Methods
this.transformPoint = function(point)
{
var x = this.a * point.x + this.c * point.y + this.tx;
var y = this.b * point.x + this.d * point.y + this.ty;
return new Point(x, y);
};
this.translate = function(x, y)
{
this.tx += x;
this.ty += y;
};
this.scale = function(scaleX, scaleY)
{
this.a *= scaleX;
this.b *= scaleY;
this.c *= scaleX;
this.d *= scaleY;
this.tx *= scaleX;
this.ty *= scaleY;
};
this.rotate = function(angle)
{
var cosAngle = Math.cos(angle);
var sinAngle = Math.sin(angle);
var a = this.a;
var b = this.b;
var c = this.c;
var d = this.d;
var tx = this.tx;
var ty = this.ty;
this.a = a * cosAngle - b * sinAngle;
this.b = b * cosAngle + a * sinAngle;
this.c = c * cosAngle - d * sinAngle;
this.d = d * cosAngle + c * sinAngle;
this.tx = tx * cosAngle - ty * sinAngle;
this.ty = ty * cosAngle + tx * sinAngle;
};
this.concat = function(matrix)
{
var a1 = matrix.a;
var b1 = matrix.b;
var c1 = matrix.c;
var d1 = matrix.d;
var tx1 = matrix.tx;
var ty1 = matrix.ty;
var a2 = this.a;
var b2 = this.b;
var c2 = this.c;
var d2 = this.d;
var tx2 = this.tx;
var ty2 = this.ty;
this.a = a1 * a2 + c1 * b2;
this.b = b1 * a2 + d1 * b2;
this.c = a1 * c2 + c1 * d2;
this.d = b1 * c2 + d1 * d2;
this.tx = a1 * tx2 + c1 * ty2 + tx1;
this.ty = b1 * tx2 + d1 * ty2 + ty1;
};
this.invert = function()
{
var det = this.determinant();
var a = this.a / det;
var b = this.b / det;
var c = this.c / det;
var d = this.d / det;
var tx = this.tx;
var ty = this.ty;
this.a = d;
this.b = -b;
this.c = -c;
this.d = a;
this.tx = c * ty - d * tx;
this.ty = b * tx - a * ty;
};
this.identity = function()
{
this.a = 1;
this.b = 0;
this.c = 0;
this.d = 1;
this.tx = 0;
this.ty = 0;
};
this.determinant = function()
{
return (this.a * this.d) - (this.b * this.c);
};
this.hasInverse = function()
{
var det = Math.abs(this.determinant());
return ((det > 0) && (det < Infinity));
};
this.hasNaN = function()
{
return (isNaN(this.a) ||
isNaN(this.b) ||
isNaN(this.c) ||
isNaN(this.d) ||
isNaN(this.tx) ||
isNaN(this.ty));
};
this.hasInfinity = function()
{
return ((this.a == Infinity) || (this.a == -Infinity) ||
(this.b == Infinity) || (this.b == -Infinity) ||
(this.c == Infinity) || (this.c == -Infinity) ||
(this.d == Infinity) || (this.d == -Infinity) ||
(this.tx == Infinity) || (this.tx == -Infinity) ||
(this.ty == Infinity) || (this.ty == -Infinity));
};
this.hasPositiveInfinity = function()
{
return ((this.a == Infinity) ||
(this.b == Infinity) ||
(this.c == Infinity) ||
(this.d == Infinity) ||
(this.tx == Infinity) ||
(this.ty == Infinity));
};
this.hasNegativeInfinity = function()
{
return ((this.a == -Infinity) ||
(this.b == -Infinity) ||
(this.c == -Infinity) ||
(this.d == -Infinity) ||
(this.tx == -Infinity) ||
(this.ty == -Infinity));
};
this.isFinite = function()
{
return (((this.a - this.a) === 0) &&
((this.b - this.b) === 0) &&
((this.c - this.c) === 0) &&
((this.d - this.d) === 0) &&
((this.tx - this.tx) === 0) &&
((this.ty - this.ty) === 0));
};
this.isIdentity = function()
{
return ((this.a == 1) &&
(this.b == 0) &&
(this.c == 0) &&
(this.d == 1) &&
(this.tx == 0) &&
(this.ty == 0));
};
this.clone = function()
{
return new Matrix(this.a, this.b, this.c, this.d, this.tx, this.ty);
};
this.equals = function(matrix)
{
return ((this.a === matrix.a) &&
(this.b === matrix.b) &&
(this.c === matrix.c) &&
(this.d === matrix.d) &&
(this.tx === matrix.tx) &&
(this.ty === matrix.ty));
};
this.toString = function()
{
return "(a=" + this.a + ", b=" + this.b + ", c=" + this.c + ", d=" + this.d + ", tx=" + this.tx + ", ty=" + this.ty + ")";
};
});
});
jg_namespace("jgatt.geom", function()
{
this.Rectangle = jg_extend(Object, function(Rectangle, base)
{
// Public Properties
this.x = 0;
this.y = 0;
this.width = 0;
this.height = 0;
// Constructor
this.constructor = function(x, y, width, height)
{
this.x = (x !== undefined) ? x : 0;
this.y = (y !== undefined) ? y : 0;
this.width = (width !== undefined) ? width : 0;
this.height = (height !== undefined) ? height : 0;
};
// Public Methods
this.hasNaN = function()
{
return (isNaN(this.x) ||
isNaN(this.y) ||
isNaN(this.width) ||
isNaN(this.height));
};
this.hasInfinity = function()
{
return ((this.x == Infinity) || (this.x == -Infinity) ||
(this.y == Infinity) || (this.y == -Infinity) ||
(this.width == Infinity) || (this.width == -Infinity) ||
(this.height == Infinity) || (this.height == -Infinity));
};
this.hasPositiveInfinity = function()
{
return ((this.x == Infinity) ||
(this.y == Infinity) ||
(this.width == Infinity) ||
(this.height == Infinity));
};
this.hasNegativeInfinity = function()
{
return ((this.x == -Infinity) ||
(this.y == -Infinity) ||
(this.width == -Infinity) ||
(this.height == -Infinity));
};
this.isFinite = function()
{
return (((this.x - this.x) === 0) &&
((this.y - this.y) === 0) &&
((this.width - this.width) === 0) &&
((this.height - this.height) === 0));
};
this.clone = function()
{
return new Rectangle(this.x, this.y, this.width, this.height);
};
this.equals = function(rectangle)
{
return ((this.x === rectangle.x) &&
(this.y === rectangle.y) &&
(this.width === rectangle.width) &&
(this.height === rectangle.height));
};
this.toString = function()
{
return "(x=" + this.x + ", y=" + this.y + ", width=" + this.width + ", height=" + this.height + ")";
};
});
});
jg_namespace("jgatt.graphics", function()
{
this.ColorUtils = jg_static(function(ColorUtils)
{
ColorUtils.toRGB = function(color)
{
var rgb = {};
rgb.r = (color >> 16) & 0xFF;
rgb.g = (color >> 8) & 0xFF;
rgb.b = color & 0xFF;
return rgb;
};
ColorUtils.fromRGB = function(rgb)
{
return ((rgb.r << 16) | (rgb.g << 8) | rgb.b);
};
ColorUtils.brightness = function(color, brightness)
{
var rgb = ColorUtils.toRGB(color);
var c;
if (brightness < 0)
{
brightness = -brightness;
c = 0x00;
}
else
{
c = 0xFF;
}
if (brightness > 1)
brightness = 1;
rgb.r += Math.round((c - rgb.r) * brightness);
rgb.g += Math.round((c - rgb.g) * brightness);
rgb.b += Math.round((c - rgb.b) * brightness);
return ColorUtils.fromRGB(rgb);
};
});
});
jg_namespace("jgatt.graphics", function()
{
var Matrix = jg_import("jgatt.geom.Matrix");
var Point = jg_import("jgatt.geom.Point");
this.Graphics = jg_extend(Object, function(Graphics, base)
{
// Private Properties
this._width = 1;
this._height = 1;
this._strokeStyle = null;
this._strokeCommands = null;
this._fillCommands = null;
this._drawingStack = null;
this._drawingStackIndex = 0;
this._penX = 0;
this._penY = 0;
this._element = null;
this._canvas = null;
this._context = null;
// Constructor
this.constructor = function(width, height)
{
this._width = ((width > 1) && (width < Infinity)) ? Math.floor(width) : 1;
this._height = ((height > 1) && (height < Infinity)) ? Math.floor(height) : 1;
this._strokeStyle = { thickness: 1, caps: "none", joints: "miter", miterLimit: 10, pixelHinting: true };
this._drawingStack = [];
};
// Public Methods
this.appendTo = function(element)
{
if (!element)
throw new Error("Parameter element must be non-null.");
if (element === this._element)
return true;
this.remove();
var canvas = document.createElement("canvas");
if (!canvas)
return false;
if (typeof canvas.getContext !== "function")
return false;
var context = canvas.getContext("2d");
if (!context)
return false;
canvas.style.position = "absolute";
canvas.width = this._width;
canvas.height = this._height;
element.appendChild(canvas);
this._element = element;
this._canvas = canvas;
this._context = context;
this._draw(true);
return true;
};
this.remove = function()
{
if (!this._element)
return false;
var context = this._context;
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
context.beginPath();
var canvas = this._canvas;
var parentNode = canvas.parentNode;
if (parentNode)
parentNode.removeChild(canvas);
this._element = null;
this._canvas = null;
this._context = null;
return true;
};
this.setSize = function(width, height)
{
width = ((width > 1) && (width < Infinity)) ? Math.floor(width) : 1;
height = ((height > 1) && (height < Infinity)) ? Math.floor(height) : 1;
if ((width === this._width) && (height === this._height))
return;
this._width = width;
this._height = height;
var canvas = this._canvas;
if (!canvas)
return;
canvas.width = width;
canvas.height = height;
this._draw(true);
};
this.setStrokeStyle = function(thickness, caps, joints, miterLimit, pixelHinting)
{
if ((caps != null) && (caps !== "none") && (caps !== "round") && (caps !== "square"))
throw new Error("Parameter caps must be one of \"none\", \"round\", or \"square\".");
if ((joints != null) && (joints !== "miter") && (joints !== "round") && (joints !== "bevel"))
throw new Error("Parameter joints must be one of \"miter\", \"round\", or \"bevel\".");
thickness *= 1;
thickness = ((thickness > 0) && (thickness < Infinity)) ? thickness : 1;
caps = caps ? caps : "none";
joints = joints ? joints : "miter";
miterLimit *= 1;
miterLimit = ((miterLimit > 0) && (miterLimit < Infinity)) ? miterLimit : 10;
pixelHinting = (pixelHinting != false);
this._strokeStyle = { thickness: thickness, caps: caps, joints: joints, miterLimit: miterLimit, pixelHinting: pixelHinting };
};
this.beginSolidStroke = function(color, alpha)
{
this.endStroke();
color = !isNaN(color) ? Math.min(Math.max(Math.floor(color), 0x000000), 0xFFFFFF) : 0x000000;
alpha = !isNaN(alpha) ? Math.min(Math.max(alpha, 0), 1) : 1;
var strokeCommands = this._strokeCommands = [];
strokeCommands.push({ name: "solidStroke", strokeStyle: this._strokeStyle, color: color, alpha: alpha });
strokeCommands.push({ name: "moveTo", x: this._penX, y: this._penY });
};
this.beginGradientStroke = function(type, colors, alphas, ratios, matrix, focalPointRatio)
{
if (type == null)
throw new Error("Parameter type must be non-null.");
if ((type !== "linear") && (type !== "radial"))
throw new Error("Parameter type must be one of \"linear\" or \"radial\".");
if (colors == null)
throw new Error("Parameter colors must be non-null.");
if (!(colors instanceof Array))
throw new Error("Parameter colors must be an array.");
if (alphas == null)
throw new Error("Parameter alphas must be non-null.");
if (!(alphas instanceof Array))
throw new Error("Parameter alphas must be an array.");
if (ratios == null)
throw new Error("Parameter ratios must be non-null.");
if (!(ratios instanceof Array))
throw new Error("Parameter ratios must be an array.");
if ((matrix != null) && !(matrix instanceof Matrix))
throw new Error("Parameter matrix must be an instance of jgatt.geom.Matrix.");
this.endStroke();
var numStops = Math.min(colors.length, alphas.length, ratios.length);
colors = colors.slice(0, numStops);
alphas = alphas.slice(0, numStops);
ratios = ratios.slice(0, numStops);
var color;
var alpha;
var ratio;
for (var i = 0; i < numStops; i++)
{
color = colors[i];
colors[i] = !isNaN(color) ? Math.min(Math.max(Math.floor(color), 0x000000), 0xFFFFFF) : 0x000000;
alpha = alphas[i];
alphas[i] = !isNaN(alpha) ? Math.min(Math.max(alpha, 0), 1) : 1;
ratio = ratios[i];
ratios[i] = !isNaN(ratio) ? Math.min(Math.max(ratio, 0), 1) : 0;
}
if (matrix)
{
matrix = new Matrix(matrix.a * 1, matrix.b * 1, matrix.c * 1, matrix.d * 1, matrix.tx * 1, matrix.ty * 1);
if ((matrix.tx - matrix.tx) !== 0)
matrix.tx = 0;
if ((matrix.ty - matrix.ty) !== 0)
matrix.ty = 0;
if (!matrix.hasInverse())
matrix = null;
}
focalPointRatio = !isNaN(focalPointRatio) ? Math.min(Math.max(focalPointRatio, -1), 1) : 0;
var strokeCommands = this._strokeCommands = [];
strokeCommands.push({ name: "gradientStroke", strokeStyle: this._strokeStyle, type: type, colors: colors, alphas: alphas, ratios: ratios, matrix: matrix, focalPointRatio: focalPointRatio });
strokeCommands.push({ name: "moveTo", x: this._penX, y: this._penY });
};
this.beginImageStroke = function(image, matrix, repeat)
{
};
this.endStroke = function()
{
if (!this._strokeCommands)
return;
this._drawingStack.push(this._strokeCommands);
this._strokeCommands = null;
this._draw();
};
this.beginSolidFill = function(color, alpha)
{
this.endFill();
color = !isNaN(color) ? Math.min(Math.max(Math.floor(color), 0x000000), 0xFFFFFF) : 0x000000;
alpha = !isNaN(alpha) ? Math.min(Math.max(alpha, 0), 1) : 1;
var fillCommands = this._fillCommands = [];
fillCommands.push({ name: "solidFill", color: color, alpha: alpha });
fillCommands.push({ name: "moveTo", x: this._penX, y: this._penY });
};
this.beginGradientFill = function(type, colors, alphas, ratios, matrix, focalPointRatio)
{
if (type == null)
throw new Error("Parameter type must be non-null.");
if ((type !== "linear") && (type !== "radial"))
throw new Error("Parameter type must be one of \"linear\" or \"radial\".");
if (colors == null)
throw new Error("Parameter colors must be non-null.");
if (!(colors instanceof Array))
throw new Error("Parameter colors must be an array.");
if (alphas == null)
throw new Error("Parameter alphas must be non-null.");
if (!(alphas instanceof Array))
throw new Error("Parameter alphas must be an array.");
if (ratios == null)
throw new Error("Parameter ratios must be non-null.");
if (!(ratios instanceof Array))
throw new Error("Parameter ratios must be an array.");
if ((matrix != null) && !(matrix instanceof Matrix))
throw new Error("Parameter matrix must be an instance of jgatt.geom.Matrix.");
this.endFill();
var numStops = Math.min(colors.length, alphas.length, ratios.length);
colors = colors.slice(0, numStops);
alphas = alphas.slice(0, numStops);
ratios = ratios.slice(0, numStops);
var color;
var alpha;
var ratio;
for (var i = 0; i < numStops; i++)
{
color = colors[i];
colors[i] = !isNaN(color) ? Math.min(Math.max(Math.floor(color), 0x000000), 0xFFFFFF) : 0x000000;
alpha = alphas[i];
alphas[i] = !isNaN(alpha) ? Math.min(Math.max(alpha, 0), 1) : 1;
ratio = ratios[i];
ratios[i] = !isNaN(ratio) ? Math.min(Math.max(ratio, 0), 1) : 0;
}
if (matrix)
{
matrix = new Matrix(matrix.a * 1, matrix.b * 1, matrix.c * 1, matrix.d * 1, matrix.tx * 1, matrix.ty * 1);
if ((matrix.tx - matrix.tx) !== 0)
matrix.tx = 0;
if ((matrix.ty - matrix.ty) !== 0)
matrix.ty = 0;
if (!matrix.hasInverse())
matrix = null;
}
focalPointRatio = !isNaN(focalPointRatio) ? Math.min(Math.max(focalPointRatio, -1), 1) : 0;
var fillCommands = this._fillCommands = [];
fillCommands.push({ name: "gradientFill", type: type, colors: colors, alphas: alphas, ratios: ratios, matrix: matrix, focalPointRatio: focalPointRatio });
fillCommands.push({ name: "moveTo", x: this._penX, y: this._penY });
};
this.beginImageFill = function(image, matrix, repeat)
{
};
this.endFill = function()
{
if (!this._fillCommands)
return;
this._drawingStack.push(this._fillCommands);
this._fillCommands = null;
this._draw();
};
this.moveTo = function(x, y)
{
x *= 1;
if ((x - x) !== 0)
x = 0;
y *= 1;
if ((y - y) !== 0)
y = 0;
this._penX = x;
this._penY = y;
var command = { name: "moveTo", x: x, y: y };
if (this._strokeCommands)
this._strokeCommands.push(command);
if (this._fillCommands)
this._fillCommands.push(command);
};
this.lineTo = function(x, y)
{
x *= 1;
if ((x - x) !== 0)
x = 0;
y *= 1;
if ((y - y) !== 0)
y = 0;
this._penX = x;
this._penY = y;
var command = { name: "lineTo", x: x, y: y };
if (this._strokeCommands)
this._strokeCommands.push(command);
if (this._fillCommands)
this._fillCommands.push(command);
};
this.curveTo = function(controlX, controlY, anchorX, anchorY)
{
controlX *= 1;
if ((controlX - controlX) !== 0)
controlX = 0;
controlY *= 1;
if ((controlY - controlY) !== 0)
controlY = 0;
anchorX *= 1;
if ((anchorX - anchorX) !== 0)
anchorX = 0;
anchorY *= 1;
if ((anchorY - anchorY) !== 0)
anchorY = 0;
this._penX = anchorX;
this._penY = anchorY;
var command = { name: "curveTo", controlX: controlX, controlY: controlY, anchorX: anchorX, anchorY: anchorY };
if (this._strokeCommands)
this._strokeCommands.push(command);
if (this._fillCommands)
this._fillCommands.push(command);
};
this.clear = function()
{
this._strokeCommands = null;
this._fillCommands = null;
this._drawingStack = [];
this._draw(true);
};
// Private Methods
this._draw = function(redraw)
{
var context = this._context;
if (!context)
return;
if (redraw == true)
{
this._drawingStackIndex = 0;
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
context.beginPath();
}
var drawingStack = this._drawingStack;
var drawingStackSize = drawingStack.length;
var commands;
var i;
for (i = this._drawingStackIndex; i < drawingStackSize; i++)
{
commands = drawingStack[i];
switch (commands[0].name)
{
case "solidStroke":
case "gradientStroke":
this._drawStroke(commands);
break;
case "solidFill":
case "gradientFill":
this._drawFill(commands);
break;
}
}
this._drawingStackIndex = i;
};
this._drawStroke = function(commands)
{
var context = this._context;
if (!context)
return;
var numCommands = commands.length;
var command = commands[0];
var strokeStyle = command.strokeStyle;
var offset = strokeStyle.pixelHinting ? (strokeStyle.thickness % 2) / 2 : 0;
var hasPath = false;
var startX;
var startY;
var endX;
var endY;
var gradient;
var numStops;
var colors;
var alphas;
var ratios;
var color;
var alpha;
var ratio;
var matrix;
var i;
context.beginPath();
for (i = 1; i < numCommands; i++)
{
command = commands[i];
if (command.name === "moveTo")
{
if (hasPath && (startX === endX) && (startY === endY))
context.closePath();
hasPath = false;
startX = command.x;
startY = command.y;
context.moveTo(startX + offset, startY + offset);
}
else if (command.name === "lineTo")
{
hasPath = true;
endX = command.x;
endY = command.y;
context.lineTo(endX + offset, endY + offset);
}
else if (command.name === "curveTo")
{
hasPath = true;
endX = command.anchorX;
endY = command.anchorY;
context.quadraticCurveTo(command.controlX + offset, command.controlY + offset, endX + offset, endY + offset);
}
}
if (hasPath && (startX === endX) && (startY === endY))
context.closePath();
context.save();
context.lineWidth = strokeStyle.thickness;
context.lineCap = (strokeStyle.caps === "none") ? "butt" : strokeStyle.caps;
context.lineJoin = strokeStyle.joints;
context.miterLimit = strokeStyle.miterLimit;
command = commands[0];
if (command.name === "solidStroke")
{
color = command.color;
alpha = command.alpha;
context.strokeStyle = "rgba(" + ((color >> 16) & 0xFF) + ", " + ((color >> 8) & 0xFF) + ", " + (color & 0xFF) + ", " + alpha + ")";
}
else if (command.name === "gradientStroke")
{
if (command.type === "radial")
gradient = context.createRadialGradient(0.5 + 0.49 * command.focalPointRatio, 0.5, 0, 0.5, 0.5, 0.5);
else
gradient = context.createLinearGradient(0, 0, 1, 0);
colors = command.colors;
alphas = command.alphas;
ratios = command.ratios;
numStops = colors.length;
for (i = 0; i < numStops; i++)
{
color = colors[i];
alpha = alphas[i];
ratio = ratios[i];
gradient.addColorStop(ratio, "rgba(" + ((color >> 16) & 0xFF) + ", " + ((color >> 8) & 0xFF) + ", " + (color & 0xFF) + ", " + alpha + ")");
}
matrix = command.matrix;
if (matrix)
context.setTransform(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty);
context.strokeStyle = gradient;
}
context.stroke();
context.restore();
context.beginPath();
};
this._drawFill = function(commands)
{
var context = this._context;
if (!context)
return;
var numCommands = commands.length;
var command;
var gradient;
var numStops;
var colors;
var alphas;
var ratios;
var color;
var alpha;
var ratio;
var matrix;
var i;
context.beginPath();
for (i = 1; i < numCommands; i++)
{
command = commands[i];
if (command.name === "moveTo")
context.moveTo(command.x, command.y);
else if (command.name === "lineTo")
context.lineTo(command.x, command.y);
else if (command.name === "curveTo")
context.quadraticCurveTo(command.controlX, command.controlY, command.anchorX, command.anchorY);
}
context.save();
command = commands[0];
if (command.name === "solidFill")
{
color = command.color;
alpha = command.alpha;
context.fillStyle = "rgba(" + ((color >> 16) & 0xFF) + ", " + ((color >> 8) & 0xFF) + ", " + (color & 0xFF) + ", " + alpha + ")";
}
else if (command.name === "gradientFill")
{
if (command.type === "radial")
gradient = context.createRadialGradient(0.5 + 0.49 * command.focalPointRatio, 0.5, 0, 0.5, 0.5, 0.5);
else
gradient = context.createLinearGradient(0, 0, 1, 0);
colors = command.colors;
alphas = command.alphas;
ratios = command.ratios;
numStops = colors.length;
for (i = 0; i < numStops; i++)
{
color = colors[i];
alpha = alphas[i];
ratio = ratios[i];
gradient.addColorStop(ratio, "rgba(" + ((color >> 16) & 0xFF) + ", " + ((color >> 8) & 0xFF) + ", " + (color & 0xFF) + ", " + alpha + ")");
}
matrix = command.matrix;
if (matrix)
context.setTransform(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty);
context.fillStyle = gradient;
}
context.fill();
context.restore();
context.beginPath();
};
});
});
jg_namespace("jgatt.graphics", function()
{
this.IBrush = jg_extend(Object, function(IBrush, base)
{
// Public Methods
this.beginBrush = function(graphics, matrix, bounds)
{
};
this.endBrush = function()
{
};
this.moveTo = function(x, y)
{
};
this.lineTo = function(x, y)
{
};
this.curveTo = function(controlX, controlY, anchorX, anchorY)
{
};
});
});
jg_namespace("jgatt.graphics", function()
{
var Matrix = jg_import("jgatt.geom.Matrix");
var Point = jg_import("jgatt.geom.Point");
var Graphics = jg_import("jgatt.graphics.Graphics");
var IBrush = jg_import("jgatt.graphics.IBrush");
this.AbstractBrush = jg_extend(IBrush, function(AbstractBrush, base)
{
// Private Properties
this._commands = null;
this._graphics = null;
this._matrix = null;
this._bounds = null;
// Public Methods
this.beginBrush = function(graphics, matrix, bounds)
{
if (!graphics)
throw new Error("Parameter graphics must be non-null.");
if (!(graphics instanceof Graphics))
throw new Error("Parameter graphics must be an instance of jgatt.graphics.Graphics.");
if ((matrix != null) && !(matrix instanceof Matrix))
throw new Error("Parameter matrix must be an instance of jgatt.geom.Matrix.");
if ((bounds != null) && !(bounds instanceof Array))
throw new Error("Parameter bounds must be an array.");
this.endBrush();
this._commands = [];
this._graphics = graphics;
this._matrix = matrix ? matrix.clone() : null;
if (bounds)
{
var bounds2 = this._bounds = [];
var numPoints = bounds.length;
var point;
for (var i = 0; i < numPoints; i++)
{
point = bounds[i];
if (point instanceof Point)
bounds2.push(point.clone());
}
}
};
this.endBrush = function()
{
if (!this._graphics)
return;
this.draw(this._commands, this._graphics, this._matrix, this._bounds);
this._commands = null;
this._graphics = null;
this._matrix = null;
this._bounds = null;
};
this.moveTo = function(x, y)
{
if (!this._graphics)
return;
this._commands.push({ name: "moveTo", x: x, y: y });
};
this.lineTo = function(x, y)
{
if (!this._graphics)
return;
this._commands.push({ name: "lineTo", x: x, y: y });
};
this.curveTo = function(controlX, controlY, anchorX, anchorY)
{
if (!this._graphics)
return;
this._commands.push({ name: "curveTo", controlX: controlX, controlY: controlY, anchorX: anchorX, anchorY: anchorY });
};
// Protected Methods
this.draw = function(commands, graphics, matrix, bounds)
{
};
});
});
jg_namespace("jgatt.graphics", function()
{
var Matrix = jg_import("jgatt.geom.Matrix");
var Point = jg_import("jgatt.geom.Point");
var AbstractBrush = jg_import("jgatt.graphics.AbstractBrush");
this.AbstractTileBrush = jg_extend(AbstractBrush, function(AbstractTileBrush, base)
{
// Private Properties
this._stretchMode = "fill";
this._alignmentX = 0.5;
this._alignmentY = 0.5;
this._tileTransform = null;
this._renderTransform = null;
this._fitToDrawing = false;
// Constructor
this.constructor = function(stretchMode, alignmentX, alignmentY, tileTransform, renderTransform, fitToDrawing)
{
base.constructor.call(this);
switch (stretchMode)
{
case "none":
case "fill":
case "uniform":
case "uniformToFill":
case "uniformToWidth":
case "uniformToHeight":
stretchMode += "";
break;
default:
stretchMode = "fill";
break;
}
this._stretchMode = stretchMode;
this._alignmentX = ((alignmentX != null) && !isNaN(alignmentX)) ? (alignmentX * 1) : 0.5;
this._alignmentY = ((alignmentY != null) && !isNaN(alignmentY)) ? (alignmentY * 1) : 0.5;
this._tileTransform = (tileTransform instanceof Matrix) ? tileTransform.clone() : null;
this._renderTransform = (renderTransform instanceof Matrix) ? renderTransform.clone() : null;
this._fitToDrawing = (fitToDrawing == true);
};
// Public Getters/Setters
this.getStretchMode = function()
{
return this._stretchMode;
};
this.setStretchMode = function(value)
{
switch (value)
{
case "none":
case "fill":
case "uniform":
case "uniformToFill":
case "uniformToWidth":
case "uniformToHeight":
value += "";
break;
default:
value = "fill";
break;
}
this._stretchMode = value;
};
this.getAlignmentX = function()
{
return this._alignmentX;
};
this.setAlignmentX = function(value)
{
this._alignmentX = ((value != null) && !isNaN(value)) ? (value * 1) : this._alignmentX;
};
this.getAlignmentY = function()
{
return this._alignmentY;
};
this.setAlignmentY = function(value)
{
this._alignmentY = ((value != null) && !isNaN(value)) ? (value * 1) : this._alignmentY;
};
this.getTileTransform = function()
{
return this._tileTransform ? this._tileTransform.clone() : null;
};
this.setTileTransform = function(value)
{
this._tileTransform = (value instanceof Matrix) ? value.clone() : null;
};
this.getRenderTransform = function()
{
return this._renderTransform ? this._renderTransform.clone() : null;
};
this.setRenderTransform = function(value)
{
this._renderTransform = (value instanceof Matrix) ? value.clone() : null;
};
this.getFitToDrawing = function()
{
return this._fitToDrawing;
};
this.setFitToDrawing = function(value)
{
this._fitToDrawing = (value == true);
};
// Protected Methods
this.computeTileMatrix = function(tileWidth, tileHeight, matrix, bounds, commands)
{
var tileMatrix;
var tileTransform = this._tileTransform;
if (tileTransform)
{
tileMatrix = tileTransform.clone();
var p1 = new Point(0, 0);
var p2 = new Point(tileWidth, 0);
var p3 = new Point(tileWidth, tileHeight);
var p4 = new Point(0, tileHeight);
p1 = tileMatrix.transformPoint(p1);
p2 = tileMatrix.transformPoint(p2);
p3 = tileMatrix.transformPoint(p3);
p4 = tileMatrix.transformPoint(p4);
var left = Math.min(p1.x, p2.x, p3.x, p4.x);
var right = Math.max(p1.x, p2.x, p3.x, p4.x);
var top = Math.min(p1.y, p2.y, p3.y, p4.y);
var bottom = Math.max(p1.y, p2.y, p3.y, p4.y);
tileWidth = right - left;
tileHeight = bottom - top;
tileMatrix.translate(-left, -top);
}
else
{
tileMatrix = new Matrix();
}
var invertedMatrix;
if (matrix && matrix.hasInverse())
{
invertedMatrix = matrix.clone();
invertedMatrix.invert();
}
var minX = Infinity;
var minY = Infinity;
var maxX = -Infinity;
var maxY = -Infinity;
var point;
var i;
if (bounds && !this._fitToDrawing)
{
var numPoints = bounds.length;
for (i = 0; i < numPoints; i++)
{
point = bounds[i];
if (invertedMatrix)
point = invertedMatrix.transformPoint(point);
minX = Math.min(point.x, minX);
minY = Math.min(point.y, minY);
maxX = Math.max(point.x, maxX);
maxY = Math.max(point.y, maxY);
}
}
else
{
var numCommands = commands.length;
var command;
for (i = 0; i < numCommands; i++)
{
command = commands[i];
if (command.name == "moveTo")
point = new Point(command.x, command.y);
else if (command.name == "lineTo")
point = new Point(command.x, command.y);
else if (command.name == "curveTo")
point = new Point(command.anchorX, command.anchorY); // control point tangents need to be properly computed
else
continue;
if (invertedMatrix)
point = invertedMatrix.transformPoint(point);
minX = Math.min(point.x, minX);
minY = Math.min(point.y, minY);
maxX = Math.max(point.x, maxX);
maxY = Math.max(point.y, maxY);
}
}
if (minX == Infinity)
minX = minY = maxX = maxY = 0;
var width = maxX - minX;
var height = maxY - minY;
var scaleX;
var scaleY;
var offsetX;
var offsetY;
switch (this._stretchMode)
{
case "none":
offsetX = (width - tileWidth) * this._alignmentX;
offsetY = (height - tileHeight) * this._alignmentY;
tileMatrix.translate(offsetX, offsetY);
break;
case "uniform":
scaleX = (tileWidth > 0) ? (width / tileWidth) : 1;
scaleY = (tileHeight > 0) ? (height / tileHeight) : 1;
scaleX = scaleY = Math.min(scaleX, scaleY);
offsetX = (width - tileWidth * scaleX) * this._alignmentX;
offsetY = (height - tileHeight * scaleY) * this._alignmentY;
tileMatrix.scale(scaleX, scaleY);
tileMatrix.translate(offsetX, offsetY);
break;
case "uniformToFill":
scaleX = (tileWidth > 0) ? (width / tileWidth) : 1;
scaleY = (tileHeight > 0) ? (height / tileHeight) : 1;
scaleX = scaleY = Math.max(scaleX, scaleY);
offsetX = (width - tileWidth * scaleX) * this._alignmentX;
offsetY = (height - tileHeight * scaleY) * this._alignmentY;
tileMatrix.scale(scaleX, scaleY);
tileMatrix.translate(offsetX, offsetY);
break;
case "uniformToWidth":
scaleX = scaleY = (tileWidth > 0) ? (width / tileWidth) : 1;
offsetX = (width - tileWidth * scaleX) * this._alignmentX;
offsetY = (height - tileHeight * scaleY) * this._alignmentY;
tileMatrix.scale(scaleX, scaleY);
tileMatrix.translate(offsetX, offsetY);
break;
case "uniformToHeight":
scaleX = scaleY = (tileHeight > 0) ? (height / tileHeight) : 1;
offsetX = (width - tileWidth * scaleX) * this._alignmentX;
offsetY = (height - tileHeight * scaleY) * this._alignmentY;
tileMatrix.scale(scaleX, scaleY);
tileMatrix.translate(offsetX, offsetY);
break;
default: // "fill"
scaleX = (tileWidth > 0) ? (width / tileWidth) : 1;
scaleY = (tileHeight > 0) ? (height / tileHeight) : 1;
tileMatrix.scale(scaleX, scaleY);
break;
}
var renderTransform = this._renderTransform;
if (renderTransform)
tileMatrix.concat(renderTransform);
tileMatrix.translate(minX, minY);
if (matrix)
tileMatrix.concat(matrix);
return tileMatrix;
};
});
});
jg_namespace("jgatt.graphics", function()
{
var Matrix = jg_import("jgatt.geom.Matrix");
var AbstractTileBrush = jg_import("jgatt.graphics.AbstractTileBrush");
this.GradientFillBrush = jg_extend(AbstractTileBrush, function(GradientFillBrush, base)
{
// Private Properties
this._type = "linear";
this._colors = null;
this._alphas = null;
this._ratios = null;
this._focalPointRatio = 0;
this._gradientWidth = 100;
this._gradientHeight = 100;
// Constructor
this.constructor = function(type, colors, alphas, ratios, focalPointRatio)
{
base.constructor.call(this);
this._type = ((type == "linear") || (type == "radial")) ? (type + "") : "linear";
this._colors = (colors instanceof Array) ? colors.concat() : [];
this._alphas = (alphas instanceof Array) ? alphas.concat() : [];
this._ratios = (ratios instanceof Array) ? ratios.concat() : [];
this._focalPointRatio = ((focalPointRatio != null) && !isNaN(focalPointRatio)) ? Math.min(Math.max(focalPointRatio, -1), 1) : 0;
this._gradientWidth = 100;
this._gradientHeight = 100;
};
// Public Getters/Setters
this.getType = function()
{
return this._type;
};
this.setType = function(value)
{
this._type = ((value == "linear") || (value == "radial")) ? (value + "") : this._type;
};
this.getColors = function()
{
return this._colors.concat();
};
this.setColors = function(value)
{
this._colors = (value instanceof Array) ? value.concat() : [];
};
this.getAlphas = function()
{
return this._alphas.concat();
};
this.setAlphas = function(value)
{
this._alphas = (value instanceof Array) ? value.concat() : [];
};
this.getRatios = function()
{
return this._ratios.concat();
};
this.setRatios = function(value)
{
this._ratios = (value instanceof Array) ? value.concat() : [];
};
this.getFocalPointRatio = function()
{
return this._focalPointRatio;
};
this.setFocalPointRatio = function(value)
{
this._focalPointRatio = ((value != null) && !isNaN(value)) ? Math.min(Math.max(value, -1), 1) : this._focalPointRatio;
};
this.getGradientWidth = function()
{
return this._gradientWidth;
};
this.setGradientWidth = function(value)
{
this._gradientWidth = ((value > 0) && (value < Infinity)) ? (value * 1) : this._gradientWidth;
};
this.getGradientHeight = function()
{
return this._gradientHeight;
};
this.setGradientHeight = function(value)
{
this._gradientHeight = ((value > 0) && (value < Infinity)) ? (value * 1) : this._gradientHeight;
};
// Protected Methods
this.draw = function(commands, graphics, matrix, bounds)
{
var gradientWidth = this._gradientWidth;
var gradientHeight = this._gradientHeight;
var tileMatrix = new Matrix(gradientWidth, 0, 0, gradientHeight);
tileMatrix.concat(this.computeTileMatrix(gradientWidth, gradientHeight, matrix, bounds, commands));
graphics.beginGradientFill(this._type, this._colors, this._alphas, this._ratios, tileMatrix, this._focalPointRatio);
var numCommands = commands.length;
var command;
for (var i = 0; i < numCommands; i++)
{
command = commands[i];
if (command.name == "moveTo")
graphics.moveTo(command.x, command.y);
else if (command.name == "lineTo")
graphics.lineTo(command.x, command.y);
else if (command.name == "curveTo")
graphics.curveTo(command.controlX, command.controlY, command.anchorX, command.anchorY);
}
graphics.endFill();
};
});
});
jg_namespace("jgatt.motion", function()
{
this.ITween = jg_extend(Object, function(ITween, base)
{
// Public Methods
this.beginTween = function()
{
// returns Boolean
};
this.endTween = function()
{
// returns Boolean
};
this.updateTween = function(position)
{
// returns Boolean
};
});
});
jg_namespace("jgatt.motion.easers", function()
{
this.IEaser = jg_extend(Object, function(IEaser, base)
{
// Public Methods
this.ease = function(position)
{
// returns Number
};
});
});
jg_namespace("jgatt.motion", function()
{
var ITween = jg_import("jgatt.motion.ITween");
var IEaser = jg_import("jgatt.motion.easers.IEaser");
this.AbstractTween = jg_extend(ITween, function(AbstractTween, base)
{
// Private Properties
this._easer = null;
this._isRunning = false;
// Constructor
this.constructor = function(easer)
{
if ((easer != null) && !(easer instanceof IEaser))
throw new Error("Parameter easer must be an instance of jgatt.motion.easers.IEaser");
this._easer = easer ? easer : null;
};
// Public Getters/Setters
this.getEaser = function()
{
return this._easer;
};
this.setEaser = function(value)
{
if ((value != null) && !(value instanceof IEaser))
throw new Error("Parameter easer must be an instance of jgatt.motion.easers.IEaser");
this._easer = value ? value : null;
this.endTween();
};
// Public Methods
this.beginTween = function()
{
this.endTween();
if (!this.beginTweenOverride())
return false;
this._isRunning = true;
//this.dispatchEvent(new TweenEvent(TweenEvent.BEGIN));
return true;
};
this.endTween = function()
{
if (!this._isRunning)
return false;
this.endTweenOverride();
this._isRunning = false;
//this.dispatchEvent(new TweenEvent(TweenEvent.END));
return true;
};
this.updateTween = function(position)
{
if (!this._isRunning)
return false;
var easer = this._easer;
if (easer)
position = easer.ease(position);
if (!this.updateTweenOverride(position))
return false;
//this.dispatchEvent(new TweenEvent(TweenEvent.UPDATE));
return true;
};
// Protected Methods
this.beginTweenOverride = function()
{
return false;
};
this.endTweenOverride = function()
{
};
this.updateTweenOverride = function(position)
{
return false;
};
});
});
jg_namespace("jgatt.motion", function()
{
var AbstractTween = jg_import("jgatt.motion.AbstractTween");
this.GroupTween = jg_extend(AbstractTween, function(GroupTween, base)
{
// Private Properties
this._tweens = null;
this._runningTweens = null;
// Constructor
this.constructor = function(tweens, easer)
{
base.constructor.call(this, easer);
if ((tweens != null) && !(tweens instanceof Array))
throw new Error("Parameter tweens must be an array.");
this._tweens = tweens ? tweens.concat() : [];
};
// Public Getters/Setters
this.getTweens = function()
{
return this._tweens.concat();
};
this.setTweens = function(value)
{
if ((value != null) && !(value instanceof Array))
throw new Error("Parameter tweens must be an array.");
this._tweens = value ? value.concat() : [];
this.endTween();
};
// Protected Methods
this.beginTweenOverride = function()
{
var runningTweens = [];
var tweens = this._tweens;
var tween;
for (var i = 0, l = tweens.length; i < l; i++)
{
tween = tweens[i];
if (tween.beginTween())
runningTweens.push(tween);
}
if (runningTweens.length == 0)
return false;
this._runningTweens = runningTweens;
return true;
};
this.endTweenOverride = function()
{
var runningTweens = this._runningTweens;
for (var i = 0, l = runningTweens.length; i < l; i++)
runningTweens[i].endTween();
this._runningTweens = null;
};
this.updateTweenOverride = function(position)
{
var runningTweens = this._runningTweens;
var numTweens = runningTweens.length;
var tween;
for (var i = 0; i < numTweens; i++)
{
tween = runningTweens[i];
if (!tween.updateTween(position))
{
tween.endTween();
runningTweens.splice(i, 1);
i--;
numTweens--;
}
}
return (runningTweens.length > 0);
};
});
});
jg_namespace("jgatt.motion.interpolators", function()
{
this.IInterpolator = jg_extend(Object, function(IInterpolator, base)
{
// Public Methods
this.interpolate = function(value1, value2, position)
{
// returns value
};
});
});
jg_namespace("jgatt.utils", function()
{
this.NumberUtils = jg_static(function(NumberUtils)
{
// Public Static Constants
NumberUtils.EPSILON = (function()
{
var eps = 1;
var temp = 1;
while ((1 + temp) > 1)
{
eps = temp;
temp /= 2;
}
return eps;
})();
NumberUtils.PRECISION = (function()
{
var prec = 0;
var temp = 9;
while ((temp % 10) == 9)
{
prec++;
temp = temp * 10 + 9;
}
return prec;
})();
// Public Static Methods
NumberUtils.parseNumber = function(value)
{
if (value == null)
return NaN;
switch (typeof value)
{
case "number":
return value;
case "string":
return value ? Number(value) : NaN;
case "boolean":
return value ? 1 : 0;
}
return NaN;
};
NumberUtils.toPrecision = function(n, precision)
{
precision = (precision !== undefined) ? precision : 0;
if (precision < 1)
precision = NumberUtils.PRECISION + precision;
if (precision < 1)
precision = 1;
else if (precision > 21)
precision = 21;
return Number(n.toPrecision(precision));
};
NumberUtils.toFixed = function(n, decimalDigits)
{
decimalDigits = (decimalDigits !== undefined) ? decimalDigits : 0;
if (decimalDigits < 0)
decimalDigits = 0;
else if (decimalDigits > 20)
decimalDigits = 20;
return Number(n.toFixed(decimalDigits));
};
NumberUtils.roundTo = function(n, units)
{
units = (units !== undefined) ? units : 1;
return NumberUtils.toPrecision(Math.round(n / units) * units, -1);
};
NumberUtils.minMax = function(n, min, max)
{
if (n < min)
n = min;
if (n > max)
n = max;
return n;
};
NumberUtils.maxMin = function(n, max, min)
{
if (n > max)
n = max;
if (n < min)
n = min;
return n;
};
NumberUtils.interpolate = function(n1, n2, f)
{
return n1 * (1 - f) + n2 * f;
};
NumberUtils.approxZero = function(n, threshold)
{
if (n == 0)
return true;
threshold = (threshold !== undefined) ? threshold : NaN;
if (isNaN(threshold))
threshold = NumberUtils.EPSILON;
return (n < 0) ? (-n < threshold) : (n < threshold);
};
NumberUtils.approxOne = function(n, threshold)
{
if (n == 1)
return true;
n -= 1;
threshold = (threshold !== undefined) ? threshold : NaN;
if (isNaN(threshold))
threshold = NumberUtils.EPSILON;
return (n < 0) ? (-n < threshold) : (n < threshold);
};
NumberUtils.approxEqual = function(n1, n2, threshold)
{
if (n1 == n2)
return true;
n1 -= n2;
threshold = (threshold !== undefined) ? threshold : NaN;
if (isNaN(threshold))
threshold = NumberUtils.EPSILON;
return (n < 0) ? (-n < threshold) : (n < threshold);
};
NumberUtils.approxLessThan = function(n1, n2, threshold)
{
return ((n1 < n2) && !NumberUtils.approxEqual(n1, n2, threshold));
};
NumberUtils.approxLessThanOrEqual = function(n1, n2, threshold)
{
return ((n1 < n2) || NumberUtils.approxEqual(n1, n2, threshold));
};
NumberUtils.approxGreaterThan = function(n1, n2, threshold)
{
return ((n1 > n2) && !NumberUtils.approxEqual(n1, n2, threshold));
};
NumberUtils.approxGreaterThanOrEqual = function(n1, n2, threshold)
{
return ((n1 > n2) || NumberUtils.approxEqual(n1, n2, threshold));
};
});
});
jg_namespace("jgatt.motion.interpolators", function()
{
var IInterpolator = jg_import("jgatt.motion.interpolators.IInterpolator");
var NumberUtils = jg_import("jgatt.utils.NumberUtils");
this.NumberInterpolator = jg_extend(IInterpolator, function(NumberInterpolator, base)
{
// Public Properties
this.snap = 0;
// Constructor
this.constructor = function(snap)
{
this.snap = (snap !== undefined) ? snap : 0;
};
// Public Methods
this.interpolate = function(value1, value2, position)
{
var number1 = Number(value1);
var number2 = Number(value2);
var number = NumberUtils.interpolate(number1, number2, position);
var snap = this.snap;
if (snap > 0)
number = Math.round(number / snap) * snap;
return number;
};
});
});
jg_namespace("jgatt.utils", function()
{
this.Dictionary = jg_extend(Object, function(Dictionary, base)
{
// Pri