/* 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) * @param {HTMLElement|null} labelElement Subscribe link, if different to element * @param {boolean} isNewTopics Is a subscribe link for new topics subscriptions */ function updateSubscribeLink( element, state, labelElement, isNewTopics ) { labelElement = labelElement || element; if ( state !== null ) { element.setAttribute( 'data-mw-subscribed', String( state ) ); } if ( state ) { labelElement.textContent = mw.msg( isNewTopics ? 'discussiontools-newtopicssubscription-button-unsubscribe-label' : 'discussiontools-topicsubscription-button-unsubscribe' ); element.setAttribute( 'title', mw.msg( isNewTopics ? 'discussiontools-newtopicssubscription-button-unsubscribe-tooltip' : 'discussiontools-topicsubscription-button-unsubscribe-tooltip' ) ); } else { labelElement.textContent = mw.msg( isNewTopics ? 'discussiontools-newtopicssubscription-button-subscribe-label' : 'discussiontools-topicsubscription-button-subscribe' ); element.setAttribute( 'title', mw.msg( isNewTopics ? 'discussiontools-newtopicssubscription-button-subscribe-tooltip' : 'discussiontools-topicsubscription-button-subscribe-tooltip' ) ); } } /** * Update a subscribe button * * @param {OO.ui.ButtonWidget} 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' ) ); } } /** * Change the subscription state of a topic subscription * * @param {string} title Page title * @param {string} commentName Comment name * @param {boolean} subscribe Subscription state * @param {boolean} isNewTopics Subscription is for new topics * @return {jQuery.Promise} Promise which resolves after change of state */ function changeSubscription( title, commentName, subscribe, isNewTopics ) { 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 ? ( isNewTopics ? 'discussiontools-newtopicssubscription-notify-subscribed-body' : 'discussiontools-topicsubscription-notify-subscribed-body' ) : ( isNewTopics ? 'discussiontools-newtopicssubscription-notify-unsubscribed-body' : 'discussiontools-topicsubscription-notify-unsubscribed-body' ) ), { title: mw.msg( result.subscribe ? ( isNewTopics ? 'discussiontools-newtopicssubscription-notify-subscribed-title' : 'discussiontools-topicsubscription-notify-subscribed-title' ) : ( isNewTopics ? 'discussiontools-newtopicssubscription-notify-unsubscribed-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' ); } ); } ); } ); // New topics subscription button ( function () { // eslint-disable-next-line no-jquery/no-global-selector var $button = $( '#ca-dt-page-subscribe > a' ); var $label = $button.find( 'span' ); var titleObj = mw.Title.newFromText( mw.config.get( 'wgRelevantPageName' ) ); var name = utils.getNewTopicsSubscriptionId( titleObj ); $button.on( 'click', function ( e ) { e.preventDefault(); // Get latest subscribedState var subscribedState = getSubscribedStateFromElement( $button[ 0 ] ); changeSubscription( titleObj.getPrefixedText(), name, !subscribedState, true ) .then( function ( result ) { updateSubscribeLink( $button[ 0 ], result.subscribe ? STATE_SUBSCRIBED : STATE_UNSUBSCRIBED, $label[ 0 ], true ); } ); } ); }() ); } function initSpecialTopicSubscriptions() { api = require( './controller.js' ).getApi(); // Unsubscribe links on special page // eslint-disable-next-line no-jquery/no-global-selector $( '.ext-discussiontools-special-unsubscribe-button' ).each( function () { var button = OO.ui.infuse( this ); var data = button.getData(); var subscribedState = STATE_SUBSCRIBED; button.on( 'click', function () { button.setDisabled( true ); changeSubscription( data.title, data.item, !subscribedState ) .then( function ( result ) { button.setLabel( mw.msg( result.subscribe ? 'discussiontools-topicsubscription-button-unsubscribe-label' : 'discussiontools-topicsubscription-button-subscribe-label' ) ); button.clearFlags(); button.setFlags( [ result.subscribe ? 'destructive' : 'progressive' ] ); subscribedState = result.subscribe ? STATE_SUBSCRIBED : STATE_UNSUBSCRIBED; } ).always( function () { button.setDisabled( false ); } ); } ); } ); } /** * 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 = $( '