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).

1  
sclass HInputField {
2  
  S id = aGlobalID(), name, text;
3  
  bool useWebSocket = true, debug;
4  
  new MapSO params; // for <input> tag
5  
  
6  
  !include #1028121 // setField + change()
7  
  bool requiresWebSocket() { ret useWebSocket; }
8  
  bool requiresJQuery() { true; }
9  
  
10  
  S html() {
11  
    new LS out;
12  
    out.add(tag input("", paramsPlus(params,
13  
      type := "text",
14  
      +name,
15  
      value := text,
16  
      +id)));
17  
    if (useWebSocket)
18  
      out.add(hjs_escapedDollarVars([[
19  
        (function() {
20  
          if (typeof wsSend === 'undefined') return;
21  
          var field = $("#" + $id);
22  
          if (field.length == 0) return;
23  
          var lastValue = field.val();
24  
          field.on('change keydown paste input', function() {
25  
            var value = field.val();
26  
            if (lastValue == value) return;
27  
            lastValue = value;
28  
            wsSend(JSON.stringify({inputFieldText: {id: $id, text: value}}));
29  
          });
30  
        })();
31  
      ]], +id));
32  
    ret lines(out);
33  
  }
34  
  
35  
  void onWebSocketJSON(Map map) {
36  
    Map map2 = optCast Map(map.get("inputFieldText")), ret if null;
37  
    S id = cast map2.get("id");
38  
    if (!eq(id, this.id)) ret;
39  
    S text = cast map2.get("text");
40  
    if (text != null) {
41  
      if (debug) print("Setting HInputField text: " + quote(text));
42  
      setField(+text);
43  
    }
44  
  }
45  
}

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: 193 / 358
Version history: 11 change(s)
Referenced in: [show references]