aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/help/_base.js
blob: 109f3ca9c634dcc9da54343564b8d28eaeeae22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
if(!dojo._hasResource["dojox.help._base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.help._base"] = true;
dojo.provide("dojox.help._base");
dojo.require("dojox.rpc.Service");
dojo.require("dojo.io.script");

dojo.experimental("dojox.help");
console.warn("Script causes side effects (on numbers, strings, and booleans). Call dojox.help.noConflict() if you plan on executing code.");

dojox.help = {
	// summary:
	//		Adds the help function to all variables.
	locate: function(/*String*/ searchFor, /*String|Object|String[]|Object[]*/ searchIn, /*Number*/ maxResults){
		// summary:
		//		Search for dojo functionality that has something to do with the given string.
		//  description:
		//		Search for locally available data; variable names and any cached
		//		documentation results for matches containing our search parameter
		// searchFor
		//		The string to search for.
		// searchIn:
		//		The namespaces to search in. Defaults to dojox.help._namespaces
		// maxResults:
		//		The maximum number of results.
		maxResults = maxResults || 20;
		var namespaces = [];
		var roots = {};
		var root;
		if(searchIn){
			if(!dojo.isArray(searchIn)){
				searchIn = [searchIn];
			}
			for(var i = 0, namespace; namespace = searchIn[i]; i++){
				root = namespace;
				if(dojo.isString(namespace)){
					namespace = dojo.getObject(namespace);
					if(!namespace){
						continue;
					}
				}else if(dojo.isObject(namespace)){
					root = namespace.__name__;
				}else{
					continue;
				}
				// Add to a list of namespace objects (in object form)
				namespaces.push(namespace);
				if(root){
					root = root.split(".")[0];
					if(!roots[root] && dojo.indexOf(dojox.help._namespaces, root) == -1){
						// Refresh anything that's not part of our global namespace list
						dojox.help.refresh(root);
					}
					roots[root] = true;
				}
			}
		}
		if(!namespaces.length){
			namespaces.push({ __name__: "window" });
			dojo.forEach(dojox.help._namespaces, function(item){ roots[item] = true; });
		}

		var found = [];
		out:
		for(var i = 0, namespace; namespace = namespaces[i]; i++){
			var name = namespace.__name__ || "";
			var shorter = dojo.some(namespaces, function(item){
				// Return true if we find a namespace below
				// the current namespace
				item = item.__name__ || "";
				return (name.indexOf(item + ".") == 0);
			});
			if(name && !shorter){
				root = name.split(".")[0];
				var names = [];
				if(name == "window"){
					for(root in dojox.help._names){
						if(dojo.isArray(dojox.help._names[root])){
							names = names.concat(dojox.help._names[root]);
						}
					}
				}else{
					names = dojox.help._names[root];
				}
				for(var j = 0, variable; variable = names[j]; j++){
					if((name == "window" || variable.indexOf(name + ".") == 0) && variable.toLowerCase().indexOf(searchFor) != -1){
						if(variable.slice(-10) == ".prototype"){ continue; }
						var obj = dojo.getObject(variable);
						if(obj){
							found.push([variable, obj]);
							if(found.length == maxResults){
								break out;
							}
						}
					}
				}
			}
		}

		dojox.help._displayLocated(found);
		if(!+dojo.isFF){
			return "";
		}
	},
	refresh: function(/*String?*/ namespace, /*Boolean?*/ recursive){
		// summary:
		//		Useful if you reset some values, and want to restore their
		//		help function
		// namespace:
		//		The string-representation of a namespace.
		// recursive:
		//		Whether to recurse through the namespace.
		if(arguments.length < 2){
			recursive = true;
		}
		dojox.help._recurse(namespace, recursive);
	},
	noConflict: function(/*Object?*/ item){
		// summary:
		//		Use this function when you want to resolve the problems
		//		created by including a dojox.help package.
		// item:
		//		If you pass an item, only that item will be cleaned
		if(arguments.length){
			return dojox.help._noConflict(item);
		}else{
			while(dojox.help._overrides.length){
				var override = dojox.help._overrides.pop();
				var parent = override[0];
				var key = override[1];
				var child = parent[key];
				parent[key] = dojox.help._noConflict(child);
			}
		}
	},
	init: function(/*String[]*/ namespaces, /*Boolen?*/ noConflict){
		// summary:
		//		Should be called by one of the implementations. Runs startup code
		// namespaces:
		//		Any namespaces to add to the default (dojox.help._namespaces)
		// noConflict:
		//		Whether to start in noConflict mode
		if(namespaces){
			dojox.help._namespaces.concat(namespaces);
		}
		dojo.addOnLoad(function(){
			dojo.require = (function(require){
				return function(){
					dojox.help.noConflict();
					require.apply(dojo, arguments);
					if(dojox.help._timer){
						clearTimeout(dojox.help._timer);
					}
					dojox.help._timer = setTimeout(function(){
						dojo.addOnLoad(function(){
							dojox.help.refresh();
							dojox.help._timer = false;
						});
					}, 500);
				}
			})(dojo.require);

			dojox.help._recurse();
		});
	},
	_noConflict: function(item){
		if(item instanceof String){
			return item.toString();
		}else if(item instanceof Number){
			return +item;
		}else if(item instanceof Boolean){
			return (item == true);
		}else if(dojo.isObject(item)){
			delete item.__name__;
			delete item.help;
		}
		return item;
	},
	_namespaces: ["dojo", "dojox", "dijit", "djConfig"],
	_rpc: new dojox.rpc.Service(dojo.moduleUrl("dojox.rpc", "documentation.smd")),
	_attributes: ["summary", "type", "returns", "parameters"],
	_clean: function(self){
		var obj = {};
		for(var i = 0, attribute; attribute = dojox.help._attributes[i]; i++){
			var value = self["__" + attribute + "__"];
			if(value){
				obj[attribute] = value;
			}
		}
		return obj;
	},
	_displayLocated: function(located){
		// summary:
		//		Stub function to be overridden in one of the dojox.help packages
		throw new Error("_displayLocated should be overridden in one of the dojox.help packages");
	},
	_displayHelp: function(loading, obj){
		// summary:
		//		Stub function to be overridden in one of the dojox.help packages
		throw new Error("_displayHelp should be overridden in one of the dojox.help packages");
	},
	_addVersion: function(obj){
		if(obj.name){
			obj.version = [dojo.version.major, dojo.version.minor, dojo.version.patch].join(".");
			var parts = obj.name.split(".");
			if(parts[0] == "dojo" || parts[0] == "dijit" || parts[0] == "dojox"){
				obj.project = parts[0];
			}
		}
		return obj;
	},
	_stripPrototype: function(original){
		var name = original.replace(/\.prototype(\.|$)/g, ".");
		var search = name;
		if(name.slice(-1) == "."){
			search = name = name.slice(0, -1);
		}else{
			name = original;
		}
		return [search, name];
	},
	_help: function(){
		var name = this.__name__;
		var search = dojox.help._stripPrototype(name)[0];
		var attributes = [];
		for(var i = 0, attribute; attribute = dojox.help._attributes[i]; i++){
			if(!this["__" + attribute + "__"]){
				attributes.push(attribute);
			}
		}

		dojox.help._displayHelp(true, { name: this.__name__ });

		if(!attributes.length || this.__searched__){
			dojox.help._displayHelp(false, dojox.help._clean(this));
		}else{
			this.__searched__ = true;
			dojox.help._rpc.get(dojox.help._addVersion({
				name: search,
				exact: true,
				attributes: attributes
			})).addCallback(this, function(data){
				if(this.toString === dojox.help._toString){
					this.toString(data);
				}
				if(data && data.length){
					data = data[0];
					for(var i = 0, attribute; attribute = dojox.help._attributes[i]; i++){
						if(data[attribute]){
							this["__" + attribute + "__"] = data[attribute];
						}
					}
					dojox.help._displayHelp(false, dojox.help._clean(this));
				}else{
					dojox.help._displayHelp(false, false);
				}
			});
		}
		if(!+dojo.isFF){
			return "";
		}
	},
	_parse: function(data){
		delete this.__searching__;
		if(data && data.length){
			var parameters = data[0].parameters;

			if(parameters){
				var signature = ["function ", this.__name__, "("];
				this.__parameters__ = parameters;
				for(var i = 0, parameter; parameter = parameters[i]; i++){
					if(i){
						signature.push(", ");
					}
					signature.push(parameter.name);
					if(parameter.types){
						var types = [];
						for(var j = 0, type; type = parameter.types[j]; j++){
							types.push(type.title);
						}
						if(types.length){
							signature.push(": ");
							signature.push(types.join("|"));
						}
					}
					if(parameter.repeating){
						signature.push("...");
					}
					if(parameter.optional){
						signature.push("?");
					}
				}
				signature.push(")");

				this.__source__ = this.__source__.replace(/function[^\(]*\([^\)]*\)/, signature.join(""));
			}

			if(this.__output__){
				delete this.__output__;
				console.log(this);
			}
		}else{
			dojox.help._displayHelp(false, false);
		}
	},
	_toStrings: {},
	_toString: function(data){
		if(!this.__source__){
			return this.__name__;
		}

		var first = (!this.__parameters__);
		this.__parameters__ = [];

		if(data){
			dojox.help._parse.call(this, data);
		}else if(first){
			this.__searching__ = true;
			dojox.help._toStrings[dojox.help._stripPrototype(this.__name__)[0]] = this;
			if(dojox.help._toStringTimer){
				clearTimeout(dojox.help._toStringTimer);
			}
			dojox.help._toStringTimer = setTimeout(function(){ dojox.help.__toString(); }, 50);
		}

		if(!first || !this.__searching__){
			return this.__source__;
		}

		var message = "function Loading info for " + this.__name__ + "... (watch console for result) {}";

		if(!+dojo.isFF){
			this.__output__ = true;
			return message;
		}

		return {
			toString: dojo.hitch(this, function(){
				// Detect if this was called by Firebug
				this.__output__ = true;
				return message;
			})
		};
	},
	__toString: function(){
		if(dojox.help._toStringTimer){
			clearTimeout(dojox.help._toStringTimer);
		}

		var names = [];
		dojox.help.noConflict(dojox.help._toStrings);
		for(var name in dojox.help._toStrings){
			names.push(name);
		}
		while(names.length){
			dojox.help._rpc.batch(dojox.help._addVersion({
				names: names.splice(-50, 50),
				exact: true,
				attributes: ["parameters"]
			})).addCallback(this, function(datas){
				for(var i = 0, data; data = datas[i]; i++){
					fn = dojox.help._toStrings[data.name];
					if(fn){
						dojox.help._parse.call(fn, [data]);
						delete dojox.help._toStrings[data.name];
					}
				}
			});
		}
	},
	_overrides: [],
	_recursions: [],
	_names: {},
	_recurse: function(/*String?*/ namespace, /*Boolean?*/ recursive){
		if(arguments.length < 2){
			recursive = true;
		}

		var items = [];

		if(namespace && dojo.isString(namespace)){
			dojox.help.__recurse(dojo.getObject(namespace), namespace, namespace, items, recursive);
		}else{
			for(var i = 0, namespace; namespace = dojox.help._namespaces[i]; i++){
				if(window[namespace]){
					dojox.help._recursions.push([window[namespace], namespace, namespace]);
					window[namespace].__name__ = namespace;
					if(!window[namespace].help){
						window[namespace].help = dojox.help._help;
					}
				}
			}
		}

		while(dojox.help._recursions.length){
			var recursion = dojox.help._recursions.shift();
			dojox.help.__recurse(recursion[0], recursion[1], recursion[2], items, recursive);
		}

		for(var i = 0, item; item = items[i]; i++){
			delete item.__seen__;
		}
	},
	__recurse: function(namespace, root, name, items, recursive){
		for(var key in namespace){
			if(key.match(/([^\w_.$]|__[\w_.$]+__)/)){
				continue;
			}

			var item = namespace[key];
			if(typeof item == "undefined"
				|| item === document
				|| item === window
				|| item === dojox.help._toString
				|| item === dojox.help._help
				|| item === null
				|| (+dojo.isIE && item.tagName)
				|| item.__seen__
			) {
				continue;
			}

			var isFunction = dojo.isFunction(item);
			var isObject = dojo.isObject(item) && !dojo.isArray(item) && !item.nodeType;

			var itemName = (name) ? (name + "." + key) : key;

			if(itemName == "dojo._blockAsync"){
				continue;
			}

			if(!item.__name__){
				var parent = null;
				if(dojo.isString(item)){
					parent = String;
				}else if(typeof item == "number"){
					parent = Number;
				}else if(typeof item == "boolean"){
					parent = Boolean;
				}
				if(parent){
					item = namespace[key] = new parent(item);
				}
			}

			item.__seen__ = true;
			item.__name__ = itemName;
			(dojox.help._names[root] = dojox.help._names[root] || []).push(itemName);
			items.push(item);
			if(!isFunction){
				dojox.help._overrides.push([namespace, key]);
			}

			if((isFunction || isObject) && recursive){
				dojox.help._recursions.push([item, root, itemName]);
			}

			if(isFunction){
				if(!item.__source__){
					item.__source__ = item.toString().replace(/^function\b ?/, "function " + itemName);
				}
				if(item.toString === Function.prototype.toString){
					item.toString = dojox.help._toString;
				}
			}

			if(!item.help){
				item.help = dojox.help._help;
			}
		}
	}
};

}