-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9cb1062
commit b919b00
Showing
6 changed files
with
162 additions
and
77 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import $ from 'cash-dom'; | ||
import qs from 'qs'; | ||
|
||
/** | ||
* Attaches event listeners | ||
*/ | ||
export function initAjax() { | ||
initAjaxLinks(); | ||
} | ||
|
||
/** | ||
* Event handler for form submissions using fetch. It gets the response and does | ||
* a body swap. | ||
* | ||
* @param {Event} event | ||
*/ | ||
export async function ajaxFormSubmit(event) { | ||
event.preventDefault(); | ||
const formEl = event.target; | ||
|
||
// get the data from the form | ||
let data = {}; | ||
for (const pair of new FormData(formEl)) { | ||
data[pair[0]] = pair[1]; | ||
} | ||
|
||
// convert to x-www-form-urlencoded | ||
data = qs.stringify(data); | ||
// get the url from the form's action attribute | ||
const url = $(formEl).attr('action'); | ||
|
||
// set request headers | ||
const myHeaders = new Headers(); | ||
myHeaders.append('Content-Type', 'application/x-www-form-urlencoded'); | ||
|
||
// set config object for fetch | ||
const config = { | ||
method: 'POST', | ||
headers: myHeaders, | ||
body: data | ||
}; | ||
|
||
// do a body swap | ||
await bodySwapWithUrl(url, config); | ||
// re-bind the link events | ||
initClickEvent() | ||
} | ||
|
||
/** | ||
* Does an initial binding for ajax links. Adds an event listener to handle the | ||
* back button. | ||
*/ | ||
function initAjaxLinks() { | ||
initClickEvent(); | ||
|
||
$(window).on('popstate', async (event) => { | ||
const url = event.target.location; | ||
await bodySwapWithUrl(url); | ||
// re-bind ajax links | ||
initClickEvent(); | ||
}); | ||
} | ||
|
||
/** | ||
* Attaches event listeners for all links with the `data-ajax-link` attribute. | ||
*/ | ||
function initClickEvent() { | ||
// handle link clicks | ||
const $links = $('[data-ajax-link]').one('click', clickHandler); | ||
} | ||
|
||
/** | ||
* Click handler for ajax-enabled links. Does a fetch on the link's `href` URL | ||
* and does a body swap with the response. | ||
* | ||
* @param {Event} event | ||
*/ | ||
async function clickHandler(event) { | ||
event.preventDefault(); | ||
const url = $(event.target).attr('href'); | ||
await bodySwapWithUrl(url); | ||
|
||
// push history | ||
window.history.pushState({}, $('title').text(), url); | ||
|
||
// re-bind the click handler for ajax links | ||
initClickEvent(); | ||
} | ||
|
||
/** | ||
* Uses fetch to make a request and swaps the body with the response text. | ||
* | ||
* @param {string} url | ||
* @param {Object} config | ||
*/ | ||
async function bodySwapWithUrl(url, config = undefined) { | ||
try { | ||
// request for the url | ||
const resp = await fetch(url, config); | ||
// get text of response | ||
const text = await resp.text(); | ||
// body swap | ||
$('html').html(text); | ||
|
||
// emit an event to signal body swap complete | ||
$(window).trigger('postbodyswap'); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters