root / tei / js / debug.js @ 2
Historique | Voir | Annoter | Télécharger (7,84 ko)
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 |
*/
|
17 |
function 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, ' '); // 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 |
|
121 |
if (!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 |
*/
|
159 |
function 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 || o.padNumbers || o.timePrefix || o.timeSuffix)
|
197 |
time += ""; // convert integer to string |
198 |
|
199 |
// pad time to 4 chars with underscores
|
200 |
if (o.padNumbers)
|
201 |
switch (time.length) {
|
202 |
case 1: time = "   "+ time; break; |
203 |
case 2: time = "  "+ time; break; |
204 |
case 3: time = " "+ time; break; |
205 |
} |
206 |
|
207 |
// add prefix and suffix
|
208 |
if (o.timePrefix || o.timeSuffix)
|
209 |
time = o.timePrefix + time + o.timeSuffix; |
210 |
|
211 |
return time;
|
212 |
}; |
213 |
|
214 |
|
215 |
/**
|
216 |
* showOptions
|
217 |
*
|
218 |
* Pass a layout-options object, and the pane/key you want to display
|
219 |
*/
|
220 |
function showOptions (o_Settings, key, options) { |
221 |
var data = o_Settings.options;
|
222 |
$.each(key.split("."), function() { |
223 |
data = data[this]; // resurse through multiple levels |
224 |
}); |
225 |
debugData( data, 'options.'+key, options );
|
226 |
}; |
227 |
|
228 |
/**
|
229 |
* showState
|
230 |
*
|
231 |
* Pass a layout-options object, and the pane/key you want to display
|
232 |
*/
|
233 |
function showState (o_Settings, key) { |
234 |
debugData( o_Settings.state[key], 'state.'+key );
|
235 |
}; |
236 |
|
237 |
|
238 |
/**
|
239 |
* addThemeSwitcher
|
240 |
*
|
241 |
* Remove the cookie set by the UI Themeswitcher to reset a page to default styles
|
242 |
*
|
243 |
* Dependancies: /lib/js/themeswitchertool.js
|
244 |
*/
|
245 |
function addThemeSwitcher ( container, position ) { |
246 |
var pos = { top: '10px', right: '10px', zIndex: 10 }; |
247 |
$('<div id="themeContainer" style="position: absolute; overflow-x: hidden;"></div>') |
248 |
.css( $.extend( pos, position ) )
|
249 |
.appendTo( container || 'body')
|
250 |
.themeswitcher() |
251 |
; |
252 |
}; |
253 |
|
254 |
/**
|
255 |
* removeUITheme
|
256 |
*
|
257 |
* Remove the cookie set by the UI Themeswitcher to reset a page to default styles
|
258 |
*/
|
259 |
function removeUITheme ( cookieName, removeCookie ) { |
260 |
$('link.ui-theme').remove(); |
261 |
$('.jquery-ui-themeswitcher-title').text( 'Switch Theme' ); |
262 |
if (removeCookie !== false) |
263 |
$.cookie( cookieName || 'jquery-ui-theme', null ); |
264 |
}; |
265 |
|