// Note: the setup script is recreated and checked if it is contained in
// the headElement. This check cater for when the field is used by another
// Control using the fly-weight pattern eg. FormTable.
String fieldId = getId();
JsScript script = new JsScript();
script.setId(fieldId + "-autocomplete");
if (!headElements.contains(script)) {
// Script must be executed as soon as browser dom is ready
script.setExecuteOnDomReady(true);
String contextPath = context.getRequest().getContextPath();
HtmlStringBuffer buffer = new HtmlStringBuffer(150);
buffer.append("new Ajax.Autocompleter(");
buffer.append("'").append(fieldId).append("'");
buffer.append(",'").append(fieldId).append("-auto-complete-div'");
buffer.append(",'").append(contextPath).append(page.getPath()).append("'");
String id = getId();
// Include the field id as a parameter
buffer.append(",{parameters: '").append(id).append("=1'");
if (hasParameters()) {
for (Entry<String, Object> entry : getParameters().entrySet()) {
// Add additional parameters
buffer.append("&");
buffer.append(entry.getKey());
buffer.append("=");
buffer.append(entry.getValue());
}
}
if (StringUtils.isNotEmpty(getAutoCompleteOptions())) {
buffer.append(",").append(getAutoCompleteOptions());
}
buffer.append("});");
script.setContent(buffer.toString());
headElements.add(script);
}
return headElements;
}