Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /**
3 * Secondary menu item
4 *
5 * @class
6 * @extends OO.ui.ButtonOptionWidget
7 * @mixes OO.ui.mixin.PendingElement
8 *
9 * @constructor
10 * @param {Object} [config] Configuration object
11 * @param {string} [config.type] Optional action type. Used to note a dynamic action, by setting it to 'dynamic-action'
12 * @param {string} [config.url] Item URL for links
13 * @param {string} [config.tooltip] Tooltip for links
14 * @param {string} [config.description] An optional description for the item
15 * @param {Object} [config.actionData] Action data
16 * @param {boolean} [config.prioritized] The item is prioritized outside the
17 * popup menu.
18 */
19 mw.echo.ui.MenuItemWidget = function MwEchoUiMenuItemWidget( config ) {
20 config = config || {};
21
22 this.dynamic = config.type === 'dynamic-action';
23 // Needs to be set before parent constructor is called
24 // as it changes the value of getTagName.
25 this.isLink = config.url && !this.isDynamicAction();
26
27 // Parent constructor
28 mw.echo.ui.MenuItemWidget.super.call( this, Object.assign( { framed: false }, config ) );
29
30 // Mixin constructors
31 OO.ui.mixin.PendingElement.call( this, config );
32
33 this.prioritized = !!config.prioritized;
34 this.messages = this.isDynamicAction() ?
35 config.actionData.messages :
36 {};
37
38 this.actionData = config.actionData || {};
39
40 // Optional description
41 this.descriptionLabel = new OO.ui.LabelWidget( {
42 classes: [ 'mw-echo-ui-menuItemWidget-description' ],
43 label: config.description || ''
44 } );
45 this.descriptionLabel.toggle( !this.prioritized && config.description );
46
47 this.$label.append( this.descriptionLabel.$element );
48
49 // Build the option
50 this.$element
51 .addClass( 'mw-echo-ui-menuItemWidget' )
52 .toggleClass( 'mw-echo-ui-menuItemWidget-prioritized', this.prioritized )
53 .toggleClass( 'mw-echo-ui-menuItemWidget-dynamic-action', this.isDynamicAction() );
54
55 if ( this.isLink ) {
56 this.$element.attr( {
57 href: config.url,
58 title: config.tooltip
59 } );
60 }
61 };
62
63 /* Initialization */
64
65 OO.inheritClass( mw.echo.ui.MenuItemWidget, OO.ui.ButtonOptionWidget );
66 OO.mixinClass( mw.echo.ui.MenuItemWidget, OO.ui.mixin.PendingElement );
67
68 /* Static Properties */
69
70 mw.echo.ui.MenuItemWidget.static.highlightable = false;
71 mw.echo.ui.MenuItemWidget.static.pressable = false;
72
73 /* Methods */
74
75 mw.echo.ui.MenuItemWidget.prototype.getTagName = function () {
76 return this.isLink ? 'a' : 'div';
77 };
78
79 mw.echo.ui.MenuItemWidget.prototype.onClick = function ( e ) {
80 // Stop propagation, so that the default dynamic action of the notification isn't triggered
81 // (e.g. expanding a bundled notification).
82 e.stopPropagation();
83
84 // If this is a dynamic action, also prevent default to disable the native browser behavior,
85 // the default link of the notification won't be followed.
86 // (If this is a link, default link of the notification is ignored as native browser behavior.)
87 if ( !this.isLink ) {
88 e.preventDefault();
89 }
90
91 return mw.echo.ui.MenuItemWidget.super.prototype.onClick.apply( this, arguments );
92 };
93
94 mw.echo.ui.MenuItemWidget.prototype.isSelectable = function () {
95 // If we have a link force selectability to false, otherwise defer to parent method
96 // Without a link (for dynamic actions or specific internal actions) we need this widget
97 // to be selectable so it emits the 'choose' event
98 return !this.isLink && mw.echo.ui.MenuItemWidget.super.prototype.isSelectable.apply( this, arguments );
99 };
100
101 /**
102 * Check whether this item is prioritized
103 *
104 * @return {boolean} Item is prioritized
105 */
106 mw.echo.ui.MenuItemWidget.prototype.isPrioritized = function () {
107 return this.prioritized;
108 };
109
110 /**
111 * @typedef {Object} ConfirmationMessages
112 * @memberof mw.echo.ui.MenuItemWidget
113 * @property {string} title Title for the confirmation dialog
114 * @property {string} description Description for the confirmation dialog
115 */
116
117 /**
118 * Get the messages for the confirmation dialog
119 * We expect optionally two messages - title and description.
120 *
121 * NOTE: The messages are parsed as HTML. If user-input is expected
122 * please make sure to properly escape it.
123 *
124 * @return {mw.echo.ui.MenuItemWidget.ConfirmationMessages} Messages for the confirmation dialog
125 */
126 mw.echo.ui.MenuItemWidget.prototype.getConfirmationMessages = function () {
127 return this.messages.confirmation;
128 };
129
130 /**
131 * Get the action data associated with this item
132 *
133 * @return {Object} Action data
134 */
135 mw.echo.ui.MenuItemWidget.prototype.getActionData = function () {
136 return this.actionData;
137 };
138
139 /**
140 * This item is a dynamic action
141 *
142 * @return {boolean} Item is a dynamic action
143 */
144 mw.echo.ui.MenuItemWidget.prototype.isDynamicAction = function () {
145 return this.dynamic;
146 };
147 }() );