dialog.js 5.1 KB

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