mobile-echo.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* eslint-disable no-jquery/no-global-selector */
  2. $( function () {
  3. var mobileMediaQuery = window.matchMedia( 'screen and (max-width: 550px)' ),
  4. echoHacked = false,
  5. echoHackActive = false,
  6. notifications = $( '#pt-notifications-alert a' ).data( 'counter-num' ) + $( '#pt-notifications-notice a' ).data( 'counter-num' ),
  7. notificationsString;
  8. // When the icons are clicked for the first time, they are replaced with a JS interface,
  9. // so don't cache this in a long-lived variable
  10. function getNotificationIcons() {
  11. return $( '#pt-notifications-alert, #pt-notifications-notice' );
  12. }
  13. // Move echo badges in/out of p-personal
  14. function monoBookMobileMoveEchoIcons() {
  15. var $notificationIcons = getNotificationIcons();
  16. if ( $notificationIcons.length ) {
  17. if ( !echoHackActive && mobileMediaQuery.matches ) {
  18. $( '#echo-hack-badges' ).append( $notificationIcons );
  19. echoHackActive = true;
  20. } else if ( echoHackActive && !mobileMediaQuery.matches ) {
  21. $( $notificationIcons ).insertBefore( '#pt-mytalk' );
  22. echoHackActive = false;
  23. }
  24. }
  25. }
  26. function monoBookMobileEchoHack() {
  27. var $notificationIcons = getNotificationIcons();
  28. if ( $notificationIcons.length ) {
  29. if ( !echoHacked && mobileMediaQuery.matches ) {
  30. if ( notifications ) {
  31. notificationsString = mw.msg( 'monobook-notifications-link', notifications );
  32. } else {
  33. notificationsString = mw.msg( 'monobook-notifications-link-none' );
  34. }
  35. // add inline p-personal echo link
  36. mw.util.addPortletLink(
  37. 'p-personal',
  38. mw.util.getUrl( 'Special:Notifications' ),
  39. notificationsString,
  40. 'pt-notifications',
  41. $( '#pt-notifications-notice' ).attr( 'tooltip' ),
  42. null,
  43. '#pt-preferences'
  44. );
  45. $( '#p-personal-toggle' ).append( $( '<ul>' ).attr( 'id', 'echo-hack-badges' ) );
  46. echoHacked = true;
  47. }
  48. monoBookMobileMoveEchoIcons();
  49. }
  50. }
  51. $( window ).on( 'resize', monoBookMobileEchoHack );
  52. monoBookMobileEchoHack();
  53. } );