LaravelTest
163 строки · 5.1 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4// Open simple dialogs on top of an editor. Relies on dialog.css.
5
6(function(mod) {7if (typeof exports == "object" && typeof module == "object") // CommonJS8mod(require("../../lib/codemirror"));9else if (typeof define == "function" && define.amd) // AMD10define(["../../lib/codemirror"], mod);11else // Plain browser env12mod(CodeMirror);13})(function(CodeMirror) {14function dialogDiv(cm, template, bottom) {15var wrap = cm.getWrapperElement();16var dialog;17dialog = wrap.appendChild(document.createElement("div"));18if (bottom)19dialog.className = "CodeMirror-dialog CodeMirror-dialog-bottom";20else21dialog.className = "CodeMirror-dialog CodeMirror-dialog-top";22
23if (typeof template == "string") {24dialog.innerHTML = template;25} else { // Assuming it's a detached DOM element.26dialog.appendChild(template);27}28CodeMirror.addClass(wrap, 'dialog-opened');29return dialog;30}31
32function closeNotification(cm, newVal) {33if (cm.state.currentNotificationClose)34cm.state.currentNotificationClose();35cm.state.currentNotificationClose = newVal;36}37
38CodeMirror.defineExtension("openDialog", function(template, callback, options) {39if (!options) options = {};40
41closeNotification(this, null);42
43var dialog = dialogDiv(this, template, options.bottom);44var closed = false, me = this;45function close(newVal) {46if (typeof newVal == 'string') {47inp.value = newVal;48} else {49if (closed) return;50closed = true;51CodeMirror.rmClass(dialog.parentNode, 'dialog-opened');52dialog.parentNode.removeChild(dialog);53me.focus();54
55if (options.onClose) options.onClose(dialog);56}57}58
59var inp = dialog.getElementsByTagName("input")[0], button;60if (inp) {61inp.focus();62
63if (options.value) {64inp.value = options.value;65if (options.selectValueOnOpen !== false) {66inp.select();67}68}69
70if (options.onInput)71CodeMirror.on(inp, "input", function(e) { options.onInput(e, inp.value, close);});72if (options.onKeyUp)73CodeMirror.on(inp, "keyup", function(e) {options.onKeyUp(e, inp.value, close);});74
75CodeMirror.on(inp, "keydown", function(e) {76if (options && options.onKeyDown && options.onKeyDown(e, inp.value, close)) { return; }77if (e.keyCode == 27 || (options.closeOnEnter !== false && e.keyCode == 13)) {78inp.blur();79CodeMirror.e_stop(e);80close();81}82if (e.keyCode == 13) callback(inp.value, e);83});84
85if (options.closeOnBlur !== false) CodeMirror.on(dialog, "focusout", function (evt) {86if (evt.relatedTarget !== null) close();87});88} else if (button = dialog.getElementsByTagName("button")[0]) {89CodeMirror.on(button, "click", function() {90close();91me.focus();92});93
94if (options.closeOnBlur !== false) CodeMirror.on(button, "blur", close);95
96button.focus();97}98return close;99});100
101CodeMirror.defineExtension("openConfirm", function(template, callbacks, options) {102closeNotification(this, null);103var dialog = dialogDiv(this, template, options && options.bottom);104var buttons = dialog.getElementsByTagName("button");105var closed = false, me = this, blurring = 1;106function close() {107if (closed) return;108closed = true;109CodeMirror.rmClass(dialog.parentNode, 'dialog-opened');110dialog.parentNode.removeChild(dialog);111me.focus();112}113buttons[0].focus();114for (var i = 0; i < buttons.length; ++i) {115var b = buttons[i];116(function(callback) {117CodeMirror.on(b, "click", function(e) {118CodeMirror.e_preventDefault(e);119close();120if (callback) callback(me);121});122})(callbacks[i]);123CodeMirror.on(b, "blur", function() {124--blurring;125setTimeout(function() { if (blurring <= 0) close(); }, 200);126});127CodeMirror.on(b, "focus", function() { ++blurring; });128}129});130
131/*132* openNotification
133* Opens a notification, that can be closed with an optional timer
134* (default 5000ms timer) and always closes on click.
135*
136* If a notification is opened while another is opened, it will close the
137* currently opened one and open the new one immediately.
138*/
139CodeMirror.defineExtension("openNotification", function(template, options) {140closeNotification(this, close);141var dialog = dialogDiv(this, template, options && options.bottom);142var closed = false, doneTimer;143var duration = options && typeof options.duration !== "undefined" ? options.duration : 5000;144
145function close() {146if (closed) return;147closed = true;148clearTimeout(doneTimer);149CodeMirror.rmClass(dialog.parentNode, 'dialog-opened');150dialog.parentNode.removeChild(dialog);151}152
153CodeMirror.on(dialog, 'click', function(e) {154CodeMirror.e_preventDefault(e);155close();156});157
158if (duration)159doneTimer = setTimeout(close, duration);160
161return close;162});163});164