listen for keyup events while alertify prompts are active

pull/1/head
ansuz 9 years ago
parent a2a8570453
commit 589b0087a1

@ -197,32 +197,87 @@ define([
})); }));
}; };
var findCancelButton = common.findCancelButton = function () {
return $('button.cancel');
};
var findOKButton = common.findOKButton = function () {
return $('button.ok');
};
var listenForKeys = function (yes, no) {
var handler = function (e) {
switch (e.which) {
case 27: // cancel
if (typeof(no) === 'function') { no(e); }
no();
break;
case 13: // enter
if (typeof(yes) === 'function') { yes(e); }
break;
}
};
$(window).keyup(handler);
return handler;
};
var stopListening = function (handler) {
$(window).off('keyup', handler);
};
common.alert = function (msg, cb) { common.alert = function (msg, cb) {
Alertify.alert(msg, cb); cb = cb || function () {};
var keyHandler = listenForKeys(function (e) { // yes
findOKButton().click();
});
Alertify.alert(msg, function (ev) {
cb(ev);
stopListening(keyHandler);
});
}; };
common.prompt = function (msg, def, cb, opt) { common.prompt = function (msg, def, cb, opt) {
opt = opt || {}; opt = opt || {};
cb = cb || function () {};
var keyHandler = listenForKeys(function (e) { // yes
findOKButton().click();
}, function (e) { // no
findCancelButton().click();
});
Alertify Alertify
.defaultValue(def || '') .defaultValue(def || '')
.okBtn(opt.ok || 'OK') .okBtn(opt.ok || 'OK')
.cancelBtn(opt.cancel || 'Cancel') .cancelBtn(opt.cancel || 'Cancel')
.prompt(msg, function (val, ev) { .prompt(msg, function (val, ev) {
cb(val, ev); cb(val, ev);
stopListening(keyHandler);
}, function (ev) { }, function (ev) {
cb(null, ev); cb(null, ev);
stopListening(keyHandler);
}); });
}; };
common.confirm = function (msg, cb, opt) { common.confirm = function (msg, cb, opt) {
opt = opt || {}; opt = opt || {};
cb = cb || function () {};
var keyHandler = listenForKeys(function (e) {
findOKButton().click();
}, function (e) {
findCancelButton().click();
});
Alertify Alertify
.okBtn(opt.ok || 'OK') .okBtn(opt.ok || 'OK')
.cancelBtn(opt.cancel || 'Cancel') .cancelBtn(opt.cancel || 'Cancel')
.confirm(msg, function () { .confirm(msg, function () {
cb(true); cb(true);
stopListening(keyHandler);
}, function () { }, function () {
cb(false); cb(false);
stopListening(keyHandler);
}); });
}; };

Loading…
Cancel
Save