package org.wicketTutorial.ajaxdatepicker;
import java.time.LocalDate;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.extensions.markup.html.form.datetime.LocalDateTextField;
import org.apache.wicket.markup.head.CssHeaderItem;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.head.OnLoadHeaderItem;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.settings.JavaScriptLibrarySettings;
public class JQueryDateFieldAjax extends LocalDateTextField {
private static final long serialVersionUID = 5088998263851588184L;
private final String datePattern;
private final String countryIsoCode;
private CharSequence urlForIcon;
private static final PackageResourceReference JQDatePickerRef =
new PackageResourceReference(JQueryDateFieldAjax.class, "JQDatePicker.js");
public JQueryDateFieldAjax(String id, IModel<LocalDate> dateModel,
String datePattern, String countryIsoCode){
super(id, dateModel, datePattern);
this.datePattern = datePattern;
this.countryIsoCode = countryIsoCode;
}
@Override
protected void onInitialize() {
super.onInitialize();
setOutputMarkupId(true);
PackageResourceReference resourceReference = new PackageResourceReference(getClass(), "calendar.jpg");
urlForIcon = urlFor(resourceReference, new PageParameters());
add(AttributeModifier.replace("size", "12"));
}
@Override
public void renderHead(IHeaderResponse response) {
super.renderHead(response);
if(!isEnabledInHierarchy())
return;
JavaScriptLibrarySettings javaScriptSettings = getApplication().getJavaScriptLibrarySettings();
response.render(JavaScriptHeaderItem.
forReference(javaScriptSettings.getJQueryReference()));
response.render(JavaScriptHeaderItem.
forReference(new PackageResourceReference(getClass(), "jquery-ui.min.js")));
response.render(JavaScriptHeaderItem.
forReference(new PackageResourceReference(getClass(), "jquery-ui-i18n.min.js")));
response.render(CssHeaderItem.
forReference(new PackageResourceReference(getClass(), "jquery-ui.css")));
response.render(JavaScriptHeaderItem.
forReference(JQDatePickerRef));
String jqueryDateFormat = datePattern.replace("yyyy", "yy").toLowerCase();
String initScript = ";initJQDatapicker('" + getMarkupId() + "', '" + countryIsoCode + "', '" + jqueryDateFormat + "', "
+ "'" + urlForIcon +"');";
response.render(OnLoadHeaderItem.forScript(initScript));
}
}