browser/devtools/sourceeditor/codemirror/dialog/dialog.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Open simple dialogs on top of an editor. Relies on dialog.css.
michael@0 2
michael@0 3 (function(mod) {
michael@0 4 if (typeof exports == "object" && typeof module == "object") // CommonJS
michael@0 5 mod(require("../../lib/codemirror"));
michael@0 6 else if (typeof define == "function" && define.amd) // AMD
michael@0 7 define(["../../lib/codemirror"], mod);
michael@0 8 else // Plain browser env
michael@0 9 mod(CodeMirror);
michael@0 10 })(function(CodeMirror) {
michael@0 11 function dialogDiv(cm, template, bottom) {
michael@0 12 var wrap = cm.getWrapperElement();
michael@0 13 var dialog;
michael@0 14 dialog = wrap.appendChild(document.createElement("div"));
michael@0 15 if (bottom) {
michael@0 16 dialog.className = "CodeMirror-dialog CodeMirror-dialog-bottom";
michael@0 17 } else {
michael@0 18 dialog.className = "CodeMirror-dialog CodeMirror-dialog-top";
michael@0 19 }
michael@0 20 if (typeof template == "string") {
michael@0 21 dialog.innerHTML = template;
michael@0 22 } else { // Assuming it's a detached DOM element.
michael@0 23 dialog.appendChild(template);
michael@0 24 }
michael@0 25 return dialog;
michael@0 26 }
michael@0 27
michael@0 28 function closeNotification(cm, newVal) {
michael@0 29 if (cm.state.currentNotificationClose)
michael@0 30 cm.state.currentNotificationClose();
michael@0 31 cm.state.currentNotificationClose = newVal;
michael@0 32 }
michael@0 33
michael@0 34 CodeMirror.defineExtension("openDialog", function(template, callback, options) {
michael@0 35 closeNotification(this, null);
michael@0 36 var dialog = dialogDiv(this, template, options && options.bottom);
michael@0 37 var closed = false, me = this;
michael@0 38 function close() {
michael@0 39 if (closed) return;
michael@0 40 closed = true;
michael@0 41 dialog.parentNode.removeChild(dialog);
michael@0 42 }
michael@0 43 var inp = dialog.getElementsByTagName("input")[0], button;
michael@0 44 if (inp) {
michael@0 45 if (options && options.value) inp.value = options.value;
michael@0 46 CodeMirror.on(inp, "keydown", function(e) {
michael@0 47 if (options && options.onKeyDown && options.onKeyDown(e, inp.value, close)) { return; }
michael@0 48 if (e.keyCode == 13 || e.keyCode == 27) {
michael@0 49 inp.blur();
michael@0 50 CodeMirror.e_stop(e);
michael@0 51 close();
michael@0 52 me.focus();
michael@0 53 if (e.keyCode == 13) callback(inp.value);
michael@0 54 }
michael@0 55 });
michael@0 56 if (options && options.onKeyUp) {
michael@0 57 CodeMirror.on(inp, "keyup", function(e) {options.onKeyUp(e, inp.value, close);});
michael@0 58 }
michael@0 59 if (options && options.value) inp.value = options.value;
michael@0 60 inp.focus();
michael@0 61 CodeMirror.on(inp, "blur", close);
michael@0 62 } else if (button = dialog.getElementsByTagName("button")[0]) {
michael@0 63 CodeMirror.on(button, "click", function() {
michael@0 64 close();
michael@0 65 me.focus();
michael@0 66 });
michael@0 67 button.focus();
michael@0 68 CodeMirror.on(button, "blur", close);
michael@0 69 }
michael@0 70 return close;
michael@0 71 });
michael@0 72
michael@0 73 CodeMirror.defineExtension("openConfirm", function(template, callbacks, options) {
michael@0 74 closeNotification(this, null);
michael@0 75 var dialog = dialogDiv(this, template, options && options.bottom);
michael@0 76 var buttons = dialog.getElementsByTagName("button");
michael@0 77 var closed = false, me = this, blurring = 1;
michael@0 78 function close() {
michael@0 79 if (closed) return;
michael@0 80 closed = true;
michael@0 81 dialog.parentNode.removeChild(dialog);
michael@0 82 me.focus();
michael@0 83 }
michael@0 84 buttons[0].focus();
michael@0 85 for (var i = 0; i < buttons.length; ++i) {
michael@0 86 var b = buttons[i];
michael@0 87 (function(callback) {
michael@0 88 CodeMirror.on(b, "click", function(e) {
michael@0 89 CodeMirror.e_preventDefault(e);
michael@0 90 close();
michael@0 91 if (callback) callback(me);
michael@0 92 });
michael@0 93 })(callbacks[i]);
michael@0 94 CodeMirror.on(b, "blur", function() {
michael@0 95 --blurring;
michael@0 96 setTimeout(function() { if (blurring <= 0) close(); }, 200);
michael@0 97 });
michael@0 98 CodeMirror.on(b, "focus", function() { ++blurring; });
michael@0 99 }
michael@0 100 });
michael@0 101
michael@0 102 /*
michael@0 103 * openNotification
michael@0 104 * Opens a notification, that can be closed with an optional timer
michael@0 105 * (default 5000ms timer) and always closes on click.
michael@0 106 *
michael@0 107 * If a notification is opened while another is opened, it will close the
michael@0 108 * currently opened one and open the new one immediately.
michael@0 109 */
michael@0 110 CodeMirror.defineExtension("openNotification", function(template, options) {
michael@0 111 closeNotification(this, close);
michael@0 112 var dialog = dialogDiv(this, template, options && options.bottom);
michael@0 113 var duration = options && (options.duration === undefined ? 5000 : options.duration);
michael@0 114 var closed = false, doneTimer;
michael@0 115
michael@0 116 function close() {
michael@0 117 if (closed) return;
michael@0 118 closed = true;
michael@0 119 clearTimeout(doneTimer);
michael@0 120 dialog.parentNode.removeChild(dialog);
michael@0 121 }
michael@0 122
michael@0 123 CodeMirror.on(dialog, 'click', function(e) {
michael@0 124 CodeMirror.e_preventDefault(e);
michael@0 125 close();
michael@0 126 });
michael@0 127 if (duration)
michael@0 128 doneTimer = setTimeout(close, options.duration);
michael@0 129 });
michael@0 130 });

mercurial