Subversion Repositories wimsdev

Rev

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 p = Debugger.prototype; //dynapi.setPrototype('Debugger','DynObject');
  25. p.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. p.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. p.evaluate = function(str) {
  40.         dynapi.frame.eval(str);
  41.         this.setEvalHistory(str);
  42. };
  43. // get evaluation history
  44. p.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. p.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. p.isLoaded = function() {
  82.         return (this.win!=null && this.win.document && typeof(this.win.document.debugform)=="object");
  83. };
  84. // opens the debugger window
  85. p.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.                 this.win.opener=window;
  93.                 this.win.evalHistory=[];
  94.                 this.win.evalIndex=0;
  95.                 this.print();
  96.         /*      dynapi.frame.onerror = function(msg, url, lno) {                       
  97.                         dynapi.debug.error(msg, url, lno);
  98.                 };
  99.                 */
  100.         }
  101. };
  102. // output text to the debug window
  103. p.print = function(s) {
  104.         if (s==null) s = '';
  105.         else s = s + '\n';
  106.         if (this.isLoaded()) {
  107.                 this.switchMode('normal');
  108.                 if (this._buffer != '') {  // dump buffer
  109.                         s = this._buffer + s;
  110.                         this._buffer = '';
  111.                 }
  112.                 this.win.document.debugform.print.value += s;
  113.                 this._normalModeData = this.win.document.debugform.print.value;
  114.                
  115.                 // Does mozilla has something like this?
  116.                 if (dynapi.ua.ie) {
  117.                         var po = this.win.document.debugform.print;
  118.                         po.scrollTop = po.scrollHeight;
  119.                         var range = po.createTextRange();
  120.                         range.collapse(false);
  121.                         range.select();
  122.                 }
  123.         }
  124.         else this._buffer += s;
  125. };
  126. // reloads selected javascripts, packages or html pages
  127. p.reload=function(t){
  128.         if (!this.isLoaded) return;    
  129.         t=t+'';
  130.         if(t.substr(0,3).toLowerCase()=='go:') {
  131.                 t=t.substr(3).replace(/\\/g,'/');
  132.                 dynapi.frame.location.href=t;
  133.                 return;
  134.         }
  135.         var i,f=t.split(';');
  136.         for(i=0;i<f.length;i++){
  137.                 t=f[i];
  138.                 if(t.indexOf('.js')<0) dynapi.library.load(t,null,true);
  139.                 else {
  140.                         var lib=dynapi.library;
  141.                         if (!lib.scripts[t]) lib.loadScript(t);
  142.                         else lib.reloadScript(t,null,true);
  143.                 }
  144.         }
  145.         if(this.win.focus) this.win.focus();
  146.         else this.win.setZIndex({topmost:true});
  147. };
  148. p.reset=function(section){
  149.         if (!this.isLoaded) return;    
  150.         this._oldWatchSrc='';
  151.         if(!section) {
  152.                 this.win.document.debugform.reset();
  153.                 this._normalModeData='';
  154.                 this.switchMode('normal');
  155.         }else{
  156.                 var t=this.win.document.debugform[section];
  157.                 if(t) t.value='';
  158.         }
  159. };
  160. p.status = function(str) {
  161.         if (this.isLoaded()) {
  162.                 for (var i=1;i<arguments.length;i++) {
  163.                         str += ', '+arguments[i];
  164.                 }
  165.                 this.win.document.debugform.stat.value = str;
  166.         };
  167. };
  168. // Set Mode
  169. p.switchMode=function(m){
  170.         if (!this.isLoaded) return;    
  171.         if(m=='watch'||(this._mode=='normal' && m!='normal')) {
  172.                 this._normalModeData = this.win.document.debugform.print.value;
  173.                 this._mode='watch';
  174.                 this._enableWatch();
  175.         }else if(m=='normal'||(this._mode=='watch' && m!='watch')){
  176.                 this.win.document.debugform.print.value=(this._normalModeData)?this._normalModeData:'';
  177.                 this._mode='normal';   
  178.                 this._disableWatch();
  179.         }
  180. };
  181. // enters text to the evaluate field in the debugger widnow
  182. p.setEvaluate = function(str) {
  183.         if (!this.isLoaded()) this._evalBuffer=str;
  184.         else {
  185.                 if (!str) str = '';
  186.                 if(this._evalBuffer!='') {
  187.                         str =this._evalBuffer+str;
  188.                         this._evalBuffer='';
  189.                 }
  190.                 this.win.document.debugform.eval.value = str;
  191.                 this.setEvalHistory(str);
  192.         }
  193. };
  194. // Set previous evaluation information
  195. p.setEvalHistory=function(s){
  196.         if(!this.isLoaded()) return;
  197.         var i,found;
  198.         if(s){
  199.                 for(i=0;i<this.win.evalHistory.length;i++){
  200.                         if(this.win.evalHistory[i]==s) {found=i;break;}
  201.                 }
  202.                 if(found!=null) this.win.evalHistory=dynapi.functions.removeFromArray(this.win.evalHistory,found);
  203.                 this.win.evalHistory[this.win.evalHistory.length]=s;
  204.                 this.win.evalIndex=this.win.evalHistory.length-1;
  205.         }
  206. };
  207. p.showHelp=function(){
  208.         var t=''
  209.         +'-----------------------\n'
  210.         +'Quick Help\n'
  211.         +'-----------------------\n'
  212.         +'1) To inspect an Object enter the name\n'
  213.         +'of the object in the "Inspect Variable/Object"\n'
  214.         +'textbox and then click on the "Inspect" button\n\n'
  215.         +'2) To Load/Reload a DynAPI Package,\n'
  216.         +'javascript or html page enter the name\n'
  217.         +'of the package or javascript in the reload\n'
  218.         +'text. For HTML pages type the prefix Go:\n'
  219.         +'before the page name.\n'
  220.         +'------------------------------------------------';
  221.         this.print(t);
  222. };
  223. // watch object variables;
  224. p.watch = function(name,value){
  225.         if(arguments.length>1) this._watch[name]=value;
  226.         else if(dynapi.frame.eval(name)) this._watch[name]='_watch object_';
  227.         else this._watch[name]='_watch object_';
  228. };
  229.  
  230. p._disableWatch = function(){
  231.         this._oldWatchSrc='';
  232.         if(this._timerWatch) {
  233.                 window.clearTimeout(this._timerWatch);
  234.                 this._timerWatch=0;
  235.         }
  236. };
  237. p._enableWatch = function(){
  238.         if(this._mode!='watch') return;
  239.         var src,row,v;
  240.         src='Name\t \t \t Value\n---------------------------------------\n';
  241.         for(i in this._watch){
  242.                 if(this._watch[i]=='_watch object_') v=dynapi.frame.eval(i);
  243.                 else v=this._watch[i];
  244.                 if(v==null) v='null';
  245.                 if(typeof(v)=='string') v=v.replace(/\n/g,' ');
  246.                 src+=(i+'                      ').substr(0,22)+'\t '+v+'\n';
  247.         }
  248.         if(src!=this._oldWatchSrc){
  249.                 this.win.document.debugform.print.value=this._oldWatchSrc=src;
  250.         }
  251.         if(this._timerWatch) window.clearTimeout(this._timerWatch);
  252.         this._timerWatch=window.setTimeout(this+'._enableWatch()',200);
  253. };
  254.  
  255.  
  256. dynapi.debug = new Debugger();
  257.  
  258. var t='------------------------------\n'
  259. +'Click "?" for help\n'
  260. +'------------------------------\n';
  261. dynapi.debug.print(t);
  262.