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

keep focus in dialog box #33

Merged
merged 1 commit into from
Dec 2, 2012
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
26 changes: 21 additions & 5 deletions lib/alertify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @license MIT <http://opensource.org/licenses/mit-license.php>
* @link http://www.github.com/fabien-d
* @module Alertify
* @version 0.2.1
* @version 0.2.2
*/

/*global define*/
Expand Down Expand Up @@ -36,7 +36,7 @@
};

delay = 5000;
keys = { ENTER: 13, ESC: 27 };
keys = { ENTER: 13, ESC: 27, SPACE: 32 };
labels = { ok: "OK", cancel: "Cancel" };
queue = [];
isopen = false;
Expand All @@ -60,13 +60,15 @@
* @return {undefined}
*/
addListeners = function (fn) {
var btnOK = $("aOK") || undefined,
var btnReset = $("aResetFocus"),
btnOK = $("aOK") || undefined,
btnCancel = $("aCancel") || undefined,
input = $("aText") || undefined,
hasOK = (typeof btnOK !== "undefined"),
hasCancel = (typeof btnCancel !== "undefined"),
hasInput = (typeof input !== "undefined"),
val = "",
ok, cancel, common, key;
ok, cancel, common, key, reset;

// ok event handler
ok = function (event) {
Expand All @@ -92,9 +94,22 @@
// keyup handler
key = function (event) {
var keyCode = event.keyCode;
if (keyCode === keys.SPACE && !hasInput) ok(event);
if (keyCode === keys.ESC && hasCancel) cancel(event);
};

// reset focus to first item in the dialog
reset = function (event) {
if (hasInput) input.focus();
else if (hasCancel) btnCancel.focus();
else btnOK.focus();
};

// handle reset focus link
// this ensures that the keyboard focus does not
// ever leave the dialog box until an action has
// been taken
bind(btnReset, "focus", reset);
// handle OK click
if (hasOK) bind(btnOK, "click", ok);
// handle Cancel click
Expand Down Expand Up @@ -149,7 +164,8 @@
html += "</article>";

if (type === "prompt") html += "</form>";


html += "<a id=\"aResetFocus\" class=\"alertify-resetFocus\" href=\"#\">Reset Focus</a>";
html += "</div>";

switch (type) {
Expand Down
4 changes: 2 additions & 2 deletions lib/alertify.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "Alertify",
"description" : "An unobtrusive customizable JavaScript notification system",
"version" : "0.2.1",
"version" : "0.2.2",
"homepage" : "http://www.github.com/fabien-d",
"author" : {
"name" : "Fabien Doiron",
Expand Down
24 changes: 20 additions & 4 deletions src/alertify.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
};

delay = 5000;
keys = { ENTER: 13, ESC: 27 };
keys = { ENTER: 13, ESC: 27, SPACE: 32 };
labels = { ok: "OK", cancel: "Cancel" };
queue = [];
isopen = false;
Expand All @@ -48,13 +48,15 @@
* @return {undefined}
*/
addListeners = function (fn) {
var btnOK = $("aOK") || undefined,
var btnReset = $("aResetFocus"),
btnOK = $("aOK") || undefined,
btnCancel = $("aCancel") || undefined,
input = $("aText") || undefined,
hasOK = (typeof btnOK !== "undefined"),
hasCancel = (typeof btnCancel !== "undefined"),
hasInput = (typeof input !== "undefined"),
val = "",
ok, cancel, common, key;
ok, cancel, common, key, reset;

// ok event handler
ok = function (event) {
Expand All @@ -80,9 +82,22 @@
// keyup handler
key = function (event) {
var keyCode = event.keyCode;
if (keyCode === keys.SPACE && !hasInput) ok(event);
if (keyCode === keys.ESC && hasCancel) cancel(event);
};

// reset focus to first item in the dialog
reset = function (event) {
if (hasInput) input.focus();
else if (hasCancel) btnCancel.focus();
else btnOK.focus();
};

// handle reset focus link
// this ensures that the keyboard focus does not
// ever leave the dialog box until an action has
// been taken
bind(btnReset, "focus", reset);
// handle OK click
if (hasOK) bind(btnOK, "click", ok);
// handle Cancel click
Expand Down Expand Up @@ -137,7 +152,8 @@
html += "</article>";

if (type === "prompt") html += "</form>";


html += "<a id=\"aResetFocus\" class=\"alertify-resetFocus\" href=\"#\">Reset Focus</a>";
html += "</div>";

switch (type) {
Expand Down
10 changes: 10 additions & 0 deletions themes/alertify.core.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@
.alertify-dialog {
padding: 25px;
}
.alertify-resetFocus {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
.alertify-inner {
text-align: center;
}
Expand Down