Caret/js/dialog.js

63 lines
1.6 KiB
JavaScript
Raw Normal View History

define(["editor", "dom2"], function(editor) {
return function(text, buttons, callback) {
if (typeof buttons == "function" || typeof buttons == "undefined") {
callback = buttons;
buttons = ["ok"];
}
var modal = document.find("template#dialog").content.cloneNode(true).find(".modal-overlay");
var buttonRow = modal.find(".button-row");
var message = modal.find(".text");
message.innerHTML = text;
document.body.append(modal);
2013-09-04 23:36:33 +00:00
var onKey = function(e) {
e.stopPropagation();
e.stopImmediatePropagation();
2013-09-06 16:33:05 +00:00
//check escape
if (e.keyCode == 27) {
modal.remove();
editor.focus();
2013-09-06 16:33:05 +00:00
if (callback) callback();
}
2013-09-04 23:36:33 +00:00
};
modal.onkeydown = onKey;
var clickButton = function() {
modal.remove();
var value = JSON.parse(this.value);
if (callback) callback(value);
editor.focus();
};
buttons.forEach(function(options) {
var button = document.createElement("button");
if (typeof options == "string") {
options = {
label: options,
value: options
}
}
button.innerHTML = options.label;
button.value = JSON.stringify(options.value || null);
if (options.focus) {
button.className = "default";
}
buttonRow.append(button);
button.on("click", clickButton);
});
2013-09-04 23:36:33 +00:00
setTimeout(function() {
//ensure focus, even from palette (which normally refocuses editor)
var button = modal.find("button.default");
if (!button) button = modal.find("button");
button.focus();
});
2013-09-04 23:36:33 +00:00
}
});