mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-10-02 07:21:05 +00:00
implement some basics
This commit is contained in:
parent
18a0b92d08
commit
a5d7e8febd
@ -1 +1 @@
|
|||||||
FOO
|
<i>FOO!!!</i>
|
@ -9,7 +9,21 @@
|
|||||||
|
|
||||||
<h1>Scratch Page</h1>
|
<h1>Scratch Page</h1>
|
||||||
|
|
||||||
<button hx-get="foo.html">Trigger</button>
|
<div>
|
||||||
|
<button hx-get="foo.html">Trigger</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<button hx-get="foo.html" hx-target="#bar">Trigger 2</button><span id="bar">Bar</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<button hx-get="foo.html" hx-target="#bar2" hx-swap-style="append">Trigger 3</button><span id="bar2">Bar</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<button hx-get="foo.html" hx-swap-style="outerHTML">Trigger 4</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
<form hx-post="/foo">
|
<form hx-post="/foo">
|
||||||
<input name="bar" type="text"/>
|
<input name="bar" type="text"/>
|
||||||
|
52
src/htmx.js
52
src/htmx.js
@ -1,17 +1,61 @@
|
|||||||
var HTMx = HTMx || (function()
|
var HTMx = HTMx || (function()
|
||||||
{
|
{
|
||||||
|
function getClosestAttributeValue(elt, attributeName)
|
||||||
|
{
|
||||||
|
let attribute = elt.getAttribute(attributeName);
|
||||||
|
if(attribute)
|
||||||
|
{
|
||||||
|
return attribute;
|
||||||
|
}
|
||||||
|
else if (elt.parentElement)
|
||||||
|
{
|
||||||
|
return getClosestAttributeValue(elt.parentElement, attributeName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTarget(elt) {
|
||||||
|
let targetVal = getClosestAttributeValue(elt, "hx-target");
|
||||||
|
if (targetVal) {
|
||||||
|
return document.querySelector(targetVal);
|
||||||
|
} else {
|
||||||
|
return elt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeNode(resp) {
|
||||||
|
let range = document.createRange();
|
||||||
|
return range.createContextualFragment(resp);
|
||||||
|
}
|
||||||
|
|
||||||
|
function swapResponse(elt, resp) {
|
||||||
|
let target = getTarget(elt);
|
||||||
|
let swapStyle = getClosestAttributeValue(elt, "hx-swap-style");
|
||||||
|
if (swapStyle === "outerHTML") {
|
||||||
|
target.outerHTML = resp;
|
||||||
|
} else if (swapStyle === "append") {
|
||||||
|
target.appendChild(makeNode(resp))
|
||||||
|
} else {
|
||||||
|
target.innerHTML = resp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// core ajax request
|
// core ajax request
|
||||||
function issueAjaxRequest(elt, url)
|
function issueAjaxRequest(elt, url)
|
||||||
{
|
{
|
||||||
var request = new XMLHttpRequest();
|
let request = new XMLHttpRequest();
|
||||||
|
// TODO - support more request types POST, PUT, DELETE, etc.
|
||||||
request.open('GET', url, true);
|
request.open('GET', url, true);
|
||||||
request.onload = function()
|
request.onload = function()
|
||||||
{
|
{
|
||||||
if (this.status >= 200 && this.status < 400)
|
if (this.status >= 200 && this.status < 400)
|
||||||
{
|
{
|
||||||
// Success!
|
// Success!
|
||||||
var resp = this.response;
|
let resp = this.response;
|
||||||
elt.innerHTML = resp;
|
swapResponse(elt, resp);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -38,7 +82,7 @@ var HTMx = HTMx || (function()
|
|||||||
}
|
}
|
||||||
|
|
||||||
function ready(fn) {
|
function ready(fn) {
|
||||||
if (document.readyState != 'loading'){
|
if (document.readyState !== 'loading'){
|
||||||
fn();
|
fn();
|
||||||
} else {
|
} else {
|
||||||
document.addEventListener('DOMContentLoaded', fn);
|
document.addEventListener('DOMContentLoaded', fn);
|
||||||
|
60
www/index.html
Normal file
60
www/index.html
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<title>HTMx - Teaching HTML new tricks</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<section>
|
||||||
|
<h1></> HTMx</h1>
|
||||||
|
<p>HTMx is a set of extensions to HTML that allows you to build AJAX powered applications using HTML attributes.
|
||||||
|
It fills a gap in functionality that has been inexplicably left out of
|
||||||
|
standard HTML, and allows you to use the full range of HTTP and HTML expressiveness originally
|
||||||
|
envisioned for the web.</p>
|
||||||
|
<p>Here is a simple example of HTMx in action:</p>
|
||||||
|
<pre>
|
||||||
|
<button hx-get="/example" hx-target="#myDiv">Click Me</button>
|
||||||
|
</pre>
|
||||||
|
<p>This example issues an AJAX request to <code>/example</code> when a user clicks on it, and swaps the response
|
||||||
|
HTML into the element with the id <code>myDiv</code></p>
|
||||||
|
<p>HTMx was inspired by <a href="http://intercoolerjs.org">intercooler.js</a>, and aims to be a minimalist &
|
||||||
|
dependency free implementation of the same concept.</p>
|
||||||
|
</section>
|
||||||
|
<section>
|
||||||
|
<h1></> Attributes</h1>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Attribute</th>
|
||||||
|
<th>Description</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>hx-get</td>
|
||||||
|
<td>Issues an HTTP GET to the given URL</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>hx-target</td>
|
||||||
|
<td>Specifies the target element that should be swapped</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>hx-swap-style</td>
|
||||||
|
<td>Specifies how target element should be swapped: innerHTML, outerHTML, append</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</section><section>
|
||||||
|
<h1></> Events</h1>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Event</th>
|
||||||
|
<th>Description</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user