Search in sources :

Example 1 with Translator

use of org.openmrs.module.htmlformentry.Translator in project openmrs-module-coreapps by openmrs.

the class CodedOrFreeTextAnswerWidget method generateHtml.

@Override
public String generateHtml(FormEntryContext context) {
    Translator translator = context.getTranslator();
    String localeStr = locale.toString();
    String title = translator.translate(localeStr, titleCode);
    String placeholder = translator.translate(localeStr, placeholderCode);
    StringBuilder ret = new StringBuilder();
    if (context.getMode().equals(FormEntryContext.Mode.VIEW)) {
        if (initialValue != null) {
            ret.append("<span class=\"value\">" + initialValue.format(locale) + "</span>");
        }
    } else {
        String hiddenInputName = context.getFieldName(this);
        Map<String, Object> fragmentConfig = new HashMap<String, Object>();
        fragmentConfig.put("formFieldName", hiddenInputName);
        fragmentConfig.put("title", title);
        fragmentConfig.put("placeholder", placeholder);
        fragmentConfig.put("containerClasses", containerClasses);
        fragmentConfig.put("initialValue", initialValueAsJson(initialValue));
        try {
            ret.append(uiUtils.includeFragment("coreapps", "htmlformentry/codedOrFreeTextAnswer", fragmentConfig));
        } catch (PageAction pageAction) {
            throw new IllegalStateException("fragment threw PageAction", pageAction);
        } catch (NullPointerException ex) {
            // (This is hacky, but I don't see a better way to do it.)
            return "<input type=\"hidden\" name=\"" + context.getFieldName(this) + "\"/> (Submitting the form, so we don't generate HTML)";
        }
    }
    return ret.toString();
}
Also used : PageAction(org.openmrs.ui.framework.page.PageAction) Translator(org.openmrs.module.htmlformentry.Translator) HashMap(java.util.HashMap) SimpleObject(org.openmrs.ui.framework.SimpleObject)

Example 2 with Translator

use of org.openmrs.module.htmlformentry.Translator in project openmrs-module-coreapps by openmrs.

the class CodedOrFreeTextAnswerListWidgetTest method setUpEnter.

public void setUpEnter() throws Exception {
    context = new FormEntryContext(FormEntryContext.Mode.ENTER) {

        @Override
        public Translator getTranslator() {
            return new Translator() {

                @Override
                public String translate(String localeStr, String key) {
                    return key;
                }
            };
        }
    };
    context.registerWidget(widget);
}
Also used : Translator(org.openmrs.module.htmlformentry.Translator) FormEntryContext(org.openmrs.module.htmlformentry.FormEntryContext)

Example 3 with Translator

use of org.openmrs.module.htmlformentry.Translator in project openmrs-module-coreapps by openmrs.

the class CodedOrFreeTextAnswerListWidget method generateHtml.

@Override
public String generateHtml(FormEntryContext context) {
    Translator translator = context.getTranslator();
    String localeStr = locale.toString();
    String title = translator.translate(localeStr, titleCode);
    String betweenElements = translator.translate(localeStr, betweenElementsCode);
    String placeholder = translator.translate(localeStr, placeholderCode);
    StringBuilder ret = new StringBuilder();
    ret.append("<div class=\"coded-or-free-text-list-widget\">");
    if (context.getMode().equals(FormEntryContext.Mode.VIEW)) {
        ret.append("<span class=\"coded-or-free-text-title\">" + title + "</span>\n");
        if (initialValue != null) {
            boolean first = true;
            for (CodedOrFreeTextAnswer answer : initialValue) {
                if (!first && StringUtils.isNotEmpty(betweenElements)) {
                    ret.append("<br/>");
                    ret.append("<span class=\"coded-or-free-text-between\">").append(betweenElements).append("</span>");
                }
                ret.append("<br/>");
                ret.append("<span class=\"value\">").append(answer.format(locale)).append("</span>");
                first = false;
            }
        }
    } else {
        String hiddenInputName = context.getFieldName(this);
        Map<String, Object> fragmentConfig = new HashMap<String, Object>();
        fragmentConfig.put("formFieldName", hiddenInputName);
        fragmentConfig.put("title", title);
        fragmentConfig.put("placeholder", placeholder);
        fragmentConfig.put("betweenElements", betweenElements);
        fragmentConfig.put("containerClasses", containerClasses);
        fragmentConfig.put("initialValue", initialValueAsJson(initialValue));
        try {
            ret.append(uiUtils.includeFragment("coreapps", "htmlformentry/codedOrFreeTextAnswerList", fragmentConfig));
        } catch (PageAction pageAction) {
            throw new IllegalStateException("fragment threw PageAction", pageAction);
        } catch (NullPointerException ex) {
            // (This is hacky, but I don't see a better way to do it.)
            return "<input type=\"hidden\" name=\"" + context.getFieldName(this) + "\"/> (Submitting the form, so we don't generate HTML)";
        }
    }
    ret.append("</div>");
    return ret.toString();
}
Also used : PageAction(org.openmrs.ui.framework.page.PageAction) Translator(org.openmrs.module.htmlformentry.Translator) HashMap(java.util.HashMap) SimpleObject(org.openmrs.ui.framework.SimpleObject) CodedOrFreeTextAnswer(org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer)

Aggregations

Translator (org.openmrs.module.htmlformentry.Translator)3 HashMap (java.util.HashMap)2 SimpleObject (org.openmrs.ui.framework.SimpleObject)2 PageAction (org.openmrs.ui.framework.page.PageAction)2 CodedOrFreeTextAnswer (org.openmrs.module.emrapi.diagnosis.CodedOrFreeTextAnswer)1 FormEntryContext (org.openmrs.module.htmlformentry.FormEntryContext)1