Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds onUnmounting Hook #18

Merged
merged 2 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions example/pages/a.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
<html>
<head>
<title>Page A</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<link rel="stylesheet" type="text/css" href="/style.css" />
</head>
<body>
<div id="indicator">Loading...</div>
<!-- PAGE CONTENT START -->
<div id='page'>
<div id="page">
<nav>
<a href="/">Home</a>
<a href="/pages/a">Page A</a>
Expand All @@ -18,9 +21,58 @@ <h1>Page A</h1>
<p>
Page A content.
</p>
<form>
<div>
<label>
Username:
<input type="text" name="username" />
</label>
</div>
<div>
<label>
Age:
<input type="number" name="age" />
</label>
</div>
<div>
<label>
Phone:
<input type="tel" name="telephone" />
</label>
</div>
<div>
<label>
Email:
<input type="email" name="email" />
</label>
</div>
<div>
<label>
Save
<input type="checkbox" name="save" />
</label>
</div>
<div>
Say something
<textarea name="something"></textarea>
</div>
<div>
<fieldset>
<label>
Yes
<input type="radio" name="cool" value="yes" />
</label>
<label>
No
<input type="radio" name="cool" value="no" />
</label>
</fieldset>
</div>
</form>
<script></script>
</div>
<!-- PAGE CONTENT END -->
<script src='/pill.js'></script>
<script src='/site.js'></script>
<script src="/pill.js"></script>
<script src="/site.js"></script>
</body>
</html>
39 changes: 38 additions & 1 deletion example/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,56 @@ const indicator = document.getElementById('indicator')
let timeout = 0
pill('#page', {
onLoading() {

if (timeout) {
clearTimeout(timeout)
timeout = 0
}

indicator.style.display = 'block'
},
onReady() {
onUnmounting(page, url, element){
PreserveFormPlugin(element);
},
onReady(page, element) {
timeout = setTimeout(() => {
indicator.style.display = 'none'
}, 1000)
PopulateFormPlugin(element);
},
onMounting(){
console.log('updating content')
}
})

const PopulateFormPlugin = element =>{
const key = location.pathname;
const fields = Array.from(element.querySelectorAll('input, textarea, select'));
if(fields.length > 0){
const obj = JSON.parse(localStorage.getItem(key));
obj.forEach(field=>{
const input = document.querySelector('[name='+field.fieldName+']');
if(input.type == 'checkbox' || input.type=='radio'){
input.checked = field.value
} else if (input.nodeName == 'TEXTAREA'){
input.textContent = field.value
} else {
input.value = field.value
}
});
}
}

const PreserveFormPlugin = (element) =>{
const key = location.pathname;
const fields = Array.from(element.querySelectorAll('input, textarea, select'));
if(fields.length > 0){
const values = fields.map(val=>{
return {
fieldName: val.name,
value: val.type == 'checkbox' || val.type == 'radio'? val.checked : val.value
}
});
localStorage.setItem(key, JSON.stringify(values));
}
}
8 changes: 5 additions & 3 deletions src/pill.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default function pill(selector, options) {
options = options || {}
var onReady = options.onReady || noop
var onLoading = options.onLoading || noop
var onUnmounting = options.onUnmounting || noop
var onMounting = options.onMounting || noop
var onError = options.onError || console.error.bind(console)
var keyFromUrl = options.keyFromUrl || keyFromUrlDefault
Expand All @@ -99,10 +100,11 @@ export default function pill(selector, options) {
var cache = {}
cache[keyFromUrl(currentUrl)] = currentPage
function render (url, page, push) {
onUnmounting(page, url, element)
updateState(null, url, page.title, push)
onMounting(page, url)
onMounting(page, url, element)
setContent(element, page)
onReady(page)
onReady(page, element)
if (push && url.hash.length > 1) {
scrollToAnchor(url.hash.slice(1))
}
Expand Down Expand Up @@ -208,4 +210,4 @@ export default function pill(selector, options) {
document.body.addEventListener('click', onClick)
window.addEventListener('popstate', onPopState)
window.addEventListener('scroll', onScroll)
}
}