source: dashboard/lib/js/debug.js@ 366

Last change on this file since 366 was 335, checked in by Rick van der Zwet, 13 years ago

New layout and UI

File size: 7.8 KB
Line 
1/*
2 * DEMO HELPERS
3 */
4
5
6/**
7 * debugData
8 *
9 * Pass me a data structure {} and I'll output all the key/value pairs - recursively
10 *
11 * @example var HTML = debugData( oElem.style, "Element.style", { keys: "top,left,width,height", recurse: true, sort: true, display: true, returnHTML: true });
12 *
13 * @param Object o_Data A JSON-style data structure
14 * @param String s_Title Title for dialog (optional)
15 * @param Hash options Pass additional options in a hash
16 */
17function debugData (o_Data, s_Title, options) {
18 options = options || {};
19 var
20 str=(s_Title || 'DATA')
21 // maintain backward compatibility with OLD 'recurseData' param
22 , recurse=(typeof options=='boolean' ? options : options.recurse !==false)
23 , keys=(options.keys?','+options.keys+',':false)
24 , display=options.display !==false
25 , html=options.returnHTML !==false
26 , sort=options.sort !==false
27 , D=[], i=0 // Array to hold data, i=counter
28 , hasSubKeys = false
29 , k, t, skip, x // loop vars
30 ;
31 s_Title=s_Title ? s_Title+'\n':'';
32
33 if (typeof o_Data != 'object') {
34 alert( s_Title + o_Data );
35 return;
36 }
37 if (o_Data.jquery) {
38 str=s_Title+'jQuery Collection ('+ o_Data.length +')\n context="'+ o_Data.context +'"';
39 }
40 else if (o_Data.tagName && typeof o_Data.style == 'object') {
41 str=s_Title+o_Data.tagName;
42 var id = o_Data.id, cls=o_Data.className, src=o_Data.src, hrf=o_Data.href;
43 if (id) str+='\n id="'+ id+'"';
44 if (cls) str+='\n class="'+ cls+'"';
45 if (src) str+='\n src="'+ src+'"';
46 if (hrf) str+='\n href="'+ hrf+'"';
47 }
48 else {
49 parse(o_Data,''); // recursive parsing
50 if (sort && !hasSubKeys) D.sort(); // sort by keyName - but NOT if has subKeys!
51 str+='\n***'+'****************************'.substr(0,str.length);
52 str+='\n'+ D.join('\n'); // add line-breaks
53 }
54
55 if (display) alert(str); // display data
56 if (html) str=str.replace(/\n/g, ' <br>').replace(/ /g, ' &nbsp;'); // format as HTML
57 return str;
58
59 function parse ( data, prefix ) {
60 if (typeof prefix=='undefined') prefix='';
61 try {
62 $.each( data, function (key, val) {
63 k = prefix+key+': ';
64 skip = (keys && keys.indexOf(','+key+',') == -1);
65 if (typeof val=='function') { // FUNCTION
66 if (!skip) D[i++] = k +'function()';
67 }
68 else if (typeof val=='string') { // STRING
69 if (!skip) D[i++] = k +'"'+ val +'"';
70 }
71 else if (typeof val !='object') { // NUMBER or BOOLEAN
72 if (!skip) D[i++] = k + val;
73 }
74 else if (isArray(val)) { // ARRAY
75 if (!skip) {
76 if (val.length && typeof val[0] == "object") { // array of objects (hashs or arrays)
77 D[i++] = k +'{';
78 parse( val, prefix+' '); // RECURSE
79 D[i++] = prefix +'}';
80 }
81 else
82 D[i++] = k +'[ '+ val.toString() +' ]'; // output delimited array
83 }
84 }
85 else if (val.jquery) {
86 if (!skip) D[i++] = k +'jQuery ('+ val.length +') context="'+ val.context +'"';
87 }
88 else if (val.tagName && typeof val.style == 'object') {
89 var id = val.id, cls=val.className, src=val.src, hrf=val.href;
90 if (skip) D[i++] = k +' '+
91 id ? 'id="'+ id+'"' :
92 src ? 'src="'+ src+'"' :
93 hrf ? 'href="'+ hrf+'"' :
94 cls ? 'class="'+cls+'"' :
95 '';
96 }
97 else { // Object or JSON
98 if (!recurse || !hasKeys(val)) { // show an empty hash
99 if (!skip) D[i++] = k +'{ }';
100 }
101 else { // recurse into JSON hash - indent output
102 D[i++] = k +'{';
103 parse( val, prefix+' '); // RECURSE
104 D[i++] = prefix +'}';
105 }
106 }
107 });
108 } catch (e) {}
109 function isArray(o) {
110 return (o && typeof o==='object' && !o.propertyIsEnumerable('length') && typeof o.length==='number');
111 }
112 function hasKeys(o) {
113 var c=0;
114 for (x in o) c++;
115 if (!hasSubKeys) hasSubKeys = !!c;
116 return !!c;
117 }
118 }
119};
120
121if (!window.console) window.console = { log: debugData };
122
123
124/**
125 * timer
126 *
127 * Utility for debug timing of events
128 * Can track multiple timers and returns either a total time or interval from last event
129 * @param String timerName Name of the timer - defaults to debugTimer
130 * @param String action Keyword for action or return-value...
131 * action: 'reset' = reset; 'clear' = delete; 'total' = ms since init; 'step' or '' = ms since last event
132 */
133/**
134 * timer
135 *
136 * Utility method for timing performance
137 * Can track multiple timers and returns either a total time or interval from last event
138 *
139 * returns time-data: {
140 * start: Date Object
141 * , last: Date Object
142 * , step: 99 // time since 'last'
143 * , total: 99 // time since 'start'
144 * }
145 *
146 * USAGE SAMPLES
147 * =============
148 * timer('name'); // create/init timer
149 * timer('name', 'reset'); // re-init timer
150 * timer('name', 'clear'); // clear/remove timer
151 * var i = timer('name'); // how long since last timer request?
152 * var i = timer('name', 'total'); // how long since timer started?
153 *
154 * @param String timerName Name of the timer - defaults to debugTimer
155 * @param String action Keyword for action or return-value...
156 * @param Hash options Options to customize return data
157 * action: 'reset' = reset; 'clear' = delete; 'total' = ms since init; 'step' or '' = ms since last event
158 */
159function timer (timerName, action, options) {
160 var
161 name = timerName || 'debugTimer'
162 , Timer = window[ name ]
163 , defaults = {
164 returnString: true
165 , padNumbers: true
166 , timePrefix: ''
167 , timeSuffix: ''
168 }
169 ;
170
171 // init the timer first time called
172 if (!Timer || action == 'reset') { // init timer
173 Timer = window[ name ] = {
174 start: new Date()
175 , last: new Date()
176 , step: 0 // time since 'last'
177 , total: 0 // time since 'start'
178 , options: $.extend({}, defaults, options)
179 };
180 }
181 else if (action == 'clear') { // remove timer
182 window[ name ] = null;
183 return null;
184 }
185 else { // update existing timer
186 Timer.step = (new Date()) - Timer.last; // time since 'last'
187 Timer.total = (new Date()) - Timer.start; // time since 'start'
188 Timer.last = new Date();
189 }
190
191 var
192 time = (action == 'total') ? Timer.total : Timer.step
193 , o = Timer.options // alias
194 ;
195
196 if (o.returnString) {
197 time += ""; // convert integer to string
198 // pad time to 4 chars with underscores
199 if (o.padNumbers)
200 switch (time.length) {
201 case 1: time = "&ensp;&ensp;&ensp;"+ time; break;
202 case 2: time = "&ensp;&ensp;"+ time; break;
203 case 3: time = "&ensp;"+ time; break;
204 }
205 // add prefix and suffix
206 if (o.timePrefix || o.timeSuffix)
207 time = o.timePrefix + time + o.timeSuffix;
208 }
209
210 return time;
211};
212
213
214/**
215 * showOptions
216 *
217 * Pass a layout-options object, and the pane/key you want to display
218 */
219function showOptions (o_Settings, key, options) {
220 var data = o_Settings.options;
221 $.each(key.split("."), function() {
222 data = data[this]; // resurse through multiple levels
223 });
224 debugData( data, 'options.'+key, options );
225};
226
227/**
228 * showState
229 *
230 * Pass a layout-options object, and the pane/key you want to display
231 */
232function showState (o_Settings, key) {
233 debugData( o_Settings.state[key], 'state.'+key );
234};
235
236
237/**
238 * addThemeSwitcher
239 *
240 * Remove the cookie set by the UI Themeswitcher to reset a page to default styles
241 *
242 * Dependancies: /lib/js/themeswitchertool.js
243 */
244function addThemeSwitcher ( container, position ) {
245 var pos = { top: '10px', right: '10px', zIndex: 10 };
246 $('<div id="themeContainer" style="position: absolute; overflow-x: hidden;"></div>')
247 .css( $.extend( pos, position ) )
248 .appendTo( container || 'body')
249 .themeswitcher()
250 ;
251};
252
253/**
254 * removeUITheme
255 *
256 * Remove the cookie set by the UI Themeswitcher to reset a page to default styles
257 */
258function removeUITheme ( cookieName, removeCookie ) {
259 $('link.ui-theme').remove();
260 $('.jquery-ui-themeswitcher-title').text( 'Switch Theme' );
261 if (removeCookie !== false)
262 $.cookie( cookieName || 'jquery-ui-theme', null );
263};
264
Note: See TracBrowser for help on using the repository browser.