2013-09-21 00:39:04 +00:00
|
|
|
define(["editor", "dom2"], function(editor) {
|
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
|
|
|
|
|
|
|
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-01 18:09:09 +00:00
|
|
|
|
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();
|
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
|
|
|
};
|
|
|
|
|
|
|
|
modal.onkeydown = onKey;
|
|
|
|
|
2013-09-01 18:09:09 +00:00
|
|
|
var clickButton = function() {
|
|
|
|
modal.remove();
|
2013-09-01 19:23:13 +00:00
|
|
|
var value = JSON.parse(this.value);
|
2013-09-06 07:04:20 +00:00
|
|
|
if (callback) callback(value);
|
2013-09-21 00:39:04 +00:00
|
|
|
editor.focus();
|
2013-09-01 19:23:13 +00:00
|
|
|
};
|
2013-09-01 18:09:09 +00:00
|
|
|
|
|
|
|
buttons.forEach(function(options) {
|
|
|
|
var button = document.createElement("button");
|
|
|
|
if (typeof options == "string") {
|
|
|
|
options = {
|
|
|
|
label: options,
|
2013-09-24 14:57:22 +00:00
|
|
|
value: options
|
2013-09-01 18:09:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
button.innerHTML = options.label;
|
2013-09-24 22:35:15 +00:00
|
|
|
button.value = JSON.stringify(options.value);
|
2013-09-21 00:39:04 +00:00
|
|
|
if (options.focus) {
|
|
|
|
button.className = "default";
|
|
|
|
}
|
2013-09-01 18:09:09 +00:00
|
|
|
buttonRow.append(button);
|
|
|
|
button.on("click", clickButton);
|
|
|
|
});
|
2013-09-04 23:36:33 +00:00
|
|
|
|
2013-10-08 18:15:52 +00:00
|
|
|
var button = modal.find("button.default");
|
|
|
|
if (!button) button = modal.find("button");
|
|
|
|
button.focus();
|
2013-09-04 23:36:33 +00:00
|
|
|
|
2013-09-01 18:09:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|