dialog.js 4.3 KB

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