| 1 |
// Open simple dialogs on top of an editor. Relies on dialog.css. |
| 2 |
|
| 3 |
(function() { |
| 4 |
function dialogDiv(cm, template, bottom) { |
| 5 |
var wrap = cm.getWrapperElement(); |
| 6 |
var dialog; |
| 7 |
dialog = wrap.appendChild(document.createElement("div")); |
| 8 |
if (bottom) { |
| 9 |
dialog.className = "CodeMirror-dialog CodeMirror-dialog-bottom"; |
| 10 |
} else { |
| 11 |
dialog.className = "CodeMirror-dialog CodeMirror-dialog-top"; |
| 12 |
} |
| 13 |
dialog.innerHTML = template; |
| 14 |
return dialog; |
| 15 |
} |
| 16 |
|
| 17 |
CodeMirror.defineExtension("openDialog", function(template, callback, options) { |
| 18 |
var dialog = dialogDiv(this, template, options && options.bottom); |
| 19 |
var closed = false, me = this; |
| 20 |
function close() { |
| 21 |
if (closed) return; |
| 22 |
closed = true; |
| 23 |
dialog.parentNode.removeChild(dialog); |
| 24 |
} |
| 25 |
var inp = dialog.getElementsByTagName("input")[0], button; |
| 26 |
if (inp) { |
| 27 |
CodeMirror.on(inp, "keydown", function(e) { |
| 28 |
if (options && options.onKeyDown && options.onKeyDown(e, inp.value, close)) { return; } |
| 29 |
if (e.keyCode == 13 || e.keyCode == 27) { |
| 30 |
CodeMirror.e_stop(e); |
| 31 |
close(); |
| 32 |
me.focus(); |
| 33 |
if (e.keyCode == 13) callback(inp.value); |
| 34 |
} |
| 35 |
}); |
| 36 |
if (options && options.onKeyUp) { |
| 37 |
CodeMirror.on(inp, "keyup", function(e) {options.onKeyUp(e, inp.value, close);}); |
| 38 |
} |
| 39 |
if (options && options.value) inp.value = options.value; |
| 40 |
inp.focus(); |
| 41 |
CodeMirror.on(inp, "blur", close); |
| 42 |
} else if (button = dialog.getElementsByTagName("button")[0]) { |
| 43 |
CodeMirror.on(button, "click", function() { |
| 44 |
close(); |
| 45 |
me.focus(); |
| 46 |
}); |
| 47 |
button.focus(); |
| 48 |
CodeMirror.on(button, "blur", close); |
| 49 |
} |
| 50 |
return close; |
| 51 |
}); |
| 52 |
|
| 53 |
CodeMirror.defineExtension("openConfirm", function(template, callbacks, options) { |
| 54 |
var dialog = dialogDiv(this, template, options && options.bottom); |
| 55 |
var buttons = dialog.getElementsByTagName("button"); |
| 56 |
var closed = false, me = this, blurring = 1; |
| 57 |
function close() { |
| 58 |
if (closed) return; |
| 59 |
closed = true; |
| 60 |
dialog.parentNode.removeChild(dialog); |
| 61 |
me.focus(); |
| 62 |
} |
| 63 |
buttons[0].focus(); |
| 64 |
for (var i = 0; i < buttons.length; ++i) { |
| 65 |
var b = buttons[i]; |
| 66 |
(function(callback) { |
| 67 |
CodeMirror.on(b, "click", function(e) { |
| 68 |
CodeMirror.e_preventDefault(e); |
| 69 |
close(); |
| 70 |
if (callback) callback(me); |
| 71 |
}); |
| 72 |
})(callbacks[i]); |
| 73 |
CodeMirror.on(b, "blur", function() { |
| 74 |
--blurring; |
| 75 |
setTimeout(function() { if (blurring <= 0) close(); }, 200); |
| 76 |
}); |
| 77 |
CodeMirror.on(b, "focus", function() { ++blurring; }); |
| 78 |
} |
| 79 |
}); |
| 80 |
})(); |
| 81 |
|