Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/tools/ve.ui.MWPopupTool.js

raw symbols SCIP
1 /*!
2 * VisualEditor MediaWiki UserInterface popup tool classes.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * MediaWiki UserInterface popup tool.
10 *
11 * @class
12 * @abstract
13 * @extends OO.ui.PopupTool
14 * @constructor
15 * @param {string} title
16 * @param {OO.ui.ToolGroup} toolGroup
17 * @param {Object} [config]
18 * @param {number} [config.width] Popup width. Upstream default is 320.
19 */
20 ve.ui.MWPopupTool = function VeUiMWPopupTool( title, toolGroup, config ) {
21 // Configuration initialization
22 config = ve.extendObject( { popup: { head: true, label: title, width: config && config.width } }, config );
23
24 // Parent constructor
25 ve.ui.MWPopupTool.super.call( this, toolGroup, config );
26
27 this.popup.connect( this, {
28 ready: 'onPopupOpened',
29 closing: 'onPopupClosing'
30 } );
31
32 this.$element.addClass( 've-ui-mwPopupTool' );
33
34 this.$link.on( 'click', this.onToolLinkClick.bind( this ) );
35 };
36
37 /* Inheritance */
38
39 OO.inheritClass( ve.ui.MWPopupTool, OO.ui.PopupTool );
40
41 /**
42 * Handle to call when popup is opened.
43 */
44 ve.ui.MWPopupTool.prototype.onPopupOpened = function () {
45 this.popup.closeButton.focus();
46 };
47
48 /**
49 * Handle to call when popup is closing
50 */
51 ve.ui.MWPopupTool.prototype.onPopupClosing = function () {
52 this.$link.trigger( 'focus' );
53 };
54
55 /**
56 * Handle clicks on the main tool button.
57 *
58 * @param {jQuery.Event} e Click event
59 */
60 ve.ui.MWPopupTool.prototype.onToolLinkClick = function () {
61 if ( this.popup.isVisible() ) {
62 // Popup will be visible if this just opened, thanks to sequencing.
63 // Can't just track this with toggle, because the notices popup is auto-opened and we
64 // want to know about deliberate interactions.
65 ve.track( 'activity.' + this.constructor.static.name + 'Popup', { action: 'show' } );
66 }
67 };
68
69 /**
70 * MediaWiki UserInterface notices popup tool.
71 *
72 * @class
73 * @extends ve.ui.MWPopupTool
74 * @constructor
75 * @param {OO.ui.ToolGroup} toolGroup
76 * @param {Object} [config]
77 */
78 ve.ui.MWNoticesPopupTool = function VeUiMWNoticesPopupTool( toolGroup, config ) {
79 // Parent constructor
80 ve.ui.MWNoticesPopupTool.super.call(
81 this,
82 ve.msg( 'visualeditor-editnotices-tooltip' ),
83 toolGroup,
84 ve.extendObject( config, { width: 380 } )
85 );
86 };
87
88 /* Inheritance */
89
90 OO.inheritClass( ve.ui.MWNoticesPopupTool, ve.ui.MWPopupTool );
91
92 /* Static Properties */
93
94 ve.ui.MWNoticesPopupTool.static.name = 'notices';
95 ve.ui.MWNoticesPopupTool.static.group = 'notices';
96 ve.ui.MWNoticesPopupTool.static.icon = 'alert';
97 ve.ui.MWNoticesPopupTool.static.title = OO.ui.deferMsg( 'visualeditor-editnotices-tooltip' );
98 ve.ui.MWNoticesPopupTool.static.autoAddToCatchall = false;
99
100 /* Methods */
101
102 /**
103 * Set notices to display
104 *
105 * @param {string[]} notices A (non-empty) list of notices
106 */
107 ve.ui.MWNoticesPopupTool.prototype.setNotices = function ( notices ) {
108 const count = notices.length;
109
110 this.popup.setLabel( ve.msg(
111 'visualeditor-editnotices-tool',
112 mw.language.convertNumber( count )
113 ) );
114
115 if ( this.$items ) {
116 this.$items.remove();
117 }
118
119 this.$items = $( '<div>' ).addClass( 've-ui-mwNoticesPopupTool-items' );
120 this.noticeItems = [];
121
122 notices.forEach( ( item ) => {
123 // eslint-disable-next-line no-jquery/no-html
124 const $element = $( '<div>' )
125 .addClass( 've-ui-mwNoticesPopupTool-item' )
126 .html( typeof item === 'string' ? item : item.message );
127 ve.targetLinksToNewWindow( $element[ 0 ] );
128
129 this.noticeItems.push( {
130 $element: $element,
131 type: item.type
132 } );
133
134 this.$items.append( $element );
135 } );
136
137 this.popup.$body.append( this.$items );
138 // Fire content hook
139 mw.hook( 'wikipage.content' ).fire( this.popup.$body );
140
141 ve.track( 'activity.notices', { action: 'show' } );
142 };
143
144 /**
145 * Get the tool title.
146 *
147 * @inheritdoc
148 */
149 ve.ui.MWNoticesPopupTool.prototype.getTitle = function () {
150 const items = this.toolbar.getTarget().getEditNotices();
151
152 // eslint-disable-next-line mediawiki/msg-doc
153 return ve.msg( this.constructor.static.title, items.length );
154 };
155
156 /* Registration */
157
158 ve.ui.toolFactory.register( ve.ui.MWNoticesPopupTool );