Clean Site.
[dtn] / www / DTN_Technical_Details / Scripts / iWebDebug.js
1 //
2 //  iWeb - iWebDebug.js
3 //  Copyright (c) 2007-2008 Apple Inc. All rights reserved.
4 //
5
6 var debugTabString="  ";var cEscapeMap={"\n":"\\n","\t":"\\t","\'":"\\''","\b":"\\b","\r":"\\r","\f":"\\f","\\":"\\\\"};var gPendingOutput="";function cEscape(s)
7 {var r="";for(var i=0;i<s.length;++i)
8 {var ch=s.charAt(i);var cc=s.charCodeAt(i);var cr=cEscapeMap[ch];if(cr!==undefined)
9 {ch=cr;}
10 else if(cc<0x20)
11 {r+=('\\'+cc.toString(8));}
12 else if(cc>0x7e)
13 {r+=('\\u'+('0000'+cc.toString(16)).slice(-4));}
14 else
15 {r+=ch;}}
16 return r;}
17 function cUnescape(s)
18 {throw Unimplemented;}
19 function convertTextForHTML(s)
20 {s=s.replace(/&/g,"&amp;");s=s.replace(/</g,"&lt;");s=s.replace(/\n/g,"<br/>");s=s.replace(/ /g,"&nbsp;");return s;}
21 function debugPrintDiv()
22 {var debugDiv=$("debugDiv");if(debugDiv===null)
23 {if(document.body!==null)
24 {debugDiv=$(document.createElement("div"));if(debugDiv)
25 {var debugDivWrapper=document.createElement("div");debugDivWrapper.id="debugDivWrapper";debugDiv.id="debugDiv";var debugDivClearButton=document.createElement("input");debugDivClearButton.title="Clear Debug Area";debugDivClearButton.value="Clear";debugDivClearButton.type="button";debugDivClearButton.onclick=debugClear;debugDiv.innerHTML=gPendingOutput;debugDivWrapper.appendChild(debugDivClearButton);debugDivWrapper.appendChild(debugDiv)
26 document.body.appendChild(debugDivWrapper);}}}
27 if(debugDiv&&debugDiv.initialized!=true)
28 {debugDiv.setStyle({textAlign:"left",zOrder:0,backgroundColor:"#ffff99",marginTop:"10px",opacity:"1.0",fontFamily:"Courier",fontSize:"10pt",border:"2px solid red"});debugDiv.initialized=true;}
29 return debugDiv;}
30 function debugRelocateDiv()
31 {var debugDiv=$("debugDiv");if(debugDiv!=null)
32 {debugDiv.parentNode.removeChild(debugDiv);document.body.appendChild(debugDiv);}}
33 function debugClear()
34 {var debugDiv=$("debugDiv");if(debugDiv)
35 {debugDiv.innerHTML="";}}
36 function debugPrintHtml(s)
37 {var debugDiv=debugPrintDiv();if(debugDiv)
38 {debugDiv.innerHTML=debugDiv.innerHTML+s;}
39 else
40 {gPendingOutput+=s+"<br/>";}}
41 var debugPrintUsesNSLog=true;function debugPrint(s)
42 {if(debugPrintUsesNSLog&&window.console&&window.console.NSLog)
43 {window.console.NSLog(s);}
44 else
45 {s=convertTextForHTML(String(s));var debugDiv=debugPrintDiv();if(debugDiv)
46 {debugDiv.innerHTML=debugDiv.innerHTML+s+"<br/>";}
47 else
48 {gPendingOutput+=s+"<br/>";}}}
49 function Undefined()
50 {}
51 Undefined.prototype.toString=function()
52 {return"undefined";}
53 function asObject(v)
54 {if(typeof v=="number")
55 {return Number(v);}
56 if(typeof v=="object")
57 {return v;}
58 if(typeof v=="string")
59 {return v;}
60 if(typeof v=="boolean")
61 {return Boolean(v);}
62 if(typeof v=="undefined")
63 {return new Undefined();}
64 debugPrint("### didn't wrap value of type "+typeof v);return null;}
65 function stringWithFormat()
66 {var result="";for(var i=0;i<arguments.length;++i)
67 {var arg=asObject(arguments[i]);var argString="null";if(arg!==null)
68 {if(arg===undefined)
69 {argString="<arg "+i+" undefined>";}
70 else
71 {if(arg.toString!==undefined)
72 {argString=arg.toString();}
73 else
74 {argString="<arg "+i+" does not define toString()>";}}}
75 var pos=result.indexOf("%s");if(pos>=0)
76 {result=result.substr(0,pos)+argString+result.substr(pos+"%s".length);}
77 else
78 {if(i>0)
79 {result+=" ";}
80 result+=argString;}}
81 return result;}
82 var trace=function(){};function print()
83 {debugPrint(stringWithFormat.apply(this,arguments));}
84 function valueTypeString(value)
85 {if(value===null)
86 {return"null";}
87 var valueType=typeof value;if(valueType=="object")
88 {if(value.constructor==Array)
89 {return"Array";}
90 if(value.constructor==Number)
91 {return"Number";}
92 if(value.constructor==String)
93 {return"String";}
94 return"Object";}
95 return valueType;}
96 function isObject(obj)
97 {return obj&&typeof obj=="object";}
98 function isArray(obj)
99 {return isObject(obj)&&obj.constructor==Array;}
100 function isArrayLike(obj)
101 {return isObject(obj)&&obj.constructor===undefined&&obj.length!==undefined&&obj.item!==undefined;}
102 function debugObjectToString(name,obj)
103 {var resultString="";if(arguments.length==1)
104 {obj=arguments[0];name="";}
105 else
106 {name+=" = ";}
107 if(obj===undefined)
108 {resultString+=stringWithFormat("%s(undefined)\n",name);}
109 else if(obj===null)
110 {resultString+=stringWithFormat("%snull\n",name);}
111 else if((obj.constructor)&&obj.constructor==Function)
112 {resultString+=stringWithFormat("%s(function)\n",name);}
113 else if(isArray(obj))
114 {resultString+=stringWithFormat("%sarray of %s %s [\n",name,obj.length,obj.length==1?"item":"items");for(var i=0;i<obj.length;++i)
115 {resultString+=stringWithFormat("  %s : %s,\n",i,debugValueToString(obj[i]));}
116 resultString+=stringWithFormat("]\n");}
117 else if(isArrayLike(obj))
118 {resultString+=stringWithFormat("%s'array' of %s %s [\n",name,obj.length,obj.length==1?"item":"items");for(var i=0;i<obj.length;++i)
119 {resultString+=stringWithFormat("  %s : %s,\n",i,debugValueToString(obj[i]));}
120 resultString+=stringWithFormat("]\n");}
121 else if(isObject(obj))
122 {resultString+=stringWithFormat("%sobject {\n",name);try
123 {var fieldWidth=0;var keys=$H(obj).keys().sort();$A(keys).each(function(key)
124 {fieldWidth=Math.max(fieldWidth,key.length);});$A(keys).each(function(key)
125 {var attr=key;attrStr=(attr+"                               ").substring(0,fieldWidth);try
126 {resultString+=stringWithFormat("  %s : %s\n",attrStr,debugValueToString(obj[attr]));}
127 catch(e)
128 {print(e);print("  !!!attr=",attr,"(type is %s)",typeof obj[attr]);}});}
129 catch(e)
130 {debugPrintException(e);print("  ## can't enumerate object contents. Might be IE 7.");}
131 resultString+=stringWithFormat("}\n");}
132 else
133 {resultString+=stringWithFormat("%s%s(%s)\n",name,valueTypeString(obj),debugValueToString(obj));}
134 return resultString;}
135 function debugPrintObject(name,obj)
136 {if(arguments.length==1)
137 {obj=arguments[0];name="";}
138 else
139 {name+=" = ";}
140 if(obj===undefined)
141 {print("%s(undefined)",name);}
142 else if(obj===null)
143 {print("%snull",name);}
144 else if((obj.constructor)&&obj.constructor==Function)
145 {print("%s(function)",name);}
146 else if(isArray(obj))
147 {print("%sarray of %s %s [",name,obj.length,obj.length==1?"item":"items");for(var i=0;i<obj.length;++i)
148 {print("  %s : %s,",i,debugValueToString(obj[i]));}
149 print("]");}
150 else if(isArrayLike(obj))
151 {print("%s'array' of %s %s [",name,obj.length,obj.length==1?"item":"items");for(var i=0;i<obj.length;++i)
152 {print("  %s : %s,",i,debugValueToString(obj[i]));}
153 print("]");}
154 else if(isObject(obj))
155 {print("%sobject {",name);try
156 {var fieldWidth=0;var keys=$H(obj).keys().sort();$A(keys).each(function(key)
157 {fieldWidth=Math.max(fieldWidth,key.length);});$A(keys).each(function(key)
158 {var attr=key;attrStr=(attr+"                               ").substring(0,fieldWidth);try
159 {print("  %s : %s",attrStr,debugValueToString(obj[attr]));}
160 catch(e)
161 {print(e);print("  !!!attr=",attr,"(type is %s)",typeof obj[attr]);}});}
162 catch(e)
163 {debugPrintException(e);print("  ## can't enumerate object contents. Might be IE 7.");}
164 print("}");}
165 else
166 {print("%s%s(%s)",name,valueTypeString(obj),debugValueToString(obj));}}
167 var printObject=debugPrintObject;function debugPrintException(e)
168 {print("# Exception: %s",e.name);print("# Message  : %s",e.message);if(e.sourceURL)
169 {var file=e.sourceURL.match(/[^\/]*$/);if(file!==null)
170 {print("# File     : %s, Line:%s",file[0],e.line);}}}
171 function indentHtmlString(s)
172 {var r=debugTabString+s;r=r.replace(/<br\/>/g,"<br/>"+debugTabString);return r;}
173 function indentString(s)
174 {var r=debugTabString+s;r=r.replace(/\n/g,"\n"+debugTabString);return r;}
175 function debugValueToString(value,maxLength,parentStack,attributeStack,refs)
176 {var result="";var valueType=valueTypeString(value);if(arguments.length==1)
177 {maxLength=800;}
178 if(parentStack===undefined)
179 {parentStack=[];}
180 if(attributeStack===undefined)
181 {attributeStack=["this"];}
182 if(refs===undefined)
183 {refs={value:"this"};}
184 if(valueType=="null")
185 {result="null";}
186 else if(valueType=="function")
187 {result="(function)";}
188 else if(valueType=="undefined")
189 {result="(undefined)";}
190 else if(valueType=="Object")
191 {if(parentStack.length>2)
192 {result="...";}
193 else
194 {var first=true;var fieldWidth=0;var attrs=$H(value).keys().sort();$A(attrs).each(function(attr)
195 {fieldWidth=Math.max(fieldWidth,attr.length);});var newParentStack=parentStack.concat(value);$A(attrs).each(function(attr)
196 {var nextMaxLength=maxLength-result.length-2-(attr.length+2);var valueAttrString;var subValue=value[attr];if(typeof subValue!="function")
197 {if(!first)
198 {result=result+", ";}
199 first=false;if(typeof subValue=="object"&&newParentStack.contains(subValue))
200 {var index=$A(newParentStack).indexOf(value[attr]);valueAttrString="#cycle("+attributeStack[index]+")";}
201 else if(typeof subValue=="object"&&refs[subValue]!==undefined)
202 {valueAttrString="#ref("+refs[subValue]+")";}
203 else
204 {try
205 {var newAttributePath=attributeStack[attributeStack.length-1]+"."+attr;var newAttributeStack=attributeStack.concat(newAttributePath);refs[value[attr]]=newAttributePath;valueAttrString=debugValueToString(value[attr],nextMaxLength,newParentStack,newAttributeStack,refs);}
206 catch(e)
207 {valueAttrString="#exception";}}
208 var newResult=result+attr+": "+valueAttrString;if(newResult.length>maxLength)
209 {result+="...";}
210 else
211 {result=newResult;}}});}
212 result="{"+result+"}";}
213 else if(valueType=="Array")
214 {var arrayLength=value.length;for(var i=0;i<arrayLength;++i)
215 {if(i!==0)
216 {result=result+", ";}
217 var nextMaxLength=maxLength-result.length;var newResult=result+debugValueToString(value[i],nextMaxLength);if(newResult.length>maxLength)
218 {result+="...";break;}
219 result=newResult;}
220 result="["+result+"]";}
221 else if(valueType=="number")
222 {result=value.toString();}
223 else if(valueType=="boolean")
224 {result=value.toString();}
225 else if(valueType=="string")
226 {result='"'+value.toString()+'"';}
227 else
228 {result="(UNKNOWN TYPE: "+valueType+")";}
229 return result;}
230 var gFadeElement;var gFadeDelta=0;var gFadeTimeout=0;function nextFadeStep()
231 {var oldOpacity=(gFadeElement.style.opacity-0);if(((gFadeDelta>0)&&(oldOpacity<gFadeTarget))||((gFadeDelta<0)&&(oldOpacity>gFadeTarget)))
232 {var newOpacity=gFadeDelta+oldOpacity;gFadeElement.style.opacity=newOpacity;setTimeout(nextFadeStep,gFadeTimeout);}
233 else
234 {gFadeDelta=0;}}
235 function startFadeIn(element)
236 {if(gFadeDelta===0.0)
237 {setTimeout(nextFadeStep,gFadeTimeout);}
238 gFadeElement=element;gFadeTimeout=20;gFadeTarget=1.0;gFadeDelta=0.1;}
239 function startFadeOut(element)
240 {if(gFadeDelta===0.0)
241 {setTimeout(nextFadeStep,gFadeTimeout);}
242 gFadeElement=element;gFadeTimeout=20;gFadeTarget=0.0;gFadeDelta=-0.1;}
243 function onMouseOverDebugMenu()
244 {if(window.event.shiftKey)
245 {var debugMenu=$("debugMenu");debugMenu.setStyle({height:"",width:""});startFadeIn(debugMenu);}}
246 function documentResourceURL(ext)
247 {resourceUrl="";htmlUrl=document.URL;while((htmlUrl.length>0)&&(htmlUrl.slice(-5)!=".html"))
248 {htmlUrl=htmlUrl.slice(0,-1);}
249 if(htmlUrl.length>0)
250 {var components=htmlUrl.split("/");var filename=components.pop();filename=filename.slice(0,-5);var folderName=filename+"_files";components.push(folderName);components.push(filename+ext);resourceUrl=components.join("/");}
251 return resourceUrl;}
252 function showCSS()
253 {cssUrl=documentResourceURL(".css");if(cssUrl.length>0)
254 {window.open(cssUrl,"CSS");}}
255 function showJavaScript()
256 {cssUrl=documentResourceURL(".js");if(cssUrl.length>0)
257 {window.open(cssUrl,"JavaScript");}}
258 function closeDebugMenu()
259 {var debugMenu=$("debugMenu");debugMenu.setStyle({height:"10px",width:"10px"});startFadeOut(debugMenu);}
260 function dumpEntryData()
261 {var myEntryData="not defined";try{myEntryData=entryData;}catch(e){}
262 debugPrintObject(myEntryData);}
263 function dumpEntryURLs()
264 {var myEntryURLs="not defined";try{myEntryURLs=entryURLs;}catch(e){}
265 debugPrintObject(myEntryURLs);}
266 function jsEvalClick()
267 {try
268 {var text=$("jstext").value;debugPrint(text);eval(text);}
269 catch(e)
270 {debugPrint("** Exception **");debugPrintObject(e);}}
271 function scriptNodes()
272 {var result=[];var body=document.body;debugPrint(body.tagName);var html=body.parentNode;debugPrint(html.tagName);var head=$$('head');for(var i=0;i<head.childNodes.length;++i)
273 {var node=head.childNodes[i];if(node.nodeName=="SCRIPT")
274 {result.push(node);}}
275 return result;}
276 function showAllScripts()
277 {var scripts=scriptNodes();var scriptUrls=[];for(var i=0;i<scripts.length;++i)
278 {if(scripts[i].src!=="")
279 {scriptUrls.push(scripts[i].src);}}
280 debugPrintHtml('<br/><b>Scripts used on this page:</b><br/>');for(i=0;i<scriptUrls.length;++i)
281 {url=scriptUrls[i];var s='<a href="%url%" target="code">%url%</a><br/>';s=s.replace(/%url%/g,url);debugPrintHtml(s);}}
282 var gVariables={};var gVariableCount=0;var gRenderItemCount=0;var gRootVariables=[];function addInspectorVariable(varName)
283 {gRootVariables.push(varName);renderInspector();}
284 function inspect(varName)
285 {addInspectorVariable(varName);}
286 function getVariableId(variable)
287 {for(v in gVariables)
288 {if(gVariables[v].object===variable)
289 {return v;}}
290 var vid="vid"+gVariableCount++;record={};record.object=variable;record.id=vid;record.open=false;record.showFunctions=false;gVariables[vid]=record;return vid;}
291 function clickItem(vid)
292 {gVariables[vid].open=!gVariables[vid].open;renderInspector();}
293 function toggleFuncs(vid)
294 {gVariables[vid].showFunctions=!gVariables[vid].showFunctions;renderInspector();}
295 function clickDelete(vid)
296 {for(var index in gRootVariables)
297 {if(gRootVariables[index]==vid)
298 {gRootVariables.splice(index,1);renderInspector();return;}}}
299 function makeControlSpan(vid,functionName,flag,onString,offString)
300 {var span=document.createElement("span");span.setAttribute("onclick",functionName+"('"+vid+"');");span.innerText=flag?onString:offString;return span;}
301 function renderInspectorItem(name,thing,parent,parentStack)
302 {gRenderItemCount++;var div=$(document.createElement("div"));div.setStyle({left:"30px",position:"relative"});var span=document.createElement("span");var text=" "+name+" = ";var vid;if(typeof thing=="object")
303 {if(thing.constructor==Array)
304 {text+="array["+thing.length+"] "+debugValueToString(thing);}
305 else
306 {text+="object "+debugValueToString(thing);}
307 vid=getVariableId(thing);span=makeControlSpan(vid,"clickItem",gVariables[vid].open,"-","+");}
308 else
309 {span.innerText="-";text+=debugValueToString(thing);}
310 var textNode=document.createTextNode(text);div.appendChild(span);div.appendChild(textNode);var closeSpan=null;if(gRootVariables.contains(name))
311 {closeSpan=makeControlSpan(name,"clickDelete",true,"[X]","[X]");div.appendChild(closeSpan);}
312 if(typeof thing=="object")
313 {if((gVariables[vid].open)&&!parentStack.contains(thing))
314 {var funcSpan=makeControlSpan(vid,"toggleFuncs",gVariables[vid].showFunctions,"[F]","[f]");div.insertBefore(funcSpan,closeSpan);try
315 {$H(thing).keys().sort().each(function(item)
316 {if((typeof thing[item]!="function")||(gVariables[vid].showFunctions))
317 {renderInspectorItem(item,thing[item],div,parentStack.concat(thing[item]));}});}
318 catch(e)
319 {}}}
320 parent.appendChild(div);}
321 function renderInspector()
322 {gRenderItemCount=0;var inspectorDiv=$("inspect");if(inspectorDiv===null)
323 {inspectorDiv=$(document.createElement("div"));inspectorDiv.id="inspect";inspectorDiv.setStyle({backgroundColor:"#d8d8d8",fontFamily:"Courier",fontSize:"10pt"});document.body.appendChild(inspectorDiv);}
324 while(inspectorDiv.childNodes.length>0)
325 {inspectorDiv.removeChild(inspectorDiv.childNodes[0]);}
326 var emptyArray=[];for(var index in gRootVariables)
327 {if(emptyArray[index]===undefined)
328 {var thing=eval(gRootVariables[index]);renderInspectorItem(gRootVariables[index],eval(gRootVariables[index]),inspectorDiv,[]);}}}
329 function evalOnKeyUp(e)
330 {if(e.keyIdentifier=="Enter")
331 {jsEvalClick();}}
332 function iWebDebugPanelInit()
333 {var headerLayer=document.body;var debugMenu=$(document.createElement("div"));debugMenu.id="debugMenu";debugMenu.setStyle({backgroundColor:"#ffff99",position:"fixed",left:0,top:0,width:"10px",height:"10px",padding:"10px",opacity:0,fontFamily:"Lucida Grande",fontSize:"10px",zIndex:100,overflow:"hidden",border:"1px solid black"});debugMenu.onmouseover=onMouseOverDebugMenu;headerLayer.appendChild(debugMenu);var myCommentsVersion="not defined";try{myCommentsVersion=commentJavascriptVersion;}catch(e){}
334 debugMenu.innerHTML="<b><u>JavaScript Debug Options</u></b>"+"<div style='float:right'><a href='#' onclick='closeDebugMenu();'>Close</a></div><br/>"+"<br/>"+"<a href='#' onclick='showCSS();'>Show Page CSS</a><br/>"+"<a href='#' onclick='showJavaScript();'>Show Page JavaScript</a><br/>"+"<a href='#' onclick='showAllScripts();'>List all scripts</a><br/>"+"<br/>"+"<a href='#' onclick='dumpEntryData();'>Show comment entryData</a><br/>"+"<a href='#' onclick='dumpEntryURLs();'>Show comment summaryData</a><br/>"+"<a href='#' onclick='debugClear();'>Clear debug output</a><br/>"+"<br/>"+"<textarea id='jstext' cols='40' rows='4'/>inspect(window);</textarea><br/>"+"<br/><hr/>"+"Comment js version: "+myCommentsVersion;var textArea=$('jstext');if(textArea)
335 {textArea.onkeyup=function(e)
336 {if(e.keyIdentifier=="Enter")
337 {try
338 {var text=$("jstext").value;debugPrintHtml(text.bold()+"<br/>");eval(text);}
339 catch(e)
340 {debugPrint("** Exception **");debugPrintObject(e);}
341 if(textArea.setSelectionRange)
342 {textArea.setSelectionRange(0,textArea.value.length);}
343 e.cancelBubble=true;}};}
344 renderInspector();}