Skip to content

Commit

Permalink
Added support for form customization with custom fields and field vars.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jan 9, 2015
1 parent 777535b commit 065730d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
Expand Up @@ -42,6 +42,7 @@ public Object content() {
Tag caption1 = titleBox("Add new task"); Tag caption1 = titleBox("Add new task");
FormWidget frm = create(task).buttons(ADD, CANCEL); FormWidget frm = create(task).buttons(ADD, CANCEL);
frm = frm.fieldType("description", FieldType.TEXTAREA); frm = frm.fieldType("description", FieldType.TEXTAREA);
frm = frm.field("comments", h3("my custom field"));


Tag caption2 = titleBox("Most recent tasks"); Tag caption2 = titleBox("Most recent tasks");
GridWidget grid = grid(Task.class, "-id", 7, "id", "priority", "title"); GridWidget grid = grid(Task.class, "-id", 7, "id", "priority", "title");
Expand Down
Expand Up @@ -58,6 +58,7 @@ public class Task extends Entity {
@CanRead({ CommonRoles.OWNER }) @CanRead({ CommonRoles.OWNER })
public final DbSet<User> sharedWith = DB.set(this, "sharedWith"); public final DbSet<User> sharedWith = DB.set(this, "sharedWith");


@Programmatic
@CanRead({ CommonRoles.OWNER, CommonRoles.SHARED_WITH }) @CanRead({ CommonRoles.OWNER, CommonRoles.SHARED_WITH })
public final DbList<Comment> comments = DB.list(this, "has"); public final DbList<Comment> comments = DB.list(this, "has");


Expand Down
5 changes: 0 additions & 5 deletions rapidoid-model/src/main/java/org/rapidoid/model/Models.java
Expand Up @@ -24,7 +24,6 @@
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;


import org.rapidoid.annotation.Programmatic;
import org.rapidoid.model.impl.BeanItem; import org.rapidoid.model.impl.BeanItem;
import org.rapidoid.model.impl.BeanListItems; import org.rapidoid.model.impl.BeanListItems;
import org.rapidoid.model.impl.BeanProperty; import org.rapidoid.model.impl.BeanProperty;
Expand Down Expand Up @@ -132,10 +131,6 @@ public static boolean isEditable(Prop prop) {
return false; return false;
} }


if (prop.getAnnotation(Programmatic.class) != null) {
return false;
}

return true; return true;
} }


Expand Down
94 changes: 75 additions & 19 deletions rapidoid-pages/src/main/java/org/rapidoid/widget/FormWidget.java
Expand Up @@ -27,6 +27,7 @@
import java.util.List; import java.util.List;


import org.rapidoid.annotation.Optional; import org.rapidoid.annotation.Optional;
import org.rapidoid.annotation.Programmatic;
import org.rapidoid.html.FieldType; import org.rapidoid.html.FieldType;
import org.rapidoid.html.FormLayout; import org.rapidoid.html.FormLayout;
import org.rapidoid.html.Tag; import org.rapidoid.html.Tag;
Expand All @@ -53,6 +54,8 @@ public class FormWidget extends AbstractWidget {
protected final Item item; protected final Item item;
protected final FormMode mode; protected final FormMode mode;


protected List<Property> props;

protected Tag[] buttons; protected Tag[] buttons;
protected FormLayout layout = FormLayout.VERTICAL; protected FormLayout layout = FormLayout.VERTICAL;
protected String[] fieldNames; protected String[] fieldNames;
Expand All @@ -62,6 +65,7 @@ public class FormWidget extends AbstractWidget {
protected boolean[] fieldRequired; protected boolean[] fieldRequired;
protected Var<?>[] vars; protected Var<?>[] vars;
protected DataPermissions[] permissions; protected DataPermissions[] permissions;
protected Tag[] fields;


protected boolean hasFields = false; protected boolean hasFields = false;


Expand All @@ -86,6 +90,25 @@ public FormWidget(DataManager dataManager, FormMode mode, FormLayout layout, Str
this.buttons = buttons; this.buttons = buttons;
} }


/************************** FIELD ********************************/

public FormWidget field(String fieldName, Tag field) {
return field(fieldIndex(fieldName), field);
}

public FormWidget field(int fieldIndex, Tag field) {
this.fields[fieldIndex] = field;
return this;
}

public Tag field(String fieldName) {
return field(fieldIndex(fieldName));
}

public Tag field(int fieldIndex) {
return this.fields[fieldIndex];
}

/************************** FIELD LABEL ********************************/ /************************** FIELD LABEL ********************************/


public FormWidget fieldLabel(String fieldName, String fieldLabel) { public FormWidget fieldLabel(String fieldName, String fieldLabel) {
Expand Down Expand Up @@ -181,26 +204,34 @@ public DataPermissions permissions(int fieldIndex) {
return this.permissions[fieldIndex]; return this.permissions[fieldIndex];
} }


/************************** BUTTONS ********************************/ /************************** FIELD VARS ********************************/


public FormWidget buttons(ButtonTag... buttons) { public FormWidget vars(String fieldName, Var<?> vars) {
this.buttons = buttons; return vars(fieldIndex(fieldName), vars);
}

public FormWidget vars(int fieldIndex, Var<?> vars) {
this.vars[fieldIndex] = vars;
return this; return this;
} }


public Tag[] buttons() { public Var<?> vars(String fieldName) {
return this.buttons; return vars(fieldIndex(fieldName));
} }


/************************** VARS ********************************/ public Var<?> vars(int fieldIndex) {
return this.vars[fieldIndex];
}


public FormWidget vars(Var<?>... vars) { /************************** BUTTONS ********************************/
this.vars = vars;
public FormWidget buttons(ButtonTag... buttons) {
this.buttons = buttons;
return this; return this;
} }


public Var<?>[] vars() { public Tag[] buttons() {
return this.vars; return this.buttons;
} }


/************************** OTHER ********************************/ /************************** OTHER ********************************/
Expand All @@ -217,8 +248,7 @@ public int fieldIndex(String fieldName) {


protected void init(Item item, String... properties) { protected void init(Item item, String... properties) {


final List<Property> props = editable() ? item.editableProperties(properties) : item props = editable() ? item.editableProperties(properties) : item.readableProperties(properties);
.readableProperties(properties);


int propN = props.size(); int propN = props.size();


Expand All @@ -229,6 +259,7 @@ protected void init(Item item, String... properties) {
fieldRequired = new boolean[propN]; fieldRequired = new boolean[propN];
vars = new Var[propN]; vars = new Var[propN];
permissions = new DataPermissions[propN]; permissions = new DataPermissions[propN];
fields = new Tag[propN];


for (int i = 0; i < propN; i++) { for (int i = 0; i < propN; i++) {
Property prop = props.get(i); Property prop = props.get(i);
Expand Down Expand Up @@ -347,23 +378,48 @@ protected FormTag create() {


protected FormTag addFormFields(FormTag form) { protected FormTag addFormFields(FormTag form) {
for (int i = 0; i < fieldNames.length; i++) { for (int i = 0; i < fieldNames.length; i++) {
if (isFieldAllowed(i)) { Tag field = getField(i);
Var<?> var = vars[i]; if (field != null) {
if (fieldRequired[i]) { form = form.append(field);
var = Vars.mandatory(var);
}
form = form.append(field(fieldNames[i], fieldLabels[i], fieldTypes[i], fieldOptions[i], var));
hasFields = true; hasFields = true;
} }
} }


if (!hasFields) { if (!hasFields) {
form = form.append(h4("Insufficient permissions!")); form = form.append(noFormFields());
} }


return form; return form;
} }


protected Tag noFormFields() {
return h4("Insufficient permissions!");
}

private Tag getField(int index) {
if (!isFieldAllowed(index)) {
return null;
}

if (fields[index] != null) {
return fields[index];
}

if (!isFieldProgrammatic(index) || mode == FormMode.SHOW) {
Var<?> var = vars[index];
if (fieldRequired[index]) {
var = Vars.mandatory(var);
return field(fieldNames[index], fieldLabels[index], fieldTypes[index], fieldOptions[index], var);
}
}

return null;
}

protected boolean isFieldProgrammatic(int index) {
return Metadata.get(props.get(index).annotations(), Programmatic.class) != null;
}

protected boolean isFieldAllowed(int index) { protected boolean isFieldAllowed(int index) {
DataPermissions perm = permissions[index]; DataPermissions perm = permissions[index];


Expand Down

0 comments on commit 065730d

Please sign in to comment.