References are first class citizens in DML ( the new JQuery, at least, for me )
Tags: dml, javascript
Published: 2021-08-19 08:48:22

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mys2ya1qtbe3w093m3l9.png)

Functions are said first class citizens in Javascript. In the same manner, references can be said first class citizens in DML.

Play with the following code live here : [Demo](https://flems.io/#0=N4IgzgpgNhDGAuEAmIBcIB0ALeBbKIANOLAE4D2UBqA2gAyF0C6xAZgJYxho2gB2AQ1wQ0mHPiIhY5PolmiAPOKgACKAL4BzALwAdEEgj6AfLr4qVSiAKSnzl4fAErYWAaUjw9IAK7xWALQAHCZmFhYK8OzwMMZRMRAKAPTxsWHhCo7OgsLeAG7sEADuAA7kpPD6LjJyXvpF7EjwWNqGBbAQAQ1NWIQq7HzR7AJQAWCwIxDaAIwYdKH2EeOk7CXwKmCksN448CVgqElJEKwlApoQGIZJACIAsgAyt48A+ljkwmcXSVDsAEbPB4BXADDAAKzAJmSy1W8DsESSWGstnSCj+5CQAE9jCpUTC1jj0hZpHwwOsyZiuCptCpgETwrAwGA7hA+D4VKgVAADenhFRlMBDGScgR-MCUPwQADcvPCSHYYBK6kxHJUfyg5FgAGsZYs+WryoZSKqAEwlAAeG0ojRUhggJRKAx1sos3WaqpUJrodAtuv14TOSHlWlVAFZfS6VLh3JoBqaI3q+bAfB5ypyygNEKQ-f61Ar4GN4JSIJy+DJpZGAF4BAaGc2oaY5-1-ATazQUHx8JCcopYaIVxPcwhmemMsAPdiqnmDiyB4OaTnh81N8Jc4d8UdMgAq5BKLeNnOnuf55EFUWFKlI0AEUTyA+P0g1pFQV6QK758sVytQrBgy8jIzsJofA1oguAHB0sgQNmkZIkBzSLj6-4zmqrZau25Cdt2doOk674MpQaa9v2+EWGCPhkuwrCYgEJK1Kgiqtp0fwQPARQQKypH8jY84NkhUp8pG6LmmMbhIOQRSqnQKgACwWrJ8lIZemgtgAFNMoYMFpjAYCaABsACU+FriOepjgAEh8EBTpGyaps+J6ZtBsomRuZlMiybIAPJ3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4DwAqZ3C5W8q54JpCHCuso7EONpNU9wfR6kQD6OPdGbVBnbJUM4vUwsnY+yelGAQcYBAOh+tC6Ffs+QByDtrDQSAYD4IdAAUTvJ7EmwBvmkFgRvWUpLsBWVvoHB+WopGt1HseCGUMYacyqbufcM9MpZwMKbZUvd9Dqk1FqIVIBra2yYZSrl7gKbAq2eaWlHdMQoy+gnZluYEbP2AAjDAxFXAtxpNMY6KgABkZqNgKr-qQChVL5kgqVXkEYPhumQsHOJZM0dyZ33pTXbpBj-H+mJnqpwpALjrAAIQ0ipX8gRgidadypePB5V1lbHhcaZPkAgwCYkDiodFHirwAEdXVkhJlmXA8a+RFyvIqGQYsaQCCKHC9YXIABimEuyqgACQkugrgS6F0+QfNIOYOt7tID2yzeEHNebYAFs1mnK1mybW4ArQO6t4Ra1fUndZJtLboiXggKWr68AqmbvwiSOalwNSaFUhOht3Tp1uTRUusGZBrCIFxYcvxspAmyuNDSLxPjSBvI9U0iJ9DmjrQLawssRRFlLIKfoNaWg+IRhAHA3p6wEWHJpCSwDl18KjvMHhl9biMUK3cPAAl9cHjkF3CTFmmq+RuC7Di-FhL4BgFrleTGP6w4XPdXyDGFQ6OyAY7ucDgl7DQjILCeElgkjoixHYZIyhjCSGuRi7g6ATUAHZUAyQMyAS6hB+BCBEOgcE3BiBD3gKIMzFmJVWdEDEyQDnRCqZVP46M4a4ycnGukNDmgpK6kutp6ANy056ZACaBsMkzMsFc8IUQpKHVYDM0AA).

See for example in the  main function how we manipulate the different components like 'topbar' and 'menu'.

You can also see in the code, how 'ul' element is provided an 'add' method like a simple list.

```html
<html lang="de">
  <head>
  <meta charset="utf-8">
    <title>title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://efpage.de/DML/DML_homepage/lib/DML-min.js"></script>
  </head>
  <body> 
  <script> 
    const styles = {...}
    
    /***************** utilities *********************/
    function toggleStyle(elt, prop, state1, state2) {
      if (elt.style[prop] == state1) {
       elt.style[prop] = state2;
      }
      else  {
        elt.style[prop] = state1;
      }
    }
    
    function hide(elt) {
      elt.style.display = "none";
    }
    
    /**************** components *********************/
    function Menu () {
        let menuItems = ["Option 1", "Option 2"];
        let menu = ul();
        let li = (content, props) => make("li", props, content); 
        menuItems.map((item,index) =>  {
          let liitem = li(item, styles.cssLi);
          liitem.onclick = () => alert(liitem.innerText);
          menu.add([liitem])
        })
        menu.style = styles.cssMenu;
        let childsmenu = Array.from(menu.childNodes);
        for (let item of childsmenu) {
          item.onmouseover = () => item.style = styles.cssMenuOver;
          item.onmouseout = () => item.style = styles.cssMenuOut;
        }
      return menu; 
    }
    
    function TopBar() {
      let topbar = div("", styles.cssTopbar);
      let input = (props) => make("input", props);
      selectBase(topbar);
        let home = div("Home", styles.cssHome);
        let searchBox = input({placeholder:"search...", style:styles.cssSearchBox})
        let help = div("help");
      unselectBase();
      let menu = Menu();
      return {topbar, home, menu, searchBox} ; 
    }

    /**************** main app *************************/
    function handleAppEvents ({topbar}) {

      topbar.home.onclick = () => 
        toggleStyle(topbar.menu, "display", "block", "none");

      topbar.searchBox.onkeypress = 
        (e) => {(e.which) == 13 && searchTerm(topbar.searchBox.value)}

      document.onclick = (e) => {
        if (e.target != topbar.home) {
          hide(topbar.menu);
        }
      }        
    }

    async function request (term) {
      let response = await `Found : ${term}`
      return response;
    }

    async function searchTerm (term) {
      let response = await request(term);
      console.log(response);
    }

    function createApp () {
      let topbar = TopBar();
      div("Nothing for now....")
      let app = {topbar};
      return app;
    }

    function startEventLoop () { 
      handleAppEvents(createApp());
    }

    startEventLoop();
    
  </script>
  </body>
</html>
```