* added jsdoc compatible inline documentation

* fixed constructor of helma.Color to behave as expected when passing a color name or decimal value
This commit is contained in:
grob 2007-02-08 13:14:32 +00:00
parent 2e2603c4e0
commit 1cd44d18a3
2 changed files with 138 additions and 70 deletions

59
helma/Chart.js vendored
View file

@ -8,28 +8,43 @@
* *
* Copyright 1998-2006 Helma Software. All Rights Reserved. * Copyright 1998-2006 Helma Software. All Rights Reserved.
* *
* $RCSfile: helma.Chart.js,v $ * $RCSfile: Chart.js,v $
* $Author: czv $ * $Author: czv $
* $Revision: 1.6 $ * $Revision: 1.2 $
* $Date: 2006/04/18 13:06:58 $ * $Date: 2006/04/24 07:02:17 $
*/ */
/**
* @fileoverview Fields and methods of the helma.Chart prototype
*/
// take care of any dependencies // take care of any dependencies
app.addRepository('modules/helma/jxl.jar'); app.addRepository('modules/helma/jxl.jar');
/**
* Define the global namespace if not existing
*/
if (!global.helma) { if (!global.helma) {
global.helma = {}; global.helma = {};
} }
// /**
// chart package by tobi schaefer * Creates a new instance of helma.Chart
// needs andy khan's java excel api: download jxl.jar at * @class Instances of this class are capable of reading
// http://www.andykhan.com/jexcelapi/ * Excel spreadsheets and rendering them as XHTML table. Internally
// * helma.Chart uses the <a href="http://www.jexcelapi.org/ ">Java Excel API</a>
* by <a href="http://www.andykhan.com/">Andy Khan</a>.
* @param {String} fpath The path to the spreadsheet file
* @param {String} prefix An optional prefix to use for all
* stylesheet classes within the rendered table
* @param {String} sheetName The name of the sheet within the
* spreadsheet file to render. If this argument is omitted, the
* first sheet is rendered.
* @returns A newly created helma.Chart instance.
* @constructor
* @author Tobi Schaefer
*/
helma.Chart = function(fpath, prefix, sheetName) { helma.Chart = function(fpath, prefix, sheetName) {
var JXLPKG = Packages.jxl.Workbook; var JXLPKG = Packages.jxl.Workbook;
var JXLPKGNAME = "jxl.jar"; var JXLPKGNAME = "jxl.jar";
@ -47,7 +62,7 @@ helma.Chart = function(fpath, prefix, sheetName) {
"[" + JXLPKGURL + "]"); "[" + JXLPKGURL + "]");
} }
function getCellStyle(c) { var getCellStyle = function(c) {
if (!c) if (!c)
return; return;
var result = new Object(); var result = new Object();
@ -66,15 +81,19 @@ helma.Chart = function(fpath, prefix, sheetName) {
return result; return result;
} }
if (sheetName) if (sheetName) {
var sheet = workbook.getSheet(sheetName); var sheet = workbook.getSheet(sheetName);
else } else {
var sheet = workbook.getSheet(0); var sheet = workbook.getSheet(0);
}
if (!sheet) if (!sheet)
return; return;
prefix = prefix ? prefix + "_" : "chart_"; prefix = prefix ? prefix + "_" : "chart_";
/**
* Renders the Excel spreadsheet as XHTML table.
*/
this.render = function() { this.render = function() {
res.write('<table border="0" cellspacing="1" class="' + res.write('<table border="0" cellspacing="1" class="' +
prefix + 'table">\n'); prefix + 'table">\n');
@ -128,12 +147,18 @@ helma.Chart = function(fpath, prefix, sheetName) {
workbook.close(); workbook.close();
}; };
/**
* Returns the spreadsheet as rendered XHTML table.
* @returns The rendered spreadsheet table
* @type String
*/
this.renderAsString = function() { this.renderAsString = function() {
res.push(); res.push();
this.render(); this.render();
return res.pop(); return res.pop();
}; };
/** @ignore */
this.toString = function() { this.toString = function() {
return "[helma.Chart " + file + "]"; return "[helma.Chart " + file + "]";
}; };
@ -145,11 +170,17 @@ helma.Chart = function(fpath, prefix, sheetName) {
} }
/** @ignore */
helma.Chart.toString = function() { helma.Chart.toString = function() {
return "[helma.Chart]"; return "[helma.Chart]";
}; };
/**
* A simple example for using helma.Chart that renders
* the passed file as XHTML table to response.
* @param {String} file The path to the Excel spreadsheet file
*/
helma.Chart.example = function(file) { helma.Chart.example = function(file) {
// var file = "/path/to/file.xls"; // var file = "/path/to/file.xls";
var chart = new helma.Chart(file); var chart = new helma.Chart(file);

View file

@ -8,33 +8,57 @@
* *
* Copyright 1998-2006 Helma Software. All Rights Reserved. * Copyright 1998-2006 Helma Software. All Rights Reserved.
* *
* $RCSfile: helma.Color.js,v $ * $RCSfile: Color.js,v $
* $Author: hannes $ * $Author: czv $
* $Revision: 1.5 $ * $Revision: 1.2 $
* $Date: 2006/04/18 13:06:58 $ * $Date: 2006/04/24 07:02:17 $
*/ */
/**
* @fileoverview Fields and methods of the helma.Chart prototype
*/
// take care of any dependencies
app.addRepository("modules/core/String.js");
/**
* Define the global namespace if not existing
*/
if (!global.helma) { if (!global.helma) {
global.helma = {}; global.helma = {};
} }
/** /**
* constructor for a color object * Constructs a new instance of helma.Color.
* @param IntegerOrRgbObject the red fraction or an rgb() object * @class Instances of this class provide methods for
* @param Integer the green fraction * converting HTML color names into their corresponding
* @param Integer the blue fraction * RGB values and vice versa, or retrieving single RGB color values.
* @return the resulting color object * @param {Number|String} R Either the red fraction of the color,
* or the name of the color.
* @param {Number} G The green fraction
* @param {Number} B The blue fraction
* @returns A newly created helma.Color instance
* @constructor
*/ */
helma.Color = function(R, G, B) { helma.Color = function(R, G, B) {
var value = null, name, hex, rgb; var value = null;
var name = null;
var hex = null;
var rgb = null;
/** /**
* return the decimal value of a color object * Returns the decimal value of this color, or of a specified
* @return Integer the decimal value * color channel.
* @param {String} channel An optional color channel which
* decimal value should be returned. Must be either "red",
* "green" or "blue". If no channel is specified this
* method returns the decimal value of the color itself.
* @returns The decimal value of this color or a single channel.
* @type Number
*/ */
this.valueOf = function() { this.valueOf = function(channel) {
if (arguments.length > 0) { if (channel) {
if (!rgb) { if (!rgb) {
var compose = function(n, bits) { var compose = function(n, bits) {
var div = Math.pow(2, bits); var div = Math.pow(2, bits);
@ -46,16 +70,18 @@ helma.Color = function(R, G, B) {
red: compose(remainder, 16), red: compose(remainder, 16),
green: compose(remainder, 8), green: compose(remainder, 8),
blue: compose(remainder, 0) blue: compose(remainder, 0)
};
} }
} return rgb[channel];
return rgb[arguments[0]];
} }
return value; return value;
}; };
/** /**
* return the hexidecimal value of a color object * Returns the hexidecimal value of this color (without
* @return String the hexidecimal value * a leading hash sign).
* @returns The hexidecimal value of this color
* @type String
*/ */
this.toString = function() { this.toString = function() {
if (!value) if (!value)
@ -65,15 +91,19 @@ helma.Color = function(R, G, B) {
return hex; return hex;
}; };
/** /**
* return the trivial name of this color * Returns the trivial name of this color
* @return String the trivial name * @returns The trivial name of this color
* @type String
*/ */
this.getName = function() { this.getName = function() {
return helma.Color.COLORVALUES[value]; return helma.Color.COLORVALUES[value];
}; };
// the main code /**
* Main constructor body
*/
if (arguments.length % 2 == 0) if (arguments.length % 2 == 0)
throw("Insufficient arguments for creating Color"); throw("Insufficient arguments for creating Color");
if (arguments.length == 1) { if (arguments.length == 1) {
@ -83,18 +113,21 @@ helma.Color = function(R, G, B) {
R = R.toLowerCase(); R = R.toLowerCase();
if (helma.Color.COLORNAMES[R]) { if (helma.Color.COLORNAMES[R]) {
this.name = R; this.name = R;
R = helma.Color.COLORNAMES[R]; value = helma.Color.COLORNAMES[R];
} else if (helma.Color.COLORVALUES[R]) { } else {
this.name = helma.Color.COLORVALUES[R]; if (R.startsWith("#")) {
} else if (R.startsWith("#"))
R = R.substring(1); R = R.substring(1);
value = parseInt(R, 16);
} }
} else value = parseInt(R, 16);
this.name = helma.Color.COLORVALUES[value];
}
}
} else {
value = R * Math.pow(2, 16) + G * Math.pow(2, 8) + B; value = R * Math.pow(2, 16) + G * Math.pow(2, 8) + B;
}
if (value == null || isNaN(value)) if (value == null || isNaN(value))
throw("Cannot create Color from " + R); throw("helma.Color: invalid argument " + R);
for (var i in this) for (var i in this)
this.dontEnum(i); this.dontEnum(i);
@ -104,30 +137,28 @@ helma.Color = function(R, G, B) {
/** /**
* create a helma.Color from trivial name * Creates a new helma.Color instance based on a color name.
* @param String the color's name (like "darkseagreen") * @param {String} name The color name (eg. "darkseagreen")
* @return helma.Color the resulting color object * @returns An instance of helma.Color representing the color specified
* @type helma.Color
*/ */
helma.Color.fromName = function(name) { helma.Color.fromName = function(name) {
var value = helma.Color.COLORNAMES[name.toLowerCase()]; var value = helma.Color.COLORNAMES[name.toLowerCase()];
if (!value) return new helma.Color(value || 0);
value = 0;
return new helma.Color(value);
}; };
/** /**
* transform a hsl representation of a color * Creates a new helma.Color instance based on a HSL color
* to the equivalent decimal value * representation. This method is adapted from the HSLtoRGB
* @param Integer the hue fraction * conversion method as described at
* @param Integer the saturation fraction * <a href="http://www1.tip.nl/~t876506/ColorDesign.html#hr">http://www1.tip.nl/~t876506/ColorDesign.html#hr</a>.
* @param Integer the lightness fraction * @param {Number} H The hue fraction of the color definition
* @return Object the resulting color object * @param {Number} S The saturation fraction
* * @param {Number} L The lightness fraction
* note: this function is adapted from the * @returns An instance of helma.Color representing the corresponding
* HSLtoRGB conversion method as described at * RGB color definition.
* http://www1.tip.nl/~t876506/ColorDesign.html#hr * @type helma.Color
* (thanks!)
*/ */
helma.Color.fromHsl = function(H,S,L) { helma.Color.fromHsl = function(H,S,L) {
function H1(H,S,L) { function H1(H,S,L) {
@ -182,7 +213,7 @@ helma.Color.fromHsl = function(H,S,L) {
else if (H < 5/6) rgb = H5(H,S,L); else if (H < 5/6) rgb = H5(H,S,L);
else rgb = H6(H,S,L); else rgb = H6(H,S,L);
return new Color( return new helma.Color(
Math.round(rgb[0]*255), Math.round(rgb[0]*255),
Math.round(rgb[1]*255), Math.round(rgb[1]*255),
Math.round(rgb[2]*255) Math.round(rgb[2]*255)
@ -191,7 +222,9 @@ helma.Color.fromHsl = function(H,S,L) {
/** /**
* object containig the hex values of named colors * Contains the hexadecimal values of named colors.
* @type Object
* @final
*/ */
helma.Color.COLORNAMES = { helma.Color.COLORNAMES = {
black: 0x000000, black: 0x000000,
@ -334,14 +367,18 @@ helma.Color.COLORNAMES = {
/** /**
* object containig the color names for specific hex values * Contains the color names for specific hex values
* @type Object
* @final
*/ */
helma.Color.COLORVALUES = {}; helma.Color.COLORVALUES = {};
for (var i in helma.Color.COLORNAMES) for (var i in helma.Color.COLORNAMES) {
helma.Color.COLORVALUES[helma.Color.COLORNAMES[i]] = i; helma.Color.COLORVALUES[helma.Color.COLORNAMES[i]] = i;
}
/** @ignore */
helma.Color.toString = function() { helma.Color.toString = function() {
return "[helma.Color]"; return "[helma.Color]";
}; };