[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/js/vendor/ -> jquery.caret.js (source)

   1  (function (root, factory) {
   2    if (typeof define === 'function' && define.amd) {
   3      // AMD. Register as an anonymous module.
   4      define(["jquery"], function ($) {
   5        return (root.returnExportsGlobal = factory($));
   6      });
   7    } else if (typeof exports === 'object') {
   8      // Node. Does not work with strict CommonJS, but
   9      // only CommonJS-like environments that support module.exports,
  10      // like Node.
  11      module.exports = factory(require("jquery"));
  12    } else {
  13      factory(jQuery);
  14    }
  15  }(this, function ($) {
  16  
  17  /*
  18    Implement Github like autocomplete mentions
  19    http://ichord.github.com/At.js
  20  
  21    Copyright (c) 2013 chord.luo@gmail.com
  22    Licensed under the MIT license.
  23  */
  24  
  25  /*
  26  本插件操作 textarea 或者 input 内的插入符
  27  只实现了获得插入符在文本框中的位置,我设置
  28  插入符的位置.
  29  */
  30  
  31  "use strict";
  32  var EditableCaret, InputCaret, Mirror, Utils, discoveryIframeOf, methods, oDocument, oFrame, oWindow, pluginName, setContextBy;
  33  
  34  pluginName = 'caret';
  35  
  36  EditableCaret = (function() {
  37    function EditableCaret($inputor) {
  38      this.$inputor = $inputor;
  39      this.domInputor = this.$inputor[0];
  40    }
  41  
  42    EditableCaret.prototype.setPos = function(pos) {
  43      var fn, found, offset, sel;
  44      if (sel = oWindow.getSelection()) {
  45        offset = 0;
  46        found = false;
  47        (fn = function(pos, parent) {
  48          var node, range, _i, _len, _ref, _results;
  49          _ref = parent.childNodes;
  50          _results = [];
  51          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  52            node = _ref[_i];
  53            if (found) {
  54              break;
  55            }
  56            if (node.nodeType === 3) {
  57              if (offset + node.length >= pos) {
  58                found = true;
  59                range = oDocument.createRange();
  60                range.setStart(node, pos - offset);
  61                sel.removeAllRanges();
  62                sel.addRange(range);
  63                break;
  64              } else {
  65                _results.push(offset += node.length);
  66              }
  67            } else {
  68              _results.push(fn(pos, node));
  69            }
  70          }
  71          return _results;
  72        })(pos, this.domInputor);
  73      }
  74      return this.domInputor;
  75    };
  76  
  77    EditableCaret.prototype.getIEPosition = function() {
  78      return this.getPosition();
  79    };
  80  
  81    EditableCaret.prototype.getPosition = function() {
  82      var inputor_offset, offset;
  83      offset = this.getOffset();
  84      inputor_offset = this.$inputor.offset();
  85      offset.left -= inputor_offset.left;
  86      offset.top -= inputor_offset.top;
  87      return offset;
  88    };
  89  
  90    EditableCaret.prototype.getOldIEPos = function() {
  91      var preCaretTextRange, textRange;
  92      textRange = oDocument.selection.createRange();
  93      preCaretTextRange = oDocument.body.createTextRange();
  94      preCaretTextRange.moveToElementText(this.domInputor);
  95      preCaretTextRange.setEndPoint("EndToEnd", textRange);
  96      return preCaretTextRange.text.length;
  97    };
  98  
  99    EditableCaret.prototype.getPos = function() {
 100      var clonedRange, pos, range;
 101      if (range = this.range()) {
 102        clonedRange = range.cloneRange();
 103        clonedRange.selectNodeContents(this.domInputor);
 104        clonedRange.setEnd(range.endContainer, range.endOffset);
 105        pos = clonedRange.toString().length;
 106        clonedRange.detach();
 107        return pos;
 108      } else if (oDocument.selection) {
 109        return this.getOldIEPos();
 110      }
 111    };
 112  
 113    EditableCaret.prototype.getOldIEOffset = function() {
 114      var range, rect;
 115      range = oDocument.selection.createRange().duplicate();
 116      range.moveStart("character", -1);
 117      rect = range.getBoundingClientRect();
 118      return {
 119        height: rect.bottom - rect.top,
 120        left: rect.left,
 121        top: rect.top
 122      };
 123    };
 124  
 125    EditableCaret.prototype.getOffset = function(pos) {
 126      var clonedRange, offset, range, rect, shadowCaret;
 127      if (oWindow.getSelection && (range = this.range())) {
 128        if (range.endOffset - 1 > 0 && range.endContainer !== this.domInputor) {
 129          clonedRange = range.cloneRange();
 130          clonedRange.setStart(range.endContainer, range.endOffset - 1);
 131          clonedRange.setEnd(range.endContainer, range.endOffset);
 132          rect = clonedRange.getBoundingClientRect();
 133          offset = {
 134            height: rect.height,
 135            left: rect.left + rect.width,
 136            top: rect.top
 137          };
 138          clonedRange.detach();
 139        }
 140        if (!offset || (offset != null ? offset.height : void 0) === 0) {
 141          clonedRange = range.cloneRange();
 142          shadowCaret = $(oDocument.createTextNode("|"));
 143          clonedRange.insertNode(shadowCaret[0]);
 144          clonedRange.selectNode(shadowCaret[0]);
 145          rect = clonedRange.getBoundingClientRect();
 146          offset = {
 147            height: rect.height,
 148            left: rect.left,
 149            top: rect.top
 150          };
 151          shadowCaret.remove();
 152          clonedRange.detach();
 153        }
 154      } else if (oDocument.selection) {
 155        offset = this.getOldIEOffset();
 156      }
 157      if (offset) {
 158        offset.top += $(oWindow).scrollTop();
 159        offset.left += $(oWindow).scrollLeft();
 160      }
 161      return offset;
 162    };
 163  
 164    EditableCaret.prototype.range = function() {
 165      var sel;
 166      if (!oWindow.getSelection) {
 167        return;
 168      }
 169      sel = oWindow.getSelection();
 170      if (sel.rangeCount > 0) {
 171        return sel.getRangeAt(0);
 172      } else {
 173        return null;
 174      }
 175    };
 176  
 177    return EditableCaret;
 178  
 179  })();
 180  
 181  InputCaret = (function() {
 182    function InputCaret($inputor) {
 183      this.$inputor = $inputor;
 184      this.domInputor = this.$inputor[0];
 185    }
 186  
 187    InputCaret.prototype.getIEPos = function() {
 188      var endRange, inputor, len, normalizedValue, pos, range, textInputRange;
 189      inputor = this.domInputor;
 190      range = oDocument.selection.createRange();
 191      pos = 0;
 192      if (range && range.parentElement() === inputor) {
 193        normalizedValue = inputor.value.replace(/\r\n/g, "\n");
 194        len = normalizedValue.length;
 195        textInputRange = inputor.createTextRange();
 196        textInputRange.moveToBookmark(range.getBookmark());
 197        endRange = inputor.createTextRange();
 198        endRange.collapse(false);
 199        if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
 200          pos = len;
 201        } else {
 202          pos = -textInputRange.moveStart("character", -len);
 203        }
 204      }
 205      return pos;
 206    };
 207  
 208    InputCaret.prototype.getPos = function() {
 209      if (oDocument.selection) {
 210        return this.getIEPos();
 211      } else {
 212        return this.domInputor.selectionStart;
 213      }
 214    };
 215  
 216    InputCaret.prototype.setPos = function(pos) {
 217      var inputor, range;
 218      inputor = this.domInputor;
 219      if (oDocument.selection) {
 220        range = inputor.createTextRange();
 221        range.move("character", pos);
 222        range.select();
 223      } else if (inputor.setSelectionRange) {
 224        inputor.setSelectionRange(pos, pos);
 225      }
 226      return inputor;
 227    };
 228  
 229    InputCaret.prototype.getIEOffset = function(pos) {
 230      var h, textRange, x, y;
 231      textRange = this.domInputor.createTextRange();
 232      pos || (pos = this.getPos());
 233      textRange.move('character', pos);
 234      x = textRange.boundingLeft;
 235      y = textRange.boundingTop;
 236      h = textRange.boundingHeight;
 237      return {
 238        left: x,
 239        top: y,
 240        height: h
 241      };
 242    };
 243  
 244    InputCaret.prototype.getOffset = function(pos) {
 245      var $inputor, offset, position;
 246      $inputor = this.$inputor;
 247      if (oDocument.selection) {
 248        offset = this.getIEOffset(pos);
 249        offset.top += $(oWindow).scrollTop() + $inputor.scrollTop();
 250        offset.left += $(oWindow).scrollLeft() + $inputor.scrollLeft();
 251        return offset;
 252      } else {
 253        offset = $inputor.offset();
 254        position = this.getPosition(pos);
 255        return offset = {
 256          left: offset.left + position.left - $inputor.scrollLeft(),
 257          top: offset.top + position.top - $inputor.scrollTop(),
 258          height: position.height
 259        };
 260      }
 261    };
 262  
 263    InputCaret.prototype.getPosition = function(pos) {
 264      var $inputor, at_rect, end_range, format, html, mirror, start_range;
 265      $inputor = this.$inputor;
 266      format = function(value) {
 267        value = value.replace(/<|>|`|"|&/g, '?').replace(/\r\n|\r|\n/g, "<br/>");
 268        if (/firefox/i.test(navigator.userAgent)) {
 269          value = value.replace(/\s/g, '&nbsp;');
 270        }
 271        return value;
 272      };
 273      if (pos === void 0) {
 274        pos = this.getPos();
 275      }
 276      start_range = $inputor.val().slice(0, pos);
 277      end_range = $inputor.val().slice(pos);
 278      html = "<span style='position: relative; display: inline;'>" + format(start_range) + "</span>";
 279      html += "<span id='caret' style='position: relative; display: inline;'>|</span>";
 280      html += "<span style='position: relative; display: inline;'>" + format(end_range) + "</span>";
 281      mirror = new Mirror($inputor);
 282      return at_rect = mirror.create(html).rect();
 283    };
 284  
 285    InputCaret.prototype.getIEPosition = function(pos) {
 286      var h, inputorOffset, offset, x, y;
 287      offset = this.getIEOffset(pos);
 288      inputorOffset = this.$inputor.offset();
 289      x = offset.left - inputorOffset.left;
 290      y = offset.top - inputorOffset.top;
 291      h = offset.height;
 292      return {
 293        left: x,
 294        top: y,
 295        height: h
 296      };
 297    };
 298  
 299    return InputCaret;
 300  
 301  })();
 302  
 303  Mirror = (function() {
 304    Mirror.prototype.css_attr = ["borderBottomWidth", "borderLeftWidth", "borderRightWidth", "borderTopStyle", "borderRightStyle", "borderBottomStyle", "borderLeftStyle", "borderTopWidth", "boxSizing", "fontFamily", "fontSize", "fontWeight", "height", "letterSpacing", "lineHeight", "marginBottom", "marginLeft", "marginRight", "marginTop", "outlineWidth", "overflow", "overflowX", "overflowY", "paddingBottom", "paddingLeft", "paddingRight", "paddingTop", "textAlign", "textOverflow", "textTransform", "whiteSpace", "wordBreak", "wordWrap"];
 305  
 306    function Mirror($inputor) {
 307      this.$inputor = $inputor;
 308    }
 309  
 310    Mirror.prototype.mirrorCss = function() {
 311      var css,
 312        _this = this;
 313      css = {
 314        position: 'absolute',
 315        left: -9999,
 316        top: 0,
 317        zIndex: -20000
 318      };
 319      if (this.$inputor.prop('tagName') === 'TEXTAREA') {
 320        this.css_attr.push('width');
 321      }
 322      $.each(this.css_attr, function(i, p) {
 323        return css[p] = _this.$inputor.css(p);
 324      });
 325      return css;
 326    };
 327  
 328    Mirror.prototype.create = function(html) {
 329      this.$mirror = $('<div></div>');
 330      this.$mirror.css(this.mirrorCss());
 331      this.$mirror.html(html);
 332      this.$inputor.after(this.$mirror);
 333      return this;
 334    };
 335  
 336    Mirror.prototype.rect = function() {
 337      var $flag, pos, rect;
 338      $flag = this.$mirror.find("#caret");
 339      pos = $flag.position();
 340      rect = {
 341        left: pos.left,
 342        top: pos.top,
 343        height: $flag.height()
 344      };
 345      this.$mirror.remove();
 346      return rect;
 347    };
 348  
 349    return Mirror;
 350  
 351  })();
 352  
 353  Utils = {
 354    contentEditable: function($inputor) {
 355      return !!($inputor[0].contentEditable && $inputor[0].contentEditable === 'true');
 356    }
 357  };
 358  
 359  methods = {
 360    pos: function(pos) {
 361      if (pos || pos === 0) {
 362        return this.setPos(pos);
 363      } else {
 364        return this.getPos();
 365      }
 366    },
 367    position: function(pos) {
 368      if (oDocument.selection) {
 369        return this.getIEPosition(pos);
 370      } else {
 371        return this.getPosition(pos);
 372      }
 373    },
 374    offset: function(pos) {
 375      var offset;
 376      offset = this.getOffset(pos);
 377      return offset;
 378    }
 379  };
 380  
 381  oDocument = null;
 382  
 383  oWindow = null;
 384  
 385  oFrame = null;
 386  
 387  setContextBy = function(settings) {
 388    var iframe;
 389    if (iframe = settings != null ? settings.iframe : void 0) {
 390      oFrame = iframe;
 391      oWindow = iframe.contentWindow;
 392      return oDocument = iframe.contentDocument || oWindow.document;
 393    } else {
 394      oFrame = void 0;
 395      oWindow = window;
 396      return oDocument = document;
 397    }
 398  };
 399  
 400  discoveryIframeOf = function($dom) {
 401    var error;
 402    oDocument = $dom[0].ownerDocument;
 403    oWindow = oDocument.defaultView || oDocument.parentWindow;
 404    try {
 405      return oFrame = oWindow.frameElement;
 406    } catch (_error) {
 407      error = _error;
 408    }
 409  };
 410  
 411  $.fn.caret = function(method, value, settings) {
 412    var caret;
 413    if (methods[method]) {
 414      if ($.isPlainObject(value)) {
 415        setContextBy(value);
 416        value = void 0;
 417      } else {
 418        setContextBy(settings);
 419      }
 420      caret = Utils.contentEditable(this) ? new EditableCaret(this) : new InputCaret(this);
 421      return methods[method].apply(caret, [value]);
 422    } else {
 423      return $.error("Method " + method + " does not exist on jQuery.caret");
 424    }
 425  };
 426  
 427  $.fn.caret.EditableCaret = EditableCaret;
 428  
 429  $.fn.caret.InputCaret = InputCaret;
 430  
 431  $.fn.caret.Utils = Utils;
 432  
 433  $.fn.caret.apis = methods;
 434  
 435  
 436  }));


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1