| 1 |
let newTopicButton, ledeSectionDialog; |
| 2 |
let viewportScrollContainer = null; |
| 3 |
let wasKeyboardOpen = null; |
| 4 |
let initialClientHeight = null; |
| 5 |
|
| 6 |
const isIos = /ipad|iphone|ipod/i.test( navigator.userAgent ); |
| 7 |
|
| 8 |
$( document.body ).toggleClass( 'ext-discussiontools-init-ios', isIos ); |
| 9 |
|
| 10 |
function onViewportChange() { |
| 11 |
let isKeyboardOpen; |
| 12 |
|
| 13 |
if ( isIos ) { |
| 14 |
isKeyboardOpen = visualViewport.height < viewportScrollContainer.clientHeight; |
| 15 |
} else { |
| 16 |
|
| 17 |
isKeyboardOpen = viewportScrollContainer.clientHeight < initialClientHeight; |
| 18 |
} |
| 19 |
|
| 20 |
if ( isKeyboardOpen !== wasKeyboardOpen ) { |
| 21 |
$( document.body ).toggleClass( 'ext-discussiontools-init-virtual-keyboard-open', isKeyboardOpen ); |
| 22 |
} |
| 23 |
|
| 24 |
wasKeyboardOpen = isKeyboardOpen; |
| 25 |
} |
| 26 |
|
| 27 |
function init( $container ) { |
| 28 |
|
| 29 |
$container.find( '.section-heading' ).attr( 'data-event-name', 'talkpage.section' ); |
| 30 |
|
| 31 |
|
| 32 |
if ( !viewportScrollContainer && window.visualViewport ) { |
| 33 |
viewportScrollContainer = OO.ui.Element.static.getClosestScrollableContainer( document.body ); |
| 34 |
initialClientHeight = viewportScrollContainer.clientHeight; |
| 35 |
const onViewportChangeThrottled = OO.ui.throttle( onViewportChange, 100 ); |
| 36 |
$( visualViewport ).on( 'resize', onViewportChangeThrottled ); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
const $ledeContent = $container.find( '.mf-section-0' ).children( ':not( .ext-discussiontools-emptystate )' ) |
| 42 |
|
| 43 |
.add( $container.find( '.mw-talkpageheader' ) ); |
| 44 |
const $ledeButton = $container.find( '.ext-discussiontools-init-lede-button' ); |
| 45 |
if ( $ledeButton.length ) { |
| 46 |
const windowManager = OO.ui.getWindowManager(); |
| 47 |
if ( !ledeSectionDialog ) { |
| 48 |
const LedeSectionDialog = require( './LedeSectionDialog.js' ); |
| 49 |
ledeSectionDialog = new LedeSectionDialog(); |
| 50 |
windowManager.addWindows( [ ledeSectionDialog ] ); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
OO.ui.infuse( $ledeButton ).on( 'click', () => { |
| 55 |
mw.loader.using( 'oojs-ui-windows' ).then( () => { |
| 56 |
windowManager.openWindow( 'ledeSection', { $content: $ledeContent } ); |
| 57 |
mw.track( 'webuiactions_log.click', 'lede-button' ); |
| 58 |
} ); |
| 59 |
} ); |
| 60 |
mw.track( 'webuiactions_log.show', 'lede-button' ); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
const $newTopicWrapper = $( '.ext-discussiontools-init-new-topic' ); |
| 65 |
|
| 66 |
if ( |
| 67 |
!newTopicButton && |
| 68 |
|
| 69 |
$( '.ext-discussiontools-init-new-topic-button' ).length |
| 70 |
) { |
| 71 |
|
| 72 |
newTopicButton = OO.ui.infuse( $( '.ext-discussiontools-init-new-topic-button' ) ); |
| 73 |
|
| 74 |
newTopicButton.$element.attr( 'data-event-name', 'talkpage.add-topic' ); |
| 75 |
const $scrollContainer = $( OO.ui.Element.static.getClosestScrollableContainer( document.body ) ); |
| 76 |
const $scrollListener = $scrollContainer.is( 'html, body' ) ? $( OO.ui.Element.static.getWindow( $scrollContainer[ 0 ] ) ) : $scrollContainer; |
| 77 |
let lastScrollTop = $scrollContainer.scrollTop(); |
| 78 |
let wasScrollDown = null; |
| 79 |
const $body = $( document.body ); |
| 80 |
|
| 81 |
$scrollListener[ 0 ].addEventListener( 'scroll', OO.ui.throttle( () => { |
| 82 |
|
| 83 |
const scrollTop = Math.max( $scrollContainer.scrollTop(), 0 ); |
| 84 |
const isScrollDown = scrollTop > lastScrollTop; |
| 85 |
if ( isScrollDown !== wasScrollDown ) { |
| 86 |
if ( !isScrollDown ) { |
| 87 |
$newTopicWrapper.css( 'transition', 'none' ); |
| 88 |
} |
| 89 |
$body.removeClass( [ 'ext-discussiontools-init-new-topic-closed', 'ext-discussiontools-init-new-topic-opened' ] ); |
| 90 |
requestAnimationFrame( () => { |
| 91 |
$newTopicWrapper.css( 'transition', '' ); |
| 92 |
$body.addClass( isScrollDown ? 'ext-discussiontools-init-new-topic-close' : 'ext-discussiontools-init-new-topic-open' ); |
| 93 |
setTimeout( () => { |
| 94 |
$body.removeClass( [ 'ext-discussiontools-init-new-topic-close', 'ext-discussiontools-init-new-topic-open' ] ); |
| 95 |
$body.addClass( isScrollDown ? 'ext-discussiontools-init-new-topic-closed' : 'ext-discussiontools-init-new-topic-opened' ); |
| 96 |
}, 250 ); |
| 97 |
} ); |
| 98 |
} |
| 99 |
|
| 100 |
const observer = new IntersectionObserver( |
| 101 |
( ( entries ) => { |
| 102 |
$newTopicWrapper.toggleClass( 'ext-discussiontools-init-new-topic-pinned', entries[ 0 ].intersectionRatio === 1 ); |
| 103 |
} ), |
| 104 |
{ threshold: [ 1 ] } |
| 105 |
); |
| 106 |
|
| 107 |
observer.observe( $newTopicWrapper[ 0 ] ); |
| 108 |
|
| 109 |
lastScrollTop = scrollTop; |
| 110 |
wasScrollDown = isScrollDown; |
| 111 |
}, 200 ), { passive: true } ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
if ( |
| 120 |
$( '.catlinks' ).filter( '[data-mw="interface"]' ).length || |
| 121 |
$( '#page-secondary-actions' ).children().length || |
| 122 |
$( '.return-link' ).length |
| 123 |
) { |
| 124 |
$newTopicWrapper.addClass( 'ext-discussiontools-init-button-notFlush' ); |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
mw.hook( 'discussionToolsOverflowMenuOnChoose' ).add( ( id, menuItem, threadItem ) => { |
| 131 |
if ( id === 'edit' ) { |
| 132 |
|
| 133 |
$( threadItem.getRange().commonAncestorContainer ) |
| 134 |
.closest( '.ext-discussiontools-init-section' ).find( '.mw-editsection > a' ).trigger( 'click' ); |
| 135 |
} |
| 136 |
} ); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
function closeLedeSectionDialog() { |
| 144 |
if ( ledeSectionDialog && ledeSectionDialog.isOpened() ) { |
| 145 |
return ledeSectionDialog.close().closed; |
| 146 |
} |
| 147 |
return $.Deferred().resolve(); |
| 148 |
} |
| 149 |
|
| 150 |
module.exports = { |
| 151 |
init: init, |
| 152 |
closeLedeSectionDialog: closeLedeSectionDialog |
| 153 |
}; |