//windowCommand 窗口命令
import win.ui.minmax;
import web.layout.behavior.windowSizer;
namespace web.layout.behavior.windowCommand;
 
onAttach = function( ltOwner ){
	var form = ltOwner.getForm();
	if( !form.initHtmlayoutWindowCommand ){
		form.initHtmlayoutWindowCommand = true;
		
		var wbLayout = ltOwner.getLayout();
		form.adjust = function( cx,cy,wParam ) {
		 	var ltRoot = wbLayout.documentElement;	
		 	var ltBody = ltRoot.querySelector("body"); 
		 	if( wParam == 0x2/*_SIZE_MAXIMIZED*/ ){
		 		var elMax = ltRoot.querySelector('[command="window-max"]')
				if( elMax ){
					elMax.command = "window-restore"; 
					if( ..string.cmp( elMax.style["font-family"],"Marlett") == 0 ) elMax.innerText = "2";
					if(ltBody)ltBody.maximize = "true";
				}
				
				ltRoot.enumQuery(
					function(ltBorder){
					 	ltBorder.style.display = "none";
					},"[windowSizer] [sizer-command]"
				)
		 	}
		 	elseif( wParam == 0x0/*_SIZE_RESTORED*/ ){
		 		var elRestore = ltRoot.querySelector('[command="window-restore"]')
				if( elRestore ) {
					elRestore.command = "window-max"; 
					if( ..string.cmp( elRestore.style["font-family"],"Marlett") == 0 ) elRestore.innerText = "1";
					if(ltBody)ltBody.maximize = null;
				}
				ltRoot.enumQuery(
					function(ltBorder){
					 	ltBorder.style.display = "block";
					},"[windowSizer] [sizer-command]"
				)
		 	}
		};
		if( form.onGetMinMaxInfo === null ){
			..win.ui.minmax( form ); 
		}
		
		
		form.onEnterSizeMove = function(){
			form.enterSizeMoveRect = form.getRect();
		}
		 
	 	form.onExitSizeMove = function(){
	 		if ..win.isZoomed(form.hwnd) return;
	 		var x,y = form.getPos();  
 	   		if( y <= 0 ){
 	   			//小于0锁定,y小于0且x小于0时阴影窗口脱离(拖动过程中不响应消息)
 	   			form.setPos(x>0?x:1,1,form.enterSizeMoveRect.width(),form.enterSizeMoveRect.height() ) 
 	   		};
	 	}
	}
	
	if( ! ltOwner.root().initHtmlayoutWindowCommand ){
		ltOwner.root().initHtmlayoutWindowCommand = "true"; 
		var elMax = ltOwner.root().querySelector('[command="window-max"],[command="window-restore"]');
		if(!..win.getStyle(form.hwnd,0x10000/*_WS_MAXIMIZEBOX*/) ){ 
			if( elMax ) elMax.style.display = "none";
			
		}
		elseif(elMax){
			var ltBody = ltOwner.root().querySelector("body");
			if( ltBody ) ltBody.insertAdjacentHTML("beforeEnd",'<div style="behavior:windowSizer"/>') 
		}

		if(!..win.getStyle(form.hwnd,0x20000/*_WS_MINIMIZEBOX*/) ){
			var elMin = ltOwner.root().querySelector('[command="window-min"]') ;
			if( elMin ) elMin.style.display = "none";
		}
	}
	return true;
}

onMouseUp = function (ltTarget,ltOwner,x,y,mouseParams) {
	var cmd = ltTarget.command or ltOwner.command;
	var form = ltTarget.getForm(); 
	
	select(cmd) {
		case "window-max"{
			form.hitMax();
		}
		case "window-restore"{
			form.hitMax();
		}
		case "window-min" {
			form.hitMin();
		}
		case "window-close" {
			form.close();
		} 
		else {
			return;
		} 
	}
	
	ltTarget.state.hover = false;
	return true;
}

/*  
-command:window-caption //允许拖动,节点可指定command为任意其他值以禁止点击时拖动窗口
*/
var titleClickableTags = { li=1;button=1;widget=1;input=1;a=1 }
var isTitleBar  = function(ltTarget,ltOwner){
	var cmdTarget = ltTarget.getCustomAttribute("command"); 
	var c = cmdTarget : ltOwner.getCustomAttribute("command");
	if( ( c== "window-caption") || !c ){
		if( !cmdTarget ){
			var ltParent = ltTarget;
			while(ltParent){ 
				var customCmd = ltParent.getCustomAttribute("command") ;
				if( customCmd ){ 
					if( customCmd != "window-caption" )
						return false;
						
					break;
				}
				else {  
					if( ltParent.style.behavior ) {
						if( ltParent != ltOwner ){ 
							return false; //other behavior;
						}
						break;
					}
				 
					if( titleClickableTags[ltParent.tagName]   ){
						return false;
					} 
				}
				
				ltParent = ltParent.parent();
			}
		}
		return true;
	};
}

onMouseDown = function (ltTarget,ltOwner,x,y,mouseParams) {  
	if( isTitleBar(ltTarget,ltOwner) ){  
		var form = ltTarget.getForm();  
		form.hitCaption();
		return true;
	}
}

onMouseDblClick = function (ltTarget,ltOwner,x,y,mouseParams) { 
	if( isTitleBar(ltTarget,ltOwner) ){ 
		var doc = ltOwner.root();
		var ltMax = doc.querySelector( '[command="window-max"]') || doc.querySelector( '[command="window-restore"]' ) ;
		if( ltMax && ltMax.isVisible() ) {
			ltOwner.getForm().hitMax();
			return true;
		}
	}
}