Debian Codegraph @

extensions/DiscussionTools/modules/dt.init.js

raw symbols SCIP
1 const controller = require( './controller.js' ),
2 url = new URL( location.href );
3
4 /**
5 * @class mw.dt
6 * @singleton
7 */
8 mw.dt = {};
9
10 mw.dt.initState = {
11 firstLoad: true
12 };
13
14 if ( url.searchParams.get( 'dtrepliedto' ) ) {
15 // If we had to reload the page to highlight the new comment, extract that data from the URL and
16 // clean it up.
17 mw.dt.initState.repliedTo = url.searchParams.get( 'dtrepliedto' );
18 mw.dt.initState.tempUserCreated = url.searchParams.has( 'dttempusercreated' );
19 url.searchParams.delete( 'dtrepliedto' );
20 url.searchParams.delete( 'dttempusercreated' );
21 window.history.replaceState( {}, '', url );
22 }
23
24 /**
25 * Hook handler for `mw.hook( 'wikipage.content' )`.
26 *
27 * @param {jQuery} $container
28 */
29 mw.dt.init = function ( $container ) {
30 function reallyInit( $node ) {
31 controller.init( $node, mw.dt.initState );
32 mw.dt.initState = {};
33
34 // TODO: This functionality could be provided by MediaWiki (T183720).
35 $( document.documentElement ).addClass( 'ext-discussiontools-init-ready' );
36 }
37
38 // Only (re)initialize if the hook is being fired on the page content – not on e.g. a single image
39 // in a gallery slideshow, or a preview in our own reply tool
40 if ( $container.is( '#mw-content-text' ) || $container.find( '#mw-content-text' ).length ) {
41 // eslint-disable-next-line no-jquery/no-global-selector
42 reallyInit( $( '#mw-content-text' ) );
43 return;
44 }
45
46 // Otherwise, if node is detached, wait to see what it actually is
47 if ( !$container.closest( 'html' ).length ) {
48 setTimeout( () => {
49 if ( $container.closest( 'html' ).length ) {
50 mw.dt.init( $container );
51 }
52 } );
53 return;
54 }
55
56 // If it's a full page live preview, (re)initialize to support highlighting comments (T309423)
57 // FIXME This really should not depend on implementation details of 2 different live previews
58 // FIXME VisualEditor (2017WTE) preview can't be supported, because it messes with `id` attributes
59 const livePreviewSelectors = '#wikiPreview, .ext-WikiEditor-realtimepreview-preview';
60 if ( $container.parent().is( livePreviewSelectors ) ) {
61 reallyInit( $container );
62 return;
63 }
64 };
65
66 if ( url.searchParams.get( 'dtdebug' ) ) {
67 mw.loader.load( 'ext.discussionTools.debug' );
68 } else {
69 // Don't use an anonymous function, because ReplyWidget needs to be able to remove this handler
70 mw.hook( 'wikipage.content' ).add( mw.dt.init );
71 }
72
73 if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'TopicSubscriptions' ) {
74 const topicSubscriptions = require( './topicsubscriptions.js' );
75 topicSubscriptions.initSpecialTopicSubscriptions();
76 }
77
78 if ( mw.config.get( 'wgAction' ) === 'history' ) {
79 const topicSubscriptions = require( './topicsubscriptions.js' );
80 topicSubscriptions.initNewTopicsSubscription();
81 }
82
83 // Clean up old localStorage entries that were erroneously set with no expiration (T339042).
84 // We are no longer using these keys since T329299.
85 // TODO: Remove this code after a few weeks.
86 mw.requestIdleCallback( () => {
87 try {
88 for ( const key in localStorage ) {
89 if ( key.startsWith( 'reply/' ) ) {
90 localStorage.removeItem( key );
91 localStorage.removeItem( '_EXPIRY_' + key );
92 }
93 }
94 } catch ( err ) {}
95 } );
96
97 module.exports = {
98 controller: controller,
99 Parser: require( './Parser.js' ),
100 parserData: require( './parser/data.json' ),
101 modifier: require( './modifier.js' ),
102 ThreadItem: require( './ThreadItem.js' ),
103 HeadingItem: require( './HeadingItem.js' ),
104 CommentItem: require( './CommentItem.js' ),
105 utils: require( './utils.js' ),
106 config: require( './config.json' )
107 };