//expandableList 扩展列表
namespace web.layout.behavior.expandableList;

onAttach = function( ltOwner ){ 
	var gotOne = false;
	
	for(i,child in ltOwner.eachChild() ){ 
		if( child.getAttribute("default") && !gotOne) {
			child.modifyState( 0x20/*_HL_STATE_CURRENT*/ |  0x200/*_HL_STATE_EXPANDED*/,0,false);
			gotOne = true;
		}  
		else{
			child.modifyState(0x400/*_HL_STATE_COLLAPSED*/,0,false);
		}
	} 
}

setCurrentItem = function( ltOwner, item )  {
	// get previously selected item:
	var  prevCurrent = ltOwner.querySelector(":root > :current");
	var  prev = ltOwner.querySelector(":root > :expanded");
	
	if(prevCurrent != item && prevCurrent)
		prevCurrent.modifyState(0,0x20/*_HL_STATE_CURRENT*/);
	
	if( prev ) {
		if( prev == item ) 
			return; // already here, nothing to do.
			
		prev.modifyState(0, 0x20/*_HL_STATE_CURRENT*/ |  0x200/*_HL_STATE_EXPANDED*/); 
		
		// notify all parties involved
		prev.postEvent(0x90/*_HL_ELEMENT_COLLAPSED*/,0, prev); // source here is old collapsed tab itself
	} 
	item.modifyState( 0x20/*_HL_STATE_CURRENT*/ |  0x200/*_HL_STATE_EXPANDED*/); // set state flags
	item.postEvent(0x91/*_HL_ELEMENT_EXPANDED*/,0, item);  // source here is new expanded tab itself
} 

targetItem = function( ltOwner,ltTarget) {
	if( ltTarget == ltOwner )
		return;
	
	if( !ltTarget )
		return;
	
	var  ltTargetParent = ltTarget.parent();
	if( !ltTargetParent )
		return;
	
	if( ltTarget.test("li > .caption") )
		return ltTargetParent; // only if click on "caption" element of <li>. Returns that <li> element.
	
	return targetItem( ltOwner, ltTarget.parent() );
}
    
onMouseDown = function (ltTarget,ltOwner,x,y,mouseParams) { 
	if( mouseParams.button_state != 1/*_HL_MAIN_MOUSE_BUTTON*/ )
		return;
		
 	// el is presumably <li>; 
    var  item = targetItem(ltOwner, ltTarget);

    if(item) // click on the item caption
        owner.setCurrentItem(ltOwner, item);

    return true; // as it is always ours then stop event bubbling
}

onMouseDblClick = onMouseDown;

onActiveChild = function (ltTarget,ltOwner,reason,behaviorParams) {
	var  item = targetItem(ltOwner, ltTarget);
	if(item) {
		// click on the item caption 
		owner.setCurrentItem(he, item);
		return true; 
	}	
}

onKeyDown = function (ltTarget,ltOwner,keyCode, altState, keyParams) {
	select(keyCode) {
		case 0x28/*_VK_DOWN*/ {
			var  c = ltOwner.querySelector(":current");
			var idx = c? (c.index() + 1):0;
			if( idx <= ltOwner.childCount() ) { 
   				owner.setCurrentItem(ltOwner, ltOwner.child(idx) ); 
			}
		}
		case 0x26/*_VK_UP*/ {
			var  c = ltOwner.querySelector(":current");
			var idx = c? (c.index() - 1):(ltOwner.childCount() - 1);
			if( idx >= 1 ) {
            	owner.setCurrentItem(ltOwner, ltOwner.child(idx)); 
        }
		} 
	}
}