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

45
LINES

< > BotCompany Repo | #1032598 // HInputField - HTML input field with WebSocket support [dev.]

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

Libraryless. Click here for Pure Java version (6015L/34K).

sclass HInputField {
  S id = aGlobalID(), name, text;
  bool useWebSocket = true, debug;
  new MapSO params; // for <input> tag
  
  !include #1028121 // setField + change()
  bool requiresWebSocket() { ret useWebSocket; }
  bool requiresJQuery() { true; }
  
  S html() {
    new LS out;
    out.add(tag input("", paramsPlus(params,
      type := "text",
      +name,
      value := text,
      +id)));
    if (useWebSocket)
      out.add(hjs_escapedDollarVars([[
        (function() {
          if (typeof wsSend === 'undefined') return;
          var field = $("#" + $id);
          if (field.length == 0) return;
          var lastValue = field.val();
          field.on('change keydown paste input', function() {
            var value = field.val();
            if (lastValue == value) return;
            lastValue = value;
            wsSend(JSON.stringify({inputFieldText: {id: $id, text: value}}));
          });
        })();
      ]], +id));
    ret lines(out);
  }
  
  void onWebSocketJSON(Map map) {
    Map map2 = optCast Map(map.get("inputFieldText")), ret if null;
    S id = cast map2.get("id");
    if (!eq(id, this.id)) ret;
    S text = cast map2.get("text");
    if (text != null) {
      if (debug) print("Setting HInputField text: " + quote(text));
      setField(+text);
    }
  }
}

Author comment

Began life as a copy of #1032454

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032598
Snippet name: HInputField - HTML input field with WebSocket support [dev.]
Eternal ID of this version: #1032598/12
Text MD5: 0cbf8f61abe3c1574b7c803088064c82
Transpilation MD5: 5b4a52c3e3995092e2a7cc3611e516c3
Author: stefan
Category: javax / html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-24 19:43:58
Source code size: 1352 bytes / 45 lines
Pitched / IR pitched: No / No
Views / Downloads: 192 / 355
Version history: 11 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)