PageSourceSearch

https://bialik.ca/cms3t/res/vit-lib/console.js

js bialik.ca collected 2026-10-02 23:30:18 UTC 19,499 bytes, 641 lines download raw bytes

1/** ************************************************************************************************************************ **\
2* 
3*	Set of classes and functions that will help create debugging consoles.
4* 
5* 	@package	VIT-Lib
6* 	@subpackage	Debug
7*
8* 	@license	MIT License
9*			Permission  is  hereby  granted,  free  of  charge, to any person obtaining a copy of this software and
10*			associated documentation files (the "Software"), to deal in the Software without restriction, including
11*			without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12*			copies  of  the  Software, and to permit persons to whom the Software is furnished to do so, subject to
13*			the following conditions:
14*			
15*			The  above  copyright  notice and this permission notice shall be included in all copies or substantial
16*			portions of the Software.
17*			
18*			THE  SOFTWARE  IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
19*			LIMITED  TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
20*			NO  EVENT  SHALL  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21*			WHETHER  IN  AN  ACTION  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22*			SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23*
24*	@author		Victor Denisenkov aka Mr.V!T
25*	@copyright	Copyright (c) 2008+ Victor Denisenkov aka Mr.V!T
26*	@version	1.0
27* 
28\** ************************************************************************************************************************ **/
29
30/** //** ----= OBJECT vConsole	=--------------------------------------------------------------------------------------\**//** \
31*
32* 	Debug console.
33*
34* 	@package	VIT-Lib
35* 	@subpackage	Debug
36* 	@category	Helper
37*
38\**//** ----------------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
39function vConsole ($options) {
40
41var $class = {
42
43	enabled			: true,		// Working state. If disabled - doing nothing, untill it's manually enabled.
44
45	width			: 600,		// Default widndow width
46	zIndex			: 6000,		// zIndex to use for window
47
48	panels			: {},		// Custom panels list and their properties/elements
49	
50	dom			: {		// Set of valuable elements as jQuery shortcuts and dom elements
51		window			: false,	// Main window object
52		panels			: false,	// Panels container (dumps watches)
53		console			: false,	// Common console (manual dumps)
54	}, //dom	
55
56	autoupdate		: false,	// Set TRUE to enable watch timer. It will cause updates of watched elements in specified inteval.
57	updateInterval		: 70,		// Autoupdate interval. Lower value - more lag, but more accurate readings.
58	updateDelay		: 1,		// Delay in ms between nearest updates. Increase if updates cause lags.
59						// Having even 1ms, causes another thread exec, and multiupdates filetering.
60
61	interval		: false,	// Autoupdate interval object
62	timer			: false,	// Update delay timer object
63	
64	dumper			: false,	// Variable dumper used to style debug
65
66// ==== SET/GET ================================================================================================================
67
68	/** //** ----= set	=--------------------------------------------------------------------------------------\**//** \
69	*
70	*	Initiates self parameters from given options array.
71	*
72	*	@return SELF
73	*
74	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
75	set		: function ($name, $value) {
76		
77		// ToDo: .set() in basic class
78		// ToDo: proper events extension support
79
80		var $o = {};
81
82		// Extending from object, so if pair given - forming object from there
83		// Otherwise name is needed object
84		if ( isStr($name) )
85			$o[$name] = $value;
86		else 		
87			$o = $name;
88			
89		jQuery.extend(this, $o);
90		
91		// init/deinit
92		if ( this.enabled )
93			this.init();
94		else
95			this.deinit();
96		
97		return this;
98	}, //FUNC set	
99
100	/** //** ----= get	=--------------------------------------------------------------------------------------\**//** \
101	*
102	*	Returns property.
103	*
104	*	@return SELF
105	*
106	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
107	get		: function ($name) {
108		// ToDo: .get() in basic class with getters support
109		// ToDo: proper events extension support
110		return this[$name];
111	}, //FUNC get	
112
113	/** //** ----= toggle	=--------------------------------------------------------------------------------------\**//** \
114	*
115	*	Toggles console.
116	*
117	*	@return SELF
118	*
119	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
120	toggle		: function ($state) {
121
122		if ( isUndefined($state) )
123			$state = !this.enabled;
124
125		return this.set('enabled', $state);
126	}, //FUNC toggle	
127
128	/** //** ----= show	=--------------------------------------------------------------------------------------\**//** \
129	*
130	*	Shows console.
131	*
132	*	@return SELF
133	*
134	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
135	show		: function () {
136		return this.toggle(true);
137	}, //FUNC show	
138
139	/** //** ----= hide	=--------------------------------------------------------------------------------------\**//** \
140	*
141	*	Hides console.
142	*
143	*	@return SELF
144	*
145	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
146	hide		: function () {
147		return this.toggle(false);
148	}, //FUNC hide	
149
150// ==== INIT ===================================================================================================================
151
152	/** //** ----= init	=--------------------------------------------------------------------------------------\**//** \
153	*
154	*	Initiates console, creates panels and prepares for output.
155	*
156	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
157	init		: function () {
158		
159		// Setting up dumper
160		if ( isEmpty(this.dumper) )
161			this.dumper = vDump({
162				'useHtml'	: true, 
163				'dumpFunctions'	: false
164			}); //vDump
165			
166		// Initiating elements
167		this.initWindow();
168		this.initPanels();
169
170		// Initial update, which will also console with info
171		this.update();
172
173		// Triggering autoupdate if necessary
174		this.setAutoupdate(this.autoupdate);
175	}, //FUNC init	
176
177	/** //** ----= deinit	=--------------------------------------------------------------------------------------\**//** \
178	*
179	*	Deinitiates console, removes dom and stops timers.
180	*
181	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
182	deinit		: function () {
183
184		// Removing dumper
185		this.dumper = false;
186		
187		// Removing elements
188		this.deinitPanels();
189		this.deinitWindow();
190
191		// Updating autoupdate :)
192		this.setAutoupdate(this.autoupdate);
193	}, //FUNC deinit
194
195// ==== DOM ====================================================================================================================
196
197	/** //** ----= initWindow	=------------------------------------------------------------------------------\**//** \
198	*
199	*	Initiates main window and common panel.
200	*
201	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
202	initWindow	: function () {
203		
204	 	// Checking if draggable available
205	 	var $isDraggable = isFunction(jQuery().draggable);
206		
207		// Compiling window css (it is updated anyway)
208		var $css = {
209			'position'	: 'fixed',
210
211			'left'		: 0,
212			'top'		: 0,
213			'width'		: this.width,
214
215			'z-index'	: 6000,
216			'cursor'	: $isDraggable ? 'move' : 'default',
217		}; // $css
218
219		// Creating window, or updating if created
220		if ( this.dom.window ) {
221
222			this.dom.window.css($css);
223
224		} else {
225	
226			this.dom.window = jQuery('<div class="debug"></div>').css($css);
227		
228			// Creating common console wrapper
229			this.dom.console = jQuery('<div></div>')
230			//	.css({'overflow' : 'scroll'}) //css
231				.appendTo(this.dom.window)
232			; //jQuery.debug
233			
234			// Making window draggable if possible
235			if ( $isDraggable )
236				this.dom.window	
237					.draggable('destroy')
238					.draggable({});	
239	
240			this.dom.window.appendTo('body');
241		} //IF new window
242		
243	}, //FUNC initWindow
244
245	/** //** ----= deinitWindow	=------------------------------------------------------------------------------\**//** \
246	*
247	*	Deinitiates main window.
248	*
249	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
250	deinitWindow	: function () {
251
252		// Nothing to do if removed already
253		if ( !this.dom.window )
254			return;
255
256		// Destroying draggable
257		if ( this.dom.window.is('.ui-draggable') )
258			this.dom.window.draggable('destroy');
259		
260		// Removing window element
261		this.dom.window.remove();
262		
263		// Cleaning self
264		this.dom.window = false;
265
266	}, //FUNC deinitWindow 
267
268// ---- PANELS -----------------------------------------------------------------------------------------------------------------
269
270	/** //** ----= initPanels	=------------------------------------------------------------------------------\**//** \
271	*
272	*	Initiates panels.
273	*
274	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
275	initPanels	: function () {
276		
277		// Creating panels wrapper
278		if ( !this.dom.panels )
279			this.dom.panels = jQuery('<div></div>')
280			//	.css({}) //css
281				.prependTo(this.dom.window)
282			; //jQuery.debug
283	
284	 	// Adding/updating custom panels, converting ones which given as arrays
285		for ( var $i in this.panels )
286			if ( isArray(this.panels[$i]) )
287				this.setPanel($i);
288			else
289				this.initPanel(this.panels[$i]);
290		
291	}, //FUNC initPanels
292
293	/** //** ----= deinitPanels	=------------------------------------------------------------------------------\**//** \
294	*
295	*	Initiates panels.
296	*
297	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
298	deinitPanels	: function () {
299	
300		// Nothing to do if no panels already
301		if ( !this.dom.panels )
302			return;
303
304		// Clearing panels
305		for ( var $i in this.panels )
306			this.deinitPanel(this.panels[$i]);
307		
308		// Removing element
309		this.dom.panels.remove();
310		
311		// Cleaning self
312		this.dom.panels = false;
313		
314	}, //FUNC deinitPanels
315
316	/** //** ----= setPanel	=--------------------------------------------------------------------------------------\**//** \
317	*
318	*	Adds named debug panel.
319	*
320	*	@param	string	$name		- Panel name to add.
321	*	@param	int	[$size]		- Panel relative size (width) percentage.
322	*	@param	object	[$watchObject]	- Object to watch in this panel.
323	*	@param	array	[$watchProps]	- Watch only this properties from this object.
324	*
325	*	@return SELF
326	*
327	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
328	setPanel	: function ($name, $size, $watchObject, $watchProps) {
329
330		// Checking for panel params
331		var $panel = this.panels[$name] || {}; 	
332
333		// Converting array
334		if ( isArray($panel) ) {
335			// Setting params from that array
336			$size		= $size || $panel[0];
337			$watchObject	= $watchObject || $panel[1];
338			$watchProps	= $watchProps || $panel[2];
339			
340			// Making sure it's object now, and resetting
341			$panel		= {};
342		} //IF array given
343
344		// Setting up
345		$panel.name	= $name;
346		$panel.title	= $name.capitalize();
347		$panel.size	= $size || 100;
348		$panel.watch	= $watchObject || false;
349		$panel.props	= $watchProps || false;
350		
351		// Resaving panel back, in case it's new
352		this.panels[$name] = $panel;
353		
354		// Done if not enabled
355		if ( !this.enabled )
356			return this;
357		
358		// Initiating panel
359		this.initPanel($panel);
360		
361		return this;
362	}, //FUNC setPanel	
363
364	/** //** ----= removePanel	=------------------------------------------------------------------------------\**//** \
365	*
366	*	Removes specified panel from console.
367	*
368	*	@param	string	$panel	- Panel name to remove.
369	*
370	*	@return SELF
371	*
372	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
373	removePanel	: function ($panel) {
374		
375		if ( !this.panels[$panel] )
376			return;
377	
378		// Deinitiating it first
379		this.deinitPanel(this.panels[$panel]);
380		
381		// Removing from list
382		delete(this.panels[$panel]);
383		
384		return this;
385	}, //FUNC removePanel
386	
387	/** //** ----= initPanel	=------------------------------------------------------------------------------\**//** \
388	*
389	*	Initiates given panel dom and events.
390	*
391	*	@param	object	$panel		- Panel to init. Should be link from internal .panels list.
392	*	
393	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
394	initPanel	: function ($panel) {
395
396		// Checking if panel should take full size or it's floating
397		var $isFull = $panel.size == 100;
398		
399		// Compiling css
400		var $css = {
401			'float'		: $isFull ? 'none' : 'left',
402			'clear'		: $isFull ? 'both' : 'none',
403			'width'		: $isFull ? 'auto' : $panel.size+'%',
404			'overflow'	: 'hidden',
405			'border-bottom' : '1px solid gray',
406		} // $css
407		
408		// Creating or updating element
409		if ( $panel.el ) {
410
411			$panel.el.css($css);
412
413		} else { //IF existing panel
414
415			$panel.el = jQuery('<div></div>')
416				.css($css) //css
417				.appendTo(this.dom.panels)
418			; //jQuery.$panel
419
420		} //IF new panel
421		
422	}, //FUNC initPanel
423
424	/** //** ----= deinitPanel	=------------------------------------------------------------------------------\**//** \
425	*
426	*	Initiates given panel dom and events.
427	*
428	*	@param	object	$panel		- Panel to deinit. Should be link from internal .panels list.
429	*
430	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
431	deinitPanel	: function ($panel) {
432
433		if ( !$panel.el )
434			return;
435
436		// Removing panel from dom
437		$panel.el.remove();
438		
439		// Cleaning panel element
440		$panel.el = false;
441		
442	}, //FUNC deinitPanel
443
444// ==== DUMP ===================================================================================================================
445
446	/** //** ----= dumpTo	=--------------------------------------------------------------------------------------\**//** \
447	*
448	*	Dumps given var into specified console panel (replaces contents). Uses delays to output.
449	*	Precise output: no output delay.
450	*
451	*	@param	string	$panel	- Panel name to input into.
452	*	@param	MIXED	$var	- Variable to dump
453	*
454	*	@return SELF
455	*
456	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
457	dumpTo		: function ($panel, $var) {
458		
459		// Doing nothing if not enabled
460		if ( !this.enabled )
461			return this;
462
463		// Panel can be ether string or object directly
464		if ( isStr($panel) )
465			$panel = this.panels[$panel];
466			
467		if ( !$panel || !$panel.el )
468			return this;
469		
470		$panel.el.html( this.dumper.dumpCap($panel.title, $var) );
471		
472		return this;
473	}, //FUNC dumpTo
474
475	/** //** ----= dump	=--------------------------------------------------------------------------------------\**//** \
476	*
477	*	Dumps given var into common console panel (adds at bottom).
478	*	Precise output: no output delay.
479	*
480	*	@param	MIXED	$var	- Variable to dump
481	*
482	*	@return SELF
483	*
484	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
485	dump		: function ($var) {
486
487		// Doing nothing if not enabled
488		if ( !this.enabled )
489			return this;
490		
491		var $con = this.dom.console; 
492		$con.html( $con.html() + this.dumper.dump($var) );
493
494		return this;
495	}, //FUNC dump
496
497	/** //** ----= clear	=--------------------------------------------------------------------------------------\**//** \
498	*
499	*	Clears common panel contents.
500	*	Precise output: no output delay.
501	*
502	*	@return SELF
503	*
504	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
505	clear		: function () {
506
507		// Doing nothing if not enabled
508		if ( !this.enabled )
509			return this;
510
511		var $con = this.dom.console; 
512		$con.html('');
513		
514		return this;
515	}, //FUNC clear
516
517// ---- UPDATES ----------------------------------------------------------------------------------------------------------------
518
519	/** //** ----= updateNow	=------------------------------------------------------------------------------\**//** \
520	*
521	*	Imideately updates watched objects (no update delay).
522	*
523	*	@return SELF
524	*
525	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
526	updateNow	: function () {
527		
528		// Doing nothing if not enabled
529		if ( !this.enabled )
530			return this;
531		
532	 	// Checking panels, which can be updated
533		for ( var $i in this.panels ) {
534			var $panel = this.panels[$i];
535
536			// Skipping if panel was not created somehow
537			if ( !$panel.el )
538				continue;
539			
540			// Skipping if not watchable
541			if ( $panel.watch === false )
542				continue;
543				
544			// Dumping whole object if no props specified
545			if ( $panel.props === false ) {
546				// Just dump into panel
547				$panel.el.html( this.dumper.dumpCap($panel.title, $panel.watch) );
548			} else { //IF whole object
549				
550				var $html = '';
551				
552				// Looping through all vars and compiling dump
553				for ( var $j in $panel.props ) {
554					
555					var $p = $panel.props[$j];
556					
557					// Skipping functions
558					if ( isFunction($p) )
559						continue;
560					
561					$html += this.dumper.dumpCap($p.capitalize(), $panel.watch[$p]);
562				} //FOR each $j
563			
564				$panel.el.html( $html );
565			} //IF properties specified
566			
567		} //FOR each panel
568		
569		return this;
570	}, //FUNC updateNow	
571
572	/** //** ----= update	=--------------------------------------------------------------------------------------\**//** \
573	*
574	*	Updates watched objects, with small delay (no lag update).
575	*
576	*	@return SELF
577	*
578	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
579	update	: function () {
580
581		// Doing nothing if not enabled
582		if ( !this.enabled )
583			return this;
584		
585		var $this = this;
586		
587		// Clearing prev delay, avoiding double updates
588		clearTimeout(this.timer);
589		
590		// Updating after small delay
591		this.timer = setTimeout( function () {
592			$this.updateNow();
593		}, this.updateDelay);
594		
595		return this;
596	}, //FUNC update
597
598	/** //** ----= setAutoupdate	=------------------------------------------------------------------------------\**//** \
599	*
600	*	Enables or disables autoupdate timer. Always disables if console disabled
601	*
602	*	@param	bool	$state	- Set TRUE to enable, FALSE otherwise.
603	*
604	*	@return SELF
605	*
606	\**//** --------------------------------------------------------------------------------------= by Mr.V!T =----/** //**/
607	setAutoupdate	: function ($state) {
608		
609		$state = !isEmpty($state);
610		var $this = this;
611		
612		this.autoupdate = $state;
613
614		// Clearing interval anyway, to be extra safe
615		clearInterval(this.interval);
616		
617		// Nothing to do if no autoupdate
618		if ( !this.enabled || !this.autoupdate )
619			return this;
620			
621		// Making sure update interval is bigget that delay
622		var $upd = this.updateInterval > this.updateDelay ? this.updateInterval : this.updateDelay + 5;
623		
624		// Setting up interval
625		this.interval = setInterval( function () {
626			$this.update();
627		}, $upd);
628			
629		return this;
630	}, //FUNC setAutoupdate
631
632}; //CLASS mwImgEd
633
634	// Applying options
635	$class.set($options);
636	
637	// Adding events
638	$class = vEventObject([], $class);
639
640	return $class; 
641} //CONSTRUCTOR vConsole

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.