dialog.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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. return dialog;
  26. }
  27. function closeNotification(cm, newVal) {
  28. if (cm.state.currentNotificationClose)
  29. cm.state.currentNotificationClose();
  30. cm.state.currentNotificationClose = newVal;
  31. }
  32. CodeMirror.defineExtension("openDialog", function(template, callback, options) {
  33. if (!options) options = {};
  34. closeNotification(this, null);
  35. var dialog = dialogDiv(this, template, options.bottom);
  36. var closed = false, me = this;
  37. function close(newVal) {
  38. if (typeof newVal == 'string') {
  39. inp.value = newVal;
  40. } else {
  41. if (closed) return;
  42. closed = true;
  43. dialog.parentNode.removeChild(dialog);
  44. me.focus();
  45. if (options.onClose) options.onClose(dialog);
  46. }
  47. }
  48. var inp = dialog.getElementsByTagName("input")[0], button;
  49. if (inp) {
  50. if (options.value) {
  51. inp.value = options.value;
  52. inp.select();
  53. }
  54. if (options.onInput)
  55. CodeMirror.on(inp, "input", function(e) { options.onInput(e, inp.value, close);});
  56. if (options.onKeyUp)
  57. CodeMirror.on(inp, "keyup", function(e) {options.onKeyUp(e, inp.value, close);});
  58. CodeMirror.on(inp, "keydown", function(e) {
  59. if (options && options.onKeyDown && options.onKeyDown(e, inp.value, close)) { return; }
  60. if (e.keyCode == 27 || (options.closeOnEnter !== false && e.keyCode == 13)) {
  61. inp.blur();
  62. CodeMirror.e_stop(e);
  63. close();
  64. }
  65. if (e.keyCode == 13) callback(inp.value, e);
  66. });
  67. if (options.closeOnBlur !== false) CodeMirror.on(inp, "blur", close);
  68. inp.focus();
  69. } else if (button = dialog.getElementsByTagName("button")[0]) {
  70. CodeMirror.on(button, "click", function() {
  71. close();
  72. me.focus();
  73. });
  74. if (options.closeOnBlur !== false) CodeMirror.on(button, "blur", close);
  75. button.focus();
  76. }
  77. return close;
  78. });
  79. CodeMirror.defineExtension("openConfirm", function(template, callbacks, options) {
  80. closeNotification(this, null);
  81. var dialog = dialogDiv(this, template, options && options.bottom);
  82. var buttons = dialog.getElementsByTagName("button");
  83. var closed = false, me = this, blurring = 1;
  84. function close() {
  85. if (closed) return;
  86. closed = true;
  87. dialog.parentNode.removeChild(dialog);
  88. me.focus();
  89. }
  90. buttons[0].focus();
  91. for (var i = 0; i < buttons.length; ++i) {
  92. var b = buttons[i];
  93. (function(callback) {
  94. CodeMirror.on(b, "click", function(e) {
  95. CodeMirror.e_preventDefault(e);
  96. close();
  97. if (callback) callback(me);
  98. });
  99. })(callbacks[i]);
  100. CodeMirror.on(b, "blur", function() {
  101. --blurring;
  102. setTimeout(function() { if (blurring <= 0) close(); }, 200);
  103. });
  104. CodeMirror.on(b, "focus", function() { ++blurring; });
  105. }
  106. });
  107. /*
  108. * openNotification
  109. * Opens a notification, that can be closed with an optional timer
  110. * (default 5000ms timer) and always closes on click.
  111. *
  112. * If a notification is opened while another is opened, it will close the
  113. * currently opened one and open the new one immediately.
  114. */
  115. CodeMirror.defineExtension("openNotification", function(template, options) {
  116. closeNotification(this, close);
  117. var dialog = dialogDiv(this, template, options && options.bottom);
  118. var closed = false, doneTimer;
  119. var duration = options && typeof options.duration !== "undefined" ? options.duration : 5000;
  120. function close() {
  121. if (closed) return;
  122. closed = true;
  123. clearTimeout(doneTimer);
  124. dialog.parentNode.removeChild(dialog);
  125. }
  126. CodeMirror.on(dialog, 'click', function(e) {
  127. CodeMirror.e_preventDefault(e);
  128. close();
  129. });
  130. if (duration)
  131. doneTimer = setTimeout(close, duration);
  132. return close;
  133. });
  134. });