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

Support of GET-forms #480

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
87 changes: 87 additions & 0 deletions examples/form/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<!DOCTYPE html>
<html>
<head>
<title>Basic</title>
<script src="/page.js"></script>
<base href="/form/" >
</head>
<body>
<h1>GET form</h1>
<p></p>
<form class="form" method="get" action="/">
<p>
<label>Text (should):</label>
<input name="text">
</p>
<p>
<label>Textarea (should):</label>
<textarea name="textarea"></textarea>
</p>
<p>
<label>Radio (should selected):</label>
<input type="radio" name="radio" value="1"> 1
<input type="radio" name="radio" value="2"> 2
</p>
<p>
<label>Checkbox (should selected):</label>
<input name="checkbox1" type="checkbox"> checkbox1
<input name="checkbox2" type="checkbox"> checkbox2
</p>
<p>
<label>Hidden (should but it's strange):</label>
<input name="hidden" type="hidden">
</p>
<p>
<label>File (should with path to file):</label>
<input name="file" type="file">
</p>
<p>
<label>Unnamed (shouldn't without name):</label>
<input class="unnamed">
<button type="button">find</button>
</p>
<p>
<label>Disabled (shouldn't because disabled):</label>
<input name="disabled" disabled>
</p>
<p>
<label>Submit Buttons (should with its name/value):</label>
<button name="button" value="find">find</button>
<input name="button" type="submit" value="search">
</p>
<p>
<label>Regular Buttons (shouldn't):</label>
<button type="button" name="button" value="find">find</button>
<input name="button" type="button" value="search">
</p>
<p>
<label>Submit to Action Button (should with different action):</label>
<button formaction="/another" name="button" value="find">find</button>
</p>

</form>

<script>

page.base('/form');

page('/', index);

page('/:action', action);

page.start({
submit: true
});

function index(ctx) {
document.querySelector('p')
.textContent = 'searching index for ' + ctx.querystring;
}

function action(ctx) {
document.querySelector('p')
.textContent = 'another action ' + (ctx.params.action || '');
}
</script>
</body>
</html>
76 changes: 76 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
* - `click` bind to click events [true]
* - `popstate` bind to popstate [true]
* - `dispatch` perform initial dispatch [true]
* - `submit` bind to submit events (GET-forms only) [true]
*
* @param {Object} options
* @api public
Expand All @@ -199,6 +200,9 @@
if (false !== options.click && hasDocument) {
pageWindow.document.addEventListener(clickEvent, onclick, false);
}
if (false !== options.submit && hasDocument) {
pageWindow.document.addEventListener('submit', onsubmit, false);
}
hashbang = !!options.hashbang;
if(hashbang && hasWindow && !hasHistory) {
pageWindow.addEventListener('hashchange', onpopstate, false);
Expand Down Expand Up @@ -233,6 +237,7 @@
page.len = 0;
running = false;
hasDocument && pageWindow.document.removeEventListener(clickEvent, onclick, false);
hasDocument && pageWindow.document.removeEventListener('submit', onsubmit, false);
hasWindow && pageWindow.removeEventListener('popstate', onpopstate, false);
hasWindow && pageWindow.removeEventListener('hashchange', onpopstate, false);
};
Expand Down Expand Up @@ -683,6 +688,77 @@
page.show(orig);
}

/**
* Handle "submit" events.
*/

function onsubmit(e) {
if (e.defaultPrevented) return;

var form = e.target,
btnSubmit = checkButtonElement(document.activeElement) && document.activeElement;

var action = form.hasAttribute('action') && form.getAttribute('action'),
method = form.hasAttribute('method') && form.getAttribute('method'),
target = form.hasAttribute('target') && form.getAttribute('target');

if (btnSubmit) {
if (btnSubmit.hasAttribute('formaction')) action = btnSubmit.getAttribute('formaction');
if (btnSubmit.hasAttribute('formmethod')) method = btnSubmit.getAttribute('formmethod');
if (btnSubmit.hasAttribute('formtarget')) target = btnSubmit.getAttribute('formtarget');
}

// Ignore if tag has
// 1. method and not method="get"
// 2. target and not target="_self"
if (method && method.toLowerCase() !== 'get') return;
if (target && target.toLowerCase() !== '_self') return;

var a = document.createElement('a');
a.href = action;

var pathname = a.pathname,
hash = a.hash || '',
search = [];

var elements = form.elements,
len = elements.length;

for (var i = 0; i < len; i++) {
if ( ! elements[i].name || elements[i].disabled) continue;
if (['checkbox', 'radio'].indexOf(elements[i].type) >= 0 && !elements[i].checked) {
continue;
}
if (checkButtonElement(elements[i]) && elements[i] !== btnSubmit) {
continue;
}
search.push(elements[i].name + '=' + elements[i].value);
}

var path = (pathname + '?' + search.join('&') + hash);
path = path[0] !== '/' ? '/' + path : path;

// strip leading "/[drive letter]:" on NW.js on Windows
if (hasProcess && path.match(/^\/[a-zA-Z]:\//)) {
path = path.replace(/^\/[a-zA-Z]:\//, '/');
}

var pageBase = getBase();

if (path.indexOf(pageBase) === 0) {
path = path.substr(base.length);
}

if (hashbang) path = path.replace('#!', '');

e.preventDefault();
page.show(path);
}

function checkButtonElement(el) {
return (el.type && ['button', 'submit', 'image'].indexOf(el.type) >= 0) || el.tagName === 'button';
}

/**
* Event button.
*/
Expand Down
76 changes: 76 additions & 0 deletions page.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
* - `click` bind to click events [true]
* - `popstate` bind to popstate [true]
* - `dispatch` perform initial dispatch [true]
* - `submit` bind to submit events (GET-forms only) [true]
*
* @param {Object} options
* @api public
Expand All @@ -599,6 +600,9 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
if (false !== options.click && hasDocument) {
pageWindow.document.addEventListener(clickEvent, onclick, false);
}
if (false !== options.submit && hasDocument) {
pageWindow.document.addEventListener('submit', onsubmit, false);
}
hashbang = !!options.hashbang;
if(hashbang && hasWindow && !hasHistory) {
pageWindow.addEventListener('hashchange', onpopstate, false);
Expand Down Expand Up @@ -633,6 +637,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
page.len = 0;
running = false;
hasDocument && pageWindow.document.removeEventListener(clickEvent, onclick, false);
hasDocument && pageWindow.document.removeEventListener('submit', onsubmit, false);
hasWindow && pageWindow.removeEventListener('popstate', onpopstate, false);
hasWindow && pageWindow.removeEventListener('hashchange', onpopstate, false);
};
Expand Down Expand Up @@ -1083,6 +1088,77 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
page.show(orig);
}

/**
* Handle "submit" events.
*/

function onsubmit(e) {
if (e.defaultPrevented) return;

var form = e.target,
btnSubmit = checkButtonElement(document.activeElement) && document.activeElement;

var action = form.hasAttribute('action') && form.getAttribute('action'),
method = form.hasAttribute('method') && form.getAttribute('method'),
target = form.hasAttribute('target') && form.getAttribute('target');

if (btnSubmit) {
if (btnSubmit.hasAttribute('formaction')) action = btnSubmit.getAttribute('formaction');
if (btnSubmit.hasAttribute('formmethod')) method = btnSubmit.getAttribute('formmethod');
if (btnSubmit.hasAttribute('formtarget')) target = btnSubmit.getAttribute('formtarget');
}

// Ignore if tag has
// 1. method and not method="get"
// 2. target and not target="_self"
if (method && method.toLowerCase() !== 'get') return;
if (target && target.toLowerCase() !== '_self') return;

var a = document.createElement('a');
a.href = action;

var pathname = a.pathname,
hash = a.hash || '',
search = [];

var elements = form.elements,
len = elements.length;

for (var i = 0; i < len; i++) {
if ( ! elements[i].name || elements[i].disabled) continue;
if (['checkbox', 'radio'].indexOf(elements[i].type) >= 0 && !elements[i].checked) {
continue;
}
if (checkButtonElement(elements[i]) && elements[i] !== btnSubmit) {
continue;
}
search.push(elements[i].name + '=' + elements[i].value);
}

var path = (pathname + '?' + search.join('&') + hash);
path = path[0] !== '/' ? '/' + path : path;

// strip leading "/[drive letter]:" on NW.js on Windows
if (hasProcess && path.match(/^\/[a-zA-Z]:\//)) {
path = path.replace(/^\/[a-zA-Z]:\//, '/');
}

var pageBase = getBase();

if (path.indexOf(pageBase) === 0) {
path = path.substr(base.length);
}

if (hashbang) path = path.replace('#!', '');

e.preventDefault();
page.show(path);
}

function checkButtonElement(el) {
return (el.type && ['button', 'submit', 'image'].indexOf(el.type) >= 0) || el.tagName === 'button';
}

/**
* Event button.
*/
Expand Down