@Override public String generateHtml(FormEntryContext context) { StringBuilder sb = new StringBuilder(); if (context.getMode() != Mode.VIEW) { sb.append("<input type=\"hidden\" name=\"" + context.getFieldName(this) + "\" id=\"" + context.getFieldName(this) + "\""); if (initialValue != null) sb.append(" value=\"" + initialValue + "\""); sb.append("/>"); } return sb.toString(); }
public static String generateHtmlForWidget(FormEntryContext context, String label, Widget widget, Widget errorWidget) { StringBuilder html = new StringBuilder(); if (widget != null){ if (label != null) { html.append(label); } html.append(widget.generateHtml(context) + " "); if (context.getMode() != Mode.VIEW && errorWidget != null) html.append(errorWidget.generateHtml(context)); } return html.toString(); }
@Override public boolean shouldIncludeContent(FormEntrySession session, Node parent, Node node) { String mode = HtmlFormEntryUtil.getNodeAttribute(node, "mode", null); String include = HtmlFormEntryUtil.getNodeAttribute(node, "include", "true"); if (mode == null) { throw new RuntimeException("ifMode tag requires the mode attribute"); } boolean modeIsCurrent = mode.equalsIgnoreCase(session.getContext().getMode().toString()); return include.equals("true") ? modeIsCurrent : !modeIsCurrent; } }
static void setupDropdownWidget(FormEntryContext context, DropdownWidget widget, List<Option> options) { if (context.getMode() != Mode.VIEW) { widget.setOptions(options); if (!options.isEmpty()) { widget.setInitialValue(options.get(0).getValue()); } } else { widget.setOptions(new ArrayList<Option>()); } context.registerWidget(widget); }
/** * @see org.openmrs.module.htmlformentry.action.FormSubmissionControllerAction#handleSubmission(org.openmrs.module.htmlformentry.FormEntrySession, javax.servlet.http.HttpServletRequest) */ @Override public void handleSubmission(FormEntrySession session, HttpServletRequest submission) { if (session.getContext().getMode() == Mode.ENTER) { session.getSubmissionActions().completeProgram(program); } }
protected void testTimeFormat(TimeWidget widget, String expectedValue) { FormEntryContext formEntryContext = mock(FormEntryContext.class); when(formEntryContext.getMode()).thenReturn(FormEntryContext.Mode.VIEW); String html = widget.generateHtml(formEntryContext); String expected = "<span class=\"value\">" + expectedValue + "</span>"; assertEquals(expected, html); }
@Test public void getSubstitution_shouldGenerateProperHtmlWithCustomSubmitLabel() { Map<String,String> params = new HashMap<String, String>(); params.put("submitLabel", "Custom Button"); when(context.getMode()).thenReturn(FormEntryContext.Mode.EDIT); String html = submitButtonHandler.getSubstitution(session, formSubmissionController, params); assertThat(html, is("<input type=\"button\" class=\"submitButton\" value=\"Custom Button\" onClick=\"submitHtmlForm()\"/>")); }
@Test public void getSubstitution_shouldGenerateProperHtmlWithCustomClassSpecifiedInSubmitClassAttribute() { Map<String,String> params = new HashMap<String, String>(); params.put("submitClass", "custom-class"); when(context.getMode()).thenReturn(FormEntryContext.Mode.EDIT); String html = submitButtonHandler.getSubstitution(session, formSubmissionController, params); assertThat(html, is("<input type=\"button\" class=\"submitButton custom-class\" value=\"Save\" onClick=\"submitHtmlForm()\"/>")); }
@Test public void getSubstitution_shouldGenerateProperHtmlWithCustomClassSpecifiedInClassAttribute() { Map<String,String> params = new HashMap<String, String>(); params.put("class", "custom-class"); when(context.getMode()).thenReturn(FormEntryContext.Mode.EDIT); String html = submitButtonHandler.getSubstitution(session, formSubmissionController, params); assertThat(html, is("<input type=\"button\" class=\"submitButton custom-class\" value=\"Save\" onClick=\"submitHtmlForm()\"/>")); }
@Test public void getSubstitution_shouldGenerateProperHtmlInViewMode() { when(context.getMode()).thenReturn(FormEntryContext.Mode.VIEW); String html = submitButtonHandler.getSubstitution(session, formSubmissionController, new HashMap<String, String>()); assertThat(html, is("")); // nothing should be rendered in view mode }
@Test public void getSubstitution_shouldGenerateProperHtmlInEnterMode() { when(context.getMode()).thenReturn(FormEntryContext.Mode.ENTER); String html = submitButtonHandler.getSubstitution(session, formSubmissionController, new HashMap<String, String>()); assertThat(html, is("<input type=\"button\" class=\"submitButton\" value=\"Enter\" onClick=\"submitHtmlForm()\"/>")); }
@Test public void getSubstitution_shouldGenerateProperHtmlInEditMode() { when(context.getMode()).thenReturn(FormEntryContext.Mode.EDIT); String html = submitButtonHandler.getSubstitution(session, formSubmissionController, new HashMap<String, String>()); assertThat(html, is("<input type=\"button\" class=\"submitButton\" value=\"Save\" onClick=\"submitHtmlForm()\"/>")); }
@Test public void testDisplaysWholeNumbersWithoutDecimal() throws Exception { when(context.getMode()).thenReturn(FormEntryContext.Mode.VIEW); NumberFieldWidget widget = new NumberFieldWidget(null, null, true); widget.setInitialValue(100d); assertThat(widget.generateHtml(context), is("<span class=\"value\">100</span>")); }
@Test public void generateHtml_shouldRenderSingleConceptProperlyInViewMode() { when(context.getMode()).thenReturn(FormEntryContext.Mode.VIEW); DynamicAutocompleteWidget dynamicAutocompleteWidget = new DynamicAutocompleteWidget(generateConceptList(), null); Concept initialValue = mock(Concept.class); when(initialValue.getDisplayString()).thenReturn("concept1"); dynamicAutocompleteWidget.setInitialValue(initialValue); String html = dynamicAutocompleteWidget.generateHtml(context); TestUtil.assertFuzzyContains("concept1", html); }
@Test public void generateHtml_shouldRenderMultipleConceptProperlyInViewMode() { when(context.getMode()).thenReturn(FormEntryContext.Mode.VIEW); DynamicAutocompleteWidget dynamicAutocompleteWidget = new DynamicAutocompleteWidget(generateConceptList(), null); Concept initialValue1 = mock(Concept.class); when(initialValue1.getDisplayString()).thenReturn("concept1"); dynamicAutocompleteWidget.addInitialValue(initialValue1); Concept initialValue2 = mock(Concept.class); when(initialValue2.getDisplayString()).thenReturn("concept2"); dynamicAutocompleteWidget.addInitialValue(initialValue2); String html = dynamicAutocompleteWidget.generateHtml(context); TestUtil.assertFuzzyContains("concept1;concept2;", html); }
@Test public void generateHtml_shouldRenderProperHtmlWithConceptList() { when(context.getMode()).thenReturn(FormEntryContext.Mode.EDIT); DynamicAutocompleteWidget dynamicAutocompleteWidget = new DynamicAutocompleteWidget(generateConceptList(), null); String html = dynamicAutocompleteWidget.generateHtml(context); TestUtil.assertFuzzyContains("<div id=\"w2_div\" class=\"dynamicAutocomplete\">" + "<input name=\"w2_hid\" id=\"w2_hid\" type=\"hidden\" class=\"autoCompleteHidden\" />" + "<input type=\"text\" id=\"w2\" name=\"w2\" onfocus=\"setupAutocomplete\\(this, 'conceptSearch.form','1,2,3','null'\\); \"class=\"autoCompleteText\" onBlur=\"onBlurAutocomplete\\(this\\)\"/>" + "<input id=\"w2_button\" type=\"button\" class=\"addConceptButton\" value=\"Add\" /></div>", html); }
@Test public void generateHtml_shouldRenderProperHtmlWithConceptClasses() { when(context.getMode()).thenReturn(FormEntryContext.Mode.EDIT); DynamicAutocompleteWidget dynamicAutocompleteWidget = new DynamicAutocompleteWidget(null, generateConceptClassList()); String html = dynamicAutocompleteWidget.generateHtml(context); TestUtil.assertFuzzyContains("<div id=\"w2_div\" class=\"dynamicAutocomplete\">" + "<input name=\"w2_hid\" id=\"w2_hid\" type=\"hidden\" class=\"autoCompleteHidden\" />" + "<input type=\"text\" id=\"w2\" name=\"w2\" onfocus=\"setupAutocomplete\\(this, 'conceptSearch.form','null','someClass,anotherClass'\\); \"class=\"autoCompleteText\" onBlur=\"onBlurAutocomplete\\(this\\)\"/>" + "<input id=\"w2_button\" type=\"button\" class=\"addConceptButton\" value=\"Add\" /></div>", html); }
@Test public void generateHtml_shouldRenderProperHtmlWithConceptClassList() { when(context.getMode()).thenReturn(FormEntryContext.Mode.EDIT); ConceptSearchAutocompleteWidget conceptSearchAutocompleteWidget = new ConceptSearchAutocompleteWidget(null, generateConceptClassList()); String html = conceptSearchAutocompleteWidget.generateHtml(context); TestUtil.assertFuzzyContains("<input type=\"text\" id=\"w2\" name=\"w2\" onfocus=\"setupAutocomplete\\(this, 'conceptSearch.form','null','someClass,anotherClass'\\); \"class=\"autoCompleteText\" onchange=\"setValWhenAutocompleteFieldBlanked\\(this\\)\" onblur=\"onBlurAutocomplete\\(this\\)\"/>" + "<input name=\"w2_hid\" id=\"w2_hid\" type=\"hidden\" class=\"autoCompleteHidden\" />", html); }
@Test public void generateHtml_shouldRenderProperHtmlWithConceptList() { when(context.getMode()).thenReturn(FormEntryContext.Mode.EDIT); ConceptSearchAutocompleteWidget conceptSearchAutocompleteWidget = new ConceptSearchAutocompleteWidget(generateConceptList(), null); String html = conceptSearchAutocompleteWidget.generateHtml(context); TestUtil.assertFuzzyContains("<input type=\"text\" id=\"w2\" name=\"w2\" onfocus=\"setupAutocomplete\\(this, 'conceptSearch.form','1,2,3','null'\\); \"class=\"autoCompleteText\" onchange=\"setValWhenAutocompleteFieldBlanked\\(this\\)\" onblur=\"onBlurAutocomplete\\(this\\)\"/>" + "<input name=\"w2_hid\" id=\"w2_hid\" type=\"hidden\" class=\"autoCompleteHidden\" />", html); }
@Test public void testShowsWholeNumberWithoutDecimalInEditMode() throws Exception { when(context.getMode()).thenReturn(FormEntryContext.Mode.EDIT); NumberFieldWidget widget = new NumberFieldWidget(null, null, true); widget.setInitialValue(100d); String html = widget.generateHtml(context); assertThat(getAttribute(html, "value"), is("100")); }