Subversion Repositories wimsdev

Rev

Rev 20 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.         DynAPI Distribution
  3.         Debugger
  4.  
  5.         The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
  6. */
  7.  
  8. // Note: Debugger does not have to be a DynObject - very important for blueprinted layers
  9. function Debugger() {
  10.         this._mode='normal';
  11.         this.win = null;
  12.         this._watch={};
  13.         this._evalBuffer='';
  14.         this._buffer = dynapi._debugBuffer;
  15.         dynapi._debugBuffer = '';
  16.         // close the debug window on unload
  17.         this.closeOnUnLoad = false;
  18.         dynapi.onUnload(function() {
  19.                 if (dynapi.debug.closeOnUnLoad) dynapi.debug.close();
  20.         });
  21.  
  22.         this.open();
  23. }
  24. var debugProto = Debugger.prototype; //dynapi.setPrototype('Debugger','DynObject');
  25. debugProto.close = function() {
  26.         if (this.isLoaded()) {
  27.                 this.win.close();
  28.                 this.win = null;
  29.         }
  30. };
  31. // error - output a browser generated error to the debug window
  32. debugProto.error = function(msg, url, lno) {
  33.         if (url && url.indexOf(dynapi.documentPath)==0) {
  34.                 url = url.substring(dynapi.documentPath.length);
  35.         }
  36.         this.print('Error:'+ (lno? ' Line '+lno : '') +' ['+url+']\n       '+msg);
  37. };
  38. // evaluates an expression in the scope of the main dynapi window
  39. debugProto.evaluate = function(str) {
  40.         dynapi.frame.eval(str);
  41.         this.setEvalHistory(str);
  42. };
  43. // get evaluation history
  44. debugProto.getEvalHistory=function(n){
  45.         if(!this.isLoaded()) return;
  46.         var t,f=this.win.document.debugform;
  47.         if(n>=1) {
  48.                 var lim=this.win.evalHistory.length-1;
  49.                 this.win.evalIndex++;
  50.                 if (this.win.evalIndex>lim) this.win.evalIndex=(lim<0)?0:lim;
  51.                 t=this.win.evalHistory[this.win.evalIndex];
  52.                 if(t)f.eval.value=t;
  53.         }else if(n<=0){
  54.                 this.win.evalIndex--;
  55.                 if(this.win.evalIndex<0) this.win.evalIndex=0;
  56.                 t=this.win.evalHistory[this.win.evalIndex];
  57.                 if(t)f.eval.value=t;
  58.         }
  59. };
  60. // lists all known properties of an object
  61. debugProto.inspect = function(obj,showFunctions) {
  62.         this.print('Inspecting:');
  63.         var v;
  64.         if (typeof(obj)=='string') obj=eval(obj);
  65.         if (typeof(obj)=='object') {
  66.                 for (var i in obj) {
  67.                         if (obj[i]==null) v = 'null'
  68.                         else if (typeof(obj[i])=='undefined') v = 'null';
  69.                         else if (typeof(obj[i])=='function') {
  70.                                 if (showFunctions==false) continue;
  71.                                 else v = '[Function]';
  72.                         }
  73.                         else if (typeof(obj[i])=='object' && typeof(obj[i].length)!='undefined') v = 'Array';// ['+obj[i]+']';
  74.                         else if (typeof(obj[i])=='object') v = '[Object]';
  75.                         else v = obj[i];
  76.                         this.print('    '+i+' = '+v);
  77.                 }
  78.         }
  79.         else this.print('    undefined');
  80. };
  81. debugProto.isLoaded = function() {
  82.         return (this.win!=null && this.win.document && typeof(this.win.document.debugform)=="object");
  83. };
  84. // opens the debugger window
  85. debugProto.open = function() {
  86.         var p = dynapi.library.path;
  87.         if (!this.isLoaded() && p) {
  88.                 var url = dynapi.documentPath+p+'ext/debug.html#';
  89.                 var w = (dynapi.ua.def||dynapi.ua.dom)? 350:355 //dynapi.ua.mac? (dynapi.ua.ie?330:300) : 350;
  90.                 var h = (dynapi.ua.def||dynapi.ua.dom)? 432:485 //dynapi.ua.mac? (dynapi.ua.ie?405:365) : (dynapi.ua.def||dynapi.ua.dom)? 420:476;
  91.                 this.win = window.open(url,'debugwin','width='+w+',height='+h+',scrollbars=no,status=no,toolbar=no');  //,resizable=no
  92.                 // Works better with Mozilla
  93.                 this.win.opener=window;
  94.                 this.win.evalHistory=[];
  95.                 this.win.evalIndex=0;
  96.                 this.print();
  97.         /*      dynapi.frame.onerror = function(msg, url, lno) {
  98.                         dynapi.debug.error(msg, url, lno);
  99.                 };
  100.                 */
  101.         }
  102. };
  103. // output text to the debug window
  104. debugProto.print = function(s) {
  105.         if (s==null) s = '';
  106.         else s = s + '\n';
  107.         if (this.isLoaded()) {
  108.                 this.switchMode('normal');
  109.                 if (this._buffer != '') {  // dump buffer
  110.                         s = this._buffer + s;
  111.                         this._buffer = '';
  112.                 }
  113.                 this.win.document.debugform.print.value += s;
  114.                 this._normalModeData = this.win.document.debugform.print.value;
  115.  
  116.                 // Does mozilla has something like this?
  117.                 if (dynapi.ua.ie) {
  118.                         var po = this.win.document.debugform.print;
  119.                         po.scrollTop = po.scrollHeight;
  120.                         var range = po.createTextRange();
  121.                         range.collapse(false);
  122.                         range.select();
  123.                 }
  124.         }
  125.         else this._buffer += s;
  126. };
  127. // reloads selected javascripts, packages or html pages
  128. debugProto.reload=function(t){
  129.         if (!this.isLoaded) return;
  130.         t=t+'';
  131.         if(t.substr(0,3).toLowerCase()=='go:') {
  132.                 t=t.substr(3).replace(/\\/g,'/');
  133.                 dynapi.frame.location.href=t;
  134.                 return;
  135.         }
  136.         var i,f=t.split(';');
  137.         for(i=0;i<f.length;i++){
  138.                 t=f[i];
  139.                 if(t.indexOf('.js')<0) dynapi.library.load(t,null,true);
  140.                 else {
  141.                         var lib=dynapi.library;
  142.                         if (!lib.scripts[t]) lib.loadScript(t);
  143.                         else lib.reloadScript(t,null,true);
  144.                 }
  145.         }
  146.         if(this.win.focus) this.win.focus();
  147.         else this.win.setZIndex({topmost:true});
  148. };
  149. debugProto.reset=function(section){
  150.         if (!this.isLoaded) return;
  151.         this._oldWatchSrc='';
  152.         if(!section) {
  153.                 this.win.document.debugform.reset();
  154.                 this._normalModeData='';
  155.                 this.switchMode('normal');
  156.         }else{
  157.                 var t=this.win.document.debugform[section];
  158.                 if(t) t.value='';
  159.         }
  160. };
  161. debugProto.status = function(str) {
  162.         if (this.isLoaded()) {
  163.                 for (var i=1;i<arguments.length;i++) {
  164.                         str += ', '+arguments[i];
  165.                 }
  166.                 this.win.document.debugform.stat.value = str;
  167.         };
  168. };
  169. // Set Mode
  170. debugProto.switchMode=function(m){
  171.         if (!this.isLoaded) return;
  172.         if(m=='watch'||(this._mode=='normal' && m!='normal')) {
  173.                 this._normalModeData = this.win.document.debugform.print.value;
  174.                 this._mode='watch';
  175.                 this._enableWatch();
  176.         }else if(m=='normal'||(this._mode=='watch' && m!='watch')){
  177.                 this.win.document.debugform.print.value=(this._normalModeData)?this._normalModeData:'';
  178.                 this._mode='normal';
  179.                 this._disableWatch();
  180.         }
  181. };
  182. // enters text to the evaluate field in the debugger widnow
  183. debugProto.setEvaluate = function(str) {
  184.         if (!this.isLoaded()) this._evalBuffer=str;
  185.         else {
  186.                 if (!str) str = '';
  187.                 if(this._evalBuffer!='') {
  188.                         str =this._evalBuffer+str;
  189.                         this._evalBuffer='';
  190.                 }
  191.                 this.win.document.debugform.eval.value = str;
  192.                 this.setEvalHistory(str);
  193.         }
  194. };
  195. // Set previous evaluation information
  196. debugProto.setEvalHistory=function(s){
  197.         if(!this.isLoaded()) return;
  198.         var i,found;
  199.         if(s){
  200.                 for(i=0;i<this.win.evalHistory.length;i++){
  201.                         if(this.win.evalHistory[i]==s) {found=i;break;}
  202.                 }
  203.                 if(found!=null) this.win.evalHistory=dynapi.functions.removeFromArray(this.win.evalHistory,found);
  204.                 this.win.evalHistory[this.win.evalHistory.length]=s;
  205.                 this.win.evalIndex=this.win.evalHistory.length-1;
  206.         }
  207. };
  208. debugProto.showHelp=function(){
  209.         var t=''
  210.         +'-----------------------\n'
  211.         +'Quick Help\n'
  212.         +'-----------------------\n'
  213.         +'1) To inspect an Object enter the name\n'
  214.         +'of the object in the "Inspect Variable/Object"\n'
  215.         +'textbox and then click on the "Inspect" button\n\n'
  216.         +'2) To Load/Reload a DynAPI Package,\n'
  217.         +'javascript or html page enter the name\n'
  218.         +'of the package or javascript in the reload\n'
  219.         +'text. For HTML pages type the prefix Go:\n'
  220.         +'before the page name.\n'
  221.         +'------------------------------------------------';
  222.         this.print(t);
  223. };
  224. // watch object variables;
  225. debugProto.watch = function(name,value){
  226.         if(arguments.length>1) this._watch[name]=value;
  227.         else if(dynapi.frame.eval(name)) this._watch[name]='_watch object_';
  228.         else this._watch[name]='_watch object_';
  229. };
  230.  
  231. debugProto._disableWatch = function(){
  232.         this._oldWatchSrc='';
  233.         if(this._timerWatch) {
  234.                 window.clearTimeout(this._timerWatch);
  235.                 this._timerWatch=0;
  236.         }
  237. };
  238. debugProto._enableWatch = function(){
  239.         if(this._mode!='watch') return;
  240.         var src,row,v;
  241.         src='Name\t \t \t Value\n---------------------------------------\n';
  242.         for(i in this._watch){
  243.                 if(this._watch[i]=='_watch object_') v=dynapi.frame.eval(i);
  244.                 else v=this._watch[i];
  245.                 if(v==null) v='null';
  246.                 if(typeof(v)=='string') v=v.replace(/\n/g,' ');
  247.                 src+=(i+'                      ').substr(0,22)+'\t '+v+'\n';
  248.         }
  249.         if(src!=this._oldWatchSrc){
  250.                 this.win.document.debugform.print.value=this._oldWatchSrc=src;
  251.         }
  252.         if(this._timerWatch) window.clearTimeout(this._timerWatch);
  253.         this._timerWatch=window.setTimeout(this+'._enableWatch()',200);
  254. };
  255.  
  256.  
  257. dynapi.debug = new Debugger();
  258.  
  259. var t='------------------------------\n'
  260. +'Click "?" for help\n'
  261. +'------------------------------\n';
  262. dynapi.debug.print(t);
  263.