wide/static/js/lib/codemirror-5.1/addon/dialog/dialog.js

158 lines
4.8 KiB
JavaScript
Raw Permalink Normal View History

2014-08-18 17:45:43 +04:00
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
// Open simple dialogs on top of an editor. Relies on dialog.css.
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
function dialogDiv(cm, template, bottom) {
var wrap = cm.getWrapperElement();
var dialog;
dialog = wrap.appendChild(document.createElement("div"));
2014-08-30 07:26:14 +04:00
if (bottom)
2014-08-18 17:45:43 +04:00
dialog.className = "CodeMirror-dialog CodeMirror-dialog-bottom";
2014-08-30 07:26:14 +04:00
else
2014-08-18 17:45:43 +04:00
dialog.className = "CodeMirror-dialog CodeMirror-dialog-top";
2014-08-30 07:26:14 +04:00
2014-08-18 17:45:43 +04:00
if (typeof template == "string") {
dialog.innerHTML = template;
} else { // Assuming it's a detached DOM element.
dialog.appendChild(template);
}
return dialog;
}
function closeNotification(cm, newVal) {
if (cm.state.currentNotificationClose)
cm.state.currentNotificationClose();
cm.state.currentNotificationClose = newVal;
}
CodeMirror.defineExtension("openDialog", function(template, callback, options) {
2014-08-30 07:26:14 +04:00
if (!options) options = {};
2014-08-18 17:45:43 +04:00
closeNotification(this, null);
2014-08-30 07:26:14 +04:00
var dialog = dialogDiv(this, template, options.bottom);
2014-08-18 17:45:43 +04:00
var closed = false, me = this;
function close(newVal) {
if (typeof newVal == 'string') {
inp.value = newVal;
} else {
if (closed) return;
closed = true;
dialog.parentNode.removeChild(dialog);
2014-08-30 07:26:14 +04:00
me.focus();
if (options.onClose) options.onClose(dialog);
2014-08-18 17:45:43 +04:00
}
}
2014-08-30 07:26:14 +04:00
2014-08-18 17:45:43 +04:00
var inp = dialog.getElementsByTagName("input")[0], button;
if (inp) {
2014-10-27 10:49:58 +03:00
if (options.value) {
inp.value = options.value;
2015-03-24 12:27:39 +03:00
if (options.selectValueOnOpen !== false) {
inp.select();
}
2014-10-27 10:49:58 +03:00
}
2014-08-30 07:26:14 +04:00
if (options.onInput)
CodeMirror.on(inp, "input", function(e) { options.onInput(e, inp.value, close);});
if (options.onKeyUp)
CodeMirror.on(inp, "keyup", function(e) {options.onKeyUp(e, inp.value, close);});
2014-08-18 17:45:43 +04:00
CodeMirror.on(inp, "keydown", function(e) {
if (options && options.onKeyDown && options.onKeyDown(e, inp.value, close)) { return; }
2014-08-30 07:26:14 +04:00
if (e.keyCode == 27 || (options.closeOnEnter !== false && e.keyCode == 13)) {
2014-08-18 17:45:43 +04:00
inp.blur();
CodeMirror.e_stop(e);
close();
}
2014-12-30 12:45:45 +03:00
if (e.keyCode == 13) callback(inp.value, e);
2014-08-18 17:45:43 +04:00
});
2014-08-30 07:26:14 +04:00
if (options.closeOnBlur !== false) CodeMirror.on(inp, "blur", close);
2014-08-18 17:45:43 +04:00
inp.focus();
} else if (button = dialog.getElementsByTagName("button")[0]) {
CodeMirror.on(button, "click", function() {
close();
me.focus();
});
2014-08-30 07:26:14 +04:00
if (options.closeOnBlur !== false) CodeMirror.on(button, "blur", close);
2014-08-18 17:45:43 +04:00
button.focus();
}
return close;
});
CodeMirror.defineExtension("openConfirm", function(template, callbacks, options) {
closeNotification(this, null);
var dialog = dialogDiv(this, template, options && options.bottom);
var buttons = dialog.getElementsByTagName("button");
var closed = false, me = this, blurring = 1;
function close() {
if (closed) return;
closed = true;
dialog.parentNode.removeChild(dialog);
me.focus();
}
buttons[0].focus();
for (var i = 0; i < buttons.length; ++i) {
var b = buttons[i];
(function(callback) {
CodeMirror.on(b, "click", function(e) {
CodeMirror.e_preventDefault(e);
close();
if (callback) callback(me);
});
})(callbacks[i]);
CodeMirror.on(b, "blur", function() {
--blurring;
setTimeout(function() { if (blurring <= 0) close(); }, 200);
});
CodeMirror.on(b, "focus", function() { ++blurring; });
}
});
/*
* openNotification
* Opens a notification, that can be closed with an optional timer
* (default 5000ms timer) and always closes on click.
*
* If a notification is opened while another is opened, it will close the
* currently opened one and open the new one immediately.
*/
CodeMirror.defineExtension("openNotification", function(template, options) {
closeNotification(this, close);
var dialog = dialogDiv(this, template, options && options.bottom);
var closed = false, doneTimer;
2014-10-27 10:49:58 +03:00
var duration = options && typeof options.duration !== "undefined" ? options.duration : 5000;
2014-08-18 17:45:43 +04:00
function close() {
if (closed) return;
closed = true;
clearTimeout(doneTimer);
dialog.parentNode.removeChild(dialog);
}
CodeMirror.on(dialog, 'click', function(e) {
CodeMirror.e_preventDefault(e);
close();
});
2014-10-27 10:49:58 +03:00
2014-08-18 17:45:43 +04:00
if (duration)
2014-10-27 10:49:58 +03:00
doneTimer = setTimeout(close, duration);
return close;
2014-08-18 17:45:43 +04:00
});
});