private HtmlComponent createSwitchScript( HtmlHiddenField field, HtmlMenu menu, HtmlTextInput other) { HtmlScript script = new HtmlScript(); script.setContentType("text/javascript"); other.setId(other.getName()); String name = "showOther" + hashCode(); String body = String.format( "\n" + "function %s(s, id) {\n" + " var e = document.getElementById(id);\n" + " if (s.value == '%s') {\n" + " e.style.display = 'inline';\n" + " }\n" + " else {\n" + " e.style.display = 'none';\n" + " e.value = '';\n" + " }\n" + "}", name, OPTION_KEY); script.setScript(body); menu.setOnChange(String.format("%s(this, '%s')", name, other.getId())); return script; }
@Override protected HtmlComponent renderComponent(Layout layout, Object object, Class type) { String value = (String) object; MetaSlot slot = (MetaSlot) getInputContext().getMetaObject(); HtmlMenu menu = (HtmlMenu) super.renderComponent(layout, object, type); HtmlMenuOption otherOption = createOtherOption(menu); HtmlHiddenField field = new HtmlHiddenField(); HtmlTextInput other = new HtmlTextInput(); other.setSize(getOtherSize()); boolean otherSelected = true; for (HtmlMenuEntry entry : menu.getEntries()) { if (entry.isSelected()) { other.setStyle("display: none;"); otherSelected = false; break; } } if (otherSelected) { otherOption.setSelected(true); other.setValue(value); } field.setValue(value); field.bind(slot); field.setController(new CopyController(menu, other)); menu.setTargetSlot(null); menu.setName(getLocalName(slot, "menu")); other.setName(getLocalName(slot, "other")); HtmlContainer container = new HtmlBlockContainer(); container.addChild(createSwitchScript(field, menu, other)); container.addChild(field); container.addChild(menu); container.addChild(other); return container; }