| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
( function () { |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
function cancel( context ) { |
| 16 |
if ( context.data.timerID !== null ) { |
| 17 |
clearTimeout( context.data.timerID ); |
| 18 |
} |
| 19 |
if ( typeof context.config.cancel === 'function' ) { |
| 20 |
context.config.cancel.call( context.data.$textbox ); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
function hide( context ) { |
| 30 |
|
| 31 |
context.data.$container.find( '.suggestions-result-current' ).removeClass( 'suggestions-result-current' ); |
| 32 |
|
| 33 |
context.data.$container.hide(); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
function restore( context ) { |
| 45 |
context.data.$textbox.val( context.data.prevText ); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
function special( context ) { |
| 52 |
|
| 53 |
if ( typeof context.config.special.render === 'function' ) { |
| 54 |
|
| 55 |
setTimeout( () => { |
| 56 |
|
| 57 |
const $special = context.data.$container.find( '.suggestions-special' ); |
| 58 |
context.config.special.render.call( $special, context.data.$textbox.val(), context ); |
| 59 |
}, 1 ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
function update( context, delayed ) { |
| 73 |
function maybeFetch() { |
| 74 |
if ( typeof context.config.update.before === 'function' ) { |
| 75 |
context.config.update.before.call( context.data.$textbox ); |
| 76 |
} |
| 77 |
|
| 78 |
const val = context.data.$textbox.val(), |
| 79 |
cache = context.data.cache; |
| 80 |
|
| 81 |
let cacheHit = false; |
| 82 |
|
| 83 |
|
| 84 |
if ( val.length === 0 ) { |
| 85 |
hide( context ); |
| 86 |
context.data.prevText = ''; |
| 87 |
} else if ( |
| 88 |
val !== context.data.prevText || |
| 89 |
|
| 90 |
!context.data.$container.is( ':visible' ) |
| 91 |
) { |
| 92 |
context.data.prevText = val; |
| 93 |
|
| 94 |
if ( context.config.cache && val in cache ) { |
| 95 |
if ( mw.now() - cache[ val ].timestamp < context.config.cacheMaxAge ) { |
| 96 |
context.data.$textbox.suggestions( 'suggestions', cache[ val ].suggestions ); |
| 97 |
if ( typeof context.config.update.after === 'function' ) { |
| 98 |
context.config.update.after.call( context.data.$textbox, cache[ val ].metadata ); |
| 99 |
} |
| 100 |
cacheHit = true; |
| 101 |
} else { |
| 102 |
|
| 103 |
delete cache[ val ]; |
| 104 |
} |
| 105 |
} |
| 106 |
if ( !cacheHit && typeof context.config.fetch === 'function' ) { |
| 107 |
context.config.fetch.call( |
| 108 |
context.data.$textbox, |
| 109 |
val, |
| 110 |
( suggestions, metadata ) => { |
| 111 |
suggestions = suggestions.slice( 0, context.config.maxRows ); |
| 112 |
context.data.$textbox.suggestions( 'suggestions', suggestions ); |
| 113 |
if ( typeof context.config.update.after === 'function' ) { |
| 114 |
context.config.update.after.call( context.data.$textbox, metadata ); |
| 115 |
} |
| 116 |
if ( context.config.cache ) { |
| 117 |
cache[ val ] = { |
| 118 |
suggestions: suggestions, |
| 119 |
metadata: metadata, |
| 120 |
timestamp: mw.now() |
| 121 |
}; |
| 122 |
} |
| 123 |
}, |
| 124 |
context.config.maxRows |
| 125 |
); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
special( context ); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
cancel( context ); |
| 135 |
|
| 136 |
if ( delayed ) { |
| 137 |
|
| 138 |
|
| 139 |
context.data.timerID = setTimeout( maybeFetch, context.config.delay ); |
| 140 |
} else { |
| 141 |
maybeFetch(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
function highlight( context, $result, updateTextbox ) { |
| 153 |
const $selected = context.data.$container.find( '.suggestions-result-current' ); |
| 154 |
if ( !$result.get || $selected.get( 0 ) !== $result.get( 0 ) ) { |
| 155 |
if ( $result === 'prev' ) { |
| 156 |
|
| 157 |
if ( $selected.hasClass( 'suggestions-special' ) ) { |
| 158 |
$result = context.data.$container.find( '.suggestions-result' ).last(); |
| 159 |
} else { |
| 160 |
$result = $selected.prev(); |
| 161 |
|
| 162 |
if ( !( $result.length && $result.hasClass( 'suggestions-result' ) ) ) { |
| 163 |
|
| 164 |
$result = $selected.parents( '.suggestions-results > *' ).prev().find( '.suggestions-result' ).eq( 0 ); |
| 165 |
} |
| 166 |
|
| 167 |
if ( $selected.length === 0 ) { |
| 168 |
|
| 169 |
if ( context.data.$container.find( '.suggestions-special' ).html() !== '' ) { |
| 170 |
$result = context.data.$container.find( '.suggestions-special' ); |
| 171 |
} else { |
| 172 |
$result = context.data.$container.find( '.suggestions-results .suggestions-result' ).last(); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} else if ( $result === 'next' ) { |
| 177 |
if ( $selected.length === 0 ) { |
| 178 |
|
| 179 |
$result = context.data.$container.find( '.suggestions-results .suggestions-result' ).first(); |
| 180 |
if ( $result.length === 0 && context.data.$container.find( '.suggestions-special' ).html() !== '' ) { |
| 181 |
|
| 182 |
$result = context.data.$container.find( '.suggestions-special' ); |
| 183 |
} |
| 184 |
} else { |
| 185 |
$result = $selected.next(); |
| 186 |
|
| 187 |
if ( !( $result.length && $result.hasClass( 'suggestions-result' ) ) ) { |
| 188 |
|
| 189 |
$result = $selected.parents( '.suggestions-results > *' ).next().find( '.suggestions-result' ).eq( 0 ); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
if ( $selected.hasClass( 'suggestions-special' ) ) { |
| 194 |
$result = $( [] ); |
| 195 |
} else if ( |
| 196 |
$result.length === 0 && |
| 197 |
context.data.$container.find( '.suggestions-special' ).html() !== '' |
| 198 |
) { |
| 199 |
|
| 200 |
$result = context.data.$container.find( '.suggestions-special' ); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
$selected.removeClass( 'suggestions-result-current' ); |
| 205 |
$result.addClass( 'suggestions-result-current' ); |
| 206 |
} |
| 207 |
if ( updateTextbox ) { |
| 208 |
if ( $result.length === 0 || $result.is( '.suggestions-special' ) ) { |
| 209 |
restore( context ); |
| 210 |
} else { |
| 211 |
context.data.$textbox.val( $result.data( 'text' ) ); |
| 212 |
|
| 213 |
|
| 214 |
context.data.$textbox.trigger( 'change' ); |
| 215 |
} |
| 216 |
context.data.$textbox.trigger( 'change' ); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
function configure( context, property, value ) { |
| 228 |
|
| 229 |
|
| 230 |
switch ( property ) { |
| 231 |
case 'fetch': |
| 232 |
case 'cancel': |
| 233 |
case 'special': |
| 234 |
case 'result': |
| 235 |
case 'update': |
| 236 |
case '$region': |
| 237 |
case 'expandFrom': |
| 238 |
context.config[ property ] = value; |
| 239 |
break; |
| 240 |
case 'suggestions': |
| 241 |
context.config[ property ] = value; |
| 242 |
|
| 243 |
if ( context.data !== undefined ) { |
| 244 |
if ( context.data.$textbox.val().length === 0 ) { |
| 245 |
|
| 246 |
hide( context ); |
| 247 |
} else { |
| 248 |
|
| 249 |
context.data.$container.show(); |
| 250 |
|
| 251 |
const regionIsFixed = ( function () { |
| 252 |
let $el = context.config.$region; |
| 253 |
do { |
| 254 |
if ( $el.css( 'position' ) === 'fixed' ) { |
| 255 |
return true; |
| 256 |
} |
| 257 |
$el = $( $el[ 0 ].offsetParent ); |
| 258 |
} while ( $el.length ); |
| 259 |
return false; |
| 260 |
}() ); |
| 261 |
const regionPosition = regionIsFixed ? |
| 262 |
context.config.$region[ 0 ].getBoundingClientRect() : |
| 263 |
context.config.$region.offset(); |
| 264 |
const newCSS = { |
| 265 |
position: regionIsFixed ? 'fixed' : 'absolute', |
| 266 |
top: regionPosition.top + context.config.$region.outerHeight(), |
| 267 |
bottom: 'auto', |
| 268 |
width: context.config.$region.outerWidth(), |
| 269 |
height: 'auto' |
| 270 |
}; |
| 271 |
|
| 272 |
|
| 273 |
context.config.expandFrom = ( function ( expandFrom ) { |
| 274 |
const isRTL = $( document.documentElement ).css( 'direction' ) === 'rtl', |
| 275 |
$region = context.config.$region; |
| 276 |
|
| 277 |
|
| 278 |
if ( context.config.positionFromLeft ) { |
| 279 |
expandFrom = 'left'; |
| 280 |
|
| 281 |
|
| 282 |
} else if ( [ 'left', 'right', 'start', 'end', 'auto' ].indexOf( expandFrom ) === -1 ) { |
| 283 |
expandFrom = 'auto'; |
| 284 |
} |
| 285 |
|
| 286 |
if ( expandFrom === 'auto' ) { |
| 287 |
if ( $region.data( 'searchsuggest-expand-dir' ) ) { |
| 288 |
|
| 289 |
expandFrom = $region.data( 'searchsuggest-expand-dir' ); |
| 290 |
} else { |
| 291 |
const regionWidth = $region.outerWidth(); |
| 292 |
const docWidth = $( document ).width(); |
| 293 |
if ( regionWidth > ( 0.85 * docWidth ) ) { |
| 294 |
|
| 295 |
|
| 296 |
expandFrom = 'start'; |
| 297 |
} else { |
| 298 |
|
| 299 |
const regionCenter = regionPosition.left + regionWidth / 2; |
| 300 |
const docCenter = docWidth / 2; |
| 301 |
if ( Math.abs( regionCenter - docCenter ) < ( 0.10 * docCenter ) ) { |
| 302 |
|
| 303 |
|
| 304 |
expandFrom = 'start'; |
| 305 |
} else { |
| 306 |
|
| 307 |
|
| 308 |
expandFrom = regionCenter > docCenter ? 'right' : 'left'; |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
if ( expandFrom === 'start' ) { |
| 315 |
expandFrom = isRTL ? 'right' : 'left'; |
| 316 |
|
| 317 |
} else if ( expandFrom === 'end' ) { |
| 318 |
expandFrom = isRTL ? 'left' : 'right'; |
| 319 |
} |
| 320 |
|
| 321 |
return expandFrom; |
| 322 |
|
| 323 |
}( context.config.expandFrom ) ); |
| 324 |
|
| 325 |
if ( context.config.expandFrom === 'left' ) { |
| 326 |
|
| 327 |
newCSS.left = regionPosition.left; |
| 328 |
newCSS.right = 'auto'; |
| 329 |
} else { |
| 330 |
|
| 331 |
newCSS.left = 'auto'; |
| 332 |
newCSS.right = document.documentElement.clientWidth - |
| 333 |
( regionPosition.left + context.config.$region.outerWidth() ); |
| 334 |
} |
| 335 |
|
| 336 |
context.data.$container.css( newCSS ); |
| 337 |
const $results = context.data.$container.children( '.suggestions-results' ); |
| 338 |
$results.empty(); |
| 339 |
let expWidth = -1; |
| 340 |
for ( let i = 0; i < context.config.suggestions.length; i++ ) { |
| 341 |
const text = context.config.suggestions[ i ]; |
| 342 |
const $result = $( '<div>' ) |
| 343 |
.addClass( 'suggestions-result' ) |
| 344 |
.attr( 'rel', i ) |
| 345 |
.data( 'text', context.config.suggestions[ i ] ) |
| 346 |
.on( 'mousemove', function () { |
| 347 |
context.data.selectedWithMouse = true; |
| 348 |
highlight( |
| 349 |
context, |
| 350 |
$( this ).closest( '.suggestions-results .suggestions-result' ), |
| 351 |
false |
| 352 |
); |
| 353 |
} ) |
| 354 |
.appendTo( $results ); |
| 355 |
|
| 356 |
if ( typeof context.config.result.render === 'function' ) { |
| 357 |
context.config.result.render.call( $result, context.config.suggestions[ i ], context ); |
| 358 |
} else { |
| 359 |
$result.text( text ); |
| 360 |
} |
| 361 |
|
| 362 |
if ( context.config.highlightInput ) { |
| 363 |
$result.highlightText( context.data.prevText, { method: 'prefixPlusComboHighlight' } ); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
const $spanForWidth = $result.wrapInner( '<span>' ).children(); |
| 380 |
const childrenWidth = $spanForWidth.css( 'position', 'absolute' ).outerWidth(); |
| 381 |
$spanForWidth.contents().unwrap(); |
| 382 |
|
| 383 |
if ( childrenWidth > $result.width() && childrenWidth > expWidth ) { |
| 384 |
|
| 385 |
expWidth = childrenWidth + ( context.data.$container.width() - $result.width() ); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
if ( expWidth > context.data.$container.width() ) { |
| 391 |
const maxWidth = context.config.maxExpandFactor * context.data.$textbox.width(); |
| 392 |
context.data.$container.width( Math.min( expWidth, maxWidth ) ); |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
break; |
| 397 |
case 'maxRows': |
| 398 |
context.config[ property ] = Math.max( 1, Math.min( 100, value ) ); |
| 399 |
break; |
| 400 |
case 'delay': |
| 401 |
context.config[ property ] = Math.max( 0, Math.min( 1200, value ) ); |
| 402 |
break; |
| 403 |
case 'cacheMaxAge': |
| 404 |
context.config[ property ] = Math.max( 1, value ); |
| 405 |
break; |
| 406 |
case 'maxExpandFactor': |
| 407 |
context.config[ property ] = Math.max( 1, value ); |
| 408 |
break; |
| 409 |
case 'cache': |
| 410 |
case 'submitOnClick': |
| 411 |
case 'positionFromLeft': |
| 412 |
case 'highlightInput': |
| 413 |
context.config[ property ] = !!value; |
| 414 |
break; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
function keypress( e, context, key ) { |
| 426 |
|
| 427 |
const wasVisible = context.data.$container.is( ':visible' ); |
| 428 |
let preventDefault = false; |
| 429 |
|
| 430 |
switch ( key ) { |
| 431 |
|
| 432 |
case 40: |
| 433 |
if ( wasVisible ) { |
| 434 |
highlight( context, 'next', true ); |
| 435 |
context.data.selectedWithMouse = false; |
| 436 |
} else { |
| 437 |
update( context, false ); |
| 438 |
} |
| 439 |
preventDefault = true; |
| 440 |
break; |
| 441 |
|
| 442 |
case 38: |
| 443 |
if ( wasVisible ) { |
| 444 |
highlight( context, 'prev', true ); |
| 445 |
context.data.selectedWithMouse = false; |
| 446 |
} |
| 447 |
preventDefault = wasVisible; |
| 448 |
break; |
| 449 |
|
| 450 |
case 27: |
| 451 |
hide( context ); |
| 452 |
restore( context ); |
| 453 |
cancel( context ); |
| 454 |
context.data.$textbox.trigger( 'change' ); |
| 455 |
preventDefault = wasVisible; |
| 456 |
break; |
| 457 |
|
| 458 |
case 13: { |
| 459 |
preventDefault = wasVisible; |
| 460 |
const $selected = context.data.$container.find( '.suggestions-result-current' ); |
| 461 |
hide( context ); |
| 462 |
if ( $selected.length === 0 || context.data.selectedWithMouse ) { |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
cancel( context ); |
| 467 |
preventDefault = false; |
| 468 |
} else if ( $selected.is( '.suggestions-special' ) ) { |
| 469 |
if ( typeof context.config.special.select === 'function' ) { |
| 470 |
|
| 471 |
if ( context.config.special.select.call( $selected, context.data.$textbox, 'keyboard' ) === true ) { |
| 472 |
preventDefault = false; |
| 473 |
} |
| 474 |
} |
| 475 |
} else { |
| 476 |
if ( typeof context.config.result.select === 'function' ) { |
| 477 |
|
| 478 |
if ( context.config.result.select.call( $selected, context.data.$textbox, 'keyboard' ) === true ) { |
| 479 |
preventDefault = false; |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
break; |
| 484 |
} |
| 485 |
default: |
| 486 |
update( context, true ); |
| 487 |
break; |
| 488 |
} |
| 489 |
if ( preventDefault ) { |
| 490 |
e.preventDefault(); |
| 491 |
e.stopPropagation(); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
$.fn.suggestions = function () { |
| 585 |
|
| 586 |
const args = arguments; |
| 587 |
|
| 588 |
$( this ).each( function () { |
| 589 |
|
| 590 |
|
| 591 |
let context = $( this ).data( 'suggestions-context' ); |
| 592 |
if ( context === undefined || context === null ) { |
| 593 |
context = { |
| 594 |
config: { |
| 595 |
fetch: function () {}, |
| 596 |
cancel: function () {}, |
| 597 |
special: {}, |
| 598 |
result: {}, |
| 599 |
update: {}, |
| 600 |
$region: $( this ), |
| 601 |
suggestions: [], |
| 602 |
maxRows: 10, |
| 603 |
delay: 120, |
| 604 |
cache: false, |
| 605 |
cacheMaxAge: 60000, |
| 606 |
submitOnClick: false, |
| 607 |
maxExpandFactor: 3, |
| 608 |
expandFrom: 'auto', |
| 609 |
highlightInput: false |
| 610 |
} |
| 611 |
}; |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
if ( args.length > 0 ) { |
| 618 |
if ( typeof args[ 0 ] === 'object' ) { |
| 619 |
|
| 620 |
for ( const key in args[ 0 ] ) { |
| 621 |
configure( context, key, args[ 0 ][ key ] ); |
| 622 |
} |
| 623 |
} else if ( typeof args[ 0 ] === 'string' ) { |
| 624 |
if ( args.length > 1 ) { |
| 625 |
|
| 626 |
configure( context, args[ 0 ], args[ 1 ] ); |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
if ( context.data === undefined ) { |
| 634 |
context.data = { |
| 635 |
|
| 636 |
timerID: null, |
| 637 |
|
| 638 |
|
| 639 |
prevText: null, |
| 640 |
|
| 641 |
|
| 642 |
cache: Object.create( null ), |
| 643 |
|
| 644 |
|
| 645 |
visibleResults: 0, |
| 646 |
|
| 647 |
|
| 648 |
$mouseDownOn: $( [] ), |
| 649 |
$textbox: $( this ), |
| 650 |
selectedWithMouse: false |
| 651 |
}; |
| 652 |
|
| 653 |
context.data.$container = $( '<div>' ) |
| 654 |
.css( 'display', 'none' ) |
| 655 |
.addClass( 'suggestions' ) |
| 656 |
.append( |
| 657 |
$( '<div>' ).addClass( 'suggestions-results' ) |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
.on( 'mousedown', ( e ) => { |
| 662 |
context.data.$mouseDownOn = $( e.target ).closest( '.suggestions-results .suggestions-result' ); |
| 663 |
} ) |
| 664 |
.on( 'mouseup', ( e ) => { |
| 665 |
const $result = $( e.target ).closest( '.suggestions-results .suggestions-result' ), |
| 666 |
$other = context.data.$mouseDownOn; |
| 667 |
|
| 668 |
context.data.$mouseDownOn = $( [] ); |
| 669 |
if ( $result.get( 0 ) !== $other.get( 0 ) ) { |
| 670 |
return; |
| 671 |
} |
| 672 |
highlight( context, $result, true ); |
| 673 |
if ( typeof context.config.result.select === 'function' ) { |
| 674 |
context.config.result.select.call( $result, context.data.$textbox, 'mouse' ); |
| 675 |
} |
| 676 |
|
| 677 |
if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) { |
| 678 |
|
| 679 |
|
| 680 |
setTimeout( () => { |
| 681 |
hide( context ); |
| 682 |
} ); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
context.data.$textbox.trigger( 'focus' ); |
| 687 |
} ) |
| 688 |
) |
| 689 |
.append( |
| 690 |
$( '<div>' ).addClass( 'suggestions-special' ) |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
.on( 'mousedown', ( e ) => { |
| 695 |
context.data.$mouseDownOn = $( e.target ).closest( '.suggestions-special' ); |
| 696 |
} ) |
| 697 |
.on( 'mouseup', ( e ) => { |
| 698 |
const $special = $( e.target ).closest( '.suggestions-special' ), |
| 699 |
$other = context.data.$mouseDownOn; |
| 700 |
|
| 701 |
context.data.$mouseDownOn = $( [] ); |
| 702 |
if ( $special.get( 0 ) !== $other.get( 0 ) ) { |
| 703 |
return; |
| 704 |
} |
| 705 |
if ( typeof context.config.special.select === 'function' ) { |
| 706 |
context.config.special.select.call( $special, context.data.$textbox, 'mouse' ); |
| 707 |
} |
| 708 |
|
| 709 |
if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) { |
| 710 |
|
| 711 |
|
| 712 |
setTimeout( () => { |
| 713 |
hide( context ); |
| 714 |
} ); |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
context.data.$textbox.trigger( 'focus' ); |
| 719 |
} ) |
| 720 |
.on( 'mousemove', ( e ) => { |
| 721 |
context.data.selectedWithMouse = true; |
| 722 |
highlight( |
| 723 |
context, $( e.target ).closest( '.suggestions-special' ), false |
| 724 |
); |
| 725 |
} ) |
| 726 |
) |
| 727 |
.appendTo( document.body ); |
| 728 |
|
| 729 |
$( this ) |
| 730 |
|
| 731 |
.attr( 'autocomplete', 'off' ) |
| 732 |
.on( 'keydown', ( e ) => { |
| 733 |
|
| 734 |
context.data.keypressed = e.which; |
| 735 |
context.data.keypressedCount = 0; |
| 736 |
} ) |
| 737 |
.on( 'keypress', ( e ) => { |
| 738 |
context.data.keypressedCount++; |
| 739 |
keypress( e, context, context.data.keypressed ); |
| 740 |
} ) |
| 741 |
.on( 'keyup', ( e ) => { |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
const allowed = [ |
| 748 |
40, |
| 749 |
38, |
| 750 |
27, |
| 751 |
13, |
| 752 |
46, |
| 753 |
8 |
| 754 |
]; |
| 755 |
if ( context.data.keypressedCount === 0 && |
| 756 |
e.which === context.data.keypressed && |
| 757 |
allowed.indexOf( e.which ) !== -1 |
| 758 |
) { |
| 759 |
keypress( e, context, context.data.keypressed ); |
| 760 |
} |
| 761 |
} ) |
| 762 |
.on( 'blur', () => { |
| 763 |
|
| 764 |
|
| 765 |
if ( context.data.$mouseDownOn.length > 0 ) { |
| 766 |
return; |
| 767 |
} |
| 768 |
hide( context ); |
| 769 |
cancel( context ); |
| 770 |
} ); |
| 771 |
|
| 772 |
|
| 773 |
if ( $( this ).is( ':focus' ) && this.value !== this.defaultValue ) { |
| 774 |
update( context, false ); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
$( this ).data( 'suggestions-context', context ); |
| 780 |
} ); |
| 781 |
return this; |
| 782 |
}; |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
}() ); |