|
509 | 509 | /* Bind mouseup handler to document if this alert has closeOnClick enabled */
|
510 | 510 | if( alert.options.closeOnClick ){
|
511 | 511 |
|
512 |
| - /* Unbind any other mouseup */ |
513 |
| - $(document).off('mouseup'); |
| 512 | + /* Unbind if already exists */ |
| 513 | + $(document).off('mouseup', $.fn.jAlert.onMouseUp); |
514 | 514 |
|
515 | 515 | /* Bind mouseup */
|
516 |
| - $(document).on('mouseup', function(e){ |
517 |
| - |
518 |
| - /* Find top visible jAlert and see if it has closeOnClick enabled */ |
519 |
| - var lastVisibleAlert = $('.jAlert:visible:last'); |
520 |
| - |
521 |
| - if( lastVisibleAlert.options.closeOnClick ) |
522 |
| - { |
523 |
| - lastVisibleAlert.closeAlert(); |
524 |
| - } |
525 |
| - |
526 |
| - }); |
| 516 | + $(document).on('mouseup', $.fn.jAlert.onMouseUp); |
527 | 517 |
|
528 | 518 | }
|
529 | 519 |
|
530 | 520 | /* Bind on keydown handler to document and if esc was pressed, find all visible jAlerts with that close option enabled and close them */
|
531 | 521 | if( alert.options.closeOnEsc ){
|
532 | 522 |
|
533 |
| - $(document).off('keydown'); |
534 |
| - |
535 |
| - $(document).on('keydown', function(e){ |
536 |
| - |
537 |
| - if(e.keyCode === 27){ |
| 523 | + /* Unbind if already exists */ |
| 524 | + $(document).off('keydown', $.fn.jAlert.onEscKeyDown); |
538 | 525 |
|
539 |
| - /* Find top visible jAlert and see if it has closeOnClick enabled */ |
540 |
| - var lastVisibleAlert = $('.jAlert:visible:last'); |
541 |
| - |
542 |
| - if( lastVisibleAlert.options.closeOnEsc ) |
543 |
| - { |
544 |
| - lastVisibleAlert.closeAlert(); |
545 |
| - } |
546 |
| - |
547 |
| - } |
548 |
| - |
549 |
| - }); |
| 526 | + /* Bind keydown */ |
| 527 | + $(document).on('keydown', $.fn.jAlert.onEscKeyDown); |
550 | 528 |
|
551 | 529 | }
|
552 | 530 |
|
|
666 | 644 | return false;
|
667 | 645 | }
|
668 | 646 | }
|
| 647 | + |
| 648 | + /* Mouseup on document */ |
| 649 | + $.fn.jAlert.onMouseUp = function(e){ |
| 650 | + |
| 651 | + /* Find top visible jAlert and see if it has closeOnClick enabled */ |
| 652 | + var lastVisibleAlert = $('.jAlert:visible:last'); |
| 653 | + |
| 654 | + if( lastVisibleAlert.options.closeOnClick ) |
| 655 | + { |
| 656 | + lastVisibleAlert.closeAlert(); |
| 657 | + } |
| 658 | + |
| 659 | + }; |
| 660 | + |
| 661 | + /* Keydown on document (escape key) */ |
| 662 | + $.fn.jAlert.onEscKeyDown = function(e){ |
| 663 | + |
| 664 | + /* Escape = 27 */ |
| 665 | + if(e.keyCode === 27){ |
| 666 | + |
| 667 | + /* Find top visible jAlert and see if it has closeOnClick enabled */ |
| 668 | + var lastVisibleAlert = $('.jAlert:visible:last'); |
| 669 | + |
| 670 | + if( lastVisibleAlert.options.closeOnEsc ) |
| 671 | + { |
| 672 | + lastVisibleAlert.closeAlert(); |
| 673 | + } |
| 674 | + |
| 675 | + } |
| 676 | + |
| 677 | + }; |
669 | 678 |
|
670 | 679 | /* If you're not using the DOM (aka, you're not hiding or showing a specific alert, you can just use $.jAlert */
|
671 | 680 | $.jAlert = function(options){
|
|
0 commit comments