dialog.js 4.7 KB

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