Fix #5660 - roojs docs - linking and other tidy ups
[roojs1] / docs / Roo.docs.init.js
1
2
3 Roo.docs.init = {
4     
5     classes : false, // flat version of list of classes 
6     currentClass : '--none--', // currently viewed class name
7     
8     hash : '',
9     
10     onReady : function()
11     {
12        
13         //if (typeof(pagedata) == 'undefined') {
14         //    Site.onReady.defer(100, Roo.docs.init);
15         //    return;
16         //}
17         //Roo.debug = 1;
18         
19         Roo.XComponent.hideProgress = true;
20         Roo.XComponent.build();
21         
22         
23         
24         
25         Roo.XComponent.on('buildcomplete', function() {
26             
27             //Roo.XComponent.modules[0].el.fireEvent('render');
28             this.loadTree();
29         }, this);
30         
31         
32         window.onhashchange = function() { Roo.docs.init.onHashChange(); }
33         
34         
35        
36         
37         //Roo.get(document.body).on('click', this.onClick, this);
38       
39         
40     },
41     
42     loadTree: function()
43     {
44         Roo.docs.doc_body_content.hide();
45         
46         Roo.Ajax.request({
47             url : 'tree.json',
48             method : 'GET',
49             success : function(res, o)
50             {
51                 var d = Roo.decode(res.responseText);
52                 //Roo.log(d);
53                 this.classes = {};
54                 // our classes witch children first..
55                 d.forEach(function(e) {
56                     if (e.cn.length) {
57                         this.addTreeItem(Roo.docs.navGroup, e, 'NavSidebarItem', true);
58                         
59                     }
60                 }, this);
61                 
62                 d.forEach(function(e) {
63                     if (!e.cn.length) {
64                         this.addTreeItem(Roo.docs.navGroup, e, 'NavSidebarItem' ,true);
65                     }
66                 }, this);
67                 
68                 d.forEach(function(e) {
69                     if (e.cn.length) {
70                         this.addTreeItem(Roo.docs.mobileNavGroup, e, 'NavSidebarItem', false);
71                         
72                     }
73                 }, this);
74                 
75                 d.forEach(function(e) {
76                     if (!e.cn.length) {
77                         this.addTreeItem(Roo.docs.mobileNavGroup, e, 'NavSidebarItem', false);
78                     }
79                 }, this);
80                 
81                 var roo = Roo.docs.navGroup.items[1].menu;
82                 roo.show(roo.triggerEl, '?', false);
83                 if (location.hash.length) {
84                     this.loadHash();
85                     return;
86                 }
87                 
88                 this.loadIntro();
89                 
90                 
91             },
92             scope : this
93         });
94         
95         
96     },
97     
98     hideChildren : function(c)
99     {
100         if (c.node.menu) {
101             c.node.menu.hide();
102         }
103         for (var i =0; i < c.cn.length; i++) {
104             this.hideChildren(c.cn[i]);
105         }
106         
107     },
108     
109     
110     addTreeItem : function(parent, e, type , parent_e) {
111         
112         this.classes[e.name] = e; 
113         // add a node..
114         var node = parent.addxtypeChild(Roo.factory({
115             html: e.name.split('.').pop(),
116            // id : e.name,
117             xns : Roo.bootstrap,
118             showArrow: false,
119             xtype : type,
120             preventDefault : true,
121           //  cls : type == 'NavSidebarItem' ? 'open' : '',
122             listeners : {
123                 click : (function(mi,ev,c)
124                 {
125                     
126                     ev.stopPropagation();
127                      
128                     if (c.cn.length) {
129                         //Roo.log(ev);
130                         if (mi.menu.el.hasClass('show')) {
131                             this.hideChildren(c); //mi.menu.hide();
132                             // collapse children..
133                             
134                             
135                             
136                         } else {
137                             mi.menu.show(mi.menu.triggerEl,'?', false);
138                         }
139                         
140                     }
141                     location.hash = '#' + c.name;
142                     //Roo.docs.init.loadDoc(c);
143                     
144                 }).createDelegate(this,[e], true)
145                 
146             },
147             fa :  e.cn.length  ? 'chevron-right' : '',
148             menu : !e.cn.length ? false  : Roo.factory({
149                 type : 'treeview',
150                 xns: Roo.bootstrap,
151                 xtype : 'Menu',
152                 listeners : {
153                     beforehide : (function(mi,c)
154                     {
155                         if (c.name.split('.').length < 2) {
156                             return false;
157                         }
158                         return true;
159                         
160                     }).createDelegate(this,[e], true)
161                     
162                 }
163                 
164             })
165         }));
166         
167         // mobile nodes..?
168         
169        
170         
171         
172         
173         
174         if (parent_e !== false) {
175             e.node = node;
176             e.parent_menu = parent;
177             e.parent = parent_e == true ? null : parent_e;
178         }
179         
180         parent.items.push(node);
181         if (e.cn.length  && type == 'NavSidebarItem') {
182             this.topm = node.menu;
183         }
184         
185         
186         if (!e.cn.length) {
187             return;
188         }
189         e.cn.forEach(function(ec) {
190             var cn = ec.name.split('.').pop();
191             //Roo.log(cn);
192             if (cn == cn.toLowerCase()) {
193                 this.addTreeItem(node.menu, ec,'MenuItem', parent_e !== false ? e : false);
194             }
195             
196         }, this);
197         e.cn.forEach(function(ec) {
198             var cn = ec.name.split('.').pop();
199             if (cn != cn.toLowerCase()) {
200                 this.addTreeItem(node.menu, ec,'MenuItem', parent_e !== false ? e : false);
201             }
202         }, this);
203         
204     },
205     
206     loadClass : function(name)
207     {
208         if(typeof(this.classes[name]) != 'undefined' && this.classes[name].is_class ) {
209             this.loadDoc(this.classes[name]);
210         }
211         
212         
213         
214     },
215     
216     loadSource : function( )
217     {
218         
219        
220         Roo.Ajax.request({
221             url : 'src/' +this.currentClass.replace(/\./g,'_') + '.js.html',
222             method : 'GET',
223             success : function(res, o)
224             {
225                 Roo.docs.ViewSource.show({
226                         source : res.responseText,
227                         fname : this.currentClass.replace(/\./g,'/') + ".js"
228                 });
229                 
230             },
231             scope : this
232         });
233         
234         
235     },
236     
237     loadDoc : function(cls)
238     {
239         if (this.currentClass == cls.name) {
240             return;
241         }
242         //Roo.docs.mobileNavGroup.hide();
243         Roo.log("loadDoc?");
244         Roo.docs.doc_body_content.hide();
245         
246         this.currentClass = cls.name;
247         if (!cls ) {
248             Roo.docs.introBody.show();
249             return;
250         }
251         
252         // expand parents..
253         
254         var m = cls.parent_menu;
255         m.show(m.triggerEl,'?', false);
256         var mp = cls;
257         while ((mp = mp.parent)) {
258             m = mp.parent_menu;
259             m.show(m.triggerEl,'?', false);
260         }
261         cls.node.el.scrollIntoView(Roo.docs.sidebar.el,false);
262         Roo.docs.sidebar.el.select('.active').removeClass('active');
263         cls.node.el.addClass('active');
264         
265         Roo.docs.introBody.hide();
266         Roo.docs.doc_body_content.show();
267         Roo.Ajax.request({
268             url : 'symbols/' + cls.name + '.json',
269             method : 'GET',
270             success : function(res, o)
271             {
272                 var d = Roo.decode(res.responseText);
273                 this.fillDoc(d);
274                 
275             },
276             scope : this
277         });
278         
279         
280     },
281     
282     fillDoc : function(d)
283     {
284         /*{
285             "name" : "Roo.bootstrap.Progress",
286             "augments" : [
287               "Roo.bootstrap.Component",
288               "Roo.Component",
289               "Roo.util.Observable"
290             ],
291             "desc" : "Bootstrap Progress class",
292             "config" : [
293               {
294         */
295         document.body.scrollTop  = 0;
296         for (var k in d) {
297             if (typeof(Roo.docs['doc_' + k])  !=  'undefined') {
298                 Roo.docs['doc_' + k].el.dom.innerHTML = Roo.docs.template.resolveLinks(d[k]);;
299             }
300         }
301         Roo.docs.doc_extends.hide();
302         Roo.docs.doc_extends_sep.hide();
303         if (d.augments.length) {
304             Roo.docs.doc_extends.show();
305             Roo.docs.doc_extends_sep.show();
306             Roo.docs.doc_extends.el.dom.innerHTML = d.augments[0];
307         }
308         Roo.docs.doc_source.el.dom.innerHTML = d.name.replace(/\./g,"/") + ".js";
309         
310         if (d.augments.length) {
311             Roo.docs.augments.show();
312             Roo.docs.augments.bodyEl().dom.innerHTML = Roo.docs.template.augments(d);
313         } else {
314             Roo.docs.augments.hide();
315         }
316         
317         
318         
319         Roo.docs.configTableContainer.hide();
320         Roo.docs.methodsTableContainer.hide();
321         Roo.docs.eventsTableContainer.hide();
322         if (d.config.length) {
323             Roo.docs.configTableContainer.show();
324             Roo.docs.configTable.store.load( { params : { data : d.config.sort(Roo.docs.template.makeSortby("name")) }});
325         } 
326         
327         if (d.methods.length) {
328             Roo.docs.methodsTable.store.load( { params : { data : Roo.docs.template.methodsSort(d) }});
329             Roo.docs.methodsTableContainer.show();
330         }
331         if (d.events.length) {
332             Roo.docs.eventsTable.store.load( { params : { data : d.events.sort(Roo.docs.template.makeSortby("name")) }});
333             Roo.docs.eventsTableContainer.show();
334         }
335         
336         
337     },
338     onClick : function(e)
339     {
340         if (e.target.nodeName != 'A') {
341             return;
342         }
343         if (!e.target.href.match(/#/)) {
344             return;
345         }
346         e.stopPropagation();
347         var link = e.target.href.split('#')[1];
348         this.loadClass(link);
349         
350     },
351     
352     onHashChange : function()
353     {
354         if (this.hash == location.hash) {
355             return;
356         }
357         this.loadHash();
358         
359     },
360     loadHash : function()
361     {
362         if (location.hash.length < 2) {
363             this.loadDoc(false);
364         }
365         this.loadClass(location.hash.substring(1));
366         this.hash = location.hash;
367     },
368     
369       
370     loadIntro : function()
371     {
372       
373         
374         Roo.Ajax.request({
375             url : 'summary.txt',
376             method : 'GET',
377             success : function(res)
378             {
379                 this.renderIntro(res.responseText);
380                
381                 
382             },
383             scope : this
384         });
385         
386         
387     },
388     // render the really simple markdown data
389     renderIntro : function(intro)
390     {
391         
392         Roo.docs.doc_body_content.hide();
393
394         
395         var lines = intro.split("\n");
396         var tree = { 'name' : 'root', cn : []};
397         var state = [ tree ];
398         for (var i=0;i< lines.length;i++) {
399             var line = lines[i];
400             if (!line.length || line.match(/^\s+$/)) {
401                 continue;
402             }
403             var sm = line.match(/^(\s+)(.*)/);
404             
405             var sml = sm ? sm[1].length: 0;
406             //Roo.log(sml);
407             sml = sml / 4; // 4 spaces indent?
408             var add = { name : sm ?  sm[2] : line, cn : [] };
409             state[sml].cn.push(add);
410             state[sml+1] = add;
411             
412         }
413         //Roo.log(tree);
414         
415         for(var i = 0; i < tree.cn.length; i++) {
416             // make a container..
417             var treei = tree.cn[i];
418             var ctree = {
419                 
420                 xtype : 'Column',
421                 xns : Roo.bootstrap,
422                 md:4,
423                 sm : 6,
424                 items : [ {
425                     header : treei.name,
426                     xtype : 'Container',
427                     panel : 'info',
428                     xns : Roo.bootstrap,
429                     items : []
430                 }]
431             };
432             for(var ii = 0; ii < treei.cn.length; ii++) {
433                 var treeii = treei.cn[ii];
434                 // another container..
435                var ctreei = {
436                     header : treeii.name,
437                     xtype : 'Container',
438                     panel : 'primary',
439                     xns : Roo.bootstrap,
440                   
441                     items : [
442                          {
443                             xtype : 'Element',
444                             tag :'ul',
445                            
446                             xns : Roo.bootstrap,
447                             items : []
448                          }
449                     ]
450                 };
451                 ctree.items[0].items.push(ctreei);
452                 var footer = '';
453                 for(var iii = 0; iii < treeii.cn.length; iii++) {
454                     var treeiii = treeii.cn[iii];
455                     var ll = treeiii.name.match(/^(\S+)\s*(.*)$/);
456                     //Roo.log(treeiii.name);
457                     if (treeiii.name == 'Examples') {
458                         for (var j =0;j< treeiii.cn.length; j++) {
459                             var exs = treeiii.cn[j].name.match(/^\[([^\]]+)\](.*)$/);
460                             footer += '<li><a target="_blank" href="../' + exs[1] + '">'+exs[2] + '</a></li>';
461                         }
462                         continue;
463                         
464                         
465                     }
466         
467         
468
469                     ctreeii = {
470                             xtype : 'Element',
471                             tag :'li',
472                             xns : Roo.bootstrap,
473                             items : [
474                                 {
475                                    xtype : 'Link',
476                                     href : '#' + ( ll ? ll[1] : treeiii.name ) ,
477                                     html : ll ? ll[1] : treeiii.name,
478                                     
479                                     xns : Roo.bootstrap 
480                                 },
481                                 {
482                                    xtype : 'Element',
483                                     tag : 'span',
484                                     html : ll && ll[2].length ? ' - ' + ll[2] : '',
485                                     xns : Roo.bootstrap 
486                                 }
487                             ]
488                             
489                             
490                             
491                     };
492                     ctreei.items.push(ctreeii);
493                     
494                 }
495                 if (footer.length) {
496                     //Roo.log("footer:"+  footer);
497                     ctreei.footer = '<h5>Examples:</h5><ul>'+footer +'</ul>';
498                 }
499         
500             }
501             
502             
503             
504             
505             
506             Roo.docs.introBody.addxtypeChild(ctree);
507         }
508         
509         
510         
511     }
512     
513     
514     
515 };
516
517
518 Roo.onReady(Roo.docs.init.onReady, Roo.docs.init);
519     
520