Debian Codegraph @

extensions/DiscussionTools/modules/mobile.js

raw symbols SCIP
1 let newTopicButton, ledeSectionDialog;
2 let viewportScrollContainer = null;
3 let wasKeyboardOpen = null;
4 let initialClientHeight = null;
5 // Copied from ve.init.Platform.static.isIos
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 // TODO: Support orientation changes?
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 // For compatibility with MobileWebUIActionsTracking logging (T295490)
29 $container.find( '.section-heading' ).attr( 'data-event-name', 'talkpage.section' );
30
31 // Keyboard body classes
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 // Mobile overflow menu
40
41 const $ledeContent = $container.find( '.mf-section-0' ).children( ':not( .ext-discussiontools-emptystate )' )
42 // On non-existent pages MobileFrontend wrapping isn't there
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 // Lede section popup
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 // eslint-disable-next-line no-jquery/no-global-selector
64 const $newTopicWrapper = $( '.ext-discussiontools-init-new-topic' );
65
66 if (
67 !newTopicButton &&
68 // eslint-disable-next-line no-jquery/no-global-selector
69 $( '.ext-discussiontools-init-new-topic-button' ).length
70 ) {
71 // eslint-disable-next-line no-jquery/no-global-selector
72 newTopicButton = OO.ui.infuse( $( '.ext-discussiontools-init-new-topic-button' ) );
73 // For compatibility with MobileWebUIActionsTracking logging (T295490)
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 // This block of code is only run once, so we don't need to remove this listener ever
81 $scrollListener[ 0 ].addEventListener( 'scroll', OO.ui.throttle( () => {
82 // Round negative values up to 0 to ignore iOS scroll bouncing (T323400)
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 // Tweak to prevent our footer buttons from overlapping Minerva skin elements (T328452).
115 // TODO: It would be more elegant to do this in just CSS somehow.
116 // BEWARE: I have wasted 4 hours here trying to make that happen. The elements are not nested in a
117 // helpful way, and moving them around tends to break the stickiness of the "Add topic" button.
118 /* eslint-disable no-jquery/no-global-selector */
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 /* eslint-enable no-jquery/no-global-selector */
127 }
128
129 // Handler for "edit" link in overflow menu, only setup once as the hook is global
130 mw.hook( 'discussionToolsOverflowMenuOnChoose' ).add( ( id, menuItem, threadItem ) => {
131 if ( id === 'edit' ) {
132 // Click the hidden section-edit link
133 $( threadItem.getRange().commonAncestorContainer )
134 .closest( '.ext-discussiontools-init-section' ).find( '.mw-editsection > a' ).trigger( 'click' );
135 }
136 } );
137
138 /**
139 * Close the lede section dialog if it is open.
140 *
141 * @return {jQuery.Promise} Promise resolved when the dialog is closed (or if it wasn't open)
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 };