Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom repeat captions #1428

Merged
merged 7 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/main/java/org/javarosa/form/api/FormEntryCaption.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.javarosa.form.api;

import org.commcare.cases.util.StringUtils;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.FormIndex;
import org.javarosa.core.model.GroupDef;
Expand Down Expand Up @@ -217,43 +218,43 @@ public String getRepeatText(String typeKey) {

String caption = null;
if ("mainheader".equals(typeKey)) {
caption = g.mainHeader;
caption = getCaptionText(g.mainHeader);
if (caption == null) {
return title;
}
} else if ("add".equals(typeKey)) {
caption = g.addCaption;
caption = getCaptionText(g.addCaption);
if (caption == null) {
return "Add another " + title;
}
} else if ("add-empty".equals(typeKey)) {
caption = g.addEmptyCaption;
caption = getCaptionText(g.addEmptyCaption);
if (caption == null) {
caption = g.addCaption;
}
if (caption == null) {
return "None - Add " + title;
}
} else if ("del".equals(typeKey)) {
caption = g.delCaption;
caption = getCaptionText(g.delCaption);
if (caption == null) {
return "Delete " + title;
}
} else if ("done".equals(typeKey)) {
caption = g.doneCaption;
caption = getCaptionText(g.doneCaption);
if (caption == null) {
return "Done";
}
} else if ("done-empty".equals(typeKey)) {
caption = g.doneEmptyCaption;
caption = getCaptionText(g.doneEmptyCaption);
if (caption == null) {
caption = g.doneCaption;
}
if (caption == null) {
return "Skip";
}
} else if ("delheader".equals(typeKey)) {
caption = g.delHeader;
caption = getCaptionText(g.delHeader);
if (caption == null) {
return "Delete which " + title + "?";
}
Expand All @@ -265,6 +266,16 @@ public String getRepeatText(String typeKey) {
return form.fillTemplateString(caption, index.getReference(), vars);
}

private String getCaptionText(String textIdOrText) {
Jtang-1 marked this conversation as resolved.
Show resolved Hide resolved
if (!StringUtils.isEmpty(textIdOrText)) {
String returnText = getIText(textIdOrText, null);
if (returnText != null) {
return substituteStringArgs(returnText);
}
}
return substituteStringArgs(textIdOrText);
}

//this should probably be somewhere better
public int getNumRepetitions() {
return form.getNumRepetitions(index);
Expand Down
51 changes: 32 additions & 19 deletions src/main/java/org/javarosa/xform/parse/XFormParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.javarosa.xform.parse;

import org.commcare.cases.util.StringUtils;
import org.javarosa.core.model.Constants;
import org.javarosa.core.model.DataBinding;
import org.javarosa.core.model.FormDef;
Expand Down Expand Up @@ -1082,27 +1083,38 @@ private void parseGroupLabel(GroupDef g, Element e) {
Vector<String> usedAtts = new Vector<>();
usedAtts.addElement(REF_ATTR);

String labelItextId = getItextReference(e);
g.setTextID(labelItextId);
Jtang-1 marked this conversation as resolved.
Show resolved Hide resolved
if (labelItextId == null) {
String label = getLabel(e);
g.setLabelInnerText(label);
}

String label = getLabel(e);
String ref = e.getAttributeValue("", REF_ATTR);
if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
}
}

private String getItextReference(Element e) {
String ref = e.getAttributeValue("", REF_ATTR);
if (ref != null) {
if (ref.startsWith(ITEXT_OPEN) && ref.endsWith(ITEXT_CLOSE)) {
String textRef = ref.substring(ITEXT_OPEN.length(), ref.indexOf(ITEXT_CLOSE));

verifyTextMappings(textRef, "Group <label>", true);
g.setTextID(textRef);
return textRef;
} else {
throw new RuntimeException("malformed ref [" + ref + "] for <label>");
throw new XFormParseException("malformed ref [" + ref + "] for <label>");
}
} else {
g.setLabelInnerText(label);
}
return null;
}


if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
private String getLabelOrTextId(Element element) {
String labelItextId = getItextReference(element);
if (!StringUtils.isEmpty(labelItextId)) {
return labelItextId;
}
return getLabel(element);
}

private String getLabel(Element e) {
Expand Down Expand Up @@ -1475,23 +1487,23 @@ private void parseGroup(IFormElement parent, Element e, int groupType) {

if (group.isRepeat() && NAMESPACE_JAVAROSA.equals(childNamespace)) {
if ("chooseCaption".equals(childName)) {
group.chooseCaption = getLabel(child);
group.chooseCaption = getLabelOrTextId(child);
} else if ("addCaption".equals(childName)) {
group.addCaption = getLabel(child);
group.addCaption = getLabelOrTextId(child);
} else if ("delCaption".equals(childName)) {
group.delCaption = getLabel(child);
group.delCaption = getLabelOrTextId(child);
} else if ("doneCaption".equals(childName)) {
group.doneCaption = getLabel(child);
group.doneCaption = getLabelOrTextId(child);
} else if ("addEmptyCaption".equals(childName)) {
group.addEmptyCaption = getLabel(child);
group.addEmptyCaption = getLabelOrTextId(child);
} else if ("doneEmptyCaption".equals(childName)) {
group.doneEmptyCaption = getLabel(child);
group.doneEmptyCaption = getLabelOrTextId(child);
} else if ("entryHeader".equals(childName)) {
group.entryHeader = getLabel(child);
group.entryHeader = getLabelOrTextId(child);
} else if ("delHeader".equals(childName)) {
group.delHeader = getLabel(child);
group.delHeader = getLabelOrTextId(child);
} else if ("mainHeader".equals(childName)) {
group.mainHeader = getLabel(child);
group.mainHeader = getLabelOrTextId(child);
}
}
}
Expand All @@ -1512,6 +1524,7 @@ private void parseGroup(IFormElement parent, Element e, int groupType) {
parent.addChild(group);
}


private TreeReference getFormElementRef(IFormElement fe) {
if (fe instanceof FormDef) {
TreeReference ref = TreeReference.rootRef();
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/javarosa/core/model/test/FormDefTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.javarosa.core.model.Constants;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.FormIndex;
import org.javarosa.core.model.GroupDef;
import org.javarosa.core.model.IFormElement;
import org.javarosa.core.model.QuestionDef;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.condition.IFunctionHandler;
Expand All @@ -20,6 +22,7 @@
import org.javarosa.core.model.instance.test.DummyInstanceInitializationFactory;
import org.javarosa.core.model.utils.test.PersistableSandbox;
import org.javarosa.core.test.FormParseInit;
import org.javarosa.form.api.FormEntryCaption;
import org.javarosa.form.api.FormEntryController;
import org.javarosa.test_utils.ExprEvalUtils;
import org.javarosa.xpath.parser.XPathSyntaxException;
Expand Down Expand Up @@ -666,4 +669,19 @@ public void testItemsetPopulationAndFilter() {

} while (fec.stepToNextEvent() != FormEntryController.EVENT_END_OF_FORM);
}

@Test
public void testRepeatCaptions() throws Exception {
FormParseInit fpi = new FormParseInit("/xform_tests/sweet_repeat_demo.xml");
FormEntryController fec = initFormEntry(fpi);
FormDef formDef = fec.getModel().getForm();
GroupDef repeat = (GroupDef)formDef.getChild(2);
assertEquals("main-header-label", repeat.mainHeader);
assertEquals("add-caption-label",repeat.addCaption);
assertEquals("add-empty-caption-label",repeat.addEmptyCaption);
assertEquals("del-caption-label",repeat.delCaption);
assertEquals("done-caption-label",repeat.doneCaption);
assertEquals("done-empty-caption-label",repeat.doneEmptyCaption);
assertEquals("choose-caption-label",repeat.chooseCaption);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<age/>
<sex/>
</child>
<child_new jr:template="">
<name/>
<age/>
<sex/>
</child_new>
</children>

<qq/>
Expand All @@ -26,7 +31,31 @@
</instance>

<bind nodeset="children/child/age" type="int"/>

<itext>
<translation lang="en" default="">
<text id="main-header-label">
<value>Main Header Label</value>
</text>
<text id="add-caption-label">
<value>Add Caption Label</value>
</text>
<text id="add-empty-caption-label">
<value>Add Empty Caption Label</value>
</text>
<text id="del-caption-label">
<value>Delete Caption Label</value>
</text>
<text id="done-caption-label">
<value>Done Caption Label</value>
</text>
<text id="done-empty-caption-label">
<value>Done Empty Caption Label</value>
</text>
<text id="choose-caption-label">
<value>Choose Caption Label</value>
</text>
</translation>
</itext>
</model>
</h:head>
<h:body>
Expand Down Expand Up @@ -78,7 +107,36 @@
</select1>
</repeat>
</group>
<group>
<label>child</label>
<repeat nodeset="children/child_new">
<jr:mainHeader ref="jr:itext('main-header-label')" />
<jr:addCaption ref="jr:itext('add-caption-label')" />
<jr:addEmptyCaption ref="jr:itext('add-empty-caption-label')" />
<jr:delCaption ref="jr:itext('del-caption-label')" />
<jr:doneCaption ref="jr:itext('done-caption-label')" />
<jr:doneEmptyCaption ref="jr:itext('done-empty-caption-label')" />
<jr:chooseCaption ref="jr:itext('choose-caption-label')" />

<input ref="name">
<label>name</label>
</input>
<input ref="age">
<label>age</label>
</input>
<select1 ref="sex">
<label>sex</label>
<item>
<label>Male</label>
<value>M</value>
</item>
<item>
<label>Female</label>
<value>F</value>
</item>
</select1>
</repeat>
</group>
<input ref="qq">
<label>post q</label>
</input>
Expand Down
Loading