Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
malwaredllc
GitHub Repository: malwaredllc/byob
Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/dialog/dialog.js
1293 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
CodeMirror.e_stop(e);
43
close();
44
me.focus();
45
if (e.keyCode == 13) callback(inp.value);
46
}
47
});
48
if (options && options.onKeyUp) {
49
CodeMirror.on(inp, "keyup", function(e) {options.onKeyUp(e, inp.value, close);});
50
}
51
if (options && options.value) inp.value = options.value;
52
inp.focus();
53
CodeMirror.on(inp, "blur", close);
54
} else if (button = dialog.getElementsByTagName("button")[0]) {
55
CodeMirror.on(button, "click", function() {
56
close();
57
me.focus();
58
});
59
button.focus();
60
CodeMirror.on(button, "blur", close);
61
}
62
return close;
63
});
64
65
CodeMirror.defineExtension("openConfirm", function(template, callbacks, options) {
66
closeNotification(this, null);
67
var dialog = dialogDiv(this, template, options && options.bottom);
68
var buttons = dialog.getElementsByTagName("button");
69
var closed = false, me = this, blurring = 1;
70
function close() {
71
if (closed) return;
72
closed = true;
73
dialog.parentNode.removeChild(dialog);
74
me.focus();
75
}
76
buttons[0].focus();
77
for (var i = 0; i < buttons.length; ++i) {
78
var b = buttons[i];
79
(function(callback) {
80
CodeMirror.on(b, "click", function(e) {
81
CodeMirror.e_preventDefault(e);
82
close();
83
if (callback) callback(me);
84
});
85
})(callbacks[i]);
86
CodeMirror.on(b, "blur", function() {
87
--blurring;
88
setTimeout(function() { if (blurring <= 0) close(); }, 200);
89
});
90
CodeMirror.on(b, "focus", function() { ++blurring; });
91
}
92
});
93
94
/*
95
* openNotification
96
* Opens a notification, that can be closed with an optional timer
97
* (default 5000ms timer) and always closes on click.
98
*
99
* If a notification is opened while another is opened, it will close the
100
* currently opened one and open the new one immediately.
101
*/
102
CodeMirror.defineExtension("openNotification", function(template, options) {
103
closeNotification(this, close);
104
var dialog = dialogDiv(this, template, options && options.bottom);
105
var duration = options && (options.duration === undefined ? 5000 : options.duration);
106
var closed = false, doneTimer;
107
108
function close() {
109
if (closed) return;
110
closed = true;
111
clearTimeout(doneTimer);
112
dialog.parentNode.removeChild(dialog);
113
}
114
115
CodeMirror.on(dialog, 'click', function(e) {
116
CodeMirror.e_preventDefault(e);
117
close();
118
});
119
if (duration)
120
doneTimer = setTimeout(close, options.duration);
121
});
122
})();
123
124