|
211 | 211 | last: function() { return widget.last(i) ;}, |
212 | 212 | selected: function() { return widget.selected(i) ;}, |
213 | 213 | open: function() { return widget.open(i) ;}, |
| 214 | + reposition: function () { return widget.reposition(i); }, |
214 | 215 | close: function() { return widget.close(i) ;}, |
215 | 216 | closed: function() { return widget.closed(i) ;}, |
216 | 217 | destroy: function() { return widget.destroy(i) ;}, |
|
397 | 398 | i.element.data('timepicker-user-clicked-outside', true).blur(); |
398 | 399 | } |
399 | 400 | }); |
| 401 | + // make sure we reposition the timepicker when the viewport changes |
| 402 | + $(window).bind('resize.timepicker-' + i.element.data('timepicker-event-namespace'), i.reposition); |
| 403 | + // use vanilla javascript to be able to use event capturing instead of bubbling: otherwise we have to listen |
| 404 | + // on all parent containers. |
| 405 | + window.addEventListener('scroll', i.reposition, true); |
400 | 406 |
|
401 | 407 | // if a date is already selected and options.dynamic is true, |
402 | 408 | // arrange the items in the list so the first item is |
|
517 | 523 | return i.element; |
518 | 524 | }, |
519 | 525 |
|
| 526 | + reposition: function (i) { |
| 527 | + var widget = this; |
| 528 | + var containerDecorationHeight = widget.container.outerHeight() - widget.container.height(), |
| 529 | + zindex = i.options.zindex ? i.options.zindex : i.element.offsetParent().css( 'z-index' ), |
| 530 | + elementOffset = i.element.offset(); |
| 531 | + |
| 532 | + // position the container right below the element, or as close to as possible. |
| 533 | + widget.container.css( { |
| 534 | + top: elementOffset.top + i.element.outerHeight(), |
| 535 | + left: elementOffset.left |
| 536 | + } ); |
| 537 | + |
| 538 | + // then show the container so that the browser can consider the timepicker's |
| 539 | + // height to calculate the page's total height and decide if adding scrollbars |
| 540 | + // is necessary. |
| 541 | + widget.container.show(); |
| 542 | + |
| 543 | + // now we need to calculate the element offset and position the container again. |
| 544 | + // If the browser added scrollbars, the container's original position is not aligned |
| 545 | + // with the element's final position. This step fixes that problem. |
| 546 | + widget.container.css( { |
| 547 | + left: i.element.offset().left, |
| 548 | + height: widget.ui.outerHeight() + containerDecorationHeight, |
| 549 | + width: i.element.outerWidth(), |
| 550 | + zIndex: zindex, |
| 551 | + cursor: 'default' |
| 552 | + } ); |
| 553 | + |
| 554 | + |
| 555 | + var calculatedWidth = widget.container.width() - ( widget.ui.outerWidth() - widget.ui.width() ); |
| 556 | + |
| 557 | + // hardcode ui, viewport and item's width. I couldn't get it to work using CSS only |
| 558 | + widget.ui.css( { width: calculatedWidth } ); |
| 559 | + widget.viewport.css( { width: calculatedWidth } ); |
| 560 | + i.items.css( { width: calculatedWidth } ); |
| 561 | + |
| 562 | + |
| 563 | + return i.element; |
| 564 | + }, |
| 565 | + |
520 | 566 | close: function(i) { |
521 | 567 | var widget = this; |
522 | 568 |
|
|
530 | 576 | 'keydown.timepicker-' + i.element.data('timepicker-event-namespace') + ' ' + |
531 | 577 | 'keyup.timepicker-' + i.element.data('timepicker-event-namespace')); |
532 | 578 |
|
| 579 | + $(window).unbind('resize.timepicker-' + i.element.data('timepicker-event-namespace')); |
| 580 | + window.removeEventListener('scroll', i.reposition, true); |
| 581 | + |
533 | 582 | return i.element; |
534 | 583 | }, |
535 | 584 |
|
|
669 | 718 | 'next', |
670 | 719 | 'previous', |
671 | 720 | 'open', |
| 721 | + 'reposition', |
672 | 722 | 'close', |
673 | 723 | 'destroy', |
674 | 724 | 'setTime' |
|
0 commit comments