diff options
Diffstat (limited to 'imports/codemirror/lib/util/dialog.js')
-rwxr-xr-x | imports/codemirror/lib/util/dialog.js | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/imports/codemirror/lib/util/dialog.js b/imports/codemirror/lib/util/dialog.js new file mode 100755 index 00000000..8950bf0c --- /dev/null +++ b/imports/codemirror/lib/util/dialog.js | |||
@@ -0,0 +1,63 @@ | |||
1 | // Open simple dialogs on top of an editor. Relies on dialog.css. | ||
2 | |||
3 | (function() { | ||
4 | function dialogDiv(cm, template) { | ||
5 | var wrap = cm.getWrapperElement(); | ||
6 | var dialog = wrap.insertBefore(document.createElement("div"), wrap.firstChild); | ||
7 | dialog.className = "CodeMirror-dialog"; | ||
8 | dialog.innerHTML = '<div>' + template + '</div>'; | ||
9 | return dialog; | ||
10 | } | ||
11 | |||
12 | CodeMirror.defineExtension("openDialog", function(template, callback) { | ||
13 | var dialog = dialogDiv(this, template); | ||
14 | var closed = false, me = this; | ||
15 | function close() { | ||
16 | if (closed) return; | ||
17 | closed = true; | ||
18 | dialog.parentNode.removeChild(dialog); | ||
19 | } | ||
20 | var inp = dialog.getElementsByTagName("input")[0]; | ||
21 | if (inp) { | ||
22 | CodeMirror.connect(inp, "keydown", function(e) { | ||
23 | if (e.keyCode == 13 || e.keyCode == 27) { | ||
24 | CodeMirror.e_stop(e); | ||
25 | close(); | ||
26 | me.focus(); | ||
27 | if (e.keyCode == 13) callback(inp.value); | ||
28 | } | ||
29 | }); | ||
30 | inp.focus(); | ||
31 | CodeMirror.connect(inp, "blur", close); | ||
32 | } | ||
33 | return close; | ||
34 | }); | ||
35 | |||
36 | CodeMirror.defineExtension("openConfirm", function(template, callbacks) { | ||
37 | var dialog = dialogDiv(this, template); | ||
38 | var buttons = dialog.getElementsByTagName("button"); | ||
39 | var closed = false, me = this, blurring = 1; | ||
40 | function close() { | ||
41 | if (closed) return; | ||
42 | closed = true; | ||
43 | dialog.parentNode.removeChild(dialog); | ||
44 | me.focus(); | ||
45 | } | ||
46 | buttons[0].focus(); | ||
47 | for (var i = 0; i < buttons.length; ++i) { | ||
48 | var b = buttons[i]; | ||
49 | (function(callback) { | ||
50 | CodeMirror.connect(b, "click", function(e) { | ||
51 | CodeMirror.e_preventDefault(e); | ||
52 | close(); | ||
53 | if (callback) callback(me); | ||
54 | }); | ||
55 | })(callbacks[i]); | ||
56 | CodeMirror.connect(b, "blur", function() { | ||
57 | --blurring; | ||
58 | setTimeout(function() { if (blurring <= 0) close(); }, 200); | ||
59 | }); | ||
60 | CodeMirror.connect(b, "focus", function() { ++blurring; }); | ||
61 | } | ||
62 | }); | ||
63 | })(); \ No newline at end of file | ||