9904dfd666711068c77e13d083358dbba82b1874
[gnome.introspection-doc-generator] / JsTemplate / Link.js
1 //<script type="text/javascript">
2
3 /**
4  * Generic Template Link handler..
5  * 
6  * 
7  * 
8  */
9
10 /** Handle the creation of HTML links to documented symbols.
11         @constructor
12 */
13 function Link() {
14         this.alias = "";
15         this.src = "";
16         this.file = "";
17         this.text = "";
18         this.innerName = "";
19         this.classLink = false;
20         this.targetName = "";
21         
22         this.target = function(targetName) {
23                 if (typeof(targetName) != 'undefined') this.targetName = targetName;
24                 return this;
25         }
26         this.inner = function(inner) {
27                 if (typeof(inner) != 'undefined') this.innerName = inner;
28                 return this;
29         }
30         this.withText = function(text) {
31                 if (typeof(text) != 'undefined') this.text = text;
32                 return this;
33         }
34         this.toSrc = function(filename) {
35                 if (typeof(filename) != 'undefined') this.src = filename;
36                 return this;
37         }
38         this.toSymbol = function(alias) {
39                 if (typeof(alias) != 'undefined') {
40             this.alias = new String(alias);
41         }
42                 return this;
43         }
44         this.toClass = function(alias) {
45                 this.classLink = true;
46                 return this.toSymbol(alias);
47         }
48         this.toFile = function(file) {
49                 if (typeof(file) != 'undefined') this.file = file;
50                 return this;
51         }
52         
53         this.toString = function() {
54                 var linkString;
55                 var thisLink = this;
56
57                 if (this.alias) {
58                         linkString = this.alias.replace(/(^|[^a-z$0-9_#.:-])([|a-z$0-9_#.:-]+)($|[^a-z$0-9_#.:-])/i,
59                                 function(match, prematch, symbolName, postmatch) {
60                                         var symbolNames = symbolName.split("|");
61                                         var links = [];
62                                         for (var i = 0, l = symbolNames.length; i < l; i++) {
63                                                 thisLink.alias = symbolNames[i];
64                                                 links.push(thisLink._makeSymbolLink(symbolNames[i]));
65                                         }
66                                         return prematch+links.join("|")+postmatch;
67                                 }
68                         );
69                 }
70                 else if (this.src) {
71                         linkString = thisLink._makeSrcLink(this.src);
72                 }
73                 else if (this.file) {
74                         linkString = thisLink._makeFileLink(this.file);
75                 }
76
77                 return linkString;
78         }
79 }
80
81 /** prefixed for hashes */
82 Link.hashPrefix = "";
83
84 /** Appended to the front of relative link paths. */
85 Link.base = "";
86
87 Link.symbolNameToLinkName = function(symbol) {
88         var linker = "";
89         if (symbol.isStatic) linker = ".";
90         else if (symbol.isInner) linker = "-";
91         
92         return Link.hashPrefix+linker+symbol.name;
93 }
94
95
96 Link.builtins = {
97     'Object' : 'http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Object',
98     'Object...' : 'http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Object',
99     'Function' : 'http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function',
100     'String' : 'http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String',
101     'Number' : 'http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Number',
102     'Boolean' : 'http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Boolean',
103     'HTMLElement' : 'http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-58190037'
104 }
105     
106
107
108 /** Create a link to a snother symbol. */
109 Link.prototype._makeSymbolLink = function(alias) {
110     
111     // look for '/' in alias..
112     if (/\//.test(alias)) {
113         var bits = alias.split('/');
114         var ret = "";
115         for(var i=0; i < bits.length; i++) {
116             if (i > 0) {
117                 ret +="/";
118             }
119             ret += this._makeSymbolLink(bits[i]);
120         }
121         return ret;
122         
123     }
124     
125     
126     
127         var linkBase = Link.base+JSDOC.publish.conf.symbolsDir;
128         var linkTo = Link.symbolSet.getSymbol(alias);
129         var linkPath;
130         var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
131     
132         // is it an internal link?
133         if (alias.charAt(0) == "#") {
134         linkPath = alias;
135         fullLinkPath = alias;
136         
137         // if there is no symbol by that name just return the name unaltered
138         } else if (!linkTo) {
139         
140         if (typeof(Link.builtins[alias]) != 'undefined') {
141             return "<a href=\""+ Link.builtins[alias]+"\""+target+">"+alias+"</a>";
142          }
143         
144         return this.text || alias;
145     
146         
147         // it's a symbol in another file
148         } else {
149
150                 if (!linkTo.is("CONSTRUCTOR") && !linkTo.isNamespace) { // it's a method or property
151                         linkPath = escape(linkTo.memberOf) || "_global_";
152                         linkPath += JSDOC.publish.conf.ext + "#" + Link.symbolNameToLinkName(linkTo);
153                 }
154                 else {
155                         linkPath = escape(linkTo.alias);
156                         linkPath += JSDOC.publish.conf.ext + (this.classLink? "":"#" + Link.hashPrefix + "constructor");
157                 }
158                 //linkPath = linkBase + linkPath;
159         fullLinkPath = linkBase + linkPath;
160         }
161         
162         var linkText = this.text || alias;
163         
164         var link = {linkPath: linkPath, linkText: linkText, fullLinkPath: fullLinkPath};
165         
166         if (typeof JSDOC.PluginManager != "undefined") {
167                 JSDOC.PluginManager.run("onSymbolLink", link);
168         }
169         
170         return "<a href=\""+link.fullLinkPath+"\""+target+" roo:cls=\""+link.linkPath+"\">"+link.linkText+"</a>";
171 }
172
173
174 /** Create a link to a source file. */
175 Link.prototype._makeSrcLink = function(srcFilePath) {
176         var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
177                 
178         // transform filepath into a filename
179         var srcFile = srcFilePath.replace(/\.\.?[\\\/]/g, "").replace(/[:\\\/]/g, "."); // was _
180     var lsrcFile = srcFilePath.replace(/\.\.?[\\\/]/g, "").replace(/[:\\\/]/g, ".");
181         var outFilePath = Link.base + JSDOC.publish.conf.srcDir + srcFile.replace(/.js$/, '') + JSDOC.publish.conf.ext;
182     
183         if (!this.text) this.text = FilePath.fileName(srcFilePath);
184         return "<a href=\""+outFilePath+"\""+target+" roo:cls=\"src/"+lsrcFile+"\">"+this.text+"</a>";
185 }
186
187 /** Create a link to a source file. */
188 Link.prototype._makeFileLink = function(filePath) {
189         var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
190                 
191         var outFilePath =  Link.base + filePath;
192
193         if (!this.text) this.text = filePath;
194         return "<a href=\""+outFilePath+"\""+target+">"+this.text+"</a>";
195 }