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

107
LINES

< > BotCompany Repo | #1032795 // HTMLPopDownButton_v2

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

Transpiled version (5381L) is out of date.

// 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 HTMLPopDownButton_v2 {
  Cl<S> entries = new L;
  int width = 200, height = 200;
  
  S id = "dropdown-" + aGlobalID(), outerID = "outer-" + id;
  
  *(S... entries) { this.entries = asList(entries); }
  *(Cl<S> *entries) {}
  
  void add(S entry) { main add(this.entries, entry); }
  void addAll(S... entries) { main addAll(this.entries, entries); }
  
  S js_closeOnClickAnywhere() {
    ret [[
      if (typeof $ !== 'undefined')
        $('body').click(function(e) {
          if (!$(e.target).closest('.popdowntrigger').length) {
            $(".popdown").hide();
          }
        });
    ]];
  }
  
  S css() {
    ret [[
      .popdown ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        width: ]] + width + [[px;
        background-color: #f1f1f1;
      }
      
      .popdown {
        position: absolute;
        display:none;
        background-color: #f9f9f9;
        width:auto;
        height: ]] + height + [[px;
        overflow: auto;
       
        min-width: 160px;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        
        z-index: 99;
      }
      
      .popdown li a {
        text-decoration: none;
        color: #000;
      }
  
      .popdown li {    
        display: block;
        border-bottom: 1px solid #555;
        padding: 8px 16px;
        color: #000;
      }
    ]]
    /*
      .popdown li:hover, .popdown li:hover a {
        background-color: #3399ff;
        color: white;
      }
      
      /* exclude sub menus - works but submenu loses hover highlights *//*
      .popdown li:hover ul li, .popdown li:hover ul li a {
        background-color: #f9f9f9;
        color: #000;
      }
    */
    // version 2 of the trick
    + [[
      .popdown li:hover, .popdown li:hover > a {
        background-color: #3399ff;
        color: white;
      }
    ]];
  }
  
  S headStuff() { ret hcss(css()); }
  
  S onclick() {
    ret jsEscapedDollarVars([[
      var x = document.getElementById($id);
      x.style.display = window.getComputedStyle(x).display === "none" ? "block" : "none";
      console.log("popup " + id + " display: " + x.style.display);
    ]], +id);
  }
  
  S html() {
    ret hdiv(
        span(htmlEncode2(unicode_smallDownPointingTriangle()), +onclick())
      + hdiv(ul(entries), +id, class := "popdown"),
      style := "position: relative; display: inline-block; cursor: context-menu",
      id := outerID, class := "popdowntrigger");
  }
  
  S complete() { ret hcombine(headStuff(), html()); }
  
  selfType width(int width) { this.width = width; this; }
  selfType height(int height) { this.height = height; this; }
}

Author comment

Began life as a copy of #1030975

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1032795
Snippet name: HTMLPopDownButton_v2
Eternal ID of this version: #1032795/8
Text MD5: 32ef164f6ab6a403dc5dd85e0e98629d
Author: stefan
Category: javax / html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-06 14:24:44
Source code size: 2930 bytes / 107 lines
Pitched / IR pitched: No / No
Views / Downloads: 172 / 305
Version history: 7 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)