diff --git a/addon/dialog/dialog.js b/addon/dialog/dialog.js index 622d689ed130411a79610580117cb1160525d040..16dcec51a3beec61fd40907006674d34f46e8e21 100644 --- a/addon/dialog/dialog.js +++ b/addon/dialog/dialog.js @@ -35,8 +35,13 @@ } CodeMirror.defineExtension("openDialog", function(template, callback, options) { + if (!options) options = {}; + if (typeof options.closeOnBlur == 'undefined') options.closeOnBlur = true; + if (typeof options.closeOnEnter == 'undefined') options.closeOnEnter = true; + closeNotification(this, null); - var dialog = dialogDiv(this, template, options && options.bottom); + + var dialog = dialogDiv(this, template, options.bottom); var closed = false, me = this; function close(newVal) { if (typeof newVal == 'string') { @@ -45,34 +50,46 @@ if (closed) return; closed = true; dialog.parentNode.removeChild(dialog); + me.focus(); + + if (options.onClose) options.onClose(dialog); } } + var inp = dialog.getElementsByTagName("input")[0], button; if (inp) { - if (options && options.value) inp.value = options.value; + if (options.value) inp.value = options.value; + + if (options.onInput) { + CodeMirror.on(inp, "input", function(e) { options.onInput(e, inp.value, close);}); + } + + if (options.onKeyUp) { + CodeMirror.on(inp, "keyup", function(e) {options.onKeyUp(e, inp.value, close);}); + } + CodeMirror.on(inp, "keydown", function(e) { if (options && options.onKeyDown && options.onKeyDown(e, inp.value, close)) { return; } - if (e.keyCode == 13 || e.keyCode == 27) { + if (e.keyCode == 27 || (options.closeOnEnter && e.keyCode == 13)) { inp.blur(); CodeMirror.e_stop(e); close(); - me.focus(); - if (e.keyCode == 13) callback(inp.value); } + if (e.keyCode == 13) callback(inp.value); }); - if (options && options.onKeyUp) { - CodeMirror.on(inp, "keyup", function(e) {options.onKeyUp(e, inp.value, close);}); - } - if (options && options.value) inp.value = options.value; + + if (options.closeOnBlur) CodeMirror.on(inp, "blur", close); + inp.focus(); - CodeMirror.on(inp, "blur", close); } else if (button = dialog.getElementsByTagName("button")[0]) { CodeMirror.on(button, "click", function() { close(); me.focus(); }); + + if (options.closeOnBlur) CodeMirror.on(button, "blur", close); + button.focus(); - CodeMirror.on(button, "blur", close); } return close; });