2013-12-04 17:24:04 +00:00
|
|
|
define([
|
|
|
|
"editor",
|
2014-01-22 16:37:28 +00:00
|
|
|
"util/template!templates/dialog.html",
|
2013-12-04 17:24:04 +00:00
|
|
|
"util/dom2"
|
2014-01-22 16:37:28 +00:00
|
|
|
], function(editor, inflate) {
|
2014-02-27 06:34:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
You can call dialog() to present a modal, since alert() isn't allowed.
|
|
|
|
|
|
|
|
Currently, your callback will be passed the value set for the button pressed. Form support is coming.
|
|
|
|
*/
|
2013-09-01 18:09:09 +00:00
|
|
|
|
|
|
|
return function(text, buttons, callback) {
|
2013-09-14 01:38:11 +00:00
|
|
|
if (typeof buttons == "function" || typeof buttons == "undefined") {
|
2013-09-01 18:09:09 +00:00
|
|
|
callback = buttons;
|
2013-09-13 23:30:05 +00:00
|
|
|
buttons = ["ok"];
|
2013-09-01 18:09:09 +00:00
|
|
|
}
|
2013-09-02 00:41:52 +00:00
|
|
|
|
2014-01-22 16:37:28 +00:00
|
|
|
buttons = buttons.map(function(options) {
|
|
|
|
if (typeof options == "string") {
|
|
|
|
return {
|
|
|
|
label: options,
|
|
|
|
value: options
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
});
|
2013-09-02 00:41:52 +00:00
|
|
|
|
2014-01-22 16:37:28 +00:00
|
|
|
var modal = inflate.get("templates/dialog.html", {
|
|
|
|
text: text,
|
|
|
|
buttons: buttons
|
|
|
|
});
|
2013-09-02 00:41:52 +00:00
|
|
|
|
|
|
|
document.body.append(modal);
|
2014-01-22 16:37:28 +00:00
|
|
|
|
|
|
|
var defaultButton = modal.find("button.default");
|
|
|
|
if (!defaultButton) defaultButton = modal.find("button");
|
|
|
|
defaultButton.focus();
|
2013-09-01 18:09:09 +00:00
|
|
|
|
2013-11-09 00:57:35 +00:00
|
|
|
var onKeyDown = function(e) {
|
2013-09-04 23:36:33 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
e.stopImmediatePropagation();
|
2013-11-19 16:19:18 +00:00
|
|
|
if (e.ctrlKey || e.metaKey || e.shiftKey) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2013-09-06 16:33:05 +00:00
|
|
|
//check escape
|
|
|
|
if (e.keyCode == 27) {
|
|
|
|
modal.remove();
|
2013-09-24 14:57:22 +00:00
|
|
|
editor.focus();
|
2013-09-06 16:33:05 +00:00
|
|
|
if (callback) callback();
|
|
|
|
}
|
2013-09-04 23:36:33 +00:00
|
|
|
};
|
2013-11-09 00:57:35 +00:00
|
|
|
|
|
|
|
var onKeyPress = function(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.stopImmediatePropagation();
|
2013-11-19 16:36:37 +00:00
|
|
|
//allow Enter to trigger clicks
|
|
|
|
if (e.keyCode != 13) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2013-11-09 00:57:35 +00:00
|
|
|
buttons.forEach(function(options) {
|
|
|
|
if (typeof options == "string") return;
|
|
|
|
if (options.shortcut && options.shortcut == String.fromCharCode(e.charCode)) {
|
|
|
|
modal.remove();
|
|
|
|
editor.focus();
|
|
|
|
if (callback) callback(options.value);
|
|
|
|
}
|
2014-02-27 06:34:02 +00:00
|
|
|
});
|
2013-11-09 00:57:35 +00:00
|
|
|
}
|
2013-09-04 23:36:33 +00:00
|
|
|
|
2014-01-22 16:37:28 +00:00
|
|
|
var clickButton = function(e) {
|
|
|
|
var target = e.target;
|
|
|
|
if (!target.matches("button")) return;
|
2013-09-01 18:09:09 +00:00
|
|
|
modal.remove();
|
2013-11-19 16:19:18 +00:00
|
|
|
try {
|
2014-01-22 16:37:28 +00:00
|
|
|
var value = JSON.parse(target.value);
|
2013-11-19 16:19:18 +00:00
|
|
|
if (callback) callback(value);
|
|
|
|
} catch (err) {
|
|
|
|
//do nothing
|
|
|
|
}
|
2013-09-21 00:39:04 +00:00
|
|
|
editor.focus();
|
2013-09-01 19:23:13 +00:00
|
|
|
};
|
2014-01-22 16:37:28 +00:00
|
|
|
|
|
|
|
modal.onkeydown = onKeyDown;
|
|
|
|
modal.onkeypress = onKeyPress;
|
|
|
|
modal.onclick = clickButton;
|
2013-09-04 23:36:33 +00:00
|
|
|
|
2014-02-27 06:34:02 +00:00
|
|
|
};
|
2013-09-01 18:09:09 +00:00
|
|
|
|
|
|
|
});
|