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

153
LINES

< > BotCompany Repo | #1026000 // HCRUD_Data

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

Libraryless. Click here for Pure Java version (17760L/109K).

sclass HCRUD_Data {
  new Map<S, Renderer> renderers;
  new SS fieldHelp;
  O currentValue; // current field value held temporarily
  bool humanizeFieldNames = true;
  
  SS rawFormValues; // temporary map with raw form values
  
  abstract sclass Renderer {
    S metaInfo;

    swappable O preprocessValue(O value) { ret value; }
  }
  
  sclass NotEditable extends Renderer {}
  
  srecord TextArea(int cols, int rows) extends Renderer {
    // star constructor syntax not working here (transpiler bug)
    TextArea(int cols, int rows, IF1 preprocessValue) {
      this.cols = cols;
      this.rows = rows;
      this.preprocessValue = preprocessValue;
    }
  }
  
  sclass AceEditor extends TextArea {
    *() {}
    *(int *cols, int *rows) {}
  }
  
  srecord TextField(int cols) extends Renderer {}
  
  srecord AbstractComboBox extends Renderer {
    bool editable;
    
    // help find item to select
    swappable S valueToEntry(O value) { ret strOrNull(value); }
  }
  
  srecord ComboBox(LS entries) extends AbstractComboBox {
    ComboBox(S... entries) { this(asList(entries)); }
    ComboBox(bool editable, S... entries) { this(entries); this.editable = editable; }
    ComboBox(bool editable, LS entries) { this(entries); this.editable = editable; }
    
    // star constructor syntax not working here (transpiler bug)
    ComboBox(LS entries, IF1<O, S> valueToEntry) {
      this.entries = entries;
      this.valueToEntry = valueToEntry;
    }
  }
  
  srecord DynamicComboBox(S info) extends AbstractComboBox {
    S url;
  }

  srecord CheckBox() extends Renderer {
    { metaInfo = "Bool"; }
  }
  
  srecord FlexibleLengthList(Renderer itemRenderer) extends Renderer {}
  
  S itemName() { ret "object"; }
  S itemNamePlural() { ret plural(itemName()); }
  
  //LS fields() { null; }
  
  L<MapSO> list() { null; }
  L<MapSO> list(IntRange range) { ret subListOrFull(list(), range); }
  
  S idField() { ret "id"; }
  MapSO emptyObject() { null; }
  MapSO getObject(O id) { null; }
  MapSO getObjectForEdit(O id) { ret getObject(id); }
  swappable MapSO getObjectForDuplication(O id) { ret getObject(id); }
  
  // return ID
  O createObject(SS fullMap, S fieldPrefix) { throw unimplemented(); }
  
  // return text msg
  S deleteObject(O id) { throw unimplemented(); }
  
  bool objectCanBeDeleted(O id) { true; }
  swappable bool objectCanBeEdited(O id) { true; }
  
  
  // return text msg
  S updateObject(O id, SS fullMap, S fieldPrefix) { throw unimplemented(); }
  
  // return null for standard input field
  Renderer getRenderer(S field) { ret renderers.get(field); }
  
  final Renderer getRenderer(S field, O value) {
    this.currentValue = value;
    try {
      ret getRenderer(field);
    } finally {
      this.currentValue = null;
    }
  }
  
  // returns HTML
  S fieldHelp(S field) { ret fieldHelp.get(field); }
  
  returnSelf addRenderer(S field, Renderer renderer) { renderers.put(field, renderer); }
  
  returnSelf fieldHelp(S field, S help, S... more) {
    fieldHelp.put(field, help);
    for (int i = 0; i+1 < l(more); i += 2)
      fieldHelp.put(more[i], more[i+1]);
  }
  
  S fieldNameToHTML(S name) {
    S help = fieldHelp.get(name);
    ret spanTitle(help, htmlencode2(
      humanizeFieldNames ? humanizeLabel(name) : name));
  }
  
  Set<S> filteredFields() { null; }
  
  LS comboBoxSearch(S info, S query) { null; }
  
  // optional page title when showing a single object
  S titleForObjectID(O id) { null; }
  
  // second parameter indicates descending
  Pair<S, Bool> defaultSortField() { null; }
  
  abstract class Item extends AbstractMap<S, O> {
    O id;
    MapSO fullMap;
    
    *(O *id) {}
    
    abstract MapSO calcFullMap();
    
    MapSO fullMap() { if (fullMap == null) fullMap = calcFullMap(); ret fullMap; }
    
    replace A with S.
    replace B with O.
    
    public int size() { ret l(fullMap()); }
    public Set<Map.Entry<A, B>> entrySet() { ret fullMap().entrySet(); }
    public bool containsKey(O o) { ret fullMap().containsKey(o); }
    public B get(O o) {
      if (fullMap == null && eq(o, idField())) ret id;
      ret fullMap().get(o);
    }
    
    public B put(A key, B value) {
      ret fullMap().put(key, value);
    }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026000
Snippet name: HCRUD_Data
Eternal ID of this version: #1026000/60
Text MD5: 2036abdf77fef15bd1834e9e4f9a311a
Transpilation MD5: c4e662864d7c8cd597889742677fe64c
Author: stefan
Category: javax / html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-20 18:04:52
Source code size: 4385 bytes / 153 lines
Pitched / IR pitched: No / No
Views / Downloads: 508 / 1094
Version history: 59 change(s)
Referenced in: [show references]