Skip to content

Commit

Permalink
add proper converter support to DateTimerPicker
Browse files Browse the repository at this point in the history
fixes #558
  • Loading branch information
Andrew authored and zhedar committed Nov 27, 2016
1 parent 4907fc9 commit a03bd62
Showing 1 changed file with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.convert.Converter;
import javax.faces.render.FacesRenderer;

import net.bootsfaces.component.ajax.AJAXRenderer;
Expand Down Expand Up @@ -66,24 +67,42 @@ public void decode(FacesContext context, UIComponent component) {
* @param dtp
* @return
*/
public String getValueAsString(Object value, FacesContext ctx, DateTimePicker dtp) {
public static String getValueAsString(Object value, FacesContext ctx, DateTimePicker dtp) {
// Else we use our own converter
if(value == null) {
return null;
}
Locale sloc = BsfUtils.selectLocale(ctx.getViewRoot().getLocale(), dtp.getLocale(), dtp);
String sdf = BsfUtils.selectDateFormat(sloc, dtp.getFormat());
// assume that the format is always specified as moment.js format
sdf = LocaleUtils.momentToJavaFormat(sdf);

return getDateAsString(value, sdf, sloc);
return getDateAsString(ctx, dtp, value, sdf, sloc);
}

/**
* Get date in string format
* @param dt
* @param value
* @param format
* @param locale
* @return
*/
public static String getDateAsString(Object dt, String format, Locale locale) {

public static String getDateAsString(FacesContext fc, DateTimePicker dtp, Object value, String format, Locale locale) {
if (value == null) {
return null;
}

Converter converter = dtp.getConverter();
return converter == null ?
getInternalDateAsString(value, format, locale)
:
converter.getAsString(fc, dtp, value);

}


public static String getInternalDateAsString(Object dt, String format, Locale locale) {
if (dt == null) {
return null;
}
Expand Down Expand Up @@ -348,7 +367,10 @@ private void encodeJS(FacesContext fc, ResponseWriter rw, DateTimePicker dtp, St
Locale sloc = BsfUtils.selectLocale(fc.getViewRoot().getLocale(), dtp.getLocale(), dtp);
String format = BsfUtils.selectDateTimeFormat(sloc, dtp.getFormat(), dtp.isShowDate(), dtp.isShowTime());
String displayFormat = "'" + (dtp.getFormat() == null ? LocaleUtils.javaToMomentFormat(format) : format) + "'";
String inlineDisplayDate = "'" + (dtp.getFormat() == null ? getDateAsString(v, format, sloc) : getDateAsString(v, LocaleUtils.momentToJavaFormat(format), sloc)) + "'";
String inlineDisplayDate = "'" + (dtp.getFormat() == null ?
getDateAsString(fc, dtp, v, format, sloc)
:
getDateAsString(fc, dtp, v, LocaleUtils.momentToJavaFormat(format), sloc)) + "'";

boolean openOnClick= !"plain".equals(mode) && !"inline".equals(mode);
String fullSelector = "#" + BsfUtils.escapeJQuerySpecialCharsInSelector(divPrefix + clientId);
Expand Down

0 comments on commit a03bd62

Please sign in to comment.