_base.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // wrapped by build app
  2. define("dojox/help/_base", ["dijit","dojo","dojox","dojo/require!dojox/rpc/Service,dojo/io/script"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.help._base");
  4. dojo.require("dojox.rpc.Service");
  5. dojo.require("dojo.io.script");
  6. dojo.experimental("dojox.help");
  7. console.warn("Script causes side effects (on numbers, strings, and booleans). Call dojox.help.noConflict() if you plan on executing code.");
  8. dojox.help = {
  9. // summary:
  10. // Adds the help function to all variables.
  11. locate: function(/*String*/ searchFor, /*String|Object|String[]|Object[]*/ searchIn, /*Number*/ maxResults){
  12. // summary:
  13. // Search for dojo functionality that has something to do with the given string.
  14. // description:
  15. // Search for locally available data; variable names and any cached
  16. // documentation results for matches containing our search parameter
  17. // searchFor
  18. // The string to search for.
  19. // searchIn:
  20. // The namespaces to search in. Defaults to dojox.help._namespaces
  21. // maxResults:
  22. // The maximum number of results.
  23. maxResults = maxResults || 20;
  24. var namespaces = [];
  25. var roots = {};
  26. var root;
  27. if(searchIn){
  28. if(!dojo.isArray(searchIn)){
  29. searchIn = [searchIn];
  30. }
  31. for(var i = 0, namespace; namespace = searchIn[i]; i++){
  32. root = namespace;
  33. if(dojo.isString(namespace)){
  34. namespace = dojo.getObject(namespace);
  35. if(!namespace){
  36. continue;
  37. }
  38. }else if(dojo.isObject(namespace)){
  39. root = namespace.__name__;
  40. }else{
  41. continue;
  42. }
  43. // Add to a list of namespace objects (in object form)
  44. namespaces.push(namespace);
  45. if(root){
  46. root = root.split(".")[0];
  47. if(!roots[root] && dojo.indexOf(dojox.help._namespaces, root) == -1){
  48. // Refresh anything that's not part of our global namespace list
  49. dojox.help.refresh(root);
  50. }
  51. roots[root] = true;
  52. }
  53. }
  54. }
  55. if(!namespaces.length){
  56. namespaces.push({ __name__: "window" });
  57. dojo.forEach(dojox.help._namespaces, function(item){ roots[item] = true; });
  58. }
  59. var searchForLower = searchFor.toLowerCase();
  60. var found = [];
  61. out:
  62. for(var i = 0, namespace; namespace = namespaces[i]; i++){
  63. var name = namespace.__name__ || "";
  64. var shorter = dojo.some(namespaces, function(item){
  65. // Return true if we find a namespace below
  66. // the current namespace
  67. item = item.__name__ || "";
  68. return (name.indexOf(item + ".") == 0);
  69. });
  70. if(name && !shorter){
  71. root = name.split(".")[0];
  72. var names = [];
  73. if(name == "window"){
  74. for(root in dojox.help._names){
  75. if(dojo.isArray(dojox.help._names[root])){
  76. names = names.concat(dojox.help._names[root]);
  77. }
  78. }
  79. }else{
  80. names = dojox.help._names[root];
  81. }
  82. for(var j = 0, variable; variable = names[j]; j++){
  83. if((name == "window" || variable.indexOf(name + ".") == 0) && variable.toLowerCase().indexOf(searchForLower) != -1){
  84. if(variable.slice(-10) == ".prototype"){ continue; }
  85. var obj = dojo.getObject(variable);
  86. if(obj){
  87. found.push([variable, obj]);
  88. if(found.length == maxResults){
  89. break out;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. dojox.help._displayLocated(found);
  97. if(!dojo.isMoz){
  98. return "";
  99. }
  100. },
  101. refresh: function(/*String?*/ namespace, /*Boolean?*/ recursive){
  102. // summary:
  103. // Useful if you reset some values, and want to restore their
  104. // help function
  105. // namespace:
  106. // The string-representation of a namespace.
  107. // recursive:
  108. // Whether to recurse through the namespace.
  109. if(arguments.length < 2){
  110. recursive = true;
  111. }
  112. dojox.help._recurse(namespace, recursive);
  113. },
  114. noConflict: function(/*Object?*/ item){
  115. // summary:
  116. // Use this function when you want to resolve the problems
  117. // created by including a dojox.help package.
  118. // item:
  119. // If you pass an item, only that item will be cleaned
  120. if(arguments.length){
  121. return dojox.help._noConflict(item);
  122. }else{
  123. while(dojox.help._overrides.length){
  124. var override = dojox.help._overrides.pop();
  125. var parent = override[0];
  126. var key = override[1];
  127. var child = parent[key];
  128. parent[key] = dojox.help._noConflict(child);
  129. }
  130. }
  131. },
  132. init: function(/*String[]*/ namespaces, /*Boolen?*/ noConflict){
  133. // summary:
  134. // Should be called by one of the implementations. Runs startup code
  135. // namespaces:
  136. // Any namespaces to add to the default (dojox.help._namespaces)
  137. // noConflict:
  138. // Whether to start in noConflict mode
  139. if(namespaces){
  140. dojox.help._namespaces.concat(namespaces);
  141. }
  142. dojo.addOnLoad(function(){
  143. dojo.require = (function(require){
  144. return function(){
  145. dojox.help.noConflict();
  146. require.apply(dojo, arguments);
  147. if(dojox.help._timer){
  148. clearTimeout(dojox.help._timer);
  149. }
  150. dojox.help._timer = setTimeout(function(){
  151. dojo.addOnLoad(function(){
  152. dojox.help.refresh();
  153. dojox.help._timer = false;
  154. });
  155. }, 500);
  156. }
  157. })(dojo.require);
  158. dojox.help._recurse();
  159. });
  160. },
  161. _noConflict: function(item){
  162. if(item instanceof String){
  163. return item.toString();
  164. }else if(item instanceof Number){
  165. return +item;
  166. }else if(item instanceof Boolean){
  167. return (item == true);
  168. }else if(dojo.isObject(item)){
  169. delete item.__name__;
  170. delete item.help;
  171. }
  172. return item;
  173. },
  174. _namespaces: ["dojo", "dojox", "dijit", "djConfig"],
  175. _rpc: new dojox.rpc.Service(dojo.moduleUrl("dojox.rpc.SMDLibrary", "dojo-api.smd")),
  176. _attributes: ["summary", "type", "returns", "parameters"],
  177. _clean: function(self){
  178. var obj = {};
  179. for(var i = 0, attribute; attribute = dojox.help._attributes[i]; i++){
  180. var value = self["__" + attribute + "__"];
  181. if(value){
  182. obj[attribute] = value;
  183. }
  184. }
  185. return obj;
  186. },
  187. _displayLocated: function(located){
  188. // summary:
  189. // Stub function to be overridden in one of the dojox.help packages
  190. throw new Error("_displayLocated should be overridden in one of the dojox.help packages");
  191. },
  192. _displayHelp: function(loading, obj){
  193. // summary:
  194. // Stub function to be overridden in one of the dojox.help packages
  195. throw new Error("_displayHelp should be overridden in one of the dojox.help packages");
  196. },
  197. _addVersion: function(obj){
  198. if(obj.name){
  199. obj.version = [dojo.version.major, dojo.version.minor, dojo.version.patch].join(".");
  200. var parts = obj.name.split(".");
  201. if(parts[0] == "dojo" || parts[0] == "dijit" || parts[0] == "dojox"){
  202. obj.project = parts[0];
  203. }
  204. }
  205. return obj;
  206. },
  207. _stripPrototype: function(original){
  208. var name = original.replace(/\.prototype(\.|$)/g, ".");
  209. var search = name;
  210. if(name.slice(-1) == "."){
  211. search = name = name.slice(0, -1);
  212. }else{
  213. name = original;
  214. }
  215. return [search, name];
  216. },
  217. _help: function(){
  218. var name = this.__name__;
  219. var search = dojox.help._stripPrototype(name)[0];
  220. var attributes = [];
  221. for(var i = 0, attribute; attribute = dojox.help._attributes[i]; i++){
  222. if(!this["__" + attribute + "__"]){
  223. attributes.push(attribute);
  224. }
  225. }
  226. dojox.help._displayHelp(true, { name: this.__name__ });
  227. if(!attributes.length || this.__searched__){
  228. dojox.help._displayHelp(false, dojox.help._clean(this));
  229. }else{
  230. this.__searched__ = true;
  231. dojox.help._rpc.get(dojox.help._addVersion({
  232. name: search,
  233. exact: true,
  234. attributes: attributes
  235. })).addCallback(this, function(data){
  236. if(this.toString === dojox.help._toString){
  237. this.toString(data);
  238. }
  239. if(data && data.length){
  240. data = data[0];
  241. for(var i = 0, attribute; attribute = dojox.help._attributes[i]; i++){
  242. if(data[attribute]){
  243. this["__" + attribute + "__"] = data[attribute];
  244. }
  245. }
  246. dojox.help._displayHelp(false, dojox.help._clean(this));
  247. }else{
  248. dojox.help._displayHelp(false, false);
  249. }
  250. });
  251. }
  252. if(!dojo.isMoz){
  253. return "";
  254. }
  255. },
  256. _parse: function(data){
  257. delete this.__searching__;
  258. if(data && data.length){
  259. var parameters = data[0].parameters;
  260. if(parameters){
  261. var signature = ["function ", this.__name__, "("];
  262. this.__parameters__ = parameters;
  263. for(var i = 0, parameter; parameter = parameters[i]; i++){
  264. if(i){
  265. signature.push(", ");
  266. }
  267. signature.push(parameter.name);
  268. if(parameter.types){
  269. var types = [];
  270. for(var j = 0, type; type = parameter.types[j]; j++){
  271. types.push(type.title);
  272. }
  273. if(types.length){
  274. signature.push(": ");
  275. signature.push(types.join("|"));
  276. }
  277. }
  278. if(parameter.repeating){
  279. signature.push("...");
  280. }
  281. if(parameter.optional){
  282. signature.push("?");
  283. }
  284. }
  285. signature.push(")");
  286. this.__source__ = this.__source__.replace(/function[^\(]*\([^\)]*\)/, signature.join(""));
  287. }
  288. if(this.__output__){
  289. delete this.__output__;
  290. console.log(this);
  291. }
  292. }else{
  293. dojox.help._displayHelp(false, false);
  294. }
  295. },
  296. _toStrings: {},
  297. _toString: function(data){
  298. if(!this.__source__){
  299. return this.__name__;
  300. }
  301. var first = (!this.__parameters__);
  302. this.__parameters__ = [];
  303. if(data){
  304. dojox.help._parse.call(this, data);
  305. }else if(first){
  306. this.__searching__ = true;
  307. dojox.help._toStrings[dojox.help._stripPrototype(this.__name__)[0]] = this;
  308. if(dojox.help._toStringTimer){
  309. clearTimeout(dojox.help._toStringTimer);
  310. }
  311. dojox.help._toStringTimer = setTimeout(function(){ dojox.help.__toString(); }, 50);
  312. }
  313. if(!first || !this.__searching__){
  314. return this.__source__;
  315. }
  316. var message = "function Loading info for " + this.__name__ + "... (watch console for result) {}";
  317. if(!dojo.isMoz){
  318. this.__output__ = true;
  319. return message;
  320. }
  321. return {
  322. toString: dojo.hitch(this, function(){
  323. // Detect if this was called by Firebug
  324. this.__output__ = true;
  325. return message;
  326. })
  327. };
  328. },
  329. __toString: function(){
  330. if(dojox.help._toStringTimer){
  331. clearTimeout(dojox.help._toStringTimer);
  332. }
  333. var names = [];
  334. dojox.help.noConflict(dojox.help._toStrings);
  335. for(var name in dojox.help._toStrings){
  336. names.push(name);
  337. }
  338. while(names.length){
  339. dojox.help._rpc.batch(dojox.help._addVersion({
  340. names: names.splice(-50, 50),
  341. exact: true,
  342. attributes: ["parameters"]
  343. })).addCallback(this, function(datas){
  344. for(var i = 0, data; data = datas[i]; i++){
  345. var fn = dojox.help._toStrings[data.name];
  346. if(fn){
  347. dojox.help._parse.call(fn, [data]);
  348. delete dojox.help._toStrings[data.name];
  349. }
  350. }
  351. });
  352. }
  353. },
  354. _overrides: [],
  355. _recursions: [],
  356. _names: {},
  357. _recurse: function(/*String?*/ namespace, /*Boolean?*/ recursive){
  358. if(arguments.length < 2){
  359. recursive = true;
  360. }
  361. var items = [];
  362. if(namespace && dojo.isString(namespace)){
  363. dojox.help.__recurse(dojo.getObject(namespace), namespace, namespace, items, recursive);
  364. }else{
  365. for(var i = 0, ns; ns = dojox.help._namespaces[i]; i++){
  366. if(window[ns]){
  367. dojox.help._recursions.push([window[ns], ns, ns]);
  368. window[ns].__name__ = ns;
  369. if(!window[ns].help){
  370. window[ns].help = dojox.help._help;
  371. }
  372. }
  373. }
  374. }
  375. while(dojox.help._recursions.length){
  376. var recursion = dojox.help._recursions.shift();
  377. dojox.help.__recurse(recursion[0], recursion[1], recursion[2], items, recursive);
  378. }
  379. for(var i = 0, item; item = items[i]; i++){
  380. delete item.__seen__;
  381. }
  382. },
  383. __recurse: function(namespace, root, name, items, recursive){
  384. for(var key in namespace){
  385. if(key.match(/([^\w_.$]|__[\w_.$]+__)/)){
  386. continue;
  387. }
  388. var item = namespace[key];
  389. if(typeof item == "undefined"
  390. || item === document
  391. || item === window
  392. || item === dojox.help._toString
  393. || item === dojox.help._help
  394. || item === null
  395. || (+dojo.isIE && item.tagName)
  396. || item.__seen__
  397. ) {
  398. continue;
  399. }
  400. var isFunction = dojo.isFunction(item);
  401. var isObject = dojo.isObject(item) && !dojo.isArray(item) && !item.nodeType;
  402. var itemName = (name) ? (name + "." + key) : key;
  403. if(itemName == "dojo._blockAsync"){
  404. continue;
  405. }
  406. if(!item.__name__){
  407. var parent = null;
  408. if(dojo.isString(item)){
  409. parent = String;
  410. }else if(typeof item == "number"){
  411. parent = Number;
  412. }else if(typeof item == "boolean"){
  413. parent = Boolean;
  414. }
  415. if(parent){
  416. item = namespace[key] = new parent(item);
  417. }
  418. }
  419. item.__seen__ = true;
  420. item.__name__ = itemName;
  421. (dojox.help._names[root] = dojox.help._names[root] || []).push(itemName);
  422. items.push(item);
  423. if(!isFunction){
  424. dojox.help._overrides.push([namespace, key]);
  425. }
  426. if((isFunction || isObject) && recursive){
  427. dojox.help._recursions.push([item, root, itemName]);
  428. }
  429. if(isFunction){
  430. if(!item.__source__){
  431. item.__source__ = item.toString().replace(/^function\b ?/, "function " + itemName);
  432. }
  433. if(item.toString === Function.prototype.toString){
  434. item.toString = dojox.help._toString;
  435. }
  436. }
  437. if(!item.help){
  438. item.help = dojox.help._help;
  439. }
  440. }
  441. }
  442. };
  443. });