Ticket #6185: apropos.diff

File apropos.diff, 3.3 kB (added by wolfram, 10 months ago)
  • help/apropos.js

     
     1dojo.provide("dojox.help.apropos"); 
     2 
     3//dojo.require("dojox.help"); 
     4 
     5dojox.help.apropos = function(/*String*/ searchFor, /*String[]*/ searchIn, /*int*/ maxResults, /*int*/ depth){ 
     6        // summary: 
     7        //              Search for dojo functionality that has something to do with the given string. 
     8        // description: 
     9        //              Each dojo class, method and function has a short description available within it. 
     10        //              This command searches the descriptions for alike terms as the one given. 
     11        // searchFor: String 
     12        //              The string to search for. 
     13        // searchIn: String[] 
     14        //              The namespaces to search in, if not given it is: ["dojo", "dijit", "dojox"] 
     15        // maxResults: int 
     16        //              The maximum number of results, when to stop searching. 
     17        var _in = searchIn || ["dojo", "dijit", "dojox"]; 
     18        var _maxResults = maxResults || 20; 
     19        var _depth = depth || 5; 
     20        console.debug("Starting search, please be patient depending on the depth this might take a while ..."); 
     21        var results = this._apropos(searchFor, _in, _maxResults, _depth, []); 
     22        if (_maxResults==results.length) { 
     23                console.debug("Done, stopped after " + results.length + " results."); 
     24        } else { 
     25                console.debug("Done, found " + results.length + " results."); 
     26        } 
     27}; 
     28 
     29dojox.help._apropos = function(/*String*/ searchFor, /*String[]*/ searchIn, /*int*/ maxResults, /*int*/ depth, /*Array*/ allResults){ 
     30        // allResults: Array 
     31        //              This contains all the results that are found until now, 
     32        //              so we can compare if we have doubles found. Since we are calling 
     33        //              the search recursively we don't have all the results available every time 
     34        //              so we pass it along, but actually ONLY for reading! 
     35        var results = []; 
     36        for(var nsCounter=0, l=searchIn.length; nsCounter<l; nsCounter++){ 
     37                var ns = searchIn[nsCounter]; 
     38                var nextLevel = []; 
     39                var obj = dojo.getObject(ns); 
     40                if (dojo.isObject(obj)) { 
     41                        // I had the case when trying to for-in over "dojo.doc.defaultView.window.applicationCache" 
     42                        // that I got this error: An attempt was made to use an object that is not, or is no longer, usable" code: "11 
     43                        // so catch this here. 
     44                        try { 
     45                                for (var i in obj) { 
     46                                        if (dojo.some(["prototype", "parentNode", "constructor"], "return item==this", i) || 
     47                                                i.indexOf("__")==0 || ns+"."+i=="dojo.global") { 
     48                                                continue; 
     49                                        } 
     50                                        try { 
     51                                                var o = dojo.getObject(ns+"."+i); 
     52                                        } catch(e) { 
     53                                                continue; 
     54                                        } 
     55                                        if (dojo.isFunction(o)) { 
     56                                                if (i.toLowerCase().indexOf(searchFor.toLowerCase())!=-1) { 
     57                                                        if (!dojo.some(allResults.concat(results), "return this===dojo.getObject(item)", o)) { 
     58                                                                maxResults--; 
     59                                                                results.push(ns+"."+i); 
     60                                                                console.debug(ns+"."+i); 
     61                                                        } else { 
     62                                                                //console.debug(ns+"."+i+" is a double, the exact same object was already found, just under another name."); 
     63                                                        } 
     64                                                } 
     65                                        } else { 
     66                                                if (!dojo.isArray(o) && dojo.isObject(o)) { 
     67                                                        nextLevel.push(ns+"."+i); 
     68                                                } 
     69                                        } 
     70                                        // Stop when max was reached. 
     71                                        if (maxResults<1) { 
     72                                                break; 
     73                                        } 
     74                                } 
     75                        } 
     76                        catch(e){ 
     77                        } 
     78                } 
     79                if (depth>1) { 
     80                        results = results.concat(this._apropos(searchFor, nextLevel, maxResults, depth-1, allResults.concat(results))); 
     81                } 
     82        }; 
     83        return results; 
     84};