1 // Open simple dialogs on top of an editor. Relies on dialog.css.
4 if (typeof exports
== "object" && typeof module
== "object") // CommonJS
5 mod(require("../../lib/codemirror"));
6 else if (typeof define
== "function" && define
.amd
) // AMD
7 define(["../../lib/codemirror"], mod
);
8 else // Plain browser env
10 })(function(CodeMirror
) {
11 function dialogDiv(cm
, template
, bottom
) {
12 var wrap
= cm
.getWrapperElement();
14 dialog
= wrap
.appendChild(document
.createElement("div"));
16 dialog
.className
= "CodeMirror-dialog CodeMirror-dialog-bottom";
18 dialog
.className
= "CodeMirror-dialog CodeMirror-dialog-top";
20 if (typeof template
== "string") {
21 dialog
.innerHTML
= template
;
22 } else { // Assuming it's a detached DOM element.
23 dialog
.appendChild(template
);
28 function closeNotification(cm
, newVal
) {
29 if (cm
.state
.currentNotificationClose
)
30 cm
.state
.currentNotificationClose();
31 cm
.state
.currentNotificationClose
= newVal
;
34 CodeMirror
.defineExtension("openDialog", function(template
, callback
, options
) {
35 closeNotification(this, null);
36 var dialog
= dialogDiv(this, template
, options
&& options
.bottom
);
37 var closed
= false, me
= this;
41 dialog
.parentNode
.removeChild(dialog
);
43 var inp
= dialog
.getElementsByTagName("input")[0], button
;
45 if (options
&& options
.value
) inp
.value
= options
.value
;
46 CodeMirror
.on(inp
, "keydown", function(e
) {
47 if (options
&& options
.onKeyDown
&& options
.onKeyDown(e
, inp
.value
, close
)) { return; }
48 if (e
.keyCode
== 13 || e
.keyCode
== 27) {
53 if (e
.keyCode
== 13) callback(inp
.value
);
56 if (options
&& options
.onKeyUp
) {
57 CodeMirror
.on(inp
, "keyup", function(e
) {options
.onKeyUp(e
, inp
.value
, close
);});
59 if (options
&& options
.value
) inp
.value
= options
.value
;
61 CodeMirror
.on(inp
, "blur", close
);
62 } else if (button
= dialog
.getElementsByTagName("button")[0]) {
63 CodeMirror
.on(button
, "click", function() {
68 CodeMirror
.on(button
, "blur", close
);
73 CodeMirror
.defineExtension("openConfirm", function(template
, callbacks
, options
) {
74 closeNotification(this, null);
75 var dialog
= dialogDiv(this, template
, options
&& options
.bottom
);
76 var buttons
= dialog
.getElementsByTagName("button");
77 var closed
= false, me
= this, blurring
= 1;
81 dialog
.parentNode
.removeChild(dialog
);
85 for (var i
= 0; i
< buttons
.length
; ++i
) {
88 CodeMirror
.on(b
, "click", function(e
) {
89 CodeMirror
.e_preventDefault(e
);
91 if (callback
) callback(me
);
94 CodeMirror
.on(b
, "blur", function() {
96 setTimeout(function() { if (blurring
<= 0) close(); }, 200);
98 CodeMirror
.on(b
, "focus", function() { ++blurring
; });
104 * Opens a notification, that can be closed with an optional timer
105 * (default 5000ms timer) and always closes on click.
107 * If a notification is opened while another is opened, it will close the
108 * currently opened one and open the new one immediately.
110 CodeMirror
.defineExtension("openNotification", function(template
, options
) {
111 closeNotification(this, close
);
112 var dialog
= dialogDiv(this, template
, options
&& options
.bottom
);
113 var duration
= options
&& (options
.duration
=== undefined ? 5000 : options
.duration
);
114 var closed
= false, doneTimer
;
119 clearTimeout(doneTimer
);
120 dialog
.parentNode
.removeChild(dialog
);
123 CodeMirror
.on(dialog
, 'click', function(e
) {
124 CodeMirror
.e_preventDefault(e
);
128 doneTimer
= setTimeout(close
, options
.duration
);