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