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