Encode client behaviors to widget config

@param context
@param sheet
@param wb
@throws IOException
540409
private void encodeBehaviors(final FacesContext context, final Sheet sheet, final WidgetBuilder wb)
                throws IOException {
        // note we write out the onchange event here so we have the selected
        // cell too
        final Map<String, List<ClientBehavior>> behaviors = sheet.getClientBehaviors();

        wb.append(",behaviors:{");
        final String clientId = sheet.getClientId();

        // sort event (manual since callBack prepends leading comma)
        if (behaviors.containsKey("sort")) {
            final ClientBehaviorContext behaviorContext = ClientBehaviorContext.createClientBehaviorContext(context,
                        sheet, "sort", sheet.getClientId(context), null);
            final AjaxBehavior ajaxBehavior = (AjaxBehavior) behaviors.get("sort").get(0);
            ajaxBehavior.setUpdate(StringUtils.defaultString(ajaxBehavior.getUpdate()) + StringUtils.SPACE + clientId);
            wb.append("sort").append(":").append("function(s, event)").append("{")
                        .append(behaviors.get("sort").get(0).getScript(behaviorContext)).append("}");
        }
        else {
            // default sort event if none defined by user
            wb.append("sort").append(":").append("function(s, event)").append("{").append("PrimeFaces.ab({source: '")
                        .append(clientId).append("',event: 'sort', process: '").append(clientId).append("', update: '")
                        .append(clientId).append("'});}");
        }

        // filter
        if (behaviors.containsKey("filter")) {
            final ClientBehaviorContext behaviorContext = ClientBehaviorContext.createClientBehaviorContext(context,
                        sheet, "filter", sheet.getClientId(context), null);
            final AjaxBehavior ajaxBehavior = (AjaxBehavior) behaviors.get("filter").get(0);
            ajaxBehavior.setUpdate(StringUtils.defaultString(ajaxBehavior.getUpdate()) + StringUtils.SPACE + clientId);
            wb.callback("filter", "function(source, event)", behaviors.get("filter").get(0).getScript(behaviorContext));
        }
        else {
            // default filter event if none defined by user
            wb.callback("filter", "function(source, event)", "PrimeFaces.ab({s: '" + clientId
                        + "', event: 'filter', process: '" + clientId + "', update: '" + clientId + "'});");
        }

        if (behaviors.containsKey("change")) {
            final ClientBehaviorContext behaviorContext = ClientBehaviorContext.createClientBehaviorContext(context,
                        sheet, "change", sheet.getClientId(context), null);
            wb.callback("change", "function(source, event)", behaviors.get("change").get(0).getScript(behaviorContext));
        }

        if (behaviors.containsKey("cellSelect")) {
            final ClientBehaviorContext behaviorContext = ClientBehaviorContext.createClientBehaviorContext(context,
                        sheet, "cellSelect", sheet.getClientId(context), null);
            wb.callback("cellSelect", "function(source, event)",
                        behaviors.get("cellSelect").get(0).getScript(behaviorContext));
        }

        if (behaviors.containsKey("columnSelect")) {
            final ClientBehaviorContext behaviorContext = ClientBehaviorContext.createClientBehaviorContext(context,
                        sheet, "columnSelect", sheet.getClientId(context), null);
            wb.callback("columnSelect", "function(source, event)",
                        behaviors.get("columnSelect").get(0).getScript(behaviorContext));
        }

        if (behaviors.containsKey("rowSelect")) {
            final ClientBehaviorContext behaviorContext = ClientBehaviorContext.createClientBehaviorContext(context,
                        sheet, "rowSelect", sheet.getClientId(context), null);
            wb.callback("rowSelect", "function(source, event)",
                        behaviors.get("rowSelect").get(0).getScript(behaviorContext));
        }

        wb.append("}");
    }
613166
protected void decodeFilters(final FacesContext context, final Sheet sheet, final Map<String, String> params,
                final String clientId) {
        int renderCol = 0;
        for (final SheetColumn column : sheet.getColumns()) {
            if (!column.isRendered()) {
                continue;
            }

            if (column.getValueExpression("filterBy") != null) {
                final String value = params.get(clientId + "_filter_" + renderCol);
                column.setFilterValue(value);
            }

            renderCol++;
        }
    }
1120957
public void setWidget (int row, int column, Widget widget, String style)
    {
        setWidget(row, column, widget);
        if (style != null) {
            getCellFormatter().setStyleName(row, column, style);
        }
    }
595779
@Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        PuiTextarea input = (PuiTextarea) component;
        writer.startElement("pui-textarea", component);
        renderNonEmptyAttribute(writer, "label", input.getLabel());
        renderNGModel(input, writer);
        renderJSR303Constraints(writer, input);
        writer.endElement("pui-textarea");
    }
320674
@Override
    protected void defineWidgets() {

        // initialize the object to use for the dialog
        initLoginMessageObject();

        setKeyPrefix(KEY_PREFIX);

        // required to read the default values for the optional context parameters for the widgets
        CmsLoginMessage def = new CmsLoginMessage();

        addWidget(new CmsWidgetDialogParameter(m_loginMessage, "enabled", PAGES[0], new CmsCheckboxWidget()));
        addWidget(new CmsWidgetDialogParameter(m_loginMessage, "message", PAGES[0], new CmsTextareaWidget()));
        addWidget(new CmsWidgetDialogParameter(m_loginMessage, "loginForbidden", PAGES[0], new CmsCheckboxWidget()));
        addWidget(
            new CmsWidgetDialogParameter(
                m_loginMessage,
                "timeStart",
                String.valueOf(def.getTimeStart()),
                PAGES[0],
                new CmsCalendarWidget(),
                0,
                1));
        addWidget(
            new CmsWidgetDialogParameter(
                m_loginMessage,
                "timeEnd",
                String.valueOf(def.getTimeEnd()),
                PAGES[0],
                new CmsCalendarWidget(),
                0,
                1));
    }
1157989
private static Map<WriterKind, PrintWriter> initWriters(Context context) {
        PrintWriter out = context.get(outKey);
        PrintWriter err = context.get(errKey);
        if (out == null && err == null) {
            out = new PrintWriter(System.out, true);
            err = new PrintWriter(System.err, true);
            return initWriters(out, err);
        } else if (out == null || err == null) {
            PrintWriter pw = (out != null) ? out : err;
            return initWriters(pw, pw);
        } else {
            return initWriters(out, err);
        }
    }
119886
private static void encodeTabLinks(FacesContext context, ResponseWriter writer, TabView tabView,
			int currentlyActiveIndex, List<UIComponent> tabs, String clientId, String hiddenInputFieldID)
			throws IOException {
		writer.startElement("ul", tabView);
		writer.writeAttribute("id", clientId, "id");
		Tooltip.generateTooltip(context, tabView, writer);
		String classes = "nav ";
		if ("left".equalsIgnoreCase(tabView.getTabPosition()) || "right".equalsIgnoreCase(tabView.getTabPosition())) {
			classes += " nav-pills nav-stacked";
		} else {
			classes = classes + (tabView.isPills() ? " nav-pills" : " nav-tabs");
		}

		if (tabView.getStyleClass() != null) {
			classes += " ";
			classes += tabView.getStyleClass();
		}

		writer.writeAttribute("class", classes, "class");

		String role = "tablist";
		AJAXRenderer.generateBootsFacesAJAXAndJavaScript(context, tabView, writer, false);
		R.encodeHTML4DHTMLAttrs(writer, tabView.getAttributes(), new String[] { "style" });
		if (tabView.getRole() != null) {
			role = tabView.getRole();
		}

		writer.writeAttribute("role", role, "role");

		encodeTabs(context, writer, tabs, currentlyActiveIndex, hiddenInputFieldID, tabView.isDisabled());
		writer.endElement("ul");
	}
390155
@Override
	public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
	    if (!component.isRendered()) {
	        return;
	    }
		Shake shake = (Shake) component;
		ResponseWriter rw = context.getResponseWriter();
		String clientId = shake.getClientId();
		
		rw.startElement("script", shake);
		rw.writeAttribute("id", clientId, null);
		rw.write("\nvar myShakeEvent = new Shake({\n");
		rw.write("  threshold: " + shake.getThreshold() + ",\n");
		rw.write("  timeout: " + shake.getInterval() + "\n");
		rw.write("});\n");
		
		if (!shake.isDisabled()) {
			// Start listening to device motion:
			rw.write("myShakeEvent.start();\n");
		}

		// Register a shake event listener on window with your callback:
		rw.write("window.addEventListener('shake', function(){\n");
		String code = new AJAXRenderer().generateBootsFacesAJAXAndJavaScriptForAnMobileEvent(context, shake, rw, "shake");
		rw.write(code + "\n");
		rw.write("}, false);\n");
		rw.endElement("script");
	}
195293
@Override
	public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
		if (!component.isRendered()) {
			return;
		}
		Gyroscope gyroscope = (Gyroscope) component;
		ResponseWriter rw = context.getResponseWriter();
		String clientId = gyroscope.getClientId();

		rw.startElement("input", component);
		rw.writeAttribute("id", clientId + ".alpha", null);
		rw.writeAttribute("name", clientId + ".alpha", null);
		rw.writeAttribute("type", "hidden", null);
		rw.endElement("input");
		rw.startElement("input", component);
		rw.writeAttribute("id", clientId + ".beta", null);
		rw.writeAttribute("name", clientId + ".beta", null);
		rw.writeAttribute("type", "hidden", null);
		rw.endElement("input");
		rw.startElement("input", component);
		rw.writeAttribute("id", clientId + ".gamma", null);
		rw.writeAttribute("name", clientId + ".gamma", null);
		rw.writeAttribute("type", "hidden", null);
		rw.endElement("input");
		rw.startElement("input", component);
		rw.writeAttribute("id", clientId + ".timer", null);
		rw.writeAttribute("name", clientId + ".timer", null);
		rw.writeAttribute("value", "0", null);
		rw.writeAttribute("type", "hidden", null);
		rw.endElement("input");
		rw.startElement("script", component);
		rw.writeAttribute("id", clientId, null);
		StringBuilder jsCode = new StringBuilder();
		// Render Ajax Capabilities
		AJAXRenderer.generateAJAXCallForASingleEvent(
				FacesContext.getCurrentInstance(), gyroscope, rw, null,
				null, null, false, "rotation", jsCode, null);
		String js = jsCode.toString().replace("callAjax(this,", "callAjax(document.getElementById('" + clientId + ".alpha'),");
		rw.write("window.addEventListener('deviceorientation', function(event) {\n");
		rw.write("  var oldAlpha = document.getElementById('" + clientId + ".alpha').value;");
		rw.write("  var alpha = Math.round(event.alpha);");
		rw.write("  var oldBeta = document.getElementById('" + clientId + ".beta').value;");
		rw.write("  var beta = Math.round(event.beta);");
		rw.write("  var oldGamma = document.getElementById('" + clientId + ".gamma').value;");
		rw.write("  var gamma = Math.round(event.gamma);");
		rw.write("  if (alpha==oldAlpha && beta == oldBeta && gamma == oldGamma) return;");
		rw.write("   if (new Date().getTime() < document.getElementById('" + clientId + ".timer').value) return;");

		rw.write("  document.getElementById('" + clientId + ".alpha').value = alpha;");
		rw.write("  document.getElementById('" + clientId + ".beta').value = beta;");
		rw.write("  document.getElementById('" + clientId + ".gamma').value = gamma;");
		rw.write("  document.getElementById('" + clientId + ".timer').value = new Date().getTime()+100;");
		rw.write(js);
		rw.write("}, true);\n");
//		rw.write("window.addEventListener('compassneedscalibration', function(event) {\n");
//		rw.write("  alert('Your compass needs calibrating! Wave your device in a figure-eight motion');");
//		rw.write("  event.preventDefault();");
//		rw.write("}, true);\n");
		rw.endElement("script");
	}
1244184
public void setContext(Context context) {
    super.setContext(context);
    if (null != context) {
      Object count = context.getDatas().get("countPerSheet");
      if (null != count && Numbers.isDigits(count.toString())) {
        int countParam = Numbers.toInt(count.toString());
        if (countParam > 0) this.countPerSheet = countParam;
      }
    }
  }
59356
private void encodeScript(final FacesContext context, final Social social) throws IOException {
        final WidgetBuilder wb = getWidgetBuilder(context);
        final String clientId = social.getClientId(context);
        wb.init("ExtSocial", social.resolveWidgetVar(), clientId);
        wb.attr("showLabel", social.isShowLabel());
        wb.attr("shareIn", social.getShareIn());
        if (!LangUtils.isValueBlank(social.getUrl())) {
            wb.attr("url", social.getUrl());
        }
        if (!LangUtils.isValueBlank(social.getText())) {
            wb.attr("text", social.getText());
        }

        final boolean showCount = BooleanUtils.toBoolean(social.getShowCount());
        if (showCount) {
            wb.attr("showCount", showCount);
        }
        else {
            if (social.getShowCount().equalsIgnoreCase("inside")) {
                wb.attr("showCount", social.getShowCount());
            }
            else {
                wb.attr("showCount", showCount);
            }
        }

        // shares array
        wb.append(",shares: [");
        final String[] shares = StringUtils.split(social.getShares(), ',');
        for (int i = 0; i < shares.length; i++) {
            // { share: "pinterest", media: "http://mysite.com" },
            final String share = StringUtils.lowerCase(shares[i]);
            if (LangUtils.isValueBlank(share)) {
                continue;
            }
            if (i != 0) {
                wb.append(",");
            }
            wb.append("{");
            addShareProperty(wb, "share", share);
            if (share.equalsIgnoreCase("twitter")) {
                wb.attr("via", social.getTwitterUsername());
                wb.attr("hashtags", social.getTwitterHashtags());
            }
            if (share.equalsIgnoreCase("email")) {
                wb.attr("to", social.getEmailTo());
            }
            if (share.equalsIgnoreCase("pinterest")) {
                wb.attr("media", social.getPinterestMedia());
            }
            wb.append("}");
        }
        wb.append("]");

        // javascript
        wb.append(",on: {");
        if (social.getOnclick() != null) {
            addCallback(wb, "click", "function(e)", social.getOnclick());
        }
        if (social.getOnmouseenter() != null) {
            addCallback(wb, "mouseenter", "function(e)", social.getOnmouseenter());
        }
        if (social.getOnmouseleave() != null) {
            addCallback(wb, "mouseleave", "function(e)", social.getOnmouseleave());
        }
        wb.append("}");

        encodeClientBehaviors(context, social);

        wb.finish();
    }
670361
protected void code(String template, String packageName, String className, Supplier<Map<String, Object>> context) {
        StringBuffer code = generate(template, context);
        writeCode(packageName, className, code);
    }
1036516
function wb_sheet_idx(wb, sh) {
	if(typeof sh == "number") {
		if(sh >= 0 && wb.SheetNames.length > sh) return sh;
		throw new Error("Cannot find sheet # " + sh);
	} else if(typeof sh == "string") {
		var idx = wb.SheetNames.indexOf(sh);
		if(idx > -1) return idx;
		throw new Error("Cannot find sheet name |" + sh + "|");
	} else throw new Error("Cannot find sheet |" + sh + "|");
}
611761
private void encodeHandle(final FacesContext context, final SlideOut slideOut) throws IOException {
        final ResponseWriter writer = context.getResponseWriter();

        String styleClass = SlideOut.HANDLE_CLASS;
        if (slideOut.getHandleStyleClass() != null) {
            styleClass = styleClass + " " + slideOut.getHandleStyleClass();
        }

        writer.startElement("a", null);
        writer.writeAttribute("id", getHandleId(context, slideOut), null);
        if (slideOut.getHandleStyle() != null) {
            writer.writeAttribute("style", slideOut.getHandleStyle(), "style");
        }
        writer.writeAttribute("class", styleClass, "styleClass");

        // icon
        encodeIcon(context, slideOut);

        // handle text
        if (slideOut.getTitle() != null) {
            writer.writeText(slideOut.getTitle(), "title");
        }

        writer.endElement("a");
    }
1247539
protected void encodeColHeaders(final FacesContext context, final Sheet sheet, final WidgetBuilder wb)
                throws IOException {
        final JavascriptVarBuilder vb = new JavascriptVarBuilder(null, false);
        for (final SheetColumn column : sheet.getColumns()) {
            if (!column.isRendered()) {
                continue;
            }
            vb.appendArrayValue(column.getHeaderText(), true);
        }
        wb.nativeAttr("colHeaders", vb.closeVar().toString());
    }
395052
protected void encodeOptionalAttr(final WidgetBuilder wb, final String attrName, final String value)
                throws IOException {
        if (value != null) {
            wb.attr(attrName, value);
        }
    }
1128031
@Override
	public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
		if (!component.isRendered()) {
			return;
		}
		DateTimePicker dtp = (DateTimePicker) component;
		ResponseWriter rw = context.getResponseWriter();

		String dateTimePickerId = encodeHTML(context, rw, dtp);
		encodeJS(context, rw, dtp, dateTimePickerId);
	}