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

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 148 lines · 113 code · 23 blank · 12 comment · 33 complexity · f5c71bbe71bb55d0ff27c4d03fcdb8be 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. if (symbol.isStatic) linker = ".";
  72. else if (symbol.isInner) linker = "-";
  73. return Link.hashPrefix+linker+symbol.name;
  74. }
  75. /** Create a link to a snother symbol. */
  76. Link.prototype._makeSymbolLink = function(alias) {
  77. var linkBase = Link.base+publish.conf.symbolsDir;
  78. var linkTo = Link.symbolSet.getSymbol(alias);
  79. var linkPath;
  80. var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
  81. // is it an internal link?
  82. if (alias.charAt(0) == "#") var linkPath = alias;
  83. // if there is no symbol by that name just return the name unaltered
  84. else if (!linkTo) return this.text || alias;
  85. // it's a symbol in another file
  86. else {
  87. if (!linkTo.is("CONSTRUCTOR") && !linkTo.isNamespace) { // it's a method or property
  88. if (linkTo.isEvent) {
  89. linkPath = escape(linkTo.memberOf) || "_global_";
  90. linkPath += publish.conf.ext + "#event:" + Link.symbolNameToLinkName(linkTo);
  91. }
  92. else {
  93. linkPath = escape(linkTo.memberOf) || "_global_";
  94. linkPath += publish.conf.ext + "#" + Link.symbolNameToLinkName(linkTo);
  95. }
  96. }
  97. else {
  98. linkPath = escape(linkTo.alias);
  99. linkPath += publish.conf.ext;// + (this.classLink? "":"#" + Link.hashPrefix + "constructor");
  100. }
  101. linkPath = linkBase + linkPath
  102. }
  103. var linkText = this.text || alias;
  104. var link = {linkPath: linkPath, linkText: linkText};
  105. if (typeof JSDOC.PluginManager != "undefined") {
  106. JSDOC.PluginManager.run("onSymbolLink", link);
  107. }
  108. return "<a href=\""+link.linkPath+"\""+target+">"+link.linkText+"</a>";
  109. }
  110. /** Create a link to a source file. */
  111. Link.prototype._makeSrcLink = function(srcFilePath) {
  112. var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
  113. // transform filepath into a filename
  114. var srcFile = srcFilePath.replace(/\.\.?[\\\/]/g, "").replace(/[:\\\/]/g, "_");
  115. var outFilePath = Link.base + publish.conf.srcDir + srcFile + publish.conf.ext;
  116. if (!this.text) this.text = FilePath.fileName(srcFilePath);
  117. return "<a href=\""+outFilePath+"\""+target+">"+this.text+"</a>";
  118. }
  119. /** Create a link to a source file. */
  120. Link.prototype._makeFileLink = function(filePath) {
  121. var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
  122. var outFilePath = Link.base + filePath;
  123. if (!this.text) this.text = filePath;
  124. return "<a href=\""+outFilePath+"\""+target+">"+this.text+"</a>";
  125. }