mediawiki-skins-MinervaNeue/resources/skins.minerva.mainMenu/MainMenu.js
Stephen Niedzielski 874d9c9e3b Hygiene: update JSDoc boxed and JQuery types
From TypeScript's do's and don'ts:[0]

  Don’t ever use the types Number, String, Boolean, or Object. These
  types refer to non-primitive boxed objects that are almost never used
  appropriately in JavaScript code.

Although Minerva only uses JSDocs at this time which seemingly doesn't
care about casing[1], we should endeavor to use the proper return types.

This patch lowercases typing to indicate primitive / boxed type as
appropriate.[2] As a special case, function types are uppercased for
compatibility with TypeScript type checking.

Also, JQuery types are of type "JQuery". The global JQuery object's
identifier is "jQuery". This patch uppercases J's where appropriate.

Lastly, replace unsupported type "Integer" with "number" and a comment.

[0] https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#general-types

[1] https://github.com/jsdoc3/jsdoc/issues/1046#issuecomment-126477791

[2] find resources tests -iname \*.js|
    xargs -rd\\n sed -ri '
      s%\{\s*(number|string|boolean|object|null|undefined)%{\L\1%gi;
      s%\{\s*function%{Function%g;
      s%\{\s*jquery%{JQuery%gi;
      s%\{\s*integer\s*\}%{number} An integer.%gi
    '

Change-Id: I6cbac15940e4501aee7ede8f421b77ffd027170d
2018-07-03 11:10:12 -05:00

124 lines
3.4 KiB
JavaScript

( function ( M, $ ) {
var browser = M.require( 'mobile.startup/Browser' ).getSingleton(),
View = M.require( 'mobile.startup/View' );
/**
* Representation of the main menu
*
* @class MainMenu
* @extends View
* @constructor
* @param {object} options Configuration options
* @module skins.minerva.mainMenu/MainMenu
*/
function MainMenu( options ) {
this.activator = options.activator;
View.call( this, options );
}
OO.mfExtend( MainMenu, View, {
isTemplateMode: true,
template: mw.template.get( 'skins.minerva.mainMenu', 'menu.hogan' ),
templatePartials: {
menuGroup: mw.template.get( 'skins.minerva.mainMenu', 'menuGroup.hogan' )
},
/**
* @cfg {object} defaults Default options hash.
* @cfg {string} defaults.activator selector for element that when clicked can open or close the menu
*/
defaults: {
activator: undefined
},
/**
* Turn on event logging on the existing main menu by reading `event-name` data
* attributes on elements.
*/
enableLogging: function () {
// Load the EventLogging module inside MobileFrontend if available
mw.loader.using( 'mobile.loggingSchemas.mobileWebMainMenuClickTracking' );
this.$( 'a' ).on( 'click', function () {
var $link = $( this ),
eventName = $link.data( 'event-name' );
if ( eventName ) {
mw.track( 'mf.schemaMobileWebMainMenuClickTracking', {
name: eventName,
destination: $link.attr( 'href' )
} );
}
} );
},
/**
* Remove the nearby menu entry if the browser doesn't support geo location
*/
postRender: function () {
if ( !browser.supportsGeoLocation() ) {
this.$el.find( '.nearby' ).parent().remove();
}
this.registerClickEvents();
},
/**
* Registers events for opening and closing the main menu
*/
registerClickEvents: function () {
var self = this;
// Listen to the main menu button clicks
$( this.activator )
.off( 'click' )
.on( 'click', function ( ev ) {
if ( self.isOpen() ) {
self.closeNavigationDrawers();
} else {
self.openNavigationDrawer();
}
ev.preventDefault();
// Stop propagation, otherwise the Skin will close the open menus on page center click
ev.stopPropagation();
} );
},
/**
* Check whether the navigation drawer is open
* @return {boolean}
*/
isOpen: function () {
// FIXME: We should be moving away from applying classes to the body
return $( 'body' ).hasClass( 'navigation-enabled' );
},
/**
* Close all open navigation drawers
*/
closeNavigationDrawers: function () {
// FIXME: We should be moving away from applying classes to the body
$( 'body' ).removeClass( 'navigation-enabled' )
.removeClass( 'secondary-navigation-enabled' )
.removeClass( 'primary-navigation-enabled' );
},
/**
* Toggle open navigation drawer
* @param {string} [drawerType] A name that identifies the navigation drawer that
* should be toggled open. Defaults to 'primary'.
* @fires MainMenu#open
*/
openNavigationDrawer: function ( drawerType ) {
// close any existing ones first.
this.closeNavigationDrawers();
drawerType = drawerType || 'primary';
// FIXME: We should be moving away from applying classes to the body
$( 'body' ).toggleClass( 'navigation-enabled' )
.toggleClass( drawerType + '-navigation-enabled' );
this.emit( 'open' );
}
} );
M.define( 'skins.minerva.mainMenu/MainMenu', MainMenu );
}( mw.mobileFrontend, jQuery ) );