Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

150
LINES

< > BotCompany Repo | #1026001 - HCRUD - CRUD in HTML with pluggable data handler

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (15401L/107K).

1  
sclass HCRUD extends HAbstractRenderable {
2  
  HCRUD_Data data;
3  
  int defaultTextFieldCols = 80;
4  
  bool mutationRights = true;
5  
6  
  *() {}
7  
  *(HCRUD_Data *data) {}
8  
  *(S *baseLink, HCRUD_Data *data) {}
9  
  
10  
  S newLink() { ret appendQueryToURL(baseLink, cmd := "new"); }
11  
  S deleteLink(O id) { ret appendQueryToURL(baseLink, "delete_" + id, 1); }
12  
  S editLink(O id) { ret appendQueryToURL(baseLink, edit := id); }
13  
  S duplicateLink(O id) { ret appendQueryToURL(baseLink, duplicate := id); }
14  
  
15  
  // also handles commands if withCmds=true
16  
  S render(bool withCmds, SS params) {
17  
    if (!withCmds) ret renderTable(false);
18  
    
19  
    try answer handleCommands(params);
20  
    
21  
    ret renderMsgs(params)
22  
      + p(ahref(newLink(), "New entry"))
23  
      + renderTable(withCmds);
24  
  }
25  
26  
  S handleCommands(SS params) {
27  
    new LS msgs;
28  
    
29  
    if (eqGet(params, "action", "create")) {
30  
      O id = data.createObject(subMapStartingWith_dropPrefix(params, "f_"));
31  
      msgs.add("Entry created (ID: " + id + ")");
32  
    }
33  
    
34  
    if (eqGet(params, "action", "update"))
35  
      msgs.add(data.updateObject(params.get("id"),
36  
        subMapStartingWith_dropPrefix(params, "f_")));
37  
38  
    for (S toDelete : keysDeprefixNemptyValue(params, "delete_"))
39  
      msgs.add(data.deleteObject(toDelete));
40  
41  
    ret nempty(msgs) ? refreshWithMsgs(msgs) : "";
42  
  }
43  
  
44  
  S encodeField(S s) {
45  
    ret or(data.fieldNameToHTML(s), s);
46  
  }
47  
  
48  
  // in table
49  
  S renderValue(S field, O value) {
50  
    if (value cast HTML) ret value.html;
51  
    ret htmlEncode_nlToBr_withIndents(strOrEmpty(value));
52  
  }
53  
  
54  
  S renderTable(bool withCmds) {
55  
    ret renderTable(withCmds, data.list());
56  
  }
57  
  
58  
  S renderTable(bool withCmds, L<Map<S, O>> l) {
59  
    if (empty(l)) ret p("No entries");
60  
    //LS fields = data.fields();
61  
    //if (fields == null) fields = allKeysFromList_inOrder();
62  
    l = map(l, map -> {
63  
      Map<S, O> map2 = mapToMap(
64  
        (key, value) -> pair(encodeField(key), renderValue(key, value)),
65  
        map);
66  
      if (withCmds) {
67  
        O id = map.get(data.idField());
68  
        map2.put(cmdsKey(),
69  
          joinWithVBar(
70  
            ahref(editLink(id), "edit"),
71  
            ahrefWithConfirm(
72  
            "Really delete item " + id + "?", deleteLink(id), htmlEncode2(unicode_DEL())),
73  
            ahref(duplicateLink(id), "dup", title := "duplicate")
74  
          ));
75  
      }
76  
      ret map2;
77  
    });
78  
    ret htmlTable2_noHtmlEncode(l, tdParams := litparams(valign := "top"));
79  
  }
80  
  
81  
  S renderForm(Map<S, O> map) {
82  
    ret htableRaw_valignTop(
83  
      map(map, (field, value) -> {
84  
        S help = data.fieldHelp(field);
85  
        ret ll(
86  
          encodeField(field),
87  
          renderInput(field, value)
88  
            + (empty(help) ? "" : "<br> " + small(help))
89  
        );
90  
      })
91  
    , border := 1, cellpadding := 4);
92  
  }
93  
  
94  
  S renderInput(S field, O value) {
95  
    S name = "f_" + field;
96  
    HCRUD_Data.Renderer r = data.getRenderer(field);
97  
    if (r cast HCRUD_Data.TextArea)
98  
      ret htextarea(strOrEmpty(value), +name, cols := r.cols, rows := r.rows);
99  
    if (r cast HCRUD_Data.TextField)
100  
      ret htextfield(name, strOrEmpty(value), size := r.cols, style := "font-family: monospace");
101  
    if (r cast HCRUD_Data.ComboBox)
102  
      ret hselect_list(r.entries, strOrEmpty(value), +name);
103  
    ret renderInput_default(field, value);
104  
  }
105  
  
106  
  S renderInput_default(S field, O value) {
107  
    S name = "f_" + field;
108  
    ret htextfield(name, strOrEmpty(value), size := defaultTextFieldCols);
109  
  }
110  
  
111  
  S renderNewForm() {
112  
    ret renderNewForm(data.emptyObject());
113  
  }
114  
  
115  
  S renderNewForm(MapSO map1) {
116  
    //printStruct("renderNewForm", map1);
117  
    Map<S, O> map = mapWithoutKey(map1, data.idField());
118  
    //printStruct("renderNewForm", map);
119  
    ret hpostform(
120  
      hhidden("action", "create") +
121  
      renderForm(map)
122  
      + p(hsubmit("Create")),
123  
      action := baseLink);
124  
  }
125  
  
126  
  S renderEditForm(S id) {
127  
    Map<S, O> map = mapWithoutKey(data.getObject(id), data.idField());
128  
    if (map == null) ret htmlEncode2("Entry " + id + " not found");
129  
    ret hpostform(
130  
      hhidden("action", "update") +
131  
      hhidden(+id) +
132  
      renderForm(map)
133  
      + p(hsubmit("Save changes")),
134  
      action := baseLink);
135  
  }
136  
  
137  
  S renderPage(SS params) {
138  
    if (eqGet(params, "cmd", "new"))
139  
      ret frame("New " + data.itemName(), renderNewForm());
140  
    if (nempty(params.get("edit")))
141  
      ret frame("Edit " + data.itemName(), renderEditForm(params.get("edit")));
142  
    if (nempty(params.get("duplicate")))
143  
      ret frame("New " + data.itemName(), renderNewForm(data.getObject(params.get("duplicate"))));
144  
    ret frame(ahref(baseLink, firstToUpper(data.itemNamePlural())), render(mutationRights, params));
145  
  }
146  
  
147  
  HCRUD makeFrame(MakeFrame makeFrame) { super.makeFrame(makeFrame); this; }
148  
  
149  
  S cmdsKey() { ret "<!-- cmds -->"; }
150  
}

download  show line numbers  debug dex   

Travelled to 4 computer(s): mqqgnosmbjvj, onxytkatvevr, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026001
Snippet name: HCRUD - CRUD in HTML with pluggable data handler
Eternal ID of this version: #1026001/40
Text MD5: 298622f8c6c9a0752d067371dd2c4167
Transpilation MD5: be507ad576741c98dc8ff11fe874b410
Author: stefan
Category: javax / html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-07 21:21:28
Source code size: 4917 bytes / 150 lines
Pitched / IR pitched: No / No
Views / Downloads: 134 / 443
Version history: 39 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1026001 & http://1026001.tinybrain.de