You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.1KB

  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(dialog, "focusout", function (evt) {
  73. if (evt.relatedTarget !== null) close();
  74. });
  75. } else if (button = dialog.getElementsByTagName("button")[0]) {
  76. CodeMirror.on(button, "click", function() {
  77. close();
  78. me.focus();
  79. });
  80. if (options.closeOnBlur !== false) CodeMirror.on(button, "blur", close);
  81. button.focus();
  82. }
  83. return close;
  84. });
  85. CodeMirror.defineExtension("openConfirm", function(template, callbacks, options) {
  86. closeNotification(this, null);
  87. var dialog = dialogDiv(this, template, options && options.bottom);
  88. var buttons = dialog.getElementsByTagName("button");
  89. var closed = false, me = this, blurring = 1;
  90. function close() {
  91. if (closed) return;
  92. closed = true;
  93. CodeMirror.rmClass(dialog.parentNode, 'dialog-opened');
  94. dialog.parentNode.removeChild(dialog);
  95. me.focus();
  96. }
  97. buttons[0].focus();
  98. for (var i = 0; i < buttons.length; ++i) {
  99. var b = buttons[i];
  100. (function(callback) {
  101. CodeMirror.on(b, "click", function(e) {
  102. CodeMirror.e_preventDefault(e);
  103. close();
  104. if (callback) callback(me);
  105. });
  106. })(callbacks[i]);
  107. CodeMirror.on(b, "blur", function() {
  108. --blurring;
  109. setTimeout(function() { if (blurring <= 0) close(); }, 200);
  110. });
  111. CodeMirror.on(b, "focus", function() { ++blurring; });
  112. }
  113. });
  114. /*
  115. * openNotification
  116. * Opens a notification, that can be closed with an optional timer
  117. * (default 5000ms timer) and always closes on click.
  118. *
  119. * If a notification is opened while another is opened, it will close the
  120. * currently opened one and open the new one immediately.
  121. */
  122. CodeMirror.defineExtension("openNotification", function(template, options) {
  123. closeNotification(this, close);
  124. var dialog = dialogDiv(this, template, options && options.bottom);
  125. var closed = false, doneTimer;
  126. var duration = options && typeof options.duration !== "undefined" ? options.duration : 5000;
  127. function close() {
  128. if (closed) return;
  129. closed = true;
  130. clearTimeout(doneTimer);
  131. CodeMirror.rmClass(dialog.parentNode, 'dialog-opened');
  132. dialog.parentNode.removeChild(dialog);
  133. }
  134. CodeMirror.on(dialog, 'click', function(e) {
  135. CodeMirror.e_preventDefault(e);
  136. close();
  137. });
  138. if (duration)
  139. doneTimer = setTimeout(close, duration);
  140. return close;
  141. });
  142. });