Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
51008 views
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
if (typeof template == "string") {
14
dialog.innerHTML = template;
15
} else { // Assuming it's a detached DOM element.
16
dialog.appendChild(template);
17
}
18
return dialog;
19
}
20
21
function closeNotification(cm, newVal) {
22
if (cm.state.currentNotificationClose)
23
cm.state.currentNotificationClose();
24
cm.state.currentNotificationClose = newVal;
25
}
26
27
CodeMirror.defineExtension("openDialog", function(template, callback, options) {
28
closeNotification(this, null);
29
var dialog = dialogDiv(this, template, options && options.bottom);
30
var closed = false, me = this;
31
function close() {
32
if (closed) return;
33
closed = true;
34
dialog.parentNode.removeChild(dialog);
35
}
36
var inp = dialog.getElementsByTagName("input")[0], button;
37
if (inp) {
38
if (options && options.value) inp.value = options.value;
39
CodeMirror.on(inp, "keydown", function(e) {
40
if (options && options.onKeyDown && options.onKeyDown(e, inp.value, close)) { return; }
41
if (e.keyCode == 13 || e.keyCode == 27) {
42
inp.blur();
43
CodeMirror.e_stop(e);
44
close();
45
me.focus();
46
if (e.keyCode == 13) callback(inp.value);
47
}
48
});
49
if (options && options.onKeyUp) {
50
CodeMirror.on(inp, "keyup", function(e) {options.onKeyUp(e, inp.value, close);});
51
}
52
if (options && options.value) inp.value = options.value;
53
inp.focus();
54
CodeMirror.on(inp, "blur", close);
55
} else if (button = dialog.getElementsByTagName("button")[0]) {
56
CodeMirror.on(button, "click", function() {
57
close();
58
me.focus();
59
});
60
button.focus();
61
CodeMirror.on(button, "blur", close);
62
}
63
return close;
64
});
65
66
CodeMirror.defineExtension("openConfirm", function(template, callbacks, options) {
67
closeNotification(this, null);
68
var dialog = dialogDiv(this, template, options && options.bottom);
69
var buttons = dialog.getElementsByTagName("button");
70
var closed = false, me = this, blurring = 1;
71
function close() {
72
if (closed) return;
73
closed = true;
74
dialog.parentNode.removeChild(dialog);
75
me.focus();
76
}
77
buttons[0].focus();
78
for (var i = 0; i < buttons.length; ++i) {
79
var b = buttons[i];
80
(function(callback) {
81
CodeMirror.on(b, "click", function(e) {
82
CodeMirror.e_preventDefault(e);
83
close();
84
if (callback) callback(me);
85
});
86
})(callbacks[i]);
87
CodeMirror.on(b, "blur", function() {
88
--blurring;
89
setTimeout(function() { if (blurring <= 0) close(); }, 200);
90
});
91
CodeMirror.on(b, "focus", function() { ++blurring; });
92
}
93
});
94
95
/*
96
* openNotification
97
* Opens a notification, that can be closed with an optional timer
98
* (default 5000ms timer) and always closes on click.
99
*
100
* If a notification is opened while another is opened, it will close the
101
* currently opened one and open the new one immediately.
102
*/
103
CodeMirror.defineExtension("openNotification", function(template, options) {
104
closeNotification(this, close);
105
var dialog = dialogDiv(this, template, options && options.bottom);
106
var duration = options && (options.duration === undefined ? 5000 : options.duration);
107
var closed = false, doneTimer;
108
109
function close() {
110
if (closed) return;
111
closed = true;
112
clearTimeout(doneTimer);
113
dialog.parentNode.removeChild(dialog);
114
}
115
116
CodeMirror.on(dialog, 'click', function(e) {
117
CodeMirror.e_preventDefault(e);
118
close();
119
});
120
if (duration)
121
doneTimer = setTimeout(close, options.duration);
122
});
123
})();
124
125