/* global moment */ var api, seenAutoTopicSubPopup = !!+mw.user.options.get( 'discussiontools-seenautotopicsubpopup' ), STATE_UNSUBSCRIBED = 0, STATE_SUBSCRIBED = 1, STATE_AUTOSUBSCRIBED = 2, utils = require( './utils.js' ), CommentItem = require( './CommentItem.js' ), HeadingItem = require( './HeadingItem.js' ), linksByName = {}, buttonsByName = {}; /** * Update a subscribe link * * @param {HTMLElement} element Subscribe link * @param {number|null} state State constant (STATE_UNSUBSCRIBED, STATE_SUBSCRIBED or STATE_AUTOSUBSCRIBED) */ function updateSubscribeLink( element, state ) { if ( state !== null ) { element.setAttribute( 'data-mw-subscribed', String( state ) ); } if ( state ) { element.textContent = mw.msg( 'discussiontools-topicsubscription-button-unsubscribe' ); element.setAttribute( 'title', mw.msg( 'discussiontools-topicsubscription-button-unsubscribe-tooltip' ) ); } else { element.textContent = mw.msg( 'discussiontools-topicsubscription-button-subscribe' ); element.setAttribute( 'title', mw.msg( 'discussiontools-topicsubscription-button-subscribe-tooltip' ) ); } } /** * Update a subscribe button * * @param {OO.ui.ButtonWidget} button Button Subscribe button * @param {number|null} state State constant (STATE_UNSUBSCRIBED, STATE_SUBSCRIBED or STATE_AUTOSUBSCRIBED) */ function updateSubscribeButton( button, state ) { if ( state !== null ) { button.$element[ 0 ].setAttribute( 'data-mw-subscribed', String( state ) ); } if ( state ) { button.setIcon( 'bell' ); button.setLabel( mw.msg( 'discussiontools-topicsubscription-button-unsubscribe-label' ) ); button.setTitle( mw.msg( 'discussiontools-topicsubscription-button-unsubscribe-tooltip' ) ); } else { button.setIcon( 'bellOutline' ); button.setLabel( mw.msg( 'discussiontools-topicsubscription-button-subscribe-label' ) ); button.setTitle( mw.msg( 'discussiontools-topicsubscription-button-subscribe-tooltip' ) ); } } function changeSubscription( title, commentName, subscribe ) { var promise = api.postWithToken( 'csrf', { action: 'discussiontoolssubscribe', page: title, commentname: commentName, subscribe: subscribe } ).then( function ( response ) { return OO.getProp( response, 'discussiontoolssubscribe' ) || {}; } ); promise.then( function ( result ) { mw.notify( mw.msg( result.subscribe ? 'discussiontools-topicsubscription-notify-subscribed-body' : 'discussiontools-topicsubscription-notify-unsubscribed-body' ), { title: mw.msg( result.subscribe ? 'discussiontools-topicsubscription-notify-subscribed-title' : 'discussiontools-topicsubscription-notify-unsubscribed-title' ) } ); }, function ( code, data ) { mw.notify( api.getErrorMessage( data ), { type: 'error' } ); } ); return promise; } function getSubscribedStateFromElement( element ) { return element.hasAttribute( 'data-mw-subscribed' ) ? Number( element.getAttribute( 'data-mw-subscribed' ) ) : null; } /** * Initialize topic subscriptions feature * * @param {jQuery} $container Page container * @param {ThreadItemSet} threadItemSet */ function initTopicSubscriptions( $container, threadItemSet ) { linksByName = {}; buttonsByName = {}; // Loads later to avoid circular dependency api = require( './controller.js' ).getApi(); // Subscription buttons (visual enhancements) $container.find( '.ext-discussiontools-init-section-subscribeButton' ).each( function () { // These attributes will be lost when infusing // TODO: Could also be fixed by subclassing ButtonWidget in PHP var subscribedStateTemp = getSubscribedStateFromElement( this ); var id = $( this ).closest( '.ext-discussiontools-init-section' ) .find( '[data-mw-comment-start]' ).attr( 'id' ); var headingItem = threadItemSet.findCommentById( id ); if ( !( headingItem instanceof HeadingItem ) ) { // This should never happen return; } var name = headingItem.name; var button = OO.ui.infuse( this ); buttonsByName[ name ] = button; // Restore data attribute button.$element[ 0 ].setAttribute( 'data-mw-subscribed', String( subscribedStateTemp ) ); var title = mw.config.get( 'wgRelevantPageName' ) + '#' + headingItem.getLinkableTitle(); button.on( 'click', function () { // Get latest subscribedState var subscribedState = getSubscribedStateFromElement( button.$element[ 0 ] ); button.setDisabled( true ); changeSubscription( title, name, !subscribedState ) .then( function ( result ) { updateSubscribeButton( button, result.subscribe ? STATE_SUBSCRIBED : STATE_UNSUBSCRIBED ); } ) .always( function () { button.setDisabled( false ); } ); } ); } ); // Subscription links (no visual enhancements) $container.find( '.ext-discussiontools-init-section-subscribe-link' ).each( function () { var $link = $( this ); var id = $link.closest( '.ext-discussiontools-init-section' ) .find( '[data-mw-comment-start]' ).attr( 'id' ); var headingItem = threadItemSet.findCommentById( id ); if ( !( headingItem instanceof HeadingItem ) ) { // This should never happen return; } var itemName = headingItem.name; var title = mw.config.get( 'wgRelevantPageName' ) + '#' + headingItem.getLinkableTitle(); linksByName[ itemName ] = this; $link.on( 'click keypress', function ( e ) { if ( e.type === 'keypress' && e.which !== OO.ui.Keys.ENTER && e.which !== OO.ui.Keys.SPACE ) { // Only handle keypresses on the "Enter" or "Space" keys return; } if ( e.type === 'click' && !utils.isUnmodifiedLeftClick( e ) ) { // Only handle unmodified left clicks return; } e.preventDefault(); // Get latest subscribedState var subscribedState = getSubscribedStateFromElement( $link[ 0 ] ); $link.addClass( 'ext-discussiontools-init-section-subscribe-link-pending' ); changeSubscription( title, itemName, !subscribedState ) .then( function ( result ) { updateSubscribeLink( $link[ 0 ], result.subscribe ? STATE_SUBSCRIBED : STATE_UNSUBSCRIBED ); } ) .always( function () { $link.removeClass( 'ext-discussiontools-init-section-subscribe-link-pending' ); } ); } ); } ); } /** * Show the first time popup for auto topic subscriptions, if required */ function maybeShowFirstTimeAutoTopicSubPopup() { var lastHighlightComment = require( './highlighter.js' ).getLastHighlightedPublishedComment(); if ( !lastHighlightComment || seenAutoTopicSubPopup ) { return; } seenAutoTopicSubPopup = true; mw.user.options.set( 'discussiontools-seenautotopicsubpopup', '1' ); api.saveOption( 'discussiontools-seenautotopicsubpopup', '1' ); var $popupContent, popup; function close() { popup.$element.removeClass( 'ext-discussiontools-autotopicsubpopup-fadein' ); setTimeout( function () { popup.$element.detach(); }, 1000 ); } $popupContent = $( '