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

82
LINES

< > BotCompany Repo | #1032366 // HPopDownButton - generalized hPopDownButton

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

Libraryless. Click here for Pure Java version (4939L/28K).

// entries are HTML (e.g. links)
// requires JQuery for click-outside-to-close trick
// Note: Make sure containing elements have overflow: visible,
// otherwise pop-up can get cut off
sclass HPopDownButton {
  S labelHTML;
  S buttonHTML = htmlEncode2(unicode_smallDownPointingTriangle());
  new LS entries;
  S id = "dropdown-" + aGlobalID(), outerID = "outer-" + id;
  S style = "position: relative; display: inline-block; cursor: context-menu";
  S menuStyle = "";
  
  *() {}
  *(S *labelHTML, LS entries) { addAll(this.entries, entries); }
  
  S onclick() {
    ret replaceDollarVars([[
      var x = document.getElementById($id);
      x.style.display = window.getComputedStyle(x).display === "none" ? "block" : "none";
      console.log("popup " + id + " display: " + x.style.display);
    ]], id := jsQuote(id));
  }
  
  S html() {
    ret hstyle(replaceDollarVars([[
      #$id ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        width: 200px;
        background-color: #f1f1f1;
      }
      
      #$id {
        position: absolute;
        display:none;
        background-color: #f9f9f9;
        width:auto;
        height:200px;
        overflow: auto;
       
        min-width: 160px;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        
        z-index: 99;
      }
      
      #$id li a {
        text-decoration: none;
        color: #000;
      }
  
      #$id li {    
        display: block;
        border-bottom: 1px solid #555;
        padding: 8px 16px;
        color: #000;
      }
      
      #$id li:hover, #$id li:hover > a {
        background-color: #3399ff;
        color: white;
      }
    ]], +id))
    + hdiv(
          buttonHTMLWithOnClick()
        + hdiv(ul(entries), +id, style := menuStyle),
      +style, id := outerID)
      + hscript(replaceDollarVars([[
        if (typeof $ !== 'undefined')
          $('body').click(function(e) {
            if (!$(e.target).closest('#$outerID').length) {
              $("#$id").hide();
            }
          });
      ]], +id, +outerID));
  }
  
  swappable S buttonHTMLWithOnClick() {
    ret span(joinNemptiesWithSpace(labelHTML, buttonHTML), onclick := onclick());
  }
}

Author comment

Began life as a copy of #1030222

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1032366
Snippet name: HPopDownButton - generalized hPopDownButton
Eternal ID of this version: #1032366/8
Text MD5: f47c5ac0a2e73ff3cbc37a2c2105cd44
Transpilation MD5: 787c3d7ce03a92c538e05e1b549e2187
Author: stefan
Category: javax / html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-25 00:03:56
Source code size: 2286 bytes / 82 lines
Pitched / IR pitched: No / No
Views / Downloads: 218 / 373
Version history: 7 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)