thirdparty/jsdoctoolkit/app/frame/Link.js
changeset 3041 c8f47f0b6697
equal deleted inserted replaced
3040:8f9580309846 3041:c8f47f0b6697
       
     1 /** Handle the creation of HTML links to documented symbols.
       
     2 	@constructor
       
     3 */
       
     4 function Link() {
       
     5 	this.alias = "";
       
     6 	this.src = "";
       
     7 	this.file = "";
       
     8 	this.text = "";
       
     9 	this.innerName = "";
       
    10 	this.classLink = false;
       
    11 	this.targetName = "";
       
    12 	
       
    13 	this.target = function(targetName) {
       
    14 		if (defined(targetName)) this.targetName = targetName;
       
    15 		return this;
       
    16 	}
       
    17 	this.inner = function(inner) {
       
    18 		if (defined(inner)) this.innerName = inner;
       
    19 		return this;
       
    20 	}
       
    21 	this.withText = function(text) {
       
    22 		if (defined(text)) this.text = text;
       
    23 		return this;
       
    24 	}
       
    25 	this.toSrc = function(filename) {
       
    26 		if (defined(filename)) this.src = filename;
       
    27 		return this;
       
    28 	}
       
    29 	this.toSymbol = function(alias) {
       
    30 		if (defined(alias)) this.alias = new String(alias);
       
    31 		return this;
       
    32 	}
       
    33 	this.toClass = function(alias) {
       
    34 		this.classLink = true;
       
    35 		return this.toSymbol(alias);
       
    36 	}
       
    37 	this.toFile = function(file) {
       
    38 		if (defined(file)) this.file = file;
       
    39 		return this;
       
    40 	}
       
    41 	
       
    42 	this.toString = function() {
       
    43 		var linkString;
       
    44 		var thisLink = this;
       
    45 
       
    46 		if (this.alias) {
       
    47 			linkString = this.alias.replace(/(^|[^a-z$0-9_#.:^-])([|a-z$0-9_#.:^-]+)($|[^a-z$0-9_#.:^-])/i,
       
    48 				function(match, prematch, symbolName, postmatch) {
       
    49 					var symbolNames = symbolName.split("|");
       
    50 					var links = [];
       
    51 					for (var i = 0, l = symbolNames.length; i < l; i++) {
       
    52 						thisLink.alias = symbolNames[i];
       
    53 						links.push(thisLink._makeSymbolLink(symbolNames[i]));
       
    54 					}
       
    55 					return prematch+links.join("|")+postmatch;
       
    56 				}
       
    57 			);
       
    58 		}
       
    59 		else if (this.src) {
       
    60 			linkString = thisLink._makeSrcLink(this.src);
       
    61 		}
       
    62 		else if (this.file) {
       
    63 			linkString = thisLink._makeFileLink(this.file);
       
    64 		}
       
    65 
       
    66 		return linkString;
       
    67 	}
       
    68 }
       
    69 
       
    70 /** prefixed for hashes */
       
    71 Link.hashPrefix = "";
       
    72 
       
    73 /** Appended to the front of relative link paths. */
       
    74 Link.base = "";
       
    75 
       
    76 Link.symbolNameToLinkName = function(symbol) {
       
    77 	var linker = "";
       
    78 	if (symbol.isStatic) linker = ".";
       
    79 	else if (symbol.isInner) linker = "-";
       
    80 	
       
    81 	return Link.hashPrefix+linker+symbol.name;
       
    82 }
       
    83 
       
    84 /** Create a link to another symbol. */
       
    85 Link.prototype._makeSymbolLink = function(alias) {
       
    86 	var linkBase = Link.base+publish.conf.symbolsDir;
       
    87 	var linkTo = Link.symbolSet.getSymbol(alias);
       
    88 	var linkPath;
       
    89 	var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
       
    90 
       
    91 	// is it an internal link?
       
    92 	if (alias.charAt(0) == "#") var linkPath = alias;
       
    93 	
       
    94 	// if there is no symbol by that name just return the name unaltered
       
    95 	else if (!linkTo) return this.text || alias;
       
    96 	
       
    97 	// it's a symbol in another file
       
    98 	else {
       
    99 		if (!linkTo.is("CONSTRUCTOR") && !linkTo.isNamespace) { // it's a method or property
       
   100 			if (linkTo.isEvent) {
       
   101 				linkPath = 
       
   102 					(Link.filemap)? Link.filemap[linkTo.memberOf] 
       
   103 					:
       
   104 					escape(linkTo.memberOf) || "_global_";
       
   105 				linkPath += publish.conf.ext + "#event:" + Link.symbolNameToLinkName(linkTo);
       
   106 			}
       
   107 			else {
       
   108 				linkPath = 
       
   109 					(Link.filemap)? Link.filemap[linkTo.memberOf] 
       
   110 					:
       
   111 					escape(linkTo.memberOf) || "_global_";
       
   112 				linkPath += publish.conf.ext + "#" + Link.symbolNameToLinkName(linkTo);
       
   113 			}
       
   114 		}
       
   115 		else {
       
   116 			linkPath = (Link.filemap)? Link.filemap[linkTo.alias] : escape(linkTo.alias);
       
   117 			linkPath += publish.conf.ext;// + (this.classLink? "":"#" + Link.hashPrefix + "constructor");
       
   118 		}
       
   119 		linkPath = linkBase + linkPath
       
   120 	}
       
   121 	
       
   122 	var linkText = this.text || alias;
       
   123 	
       
   124 	var link = {linkPath: linkPath, linkText: linkText, linkInner: (this.innerName? "#"+this.innerName : "")};
       
   125 	
       
   126 	if (typeof JSDOC.PluginManager != "undefined") {
       
   127 		JSDOC.PluginManager.run("onSymbolLink", link);
       
   128 	}
       
   129 	
       
   130 	return "<a href=\""+link.linkPath+link.linkInner+"\""+target+">"+link.linkText+"</a>";
       
   131 }
       
   132 
       
   133 /** Create a link to a source file. */
       
   134 Link.prototype._makeSrcLink = function(srcFilePath) {
       
   135 	var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
       
   136 		
       
   137 	// transform filepath into a filename
       
   138 	var srcFile = srcFilePath.replace(/\.\.?[\\\/]/g, "").replace(/[:\\\/]/g, "_");
       
   139 	var outFilePath = Link.base + publish.conf.srcDir + srcFile + publish.conf.ext;
       
   140 
       
   141 	if (!this.text) this.text = FilePath.fileName(srcFilePath);
       
   142 	return "<a href=\""+outFilePath+"\""+target+">"+this.text+"</a>";
       
   143 }
       
   144 
       
   145 /** Create a link to a source file. */
       
   146 Link.prototype._makeFileLink = function(filePath) {
       
   147 	var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
       
   148 		
       
   149 	var outFilePath =  Link.base + filePath;
       
   150 
       
   151 	if (!this.text) this.text = filePath;
       
   152 	return "<a href=\""+outFilePath+"\""+target+">"+this.text+"</a>";
       
   153 }