Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/dialog/dialog.js
1293 views
// Open simple dialogs on top of an editor. Relies on dialog.css.12(function() {3function dialogDiv(cm, template, bottom) {4var wrap = cm.getWrapperElement();5var dialog;6dialog = wrap.appendChild(document.createElement("div"));7if (bottom) {8dialog.className = "CodeMirror-dialog CodeMirror-dialog-bottom";9} else {10dialog.className = "CodeMirror-dialog CodeMirror-dialog-top";11}12if (typeof template == "string") {13dialog.innerHTML = template;14} else { // Assuming it's a detached DOM element.15dialog.appendChild(template);16}17return dialog;18}1920function closeNotification(cm, newVal) {21if (cm.state.currentNotificationClose)22cm.state.currentNotificationClose();23cm.state.currentNotificationClose = newVal;24}2526CodeMirror.defineExtension("openDialog", function(template, callback, options) {27closeNotification(this, null);28var dialog = dialogDiv(this, template, options && options.bottom);29var closed = false, me = this;30function close() {31if (closed) return;32closed = true;33dialog.parentNode.removeChild(dialog);34}35var inp = dialog.getElementsByTagName("input")[0], button;36if (inp) {37if (options && options.value) inp.value = options.value;38CodeMirror.on(inp, "keydown", function(e) {39if (options && options.onKeyDown && options.onKeyDown(e, inp.value, close)) { return; }40if (e.keyCode == 13 || e.keyCode == 27) {41CodeMirror.e_stop(e);42close();43me.focus();44if (e.keyCode == 13) callback(inp.value);45}46});47if (options && options.onKeyUp) {48CodeMirror.on(inp, "keyup", function(e) {options.onKeyUp(e, inp.value, close);});49}50if (options && options.value) inp.value = options.value;51inp.focus();52CodeMirror.on(inp, "blur", close);53} else if (button = dialog.getElementsByTagName("button")[0]) {54CodeMirror.on(button, "click", function() {55close();56me.focus();57});58button.focus();59CodeMirror.on(button, "blur", close);60}61return close;62});6364CodeMirror.defineExtension("openConfirm", function(template, callbacks, options) {65closeNotification(this, null);66var dialog = dialogDiv(this, template, options && options.bottom);67var buttons = dialog.getElementsByTagName("button");68var closed = false, me = this, blurring = 1;69function close() {70if (closed) return;71closed = true;72dialog.parentNode.removeChild(dialog);73me.focus();74}75buttons[0].focus();76for (var i = 0; i < buttons.length; ++i) {77var b = buttons[i];78(function(callback) {79CodeMirror.on(b, "click", function(e) {80CodeMirror.e_preventDefault(e);81close();82if (callback) callback(me);83});84})(callbacks[i]);85CodeMirror.on(b, "blur", function() {86--blurring;87setTimeout(function() { if (blurring <= 0) close(); }, 200);88});89CodeMirror.on(b, "focus", function() { ++blurring; });90}91});9293/*94* openNotification95* Opens a notification, that can be closed with an optional timer96* (default 5000ms timer) and always closes on click.97*98* If a notification is opened while another is opened, it will close the99* currently opened one and open the new one immediately.100*/101CodeMirror.defineExtension("openNotification", function(template, options) {102closeNotification(this, close);103var dialog = dialogDiv(this, template, options && options.bottom);104var duration = options && (options.duration === undefined ? 5000 : options.duration);105var closed = false, doneTimer;106107function close() {108if (closed) return;109closed = true;110clearTimeout(doneTimer);111dialog.parentNode.removeChild(dialog);112}113114CodeMirror.on(dialog, 'click', function(e) {115CodeMirror.e_preventDefault(e);116close();117});118if (duration)119doneTimer = setTimeout(close, options.duration);120});121})();122123124