#!/usr/bin/w3tclsh
# This script demonstrates how to receive bulk HTML content
# (such as a complete <table>) and insert it in the middle
# of the DOM using XMLHttpRequest
#
package require w3
proc common-header {} {
  w3-trim {
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link href="%url([w3-param SCRIPT_NAME]/style.css)" rel="stylesheet">
    <body>
  }
}
proc common-footer {} {
  w3-trim {
    </body>
    </html>
  }
}

proc w3-default {} {
  common-header
  w3-trim {
    <p>This application demonstrates how to use XMLHttpResult to obtain
    bulk HTML text (such as a large &lt;table&gt;) and then insert that
    text in the middle of the DOM.</p>

    <div id="insertionPoint"></div>

    <p><form id="theForm">
    Click the button 
    <input type="submit" id="theButton" value="Click Here">
    to cause content to be inserted above this paragraph and below
    the initial paragraph
    </form></p>
    <script src='%url([w3-param SCRIPT_NAME]/script.js)'></script>

    <p><a href='%html([w3-param SCRIPT_NAME])/self'>Show the W3 script
    that generates this page</a></p>
  }
  common-footer
}

# This is the javascript that takes control of the form and causes form
# submissions to fetch and insert HTML text.
#
proc w3-page-script.js {} {
  w3-mimetype text/javascript
  w3-cache-control max-age=3600
  w3-trim {
    document.getElementById("theForm").onsubmit = function(){
      var xhttp = new XMLHttpRequest();
      xhttp.open("GET", "%string([w3-param BASE_URL])/gettable", true);
      xhttp.onreadystatechange = function(){
        if(this.readyState!=4)return
        document.getElementById("insertionPoint").innerHTML = this.responseText;
        document.getElementById("insCancel").onclick = function(){
          document.getElementById("insertionPoint").innerHTML = null
        }
      }
      xhttp.send();
      return false
    }
  }
}

# This counter increments on each /gettable call.
set counter 0

# The /gettable page returns the content of a table to be inserted
# in the original page.
#
proc w3-page-gettable {} {
  global counter
  incr counter
  w3-trim {
    <table border="1">
    <tr><th>Column 1<th>Column 2<th>Column 3
    <tr><td>1.1<td>1.2<td>1.3
    <tr><td>2.1<td>2.2<td>2.3
    <tr><td>%html($counter)<td><td><button id="insCancel">Cancel</button>
    </table>
  }
}

# The /self page that shows the text of this script.
#
proc w3-page-self {} {
  w3-cache-control max-age=3600
  common-header
  set fd [open [w3-param SCRIPT_FILENAME] rb]
  set script [read $fd]
  close $fd
  w3-trim {
    <h1>W3 Script That Shows A Copy Of Itself</h1>
    <pre>%html($script)</pre>
  }
  common-footer
}
proc w3-page-style.css {} {
  w3-mimetype text/css
  w3-cache-control max-age=3600
  w3-trim {
    pre {
       border: 1px solid black;
       padding: 1ex;
    }
  }
}

w3-start $argv
