Some JSDoc
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2731 eee81c28-f429-11dd-99c0-75d572ba1ddd
This commit is contained in:
160
editor/draw.js
160
editor/draw.js
@@ -1,5 +1,5 @@
|
|||||||
/*globals $, svgedit*/
|
/*globals $, svgedit*/
|
||||||
/*jslint vars: true, eqeq: true*/
|
/*jslint vars: true, eqeq: true, todo: true*/
|
||||||
/**
|
/**
|
||||||
* Package: svgedit.draw
|
* Package: svgedit.draw
|
||||||
*
|
*
|
||||||
@@ -32,28 +32,35 @@ var randomize_ids = RandomizeModes.LET_DOCUMENT_DECIDE;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This class encapsulates the concept of a layer in the drawing
|
* This class encapsulates the concept of a layer in the drawing
|
||||||
* @param name {String} Layer name
|
* @param {String} name - Layer name
|
||||||
* @param child {SVGGElement} Layer SVG group.
|
* @param {SVGGElement} child - Layer SVG group.
|
||||||
*/
|
*/
|
||||||
svgedit.draw.Layer = function(name, group) {
|
svgedit.draw.Layer = function(name, group) {
|
||||||
this.name_ = name;
|
this.name_ = name;
|
||||||
this.group_ = group;
|
this.group_ = group;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {string} The layer name
|
||||||
|
*/
|
||||||
svgedit.draw.Layer.prototype.getName = function() {
|
svgedit.draw.Layer.prototype.getName = function() {
|
||||||
return this.name_;
|
return this.name_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {SVGGElement} The layer SVG group
|
||||||
|
*/
|
||||||
svgedit.draw.Layer.prototype.getGroup = function() {
|
svgedit.draw.Layer.prototype.getGroup = function() {
|
||||||
return this.group_;
|
return this.group_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Called to ensure that drawings will or will not have randomized ids.
|
/**
|
||||||
// The currentDrawing will have its nonce set if it doesn't already.
|
* Called to ensure that drawings will or will not have randomized ids.
|
||||||
//
|
* The currentDrawing will have its nonce set if it doesn't already.
|
||||||
// Params:
|
* @param {boolean} enableRandomization - flag indicating if documents should have randomized ids
|
||||||
// enableRandomization - flag indicating if documents should have randomized ids
|
* @param {svgedit.draw.Drawing} currentDrawing
|
||||||
|
*/
|
||||||
svgedit.draw.randomizeIds = function(enableRandomization, currentDrawing) {
|
svgedit.draw.randomizeIds = function(enableRandomization, currentDrawing) {
|
||||||
randomize_ids = enableRandomization === false ?
|
randomize_ids = enableRandomization === false ?
|
||||||
RandomizeModes.NEVER_RANDOMIZE :
|
RandomizeModes.NEVER_RANDOMIZE :
|
||||||
@@ -68,12 +75,10 @@ svgedit.draw.randomizeIds = function(enableRandomization, currentDrawing) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This class encapsulates the concept of a SVG-edit drawing
|
* This class encapsulates the concept of a SVG-edit drawing
|
||||||
*
|
* @param {SVGSVGElement} svgElem - The SVG DOM Element that this JS object
|
||||||
* @param svgElem {SVGSVGElement} The SVG DOM Element that this JS object
|
|
||||||
* encapsulates. If the svgElem has a se:nonce attribute on it, then
|
* encapsulates. If the svgElem has a se:nonce attribute on it, then
|
||||||
* IDs will use the nonce as they are generated.
|
* IDs will use the nonce as they are generated.
|
||||||
* @param opt_idPrefix {String} The ID prefix to use. Defaults to "svg_"
|
* @param {String=svg_} [opt_idPrefix] - The ID prefix to use.
|
||||||
* if not specified.
|
|
||||||
*/
|
*/
|
||||||
svgedit.draw.Drawing = function(svgElem, opt_idPrefix) {
|
svgedit.draw.Drawing = function(svgElem, opt_idPrefix) {
|
||||||
if (!svgElem || !svgElem.tagName || !svgElem.namespaceURI ||
|
if (!svgElem || !svgElem.tagName || !svgElem.namespaceURI ||
|
||||||
@@ -124,7 +129,7 @@ svgedit.draw.Drawing = function(svgElem, opt_idPrefix) {
|
|||||||
* The nonce to use to uniquely identify elements across drawings.
|
* The nonce to use to uniquely identify elements across drawings.
|
||||||
* @type {!String}
|
* @type {!String}
|
||||||
*/
|
*/
|
||||||
this.nonce_ = "";
|
this.nonce_ = '';
|
||||||
var n = this.svgElem_.getAttributeNS(NS.SE, 'nonce');
|
var n = this.svgElem_.getAttributeNS(NS.SE, 'nonce');
|
||||||
// If already set in the DOM, use the nonce throughout the document
|
// If already set in the DOM, use the nonce throughout the document
|
||||||
// else, if randomizeIds(true) has been called, create and set the nonce.
|
// else, if randomizeIds(true) has been called, create and set the nonce.
|
||||||
@@ -135,40 +140,56 @@ svgedit.draw.Drawing = function(svgElem, opt_idPrefix) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
svgedit.draw.Drawing.prototype.getElem_ = function(id) {
|
/**
|
||||||
if(this.svgElem_.querySelector) {
|
* @param {string} id Element ID to retrieve
|
||||||
|
* @returns {Element} SVG element within the root SVGSVGElement
|
||||||
|
*/
|
||||||
|
svgedit.draw.Drawing.prototype.getElem_ = function (id) {
|
||||||
|
if (this.svgElem_.querySelector) {
|
||||||
// querySelector lookup
|
// querySelector lookup
|
||||||
return this.svgElem_.querySelector('#'+id);
|
return this.svgElem_.querySelector('#' + id);
|
||||||
}
|
}
|
||||||
// jQuery lookup: twice as slow as xpath in FF
|
// jQuery lookup: twice as slow as xpath in FF
|
||||||
return $(this.svgElem_).find('[id=' + id + ']')[0];
|
return $(this.svgElem_).find('[id=' + id + ']')[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
svgedit.draw.Drawing.prototype.getSvgElem = function() {
|
/**
|
||||||
|
* @returns {SVGSVGElement}
|
||||||
|
*/
|
||||||
|
svgedit.draw.Drawing.prototype.getSvgElem = function () {
|
||||||
return this.svgElem_;
|
return this.svgElem_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {!string|number} The previously set nonce
|
||||||
|
*/
|
||||||
svgedit.draw.Drawing.prototype.getNonce = function() {
|
svgedit.draw.Drawing.prototype.getNonce = function() {
|
||||||
return this.nonce_;
|
return this.nonce_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {!string|number} n The nonce to set
|
||||||
|
*/
|
||||||
svgedit.draw.Drawing.prototype.setNonce = function(n) {
|
svgedit.draw.Drawing.prototype.setNonce = function(n) {
|
||||||
this.svgElem_.setAttributeNS(NS.XMLNS, 'xmlns:se', NS.SE);
|
this.svgElem_.setAttributeNS(NS.XMLNS, 'xmlns:se', NS.SE);
|
||||||
this.svgElem_.setAttributeNS(NS.SE, 'se:nonce', n);
|
this.svgElem_.setAttributeNS(NS.SE, 'se:nonce', n);
|
||||||
this.nonce_ = n;
|
this.nonce_ = n;
|
||||||
};
|
};
|
||||||
|
|
||||||
svgedit.draw.Drawing.prototype.clearNonce = function() {
|
/**
|
||||||
|
* Clears any previously set nonce
|
||||||
|
*/
|
||||||
|
svgedit.draw.Drawing.prototype.clearNonce = function () {
|
||||||
// We deliberately leave any se:nonce attributes alone,
|
// We deliberately leave any se:nonce attributes alone,
|
||||||
// we just don't use it to randomize ids.
|
// we just don't use it to randomize ids.
|
||||||
this.nonce_ = "";
|
this.nonce_ = '';
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the latest object id as a string.
|
* Returns the latest object id as a string.
|
||||||
* @return {String} The latest object Id.
|
* @return {String} The latest object Id.
|
||||||
*/
|
*/
|
||||||
svgedit.draw.Drawing.prototype.getId = function() {
|
svgedit.draw.Drawing.prototype.getId = function () {
|
||||||
return this.nonce_ ?
|
return this.nonce_ ?
|
||||||
this.idPrefix + this.nonce_ + '_' + this.obj_num :
|
this.idPrefix + this.nonce_ + '_' + this.obj_num :
|
||||||
this.idPrefix + this.obj_num;
|
this.idPrefix + this.obj_num;
|
||||||
@@ -178,7 +199,7 @@ svgedit.draw.Drawing.prototype.getId = function() {
|
|||||||
* Returns the next object Id as a string.
|
* Returns the next object Id as a string.
|
||||||
* @return {String} The next object Id to use.
|
* @return {String} The next object Id to use.
|
||||||
*/
|
*/
|
||||||
svgedit.draw.Drawing.prototype.getNextId = function() {
|
svgedit.draw.Drawing.prototype.getNextId = function () {
|
||||||
var oldObjNum = this.obj_num;
|
var oldObjNum = this.obj_num;
|
||||||
var restoreOldObjNum = false;
|
var restoreOldObjNum = false;
|
||||||
|
|
||||||
@@ -210,20 +231,17 @@ svgedit.draw.Drawing.prototype.getNextId = function() {
|
|||||||
return id;
|
return id;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function: svgedit.draw.Drawing.releaseId
|
/**
|
||||||
// Releases the object Id, letting it be used as the next id in getNextId().
|
* Releases the object Id, letting it be used as the next id in getNextId().
|
||||||
// This method DOES NOT remove any elements from the DOM, it is expected
|
* This method DOES NOT remove any elements from the DOM, it is expected
|
||||||
// that client code will do this.
|
* that client code will do this.
|
||||||
//
|
* @param {string} id - The id to release.
|
||||||
// Parameters:
|
* @returns {boolean} True if the id was valid to be released, false otherwise.
|
||||||
// id - The id to release.
|
*/
|
||||||
//
|
svgedit.draw.Drawing.prototype.releaseId = function (id) {
|
||||||
// Returns:
|
|
||||||
// True if the id was valid to be released, false otherwise.
|
|
||||||
svgedit.draw.Drawing.prototype.releaseId = function(id) {
|
|
||||||
// confirm if this is a valid id for this Document, else return false
|
// confirm if this is a valid id for this Document, else return false
|
||||||
var front = this.idPrefix + (this.nonce_ ? this.nonce_ + '_' : '');
|
var front = this.idPrefix + (this.nonce_ ? this.nonce_ + '_' : '');
|
||||||
if (typeof id != typeof '' || id.indexOf(front) != 0) {
|
if (typeof id !== 'string' || id.indexOf(front) !== 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// extract the obj_num of this id
|
// extract the obj_num of this id
|
||||||
@@ -231,7 +249,7 @@ svgedit.draw.Drawing.prototype.releaseId = function(id) {
|
|||||||
|
|
||||||
// if we didn't get a positive number or we already released this number
|
// if we didn't get a positive number or we already released this number
|
||||||
// then return false.
|
// then return false.
|
||||||
if (typeof num != typeof 1 || num <= 0 || this.releasedNums.indexOf(num) != -1) {
|
if (typeof num !== 'number' || num <= 0 || this.releasedNums.indexOf(num) != -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,18 +259,19 @@ svgedit.draw.Drawing.prototype.releaseId = function(id) {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function: svgedit.draw.Drawing.getNumLayers
|
/**
|
||||||
// Returns the number of layers in the current drawing.
|
* Returns the number of layers in the current drawing.
|
||||||
//
|
* @returns {integer} The number of layers in the current drawing.
|
||||||
// Returns:
|
*/
|
||||||
// The number of layers in the current drawing.
|
|
||||||
svgedit.draw.Drawing.prototype.getNumLayers = function() {
|
svgedit.draw.Drawing.prototype.getNumLayers = function() {
|
||||||
return this.all_layers.length;
|
return this.all_layers.length;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function: svgedit.draw.Drawing.hasLayer
|
/**
|
||||||
// Check if layer with given name already exists
|
* Check if layer with given name already exists
|
||||||
svgedit.draw.Drawing.prototype.hasLayer = function(name) {
|
* @param {string} name - The layer name to check
|
||||||
|
*/
|
||||||
|
svgedit.draw.Drawing.prototype.hasLayer = function (name) {
|
||||||
var i;
|
var i;
|
||||||
for (i = 0; i < this.getNumLayers(); i++) {
|
for (i = 0; i < this.getNumLayers(); i++) {
|
||||||
if(this.all_layers[i][0] == name) {return true;}
|
if(this.all_layers[i][0] == name) {return true;}
|
||||||
@@ -261,42 +280,38 @@ svgedit.draw.Drawing.prototype.hasLayer = function(name) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Function: svgedit.draw.Drawing.getLayerName
|
/**
|
||||||
// Returns the name of the ith layer. If the index is out of range, an empty string is returned.
|
* Returns the name of the ith layer. If the index is out of range, an empty string is returned.
|
||||||
//
|
* @param {integer} i - The zero-based index of the layer you are querying.
|
||||||
// Parameters:
|
* @returns {string} The name of the ith layer (or the empty string if none found)
|
||||||
// i - the zero-based index of the layer you are querying.
|
*/
|
||||||
//
|
svgedit.draw.Drawing.prototype.getLayerName = function (i) {
|
||||||
// Returns:
|
|
||||||
// The name of the ith layer
|
|
||||||
svgedit.draw.Drawing.prototype.getLayerName = function(i) {
|
|
||||||
if (i >= 0 && i < this.getNumLayers()) {
|
if (i >= 0 && i < this.getNumLayers()) {
|
||||||
return this.all_layers[i][0];
|
return this.all_layers[i][0];
|
||||||
}
|
}
|
||||||
return "";
|
return '';
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function: svgedit.draw.Drawing.getCurrentLayer
|
/**
|
||||||
// Returns:
|
* @returns {SVGGElement} The SVGGElement representing the current layer.
|
||||||
// The SVGGElement representing the current layer.
|
*/
|
||||||
svgedit.draw.Drawing.prototype.getCurrentLayer = function() {
|
svgedit.draw.Drawing.prototype.getCurrentLayer = function() {
|
||||||
return this.current_layer;
|
return this.current_layer;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function: getCurrentLayerName
|
/**
|
||||||
// Returns the name of the currently selected layer. If an error occurs, an empty string
|
* Returns the name of the currently selected layer. If an error occurs, an empty string
|
||||||
// is returned.
|
* is returned.
|
||||||
//
|
* @returns The name of the currently active layer (or the empty string if none found).
|
||||||
// Returns:
|
*/
|
||||||
// The name of the currently active layer.
|
svgedit.draw.Drawing.prototype.getCurrentLayerName = function () {
|
||||||
svgedit.draw.Drawing.prototype.getCurrentLayerName = function() {
|
|
||||||
var i;
|
var i;
|
||||||
for (i = 0; i < this.getNumLayers(); ++i) {
|
for (i = 0; i < this.getNumLayers(); ++i) {
|
||||||
if (this.all_layers[i][1] == this.current_layer) {
|
if (this.all_layers[i][1] == this.current_layer) {
|
||||||
return this.getLayerName(i);
|
return this.getLayerName(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "";
|
return '';
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function: setCurrentLayer
|
// Function: setCurrentLayer
|
||||||
@@ -464,7 +479,7 @@ svgedit.draw.Drawing.prototype.getLayerVisibility = function(layername) {
|
|||||||
// Returns:
|
// Returns:
|
||||||
// The SVGGElement representing the layer if the layername was valid, otherwise null.
|
// The SVGGElement representing the layer if the layername was valid, otherwise null.
|
||||||
svgedit.draw.Drawing.prototype.setLayerVisibility = function(layername, bVisible) {
|
svgedit.draw.Drawing.prototype.setLayerVisibility = function(layername, bVisible) {
|
||||||
if (typeof bVisible != typeof true) {
|
if (typeof bVisible !== 'boolean') {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// find the layer
|
// find the layer
|
||||||
@@ -485,15 +500,12 @@ svgedit.draw.Drawing.prototype.setLayerVisibility = function(layername, bVisible
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Function: svgedit.draw.Drawing.getLayerOpacity
|
/**
|
||||||
// Returns the opacity of the given layer. If the input name is not a layer, null is returned.
|
* Returns the opacity of the given layer. If the input name is not a layer, null is returned.
|
||||||
//
|
* @param {string} layername - name of the layer on which to get the opacity
|
||||||
// Parameters:
|
* @returns {?number} The opacity value of the given layer. This will be a value between 0.0 and 1.0, or null
|
||||||
// layername - name of the layer on which to get the opacity
|
* if layername is not a valid layer
|
||||||
//
|
*/
|
||||||
// Returns:
|
|
||||||
// The opacity value of the given layer. This will be a value between 0.0 and 1.0, or null
|
|
||||||
// if layername is not a valid layer
|
|
||||||
svgedit.draw.Drawing.prototype.getLayerOpacity = function(layername) {
|
svgedit.draw.Drawing.prototype.getLayerOpacity = function(layername) {
|
||||||
var i;
|
var i;
|
||||||
for (i = 0; i < this.getNumLayers(); ++i) {
|
for (i = 0; i < this.getNumLayers(); ++i) {
|
||||||
@@ -517,7 +529,7 @@ svgedit.draw.Drawing.prototype.getLayerOpacity = function(layername) {
|
|||||||
// layername - name of the layer on which to set the opacity
|
// layername - name of the layer on which to set the opacity
|
||||||
// opacity - a float value in the range 0.0-1.0
|
// opacity - a float value in the range 0.0-1.0
|
||||||
svgedit.draw.Drawing.prototype.setLayerOpacity = function(layername, opacity) {
|
svgedit.draw.Drawing.prototype.setLayerOpacity = function(layername, opacity) {
|
||||||
if (typeof opacity != typeof 1.0 || opacity < 0.0 || opacity > 1.0) {
|
if (typeof opacity !== 'number' || opacity < 0.0 || opacity > 1.0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var i;
|
var i;
|
||||||
|
|||||||
Reference in New Issue
Block a user