/tags/jsdoc_toolkit-2.4.0/jsdoc-toolkit/app/frame/Link.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 173 lines · 130 code · 30 blank · 13 comment · 35 complexity · b3646c0423d1ba2df4fa7cdbbd2957b9 MD5 · raw file

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