mobile.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* eslint-disable no-jquery/no-global-selector */
  2. $( function () {
  3. var mobileMediaQuery = window.matchMedia( 'screen and (max-width: 550px)' ),
  4. // Track if DOM has been set up for mobile fanciness yet
  5. monobookMobileElements = false,
  6. // Toggles and targets for popouts
  7. toggles = {
  8. '#sidebar-toggle': '#sidebar-mobilejs',
  9. '#p-personal-toggle': '#p-personal',
  10. '#ca-more a': '#p-cactions',
  11. '#ca-languages a': '#p-lang',
  12. '#ca-tools a': '#p-tb'
  13. };
  14. // Close menus
  15. function closeMenus() {
  16. $( '.mobile-menu-active' ).removeClass( 'mobile-menu-active' );
  17. $( '.menus-cover' ).removeClass( 'visible' );
  18. }
  19. // Set up DOM for mobile fanciness
  20. // We don't automatically do this because MonoBook; most users will be on desktop
  21. function setupMonoBookMobile() {
  22. if ( !monobookMobileElements && mobileMediaQuery.matches ) {
  23. // Duplicate nav
  24. $( '#column-one' ).append(
  25. $( '#sidebar' ).clone().find( '*' ).addBack().each( function () {
  26. if ( this.id ) {
  27. this.id = this.id + '-mobilejs';
  28. }
  29. } ).end().end()
  30. );
  31. // Thing to fade out the content while menus are active
  32. $( '#column-one' ).append( $( '<div>' ).attr( 'id', 'menus-cover-background' ).addClass( 'menus-cover' ) );
  33. $( '#column-one' ).append( $( '<div>' ).attr( 'id', 'menus-cover' ).addClass( 'menus-cover' ) );
  34. // Add extra cactions tabs - edit, editsource, contributions
  35. // Wrap in function to keep jenkins from whining
  36. $( function () {
  37. var newTabs = [
  38. 'ca-edit',
  39. // 'ca-ve-edit', // TODO when VE is more usable to begin with here
  40. // 'ca-watch', 'ca-unwatch', // Maybe?
  41. 't-contributions'
  42. ];
  43. newTabs.forEach( function ( item ) {
  44. var a = $( '#' + item + ' a' );
  45. // TODO check if we're on the page and add class=selected
  46. if ( a.length ) {
  47. mw.util.addPortletLink(
  48. 'p-cactions-mobile',
  49. a.attr( 'href' ),
  50. a.text(),
  51. a.parent().attr( 'id' ) + '-mobile',
  52. a.attr( 'tooltip' ),
  53. a.attr( 'accesskey' ),
  54. '#ca-more'
  55. );
  56. }
  57. } );
  58. } );
  59. // eslint-disable-next-line no-jquery/no-each-util
  60. $.each( toggles, function ( toggle, target ) {
  61. // Add close buttons
  62. $( target ).append( $( '<div>' ).addClass( 'mobile-close-button' ) );
  63. // Open menus
  64. $( toggle ).on( 'click', function () {
  65. if ( mobileMediaQuery.matches ) {
  66. $( target ).addClass( 'mobile-menu-active' );
  67. $( '.menus-cover' ).addClass( 'visible' );
  68. }
  69. // Don't still link to # targets
  70. return false;
  71. } );
  72. } );
  73. $( '.mobile-close-button, .menus-cover' ).on( 'click', closeMenus );
  74. // TODO: tap events on same (if not already included in 'click') - also close
  75. // TODO: appropriate swipe event(s) - also close
  76. monobookMobileElements = true;
  77. }
  78. }
  79. $( window ).on( 'resize', setupMonoBookMobile );
  80. setupMonoBookMobile();
  81. } );