5f1f4aa4cc049b1a2236ae5729cbbfe085ae8b17
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/LICENSE
4 // Open simple dialogs on top of an editor. Relies on dialog.css.
7 if (typeof exports
== "object" && typeof module
== "object") // CommonJS
8 mod(require("../../lib/codemirror"));
9 else if (typeof define
== "function" && define
.amd
) // AMD
10 define(["../../lib/codemirror"], mod
);
11 else // Plain browser env
13 })(function(CodeMirror
) {
14 function dialogDiv(cm
, template
, bottom
) {
15 var wrap
= cm
.getWrapperElement();
17 dialog
= wrap
.appendChild(document
.createElement("div"));
19 dialog
.className
= "CodeMirror-dialog CodeMirror-dialog-bottom";
21 dialog
.className
= "CodeMirror-dialog CodeMirror-dialog-top";
23 if (typeof template
== "string") {
24 dialog
.innerHTML
= template
;
25 } else { // Assuming it's a detached DOM element.
26 dialog
.appendChild(template
);
28 CodeMirror
.addClass(wrap
, 'dialog-opened');
32 function closeNotification(cm
, newVal
) {
33 if (cm
.state
.currentNotificationClose
)
34 cm
.state
.currentNotificationClose();
35 cm
.state
.currentNotificationClose
= newVal
;
38 CodeMirror
.defineExtension("openDialog", function(template
, callback
, options
) {
39 if (!options
) options
= {};
41 closeNotification(this, null);
43 var dialog
= dialogDiv(this, template
, options
.bottom
);
44 var closed
= false, me
= this;
45 function close(newVal
) {
46 if (typeof newVal
== 'string') {
51 CodeMirror
.rmClass(dialog
.parentNode
, 'dialog-opened');
52 dialog
.parentNode
.removeChild(dialog
);
55 if (options
.onClose
) options
.onClose(dialog
);
59 var inp
= dialog
.getElementsByTagName("input")[0], button
;
64 inp
.value
= options
.value
;
65 if (options
.selectValueOnOpen
!== false) {
71 CodeMirror
.on(inp
, "input", function(e
) { options
.onInput(e
, inp
.value
, close
);});
73 CodeMirror
.on(inp
, "keyup", function(e
) {options
.onKeyUp(e
, inp
.value
, close
);});
75 CodeMirror
.on(inp
, "keydown", function(e
) {
76 if (options
&& options
.onKeyDown
&& options
.onKeyDown(e
, inp
.value
, close
)) { return; }
77 if (e
.keyCode
== 27 || (options
.closeOnEnter
!== false && e
.keyCode
== 13)) {
82 if (e
.keyCode
== 13) callback(inp
.value
, e
);
85 if (options
.closeOnBlur
!== false) CodeMirror
.on(dialog
, "focusout", function (evt
) {
86 if (evt
.relatedTarget
!== null) close();
88 } else if (button
= dialog
.getElementsByTagName("button")[0]) {
89 CodeMirror
.on(button
, "click", function() {
94 if (options
.closeOnBlur
!== false) CodeMirror
.on(button
, "blur", close
);
101 CodeMirror
.defineExtension("openConfirm", function(template
, callbacks
, options
) {
102 closeNotification(this, null);
103 var dialog
= dialogDiv(this, template
, options
&& options
.bottom
);
104 var buttons
= dialog
.getElementsByTagName("button");
105 var closed
= false, me
= this, blurring
= 1;
109 CodeMirror
.rmClass(dialog
.parentNode
, 'dialog-opened');
110 dialog
.parentNode
.removeChild(dialog
);
114 for (var i
= 0; i
< buttons
.length
; ++i
) {
116 (function(callback
) {
117 CodeMirror
.on(b
, "click", function(e
) {
118 CodeMirror
.e_preventDefault(e
);
120 if (callback
) callback(me
);
123 CodeMirror
.on(b
, "blur", function() {
125 setTimeout(function() { if (blurring
<= 0) close(); }, 200);
127 CodeMirror
.on(b
, "focus", function() { ++blurring
; });
133 * Opens a notification, that can be closed with an optional timer
134 * (default 5000ms timer) and always closes on click.
136 * If a notification is opened while another is opened, it will close the
137 * currently opened one and open the new one immediately.
139 CodeMirror
.defineExtension("openNotification", function(template
, options
) {
140 closeNotification(this, close
);
141 var dialog
= dialogDiv(this, template
, options
&& options
.bottom
);
142 var closed
= false, doneTimer
;
143 var duration
= options
&& typeof options
.duration
!== "undefined" ? options
.duration
: 5000;
148 clearTimeout(doneTimer
);
149 CodeMirror
.rmClass(dialog
.parentNode
, 'dialog-opened');
150 dialog
.parentNode
.removeChild(dialog
);
153 CodeMirror
.on(dialog
, 'click', function(e
) {
154 CodeMirror
.e_preventDefault(e
);
159 doneTimer
= setTimeout(close
, duration
);