|
| 1 | +/* |
| 2 | + Implement Github like autocomplete mentions |
| 3 | + http://ichord.github.com/At.js |
| 4 | +
|
| 5 | + Copyright (c) 2013 chord.luo@gmail.com |
| 6 | + Licensed under the MIT license. |
| 7 | +*/ |
| 8 | + |
| 9 | + |
| 10 | +/* |
| 11 | +本插件操作 textarea 或者 input 内的插入符 |
| 12 | +只实现了获得插入符在文本框中的位置,我设置 |
| 13 | +插入符的位置. |
| 14 | +*/ |
| 15 | + |
| 16 | + |
| 17 | +(function(){ |
| 18 | +(function(factory){ |
| 19 | +if(typeofdefine==='function'&&define.amd){ |
| 20 | +returndefine(['jquery'],factory); |
| 21 | +}else{ |
| 22 | +returnfactory(window.jQuery); |
| 23 | +} |
| 24 | +})(function($){ |
| 25 | +"use strict"; |
| 26 | +varEditableCaret,InputCaret,Mirror,Utils,discoveryIframeOf,methods,oDocument,oFrame,oWindow,pluginName,setContextBy; |
| 27 | +pluginName='caret'; |
| 28 | +EditableCaret=(function(){ |
| 29 | +functionEditableCaret($inputor){ |
| 30 | +this.$inputor=$inputor; |
| 31 | +this.domInputor=this.$inputor[0]; |
| 32 | +} |
| 33 | + |
| 34 | +EditableCaret.prototype.setPos=function(pos){ |
| 35 | +returnthis.domInputor; |
| 36 | +}; |
| 37 | + |
| 38 | +EditableCaret.prototype.getIEPosition=function(){ |
| 39 | +returnthis.getPosition(); |
| 40 | +}; |
| 41 | + |
| 42 | +EditableCaret.prototype.getPosition=function(){ |
| 43 | +varinputor_offset,offset; |
| 44 | +offset=this.getOffset(); |
| 45 | +if(!oFrame){ |
| 46 | +inputor_offset=this.$inputor.offset(); |
| 47 | +offset.left-=inputor_offset.left; |
| 48 | +offset.top-=inputor_offset.top; |
| 49 | +} |
| 50 | +returnoffset; |
| 51 | +}; |
| 52 | + |
| 53 | +EditableCaret.prototype.getOldIEPos=function(){ |
| 54 | +varpreCaretTextRange,textRange; |
| 55 | +textRange=oDocument.selection.createRange(); |
| 56 | +preCaretTextRange=oDocument.body.createTextRange(); |
| 57 | +preCaretTextRange.moveToElementText(this.domInputor); |
| 58 | +preCaretTextRange.setEndPoint("EndToEnd",textRange); |
| 59 | +returnpreCaretTextRange.text.length; |
| 60 | +}; |
| 61 | + |
| 62 | +EditableCaret.prototype.getPos=function(){ |
| 63 | +varclonedRange,pos,range; |
| 64 | +if(range=this.range()){ |
| 65 | +clonedRange=range.cloneRange(); |
| 66 | +clonedRange.selectNodeContents(this.domInputor); |
| 67 | +clonedRange.setEnd(range.endContainer,range.endOffset); |
| 68 | +pos=clonedRange.toString().length; |
| 69 | +clonedRange.detach(); |
| 70 | +returnpos; |
| 71 | +}elseif(oDocument.selection){ |
| 72 | +returnthis.getOldIEPos(); |
| 73 | +} |
| 74 | +}; |
| 75 | + |
| 76 | +EditableCaret.prototype.getOldIEOffset=function(){ |
| 77 | +varrange,rect; |
| 78 | +range=oDocument.selection.createRange().duplicate(); |
| 79 | +range.moveStart("character",-1); |
| 80 | +rect=range.getBoundingClientRect(); |
| 81 | +return{ |
| 82 | +height:rect.bottom-rect.top, |
| 83 | +left:rect.left, |
| 84 | +top:rect.top |
| 85 | +}; |
| 86 | +}; |
| 87 | + |
| 88 | +EditableCaret.prototype.getOffset=function(pos){ |
| 89 | +varclonedRange,offset,range,rect; |
| 90 | +if(oWindow.getSelection&&(range=this.range())){ |
| 91 | +if(range.endOffset-1<0){ |
| 92 | +returnnull; |
| 93 | +} |
| 94 | +clonedRange=range.cloneRange(); |
| 95 | +clonedRange.setStart(range.endContainer,range.endOffset-1); |
| 96 | +clonedRange.setEnd(range.endContainer,range.endOffset); |
| 97 | +rect=clonedRange.getBoundingClientRect(); |
| 98 | +offset={ |
| 99 | +height:rect.height, |
| 100 | +left:rect.left+rect.width, |
| 101 | +top:rect.top |
| 102 | +}; |
| 103 | +clonedRange.detach(); |
| 104 | +}elseif(oDocument.selection){ |
| 105 | +offset=this.getOldIEOffset(); |
| 106 | +} |
| 107 | +if(offset&&!oFrame){ |
| 108 | +offset.top+=$(oWindow).scrollTop(); |
| 109 | +offset.left+=$(oWindow).scrollLeft(); |
| 110 | +} |
| 111 | +returnoffset; |
| 112 | +}; |
| 113 | + |
| 114 | +EditableCaret.prototype.range=function(){ |
| 115 | +varsel; |
| 116 | +if(!oWindow.getSelection){ |
| 117 | +return; |
| 118 | +} |
| 119 | +sel=oWindow.getSelection(); |
| 120 | +if(sel.rangeCount>0){ |
| 121 | +returnsel.getRangeAt(0); |
| 122 | +}else{ |
| 123 | +returnnull; |
| 124 | +} |
| 125 | +}; |
| 126 | + |
| 127 | +returnEditableCaret; |
| 128 | + |
| 129 | +})(); |
| 130 | +InputCaret=(function(){ |
| 131 | +functionInputCaret($inputor){ |
| 132 | +this.$inputor=$inputor; |
| 133 | +this.domInputor=this.$inputor[0]; |
| 134 | +} |
| 135 | + |
| 136 | +InputCaret.prototype.getIEPos=function(){ |
| 137 | +varendRange,inputor,len,normalizedValue,pos,range,textInputRange; |
| 138 | +inputor=this.domInputor; |
| 139 | +range=oDocument.selection.createRange(); |
| 140 | +pos=0; |
| 141 | +if(range&&range.parentElement()===inputor){ |
| 142 | +normalizedValue=inputor.value.replace(/\r\n/g,"\n"); |
| 143 | +len=normalizedValue.length; |
| 144 | +textInputRange=inputor.createTextRange(); |
| 145 | +textInputRange.moveToBookmark(range.getBookmark()); |
| 146 | +endRange=inputor.createTextRange(); |
| 147 | +endRange.collapse(false); |
| 148 | +if(textInputRange.compareEndPoints("StartToEnd",endRange)>-1){ |
| 149 | +pos=len; |
| 150 | +}else{ |
| 151 | +pos=-textInputRange.moveStart("character",-len); |
| 152 | +} |
| 153 | +} |
| 154 | +returnpos; |
| 155 | +}; |
| 156 | + |
| 157 | +InputCaret.prototype.getPos=function(){ |
| 158 | +if(oDocument.selection){ |
| 159 | +returnthis.getIEPos(); |
| 160 | +}else{ |
| 161 | +returnthis.domInputor.selectionStart; |
| 162 | +} |
| 163 | +}; |
| 164 | + |
| 165 | +InputCaret.prototype.setPos=function(pos){ |
| 166 | +varinputor,range; |
| 167 | +inputor=this.domInputor; |
| 168 | +if(oDocument.selection){ |
| 169 | +range=inputor.createTextRange(); |
| 170 | +range.move("character",pos); |
| 171 | +range.select(); |
| 172 | +}elseif(inputor.setSelectionRange){ |
| 173 | +inputor.setSelectionRange(pos,pos); |
| 174 | +} |
| 175 | +returninputor; |
| 176 | +}; |
| 177 | + |
| 178 | +InputCaret.prototype.getIEOffset=function(pos){ |
| 179 | +varh,textRange,x,y; |
| 180 | +textRange=this.domInputor.createTextRange(); |
| 181 | +pos||(pos=this.getPos()); |
| 182 | +textRange.move('character',pos); |
| 183 | +x=textRange.boundingLeft; |
| 184 | +y=textRange.boundingTop; |
| 185 | +h=textRange.boundingHeight; |
| 186 | +return{ |
| 187 | +left:x, |
| 188 | +top:y, |
| 189 | +height:h |
| 190 | +}; |
| 191 | +}; |
| 192 | + |
| 193 | +InputCaret.prototype.getOffset=function(pos){ |
| 194 | +var$inputor,offset,position; |
| 195 | +$inputor=this.$inputor; |
| 196 | +if(oDocument.selection){ |
| 197 | +offset=this.getIEOffset(pos); |
| 198 | +offset.top+=$(oWindow).scrollTop()+$inputor.scrollTop(); |
| 199 | +offset.left+=$(oWindow).scrollLeft()+$inputor.scrollLeft(); |
| 200 | +returnoffset; |
| 201 | +}else{ |
| 202 | +offset=$inputor.offset(); |
| 203 | +position=this.getPosition(pos); |
| 204 | +returnoffset={ |
| 205 | +left:offset.left+position.left-$inputor.scrollLeft(), |
| 206 | +top:offset.top+position.top-$inputor.scrollTop(), |
| 207 | +height:position.height |
| 208 | +}; |
| 209 | +} |
| 210 | +}; |
| 211 | + |
| 212 | +InputCaret.prototype.getPosition=function(pos){ |
| 213 | +var$inputor,at_rect,end_range,format,html,mirror,start_range; |
| 214 | +$inputor=this.$inputor; |
| 215 | +format=function(value){ |
| 216 | +returnvalue.replace(/</g,'<').replace(/>/g,'>').replace(/`/g,'`').replace(/"/g,'"').replace(/\r\n|\r|\n/g,"<br />"); |
| 217 | +}; |
| 218 | +if(pos===void0){ |
| 219 | +pos=this.getPos(); |
| 220 | +} |
| 221 | +start_range=$inputor.val().slice(0,pos); |
| 222 | +end_range=$inputor.val().slice(pos); |
| 223 | +html="<span style='position: relative; display: inline;'>"+format(start_range)+"</span>"; |
| 224 | +html+="<span id='caret' style='position: relative; display: inline;'>|</span>"; |
| 225 | +html+="<span style='position: relative; display: inline;'>"+format(end_range)+"</span>"; |
| 226 | +mirror=newMirror($inputor); |
| 227 | +returnat_rect=mirror.create(html).rect(); |
| 228 | +}; |
| 229 | + |
| 230 | +InputCaret.prototype.getIEPosition=function(pos){ |
| 231 | +varh,inputorOffset,offset,x,y; |
| 232 | +offset=this.getIEOffset(pos); |
| 233 | +inputorOffset=this.$inputor.offset(); |
| 234 | +x=offset.left-inputorOffset.left; |
| 235 | +y=offset.top-inputorOffset.top; |
| 236 | +h=offset.height; |
| 237 | +return{ |
| 238 | +left:x, |
| 239 | +top:y, |
| 240 | +height:h |
| 241 | +}; |
| 242 | +}; |
| 243 | + |
| 244 | +returnInputCaret; |
| 245 | + |
| 246 | +})(); |
| 247 | +Mirror=(function(){ |
| 248 | +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"]; |
| 249 | + |
| 250 | +functionMirror($inputor){ |
| 251 | +this.$inputor=$inputor; |
| 252 | +} |
| 253 | + |
| 254 | +Mirror.prototype.mirrorCss=function(){ |
| 255 | +varcss, |
| 256 | +_this=this; |
| 257 | +css={ |
| 258 | +position:'absolute', |
| 259 | +left:-9999, |
| 260 | +top:0, |
| 261 | +zIndex:-20000 |
| 262 | +}; |
| 263 | +if(this.$inputor.prop('tagName')==='TEXTAREA'){ |
| 264 | +this.css_attr.push('width'); |
| 265 | +} |
| 266 | +$.each(this.css_attr,function(i,p){ |
| 267 | +returncss[p]=_this.$inputor.css(p); |
| 268 | +}); |
| 269 | +returncss; |
| 270 | +}; |
| 271 | + |
| 272 | +Mirror.prototype.create=function(html){ |
| 273 | +this.$mirror=$('<div></div>'); |
| 274 | +this.$mirror.css(this.mirrorCss()); |
| 275 | +this.$mirror.html(html); |
| 276 | +this.$inputor.after(this.$mirror); |
| 277 | +returnthis; |
| 278 | +}; |
| 279 | + |
| 280 | +Mirror.prototype.rect=function(){ |
| 281 | +var$flag,pos,rect; |
| 282 | +$flag=this.$mirror.find("#caret"); |
| 283 | +pos=$flag.position(); |
| 284 | +rect={ |
| 285 | +left:pos.left, |
| 286 | +top:pos.top, |
| 287 | +height:$flag.height() |
| 288 | +}; |
| 289 | +this.$mirror.remove(); |
| 290 | +returnrect; |
| 291 | +}; |
| 292 | + |
| 293 | +returnMirror; |
| 294 | + |
| 295 | +})(); |
| 296 | +Utils={ |
| 297 | +contentEditable:function($inputor){ |
| 298 | +return!!($inputor[0].contentEditable&&$inputor[0].contentEditable==='true'); |
| 299 | +} |
| 300 | +}; |
| 301 | +methods={ |
| 302 | +pos:function(pos){ |
| 303 | +if(pos||pos===0){ |
| 304 | +returnthis.setPos(pos); |
| 305 | +}else{ |
| 306 | +returnthis.getPos(); |
| 307 | +} |
| 308 | +}, |
| 309 | +position:function(pos){ |
| 310 | +if(oDocument.selection){ |
| 311 | +returnthis.getIEPosition(pos); |
| 312 | +}else{ |
| 313 | +returnthis.getPosition(pos); |
| 314 | +} |
| 315 | +}, |
| 316 | +offset:function(pos){ |
| 317 | +variOffset,offset; |
| 318 | +offset=this.getOffset(pos); |
| 319 | +if(oFrame){ |
| 320 | +iOffset=$(oFrame).offset(); |
| 321 | +offset.top+=iOffset.top; |
| 322 | +offset.left+=iOffset.left; |
| 323 | +} |
| 324 | +returnoffset; |
| 325 | +} |
| 326 | +}; |
| 327 | +oDocument=null; |
| 328 | +oWindow=null; |
| 329 | +oFrame=null; |
| 330 | +setContextBy=function(settings){ |
| 331 | +variframe; |
| 332 | +if(iframe=settings!=null ?settings.iframe :void0){ |
| 333 | +oFrame=iframe; |
| 334 | +oWindow=iframe.contentWindow; |
| 335 | +returnoDocument=iframe.contentDocument||oWindow.document; |
| 336 | +}else{ |
| 337 | +oFrame=void0; |
| 338 | +oWindow=window; |
| 339 | +returnoDocument=document; |
| 340 | +} |
| 341 | +}; |
| 342 | +discoveryIframeOf=function($dom){ |
| 343 | +varerror; |
| 344 | +oDocument=$dom[0].ownerDocument; |
| 345 | +oWindow=oDocument.defaultView||oDocument.parentWindow; |
| 346 | +try{ |
| 347 | +returnoFrame=oWindow.frameElement; |
| 348 | +}catch(_error){ |
| 349 | +error=_error; |
| 350 | +} |
| 351 | +}; |
| 352 | +$.fn.caret=function(method,value,settings){ |
| 353 | +varcaret; |
| 354 | +if(methods[method]){ |
| 355 | +if($.isPlainObject(value)){ |
| 356 | +setContextBy(value); |
| 357 | +value=void0; |
| 358 | +}else{ |
| 359 | +setContextBy(settings); |
| 360 | +} |
| 361 | +caret=Utils.contentEditable(this) ?newEditableCaret(this) :newInputCaret(this); |
| 362 | +returnmethods[method].apply(caret,[value]); |
| 363 | +}else{ |
| 364 | +return$.error("Method "+method+" does not exist on jQuery.caret"); |
| 365 | +} |
| 366 | +}; |
| 367 | +$.fn.caret.EditableCaret=EditableCaret; |
| 368 | +$.fn.caret.InputCaret=InputCaret; |
| 369 | +$.fn.caret.Utils=Utils; |
| 370 | +return$.fn.caret.apis=methods; |
| 371 | +}); |
| 372 | + |
| 373 | +}).call(this); |