Debian Codegraph @

extensions/Echo/modules/ui/mw.echo.ui.BadgeLinkWidget.js

raw symbols SCIP
1 ( function () {
2 /**
3 * Notification badge button widget for echo popup.
4 *
5 * @class
6 * @extends OO.ui.ButtonWidget
7 *
8 * @constructor
9 * @param {Object} [config] Configuration object
10 * @param {string} [config.type] The notification types this button represents;
11 * 'message', 'alert' or 'all'
12 * @param {string} [config.href] URL the badge links to
13 * @param {string} [config.numItems=0] The number of items that are in the button display
14 * @param {string} [config.convertedNumber] A converted version of the initial count
15 */
16 mw.echo.ui.BadgeLinkWidget = function MwEchoUiBadgeLinkWidget( config ) {
17 config = config || {};
18
19 // Parent constructor
20 mw.echo.ui.BadgeLinkWidget.super.call( this, config );
21
22 // Mixin constructors
23 OO.ui.mixin.LabelElement.call( this, Object.assign( { $label: this.$element }, config ) );
24 OO.ui.mixin.ButtonElement.call( this, Object.assign( { $button: this.$element }, config ) );
25 OO.ui.mixin.TitledElement.call( this, Object.assign( { $titled: this.$element }, config ) );
26 OO.ui.mixin.FlaggedElement.call( this, Object.assign( {}, config, { $flagged: this.$element } ) );
27
28 this.$element
29 .addClass( 'mw-echo-notifications-badge' );
30
31 this.count = 0;
32 this.type = config.type || 'alert';
33 this.setCount( config.numItems || 0, config.convertedNumber );
34
35 if ( config.href !== undefined && OO.ui.isSafeUrl( config.href ) ) {
36 this.$element.attr( 'href', config.href );
37 }
38 if ( this.type === 'alert' ) {
39 this.$element
40 .addClass( 'oo-ui-icon-bell' );
41 } else {
42 this.$element
43 .addClass( 'oo-ui-icon-tray' );
44 }
45 };
46
47 OO.inheritClass( mw.echo.ui.BadgeLinkWidget, OO.ui.Widget );
48 OO.mixinClass( mw.echo.ui.BadgeLinkWidget, OO.ui.mixin.LabelElement );
49 OO.mixinClass( mw.echo.ui.BadgeLinkWidget, OO.ui.mixin.ButtonElement );
50 OO.mixinClass( mw.echo.ui.BadgeLinkWidget, OO.ui.mixin.TitledElement );
51 OO.mixinClass( mw.echo.ui.BadgeLinkWidget, OO.ui.mixin.FlaggedElement );
52
53 mw.echo.ui.BadgeLinkWidget.static.tagName = 'a';
54
55 /**
56 * Overrides ButtonElement.prototype.onClick so that it doesn't call ev.stopPropagation.
57 * This ensures the dialog dismisses other open overlays e.g. ULS (See T295796 for more
58 * information).
59 *
60 * @inheritDoc
61 */
62 mw.echo.ui.BadgeLinkWidget.prototype.onClick = function ( ev ) {
63 ev.preventDefault();
64 this.emit( 'click' );
65 };
66
67 /**
68 * Set the count labels for this button.
69 *
70 * @param {number} numItems Number of items
71 * @param {string} [convertedNumber] Label of the button. Defaults to the default message
72 * showing the item number.
73 */
74 mw.echo.ui.BadgeLinkWidget.prototype.setCount = function ( numItems, convertedNumber ) {
75 convertedNumber = convertedNumber !== undefined ? convertedNumber : numItems;
76
77 this.$element
78 .toggleClass( 'mw-echo-notifications-badge-all-read', !numItems )
79 .toggleClass( 'mw-echo-notifications-badge-long-label', convertedNumber.length > 2 )
80 .attr( 'data-counter-num', numItems )
81 .attr( 'data-counter-text', convertedNumber );
82
83 this.setLabel( mw.msg(
84 this.type === 'alert' ?
85 'echo-notification-alert' :
86 'echo-notification-notice',
87 convertedNumber
88 ) );
89
90 if ( this.count !== numItems ) {
91 this.count = numItems;
92
93 // Fire badge count change hook
94 mw.hook( 'ext.echo.badge.countChange' ).fire( this.type, this.count, convertedNumber );
95 }
96 };
97 }() );