      1 ﻿
      2 /**
      3  * @name	CeL function for debug
      4  * @fileoverview
      5  * 本檔案包含了 debug 用的 functions。
      6  * @since
      7  */
      8
      9 /*
     10
     11 http://code.google.com/apis/ajax/playground/
     12
     13 */
     14
     15 if (typeof CeL === 'function')
     16 CeL.setup_module('application.debug',
     17 function(library_namespace, load_arguments) {
     18
     19
     20 //	nothing required
     21
     22
     23
     24
     25
     26 /**
     27  * null module constructor
     28  * @class	code.debug 的 functions
     29  */
     30 CeL.application.debug
     31 = function() {
     32 	//	null module constructor
     33 };
     34
     35 /**
     36  * for JSDT: 有 prototype 才會將之當作 Class
     37  */
     38 CeL.application.debug
     39 .prototype = {
     40 };
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50 //JSalert[generateCode.dLK]='getScriptName';//,*var ScriptName=getScriptName();
     51 CeL.application.debug
     52 .
     53 /**
     54  * 顯示訊息視窗<br/>
     55  * alert() 改用VBScript的MsgBox可產生更多效果，但NS不支援的樣子。
     56  * @param message	message or object
     57  * @param {Number} [wait]	the maximum length of time (in seconds) you want the pop-up message box displayed.
     58  * @param {String} [title]	title of the pop-up message box.
     59  * @param {Number} [type]	type of buttons and icons you want in the pop-up message box.
     60  * @return	{Integer} number of the button the user clicked to dismiss the message box.
     61  * @requires	CeL.get_script_name
     62  * @see	<a href="http://msdn.microsoft.com/library/en-us/script56/html/wsmthpopup.asp">Popup Method</a>
     63  * @memberOf	CeL.application.debug
     64  */
     65 JSalert=function (message, wait, title, type) {
     66 	var _f=arguments.callee;
     67 	if (typeof _f.cmd === 'undefined') // 控制是否彈跳出視窗
     68 		_f.cmd = typeof WScript === 'object'
     69 				&& /cscript\.exe$/i.test(WScript.FullName);
     70
     71 	// if(!message)message+='';//if(typeof message==='undefined')message='';else if(!message)message+=''; //
     72 	// 有時傳入如message==null會造成error
     73 	// WScript.Echo()會視情況：視窗執行時彈跳出視窗，cmd執行時直接顯示。但需要用cscript執行時才有效果。
     74 	// http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_mokz.mspx
     75 	// 可以用 WScript.Echo(t1,t2,..)，中間會以' '間隔
     76 	if (_f.cmd && argument.length < 2)
     77 		return WScript.Echo(message);
     78
     79 	if (!title &&
     80 			// typeof getScriptName === 'function'
     81 			this.get_script_name
     82 			)
     83 		title = getScriptName();
     84
     85 	if (isNaN(type))// typeof type!=='number'
     86 		type = 64;
     87
     88 /*
     89 	if (typeof WshShell != 'object')
     90 		if (typeof WScript === 'object')
     91 			WshShell = WScript.CreateObject("WScript.Shell");
     92 		else
     93 			return undefined;
     94 */
     95 	if (this.WshShell != 'object')
     96 		if (typeof WScript === 'object')
     97 			this.WshShell = WScript.CreateObject("WScript.Shell");
     98 		else
     99 			return undefined;
    100
    101
    102 	return this.WshShell.Popup(
    103 			//	''+message: 會出現 typeof message==='object' 卻不能顯示的
    104 			'' + message,
    105 			wait, title, type
    106 			);
    107 };
    108
    109 // popup object Error(錯誤)
    110 //popErr[generateCode.dLK]='JSalert,setTool,parse_Function';
    111 function popErr(e,t,f){	//	error object, title, additional text(etc. function name)
    112  var T=typeof e;
    113  //alert((T=='object')+','+(e.constructor)+','+(Error)+','+(e instanceof Error))
    114  //	這裡e instanceof Error若是T=='object'&&e.constructor==Error有時不能達到效果！
    115  //	use: for(i in e)
    116  T=e instanceof Error?'Error '+(e.number&0xFFFF)+(e.name?' ['+e.name+']':'')+' (facility code '+(e.number>>16&0x1FFF)+'):\n'+e.description+(!e.message||e.message==e.description?'':'\n\n'+e.message):!e||T=='string'?e:'('+T+')'+e;
    117  f=f?(''+f).replace(/\0/g,'\\0')+'\n\n'+T:T;
    118  //	.caller只在執行期間有效。_function_self_.caller可用 arguments.callee.caller 代替，卻不能用arguments.caller
    119  //	arguments.callee.caller 被取消了。	http://www.opera.com/docs/specs/js/ecma/	http://bytes.com/forum/thread761008.html	http://www.javaeye.com/post/602661	http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/cd3d6d6abcdd048b
    120  if(typeof WshShell=='object')
    121   WshShell.Popup(f,0,t||'Error '+(arguments.callee.caller==null?'from the top level':'on '+(typeof parse_Function=='function'?parse_Function(arguments.callee.caller).funcName:'function'))+' of '+ScriptName,16);
    122  else alert(f);
    123  return T;
    124 }
    125
    126
    127
    128
    129
    130
    131 /*	debug用:	show function Class	2008/7/23 16:33:42
    132 	!! unfinished !!
    133 	//	http://fillano.blog.ithome.com.tw/post/257/59403
    134 	//	** 一些內建的物件，他的屬性可能會是[[DontEnum]]，也就是不可列舉的，而自訂的物件在下一版的ECMA-262中，也可以這樣設定他的屬性。
    135
    136
    137 usage:
    138 showClass('registryF');
    139 showClass(registryF);
    140
    141
    142 trace 的技巧：
    143
    144 對沒 prototype 的，可能是：
    145 var cl=(function(){
    146  return new ((function(){var kkk='xsa',aa=function(){return kkk;},init=function(){kkk='22';},_=function(){init();};_.prototype.get=function(){return aa();};return _;})());
    147 })();
    148 cl.constructor=null;	//	絕技…無效
    149
    150 sl(cl.get());
    151 showClass('cl');
    152 eval('sl(kkk)',cl.get);
    153
    154
    155 */
    156
    157 function showClass(c,n){
    158  var i,sp='<hr style="width:40%;float:left;"/><br style="clear:both;"/>',h='<span style="color:#bbb;font-size:.8em;">'
    159 	,p=function(m,p){sl(h+n+(p?'.prototype':'')+'.</span><em>'+m+'</em> '+h+'=</span> '+f(c[m]));}
    160 	,f=function(f){return (''+f).replace(/\n/g,'<br/>').replace(/ /g,' ');};
    161  if(typeof c=='string'){
    162   if(!n)n=c;
    163   c=eval(c);
    164  }
    165  if(!n)n='';
    166  sl('<hr/>Show class: ('+(typeof c)+')'+(n?' [<em>'+n+'</em>]':'')+'<br/>'
    167 	//+(n?'<em>'+n+'</em> '+h+'=</span> ':'')
    168 	+f(c));
    169  if(c){
    170   sl(sp+'class member:');
    171   for(i in c)
    172    if(i!='prototype')p(i);
    173   sl(sp+'prototype:');
    174   c=c.prototype;
    175   for(i in c)
    176    p(i,1);
    177  }
    178  sl('<hr/>');
    179 }
    180
    181 showClass.repository={};
    182 showClass.repositoryName='';
    183 showClass.setRepository=function(n){
    184  var _f=arguments.callee;
    185  _f.repositoryName=n;
    186  _f.repository=eval(n);
    187  if(!_f.repository)_f.repository=eval(n+'={}');
    188 };
    189 /**
    190  * ** loop?
    191  * @param	name	name
    192  * @param	scope	scope
    193  * @ignore
    194  * @return
    195  */
    196 showClass.showOnScope = function(name, scope) {
    197 	var _f = arguments.callee, r = _f.repository;
    198 	// 遇到 _f.repositoryName 剛好為 local 值時會失效。
    199 	eval(_f.repositoryName + '.' + name + '=' + name, typeof scope == 'string' ? r[scope] : scope);
    200 	return _f(r[name], name);
    201 };
    202
    203
    204 //	debug用:	show contents of object	2000-2003/2/22 15:49
    205 //var i,t='';for(i in o)t+=i+':'+o[i];alert(t);
    206 function showObj(obj,mode,searchKey,printmode,range){//object,mode,search string
    207  var Obj,m='',M=[],M_=0,i,v,search,r2=99,sp='	';if(!range)range=2e3;
    208  if(typeof obj=='object')Obj=obj;else if(typeof obj=='string'&&typeof document!='undefined')
    209   if((i=obj.indexOf('.'))<1)Obj=document.getElementById(obj);
    210   else if(Obj=document.getElementById(obj.slice(0,i)),typeof Obj=='object')Obj=eval('Obj.'+obj.substr(i+1));
    211  if(!Obj)try{Obj=eval(obj);}catch(e){Obj=obj;}
    212  search=searchKey?isNaN(searchKey)?searchKey==''?0:2:1:searchKey==0?1:0;//0:not search,1:num,2:string
    213  if(search==1&&searchKey!=''+parseFloat(searchKey))search=2;
    214  //if(searchKey)if(isNaN(searchKey))if(searchKey=='')search=0;else search=2;else search=1;else search=0;
    215  //if(!mode&&mode!=0&&is.ns4)mode=1;
    216  if(typeof Obj=='object'&&Obj)
    217   if(!mode){for(i in Obj)m+=i+sp;if(m)M.push(m),M_+=m.length,m='';}
    218   else if(mode==1){
    219    for(i in Obj){v=''+Obj[i];//''+eval('Obj.'+i);
    220     if(search&&i.indexOf(searchKey)==-1&&(!v||search==2&&v.indexOf(searchKey)==-1||v!=searchKey))continue;
    221     m+=i+'='+(v?typeof v=='string'&&v.length>r2?v.slice(0,r2)+'..(string length '+v.length+')':v:'(nothing:'+typeof v+')')+sp;
    222     if(m.length>range)M.push(m),M_+=m.length,m='';
    223    }
    224    if(m)M.push(m),M_+=m.length,m='';
    225   }else m+='Error mode '+mode+' .';
    226  else m='** No such object: '+obj+' ! **\n('+typeof obj+')'+obj+'='+Obj;
    227  if(printmode&&printmode==1&&typeof document!='undefined')
    228   with(document)open('text/plain'),clear(),write('content of '+obj+':'+(search?'search for '+searchKey+(search==1?'(num)':'(str)'):'')+' <!-- reload to HTML --><br/>\n'+m),close();
    229  else if(M_)for(v=i=0;i<M.length;i++)t=obj+' : '+v+'-'+(v+=M[i].length)+'/'+M_+(search?', search for '+searchKey+(search==1?'(number)':'(string)')+'.':''),alert(t+' [Enter] to continue..\n\n'+M[i]);
    230  else alert('showObj() error:\n'+(m||'show '+obj+': Got nothing!'));
    231 }
    232
    233
    234
    235
    236
    237
    238
    239 return (
    240 	CeL.application.debug
    241 );
    242 }
    243
    244
    245 );
    246
    247