github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/app/public/codemirror/addon/dialog/dialog.js (about)

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