1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/sourceeditor/codemirror/dialog/dialog.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,130 @@ 1.4 +// Open simple dialogs on top of an editor. Relies on dialog.css. 1.5 + 1.6 +(function(mod) { 1.7 + if (typeof exports == "object" && typeof module == "object") // CommonJS 1.8 + mod(require("../../lib/codemirror")); 1.9 + else if (typeof define == "function" && define.amd) // AMD 1.10 + define(["../../lib/codemirror"], mod); 1.11 + else // Plain browser env 1.12 + mod(CodeMirror); 1.13 +})(function(CodeMirror) { 1.14 + function dialogDiv(cm, template, bottom) { 1.15 + var wrap = cm.getWrapperElement(); 1.16 + var dialog; 1.17 + dialog = wrap.appendChild(document.createElement("div")); 1.18 + if (bottom) { 1.19 + dialog.className = "CodeMirror-dialog CodeMirror-dialog-bottom"; 1.20 + } else { 1.21 + dialog.className = "CodeMirror-dialog CodeMirror-dialog-top"; 1.22 + } 1.23 + if (typeof template == "string") { 1.24 + dialog.innerHTML = template; 1.25 + } else { // Assuming it's a detached DOM element. 1.26 + dialog.appendChild(template); 1.27 + } 1.28 + return dialog; 1.29 + } 1.30 + 1.31 + function closeNotification(cm, newVal) { 1.32 + if (cm.state.currentNotificationClose) 1.33 + cm.state.currentNotificationClose(); 1.34 + cm.state.currentNotificationClose = newVal; 1.35 + } 1.36 + 1.37 + CodeMirror.defineExtension("openDialog", function(template, callback, options) { 1.38 + closeNotification(this, null); 1.39 + var dialog = dialogDiv(this, template, options && options.bottom); 1.40 + var closed = false, me = this; 1.41 + function close() { 1.42 + if (closed) return; 1.43 + closed = true; 1.44 + dialog.parentNode.removeChild(dialog); 1.45 + } 1.46 + var inp = dialog.getElementsByTagName("input")[0], button; 1.47 + if (inp) { 1.48 + if (options && options.value) inp.value = options.value; 1.49 + CodeMirror.on(inp, "keydown", function(e) { 1.50 + if (options && options.onKeyDown && options.onKeyDown(e, inp.value, close)) { return; } 1.51 + if (e.keyCode == 13 || e.keyCode == 27) { 1.52 + inp.blur(); 1.53 + CodeMirror.e_stop(e); 1.54 + close(); 1.55 + me.focus(); 1.56 + if (e.keyCode == 13) callback(inp.value); 1.57 + } 1.58 + }); 1.59 + if (options && options.onKeyUp) { 1.60 + CodeMirror.on(inp, "keyup", function(e) {options.onKeyUp(e, inp.value, close);}); 1.61 + } 1.62 + if (options && options.value) inp.value = options.value; 1.63 + inp.focus(); 1.64 + CodeMirror.on(inp, "blur", close); 1.65 + } else if (button = dialog.getElementsByTagName("button")[0]) { 1.66 + CodeMirror.on(button, "click", function() { 1.67 + close(); 1.68 + me.focus(); 1.69 + }); 1.70 + button.focus(); 1.71 + CodeMirror.on(button, "blur", close); 1.72 + } 1.73 + return close; 1.74 + }); 1.75 + 1.76 + CodeMirror.defineExtension("openConfirm", function(template, callbacks, options) { 1.77 + closeNotification(this, null); 1.78 + var dialog = dialogDiv(this, template, options && options.bottom); 1.79 + var buttons = dialog.getElementsByTagName("button"); 1.80 + var closed = false, me = this, blurring = 1; 1.81 + function close() { 1.82 + if (closed) return; 1.83 + closed = true; 1.84 + dialog.parentNode.removeChild(dialog); 1.85 + me.focus(); 1.86 + } 1.87 + buttons[0].focus(); 1.88 + for (var i = 0; i < buttons.length; ++i) { 1.89 + var b = buttons[i]; 1.90 + (function(callback) { 1.91 + CodeMirror.on(b, "click", function(e) { 1.92 + CodeMirror.e_preventDefault(e); 1.93 + close(); 1.94 + if (callback) callback(me); 1.95 + }); 1.96 + })(callbacks[i]); 1.97 + CodeMirror.on(b, "blur", function() { 1.98 + --blurring; 1.99 + setTimeout(function() { if (blurring <= 0) close(); }, 200); 1.100 + }); 1.101 + CodeMirror.on(b, "focus", function() { ++blurring; }); 1.102 + } 1.103 + }); 1.104 + 1.105 + /* 1.106 + * openNotification 1.107 + * Opens a notification, that can be closed with an optional timer 1.108 + * (default 5000ms timer) and always closes on click. 1.109 + * 1.110 + * If a notification is opened while another is opened, it will close the 1.111 + * currently opened one and open the new one immediately. 1.112 + */ 1.113 + CodeMirror.defineExtension("openNotification", function(template, options) { 1.114 + closeNotification(this, close); 1.115 + var dialog = dialogDiv(this, template, options && options.bottom); 1.116 + var duration = options && (options.duration === undefined ? 5000 : options.duration); 1.117 + var closed = false, doneTimer; 1.118 + 1.119 + function close() { 1.120 + if (closed) return; 1.121 + closed = true; 1.122 + clearTimeout(doneTimer); 1.123 + dialog.parentNode.removeChild(dialog); 1.124 + } 1.125 + 1.126 + CodeMirror.on(dialog, 'click', function(e) { 1.127 + CodeMirror.e_preventDefault(e); 1.128 + close(); 1.129 + }); 1.130 + if (duration) 1.131 + doneTimer = setTimeout(close, options.duration); 1.132 + }); 1.133 +});