use of com.liferay.faces.util.render.JavaScriptFragment in project liferay-faces-alloy by liferay.
the class InputTimeRenderer method encodeJavaScriptCustom.
@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
InputTime inputTime = (InputTime) uiComponent;
String clientVarName = getClientVarName(facesContext, inputTime);
String clientKey = inputTime.getClientKey();
if (clientKey == null) {
clientKey = clientVarName;
}
ResponseWriter responseWriter = facesContext.getResponseWriter();
BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
if (isNative(browserSniffer, inputTime)) {
JavaScriptFragment liferayComponent = new JavaScriptFragment("Liferay.component('" + clientKey + "')");
String clientId = uiComponent.getClientId(facesContext);
String inputClientId = clientId.concat(INPUT_SUFFIX);
// Get the max and min times in the HTML5 format which does not include seconds.
int defaultHTML5PatternLength = InputTime.DEFAULT_HTML5_TIME_PATTERN.length();
String maxTime = inputTime.getMaxTime().substring(0, defaultHTML5PatternLength);
String minTime = inputTime.getMinTime().substring(0, defaultHTML5PatternLength);
encodeFunctionCall(responseWriter, "LFAI.initDateTimePickerMobile", liferayComponent, inputClientId, maxTime, minTime);
} else {
encodeLiferayComponentVar(responseWriter, clientVarName, clientKey);
// Replace the default TimePicker._setValues() method with setValues() (defined in alloy.js) which simply
// passes values through to the autocomplete without processing them.
responseWriter.write(clientVarName);
responseWriter.write("._setValues=LFAI.timePickerSetValues;");
// Set the values of the timePicker.
responseWriter.write(clientVarName);
responseWriter.write(".set('values',[");
String minTimeStamp = inputTime.getMinTime();
String maxTimeStamp = inputTime.getMaxTime();
long minTime;
long maxTime;
try {
minTime = getMillisFromTimeStamp(minTimeStamp);
maxTime = getMillisFromTimeStamp(maxTimeStamp);
} catch (ParseException e) {
throw new IOException(e);
}
if (minTime > maxTime) {
throw new IOException("minTime must not be later than maxTime.");
}
String timePattern = inputTime.getPattern();
Object objectLocale = inputTime.getLocale();
Locale locale = inputTime.getObjectAsLocale(objectLocale);
TimeZone timeZone = TimeZone.getTimeZone("Greenwich");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timePattern, locale);
simpleDateFormat.setTimeZone(timeZone);
Integer millisecondStep = inputTime.getStep() * 1000;
boolean firstTimeStamp = true;
if (millisecondStep < 1) {
throw new IOException("step cannot be less than 1.");
}
// step, and printing each resulting value.
for (long milliseconds = minTime; milliseconds <= maxTime; milliseconds = milliseconds + millisecondStep) {
if (!firstTimeStamp) {
responseWriter.write(",");
} else {
firstTimeStamp = false;
}
Date time = new Date(milliseconds);
String dateString = simpleDateFormat.format(time);
String escapedDateString = RendererUtil.escapeJavaScript(dateString);
responseWriter.write("'");
responseWriter.write(escapedDateString);
responseWriter.write("'");
}
responseWriter.write("]);");
}
}
use of com.liferay.faces.util.render.JavaScriptFragment in project liferay-faces-alloy by liferay.
the class OverlayRendererBase method encodeOverlayDismissible.
protected void encodeOverlayDismissible(ResponseWriter responseWriter, UIComponent overlay, String clientKey) throws IOException {
JavaScriptFragment overlayClientVar = new JavaScriptFragment("Liferay.component('" + clientKey + "')");
encodeFunctionCall(responseWriter, "LFAI.initOverlayDismissible", 'A', overlayClientVar);
}
use of com.liferay.faces.util.render.JavaScriptFragment in project liferay-faces-alloy by liferay.
the class TabViewRenderer method encodeJavaScriptCustom.
@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
// Encode a script that will handle the client-side state of the selected tab index, as well as submitting
// Ajax requests to support server-side events.
TabView tabView = (TabView) uiComponent;
// var tabViewClientVarName = Liferay.component('{clientKey}');
String clientVarName = getClientVarName(facesContext, tabView);
String clientKey = tabView.getClientKey();
if (clientKey == null) {
clientKey = clientVarName;
}
ResponseWriter responseWriter = facesContext.getResponseWriter();
encodeLiferayComponentVar(responseWriter, clientVarName, clientKey);
// Enable the enabled tabs, disable the disabled tabs, and select the selected tab on the client side.
Integer selectedIndex = tabView.getSelectedIndex();
DataEncoderBase dataEncoder = new DataEncoderTabJavaScriptImpl(selectedIndex, clientVarName);
dataEncoder.encodeColumns(facesContext, tabView);
// LFAI.initTabView(tabViewClientVarName, '{clientId}selectedIndex', '{clientId}',
// [tabSelectClientBehaviorScriptFunction1, tabSelectClientBehaviorScriptFunction2,
// tabSelectClientBehaviorScriptFunction3]);
String clientId = uiComponent.getClientId(facesContext);
String hiddenFieldId = clientId + "selectedIndex";
StringBuilder clientBehaviorsArrayStringBuilder = new StringBuilder();
clientBehaviorsArrayStringBuilder.append("[");
boolean first = true;
Map<String, List<ClientBehavior>> clientBehaviorMap = tabView.getClientBehaviors();
Collection<String> eventNames = tabView.getEventNames();
for (String eventName : eventNames) {
List<ClientBehavior> clientBehaviorsForEvent = clientBehaviorMap.get(eventName);
if (clientBehaviorsForEvent != null) {
for (ClientBehavior clientBehavior : clientBehaviorsForEvent) {
ClientBehaviorContext clientBehaviorContext = ClientBehaviorContext.createClientBehaviorContext(facesContext, tabView, eventName, clientId, null);
// an Ajax request.
if (TabSelectEvent.TAB_SELECT.equals(eventName)) {
if (!first) {
clientBehaviorsArrayStringBuilder.append(",");
}
// J-
// function(event) {
// jsf.ajax.request(this, event, {'javax.faces.behavior.event': 'tabSelect'})
// };
// J+
clientBehaviorsArrayStringBuilder.append("function(event){");
clientBehaviorsArrayStringBuilder.append(clientBehavior.getScript(clientBehaviorContext));
clientBehaviorsArrayStringBuilder.append("}");
first = false;
}
}
}
}
clientBehaviorsArrayStringBuilder.append("]");
encodeFunctionCall(responseWriter, "LFAI.initTabView", new JavaScriptFragment(clientVarName), hiddenFieldId, clientId, new JavaScriptFragment(clientBehaviorsArrayStringBuilder.toString()));
}
use of com.liferay.faces.util.render.JavaScriptFragment in project liferay-faces-alloy by liferay.
the class AlloyRendererUtil method toEscapedJavaScriptStringArray.
public static JavaScriptFragment[] toEscapedJavaScriptStringArray(String[] strings) {
JavaScriptFragment[] escapedJavaScriptStringArray = new JavaScriptFragment[strings.length];
for (int i = 0; i < strings.length; i++) {
String escapedJavaScript = RendererUtil.escapeJavaScript(strings[i]);
escapedJavaScriptStringArray[i] = new JavaScriptFragment("'" + escapedJavaScript + "'");
}
return escapedJavaScriptStringArray;
}
use of com.liferay.faces.util.render.JavaScriptFragment in project liferay-faces-alloy by liferay.
the class InputFileRenderer method encodeJavaScriptCustom.
@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
ResponseWriter responseWriter = facesContext.getResponseWriter();
InputFile inputFile = (InputFile) uiComponent;
// Determine the valid content-types and maximum file size from the validator (if specified).
String validContentTypesString = inputFile.getContentTypes();
JavaScriptFragment[] validContentTypes;
if ((validContentTypesString == null) || "".equals(validContentTypesString)) {
validContentTypes = new JavaScriptFragment[] {};
} else {
validContentTypes = AlloyRendererUtil.toEscapedJavaScriptStringArray(validContentTypesString.split("\\s*,\\s*"));
}
String clientId = inputFile.getClientId(facesContext);
Long maxFileSize = inputFile.getMaxFileSize();
if (maxFileSize == null) {
maxFileSize = Long.MAX_VALUE;
}
// If the component should render the upload progress table, then initialize the YUI progress uploader widget.
if (inputFile.isShowProgress()) {
String clientVarName = getClientVarName(facesContext, inputFile);
String clientKey = inputFile.getClientKey();
if (clientKey == null) {
clientKey = clientVarName;
}
UIViewRoot viewRoot = facesContext.getViewRoot();
Locale locale = viewRoot.getLocale();
String formClientId = getParentFormClientId(inputFile);
Application application = facesContext.getApplication();
ViewHandler viewHandler = application.getViewHandler();
String actionURL = viewHandler.getActionURL(facesContext, viewRoot.getViewId());
String partialActionURL = facesContext.getExternalContext().encodePartialActionURL(actionURL);
String namingContainerId = "";
if (viewRoot instanceof NamingContainer) {
namingContainerId = viewRoot.getContainerClientId(facesContext);
}
AjaxParameters ajaxParameters = new AjaxParameters(inputFile, clientId, formClientId);
String execute = ajaxParameters.getExecute();
String render = ajaxParameters.getRender();
ExternalContext externalContext = facesContext.getExternalContext();
I18n i18n = I18nFactory.getI18nInstance(externalContext);
String notStartedMessage = i18n.getMessage(facesContext, locale, "not-started");
JavaScriptFragment clientComponent = new JavaScriptFragment("Liferay.component('" + clientKey + "')");
encodeFunctionCall(responseWriter, "LFAI.initProgressUploader", 'A', clientComponent, validContentTypes, clientId, formClientId, namingContainerId, inputFile.isAuto(), execute, render, partialActionURL, maxFileSize, notStartedMessage);
} else // template and write it to the response.
if (inputFile.isShowPreview()) {
encodeFunctionCall(responseWriter, "LFAI.initPreviewUploader", 'A', validContentTypes, clientId, maxFileSize);
}
}
Aggregations