/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */

package com.icesoft.components.accessible.ext.renderkit;

import com.icesoft.components.accessible.ext.HtmlCommandLink;
import com.icesoft.components.accessible.ext.HtmlCommandButton;
import org.w3c.dom.Element;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import com.icesoft.components.accessible.panelconfirmation.PanelConfirmation;
import com.icesoft.components.accessible.panelconfirmation.PanelConfirmationRenderer;
import com.icesoft.faces.component.util.CustomComponentUtils;
import com.icesoft.components.accessible.util.CoreComponentUtils;

public class ButtonRenderer
        extends com.icesoft.faces.renderkit.dom_html_basic.ButtonRenderer {

    public void decode(FacesContext facesContext, UIComponent uiComponent) {
        super.decode(facesContext, uiComponent);
    	if (didThisButtonInvokeSubmit(facesContext, uiComponent)) {
        	if (CoreComponentUtils.isJavaScriptDisabled(facesContext)) {
                String panelConfirmationId = String.valueOf(uiComponent.getAttributes().get("panelConfirmation"));
                PanelConfirmation panelConfirmation = (PanelConfirmation) CoreComponentUtils
                        .findComponent(panelConfirmationId, uiComponent);
                if (panelConfirmation != null) {   
                	panelConfirmation.setShowWhenJSDisable(true);
                	facesContext.renderResponse();
                }
        	}
        }    	
    }
    
    protected void renderOnClick(UIComponent uiComponent, Element root) {
        
		UIComponent button = (UIComponent) uiComponent;
        String onclick = (String) uiComponent.getAttributes().get("onclick");
        String submitCode;
		                        
		boolean isPartialSubmit = false;
		try {
			isPartialSubmit = (boolean)((HtmlCommandButton)button).getPartialSubmit(); 
		} catch (ClassCastException e) {
			isPartialSubmit = (boolean)((HtmlCommandLink)button).getPartialSubmit();
		}

        if (isPartialSubmit) {
            submitCode = this.ICESUBMITPARTIAL + "return false;";
        } else {
            submitCode = this.ICESUBMIT + "return false;";
        }
        
        if (null != uiComponent.getAttributes().get("panelConfirmation")) {
            root.setAttribute("onclick", PanelConfirmationRenderer.renderOnClickString(
                uiComponent, combinedPassThru(onclick, submitCode)));
        } else {
            root.setAttribute("onclick", combinedPassThru(onclick, submitCode));
        }
    }

}
