/* JSFUpdate.java

	Purpose:
		
	Description:
		
	History:
		2013/6/27, Dennis

Copyright (C) 2010 Potix Corporation. All Rights Reserved.

*/
package org.zkoss.zss.jsf.ui;

import java.io.IOException;

import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.zkoss.util.logging.Log;
import org.zkoss.zss.jsf.ui.impl.FacesUtil;

/**
 * Represents a custom JSF component that wraps zk execution update scripts
 * @author Dennis
 *
 */
@FacesComponent(value = "org.zkoss.zss.Update")
public class Update extends UIComponentBase {
	private static final Log log = Log.lookup(Update.class);
	
	public void encodeBegin(FacesContext context) throws IOException {
		ResponseWriter responseWriter = context.getResponseWriter();
		
		String id =  getClientId(context);
		responseWriter.startElement("span", this);//a hook for update
		responseWriter.writeAttribute("id", id, "id");
		responseWriter.writeAttribute("class", "zkupdate", null);
		responseWriter.endElement("span");
		
		//execute the update script
		if(FacesUtil.isAjaxRequest()){//only update when au (don't update in whole page post back)
			for(String script:FacesUtil.getUpdateScript()){
				responseWriter.write(FacesUtil.toScriptString(script));
				responseWriter.write("\n");
			}
		}
		FacesUtil.clearUpdateScript();
	}

	public String getFamily() {
		return "zk";
	}
	
}
