Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/widgets/ve.ui.MWEducationPopupWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MediaWiki EducationPopup class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * UserInterface education popup.
10 *
11 * Shows a pulsating blue dot which, when you click,
12 * reveals a popup with useful information.
13 *
14 * @class
15 *
16 * @constructor
17 * @extends OO.ui.Widget
18 * @param {jQuery} $target Element to attach to
19 * @param {Object} config Configuration options
20 * @param {string} config.popupTitle
21 * @param {string|jQuery} config.popupText
22 * @param {string} [config.popupImage] Popup image class
23 * @param {string} [config.trackingName]
24 */
25 ve.ui.MWEducationPopupWidget = function VeUiMwEducationPopup( $target, config ) {
26 config = config || {};
27
28 // HACK: Do not display on platforms other than desktop
29 if ( !( ve.init.mw.DesktopArticleTarget && ve.init.target instanceof ve.init.mw.DesktopArticleTarget ) ) {
30 return;
31 }
32
33 // Do not display if the user already acknowledged the popups
34 if ( !mw.libs.ve.shouldShowEducationPopups() ) {
35 return;
36 }
37
38 // Parent method
39 ve.ui.MWEducationPopupWidget.super.call( this, config );
40
41 // Properties
42 this.$target = $target;
43 this.popupCloseButton = new OO.ui.ButtonWidget( {
44 label: ve.msg( 'visualeditor-educationpopup-dismiss' ),
45 flags: [ 'progressive', 'primary' ],
46 classes: [ 've-ui-educationPopup-dismiss' ]
47 } );
48 this.trackingName = config.trackingName;
49 this.$pulsatingDot = $( '<div>' ).addClass( 'mw-pulsating-dot' );
50
51 const $popupContent = $( '<div>' ).append(
52 $( '<h3>' ).text( config.popupTitle ),
53 // eslint-disable-next-line no-jquery/no-append-html
54 $( '<p>' ).append(
55 config.popupText instanceof $ ?
56 config.popupText :
57 document.createTextNode( config.popupText )
58 ),
59 this.popupCloseButton.$element
60 );
61 ve.targetLinksToNewWindow( $popupContent[ 0 ] );
62 if ( config.popupImage ) {
63 $popupContent.prepend(
64 // The following classes can be generated here:
65 // * ve-ui-educationPopup-image-link
66 // * others generated by extensions
67 $( '<div>' ).addClass( 've-ui-educationPopup-image ve-ui-educationPopup-image-' + config.popupImage )
68 );
69 }
70
71 this.popup = new OO.ui.PopupWidget( {
72 $floatableContainer: this.$target,
73 $content: $popupContent,
74 padded: true,
75 width: 300
76 } );
77
78 this.onTargetMouseDownHandler = this.onTargetMouseDown.bind( this );
79
80 // Events
81 this.$target.on( 'mousedown', this.onTargetMouseDownHandler );
82 this.popupCloseButton.connect( this, { click: 'onPopupCloseButtonClick' } );
83
84 // DOME
85 this.$element.addClass( 've-ui-educationPopup' ).append( this.$pulsatingDot, this.popup.$element );
86 };
87
88 /* Inheritance */
89
90 OO.inheritClass( ve.ui.MWEducationPopupWidget, OO.ui.Widget );
91
92 /* Methods */
93
94 /**
95 * Handle mouse down events on the handle
96 *
97 * @param {jQuery.Event} e
98 * @return {boolean|undefined}
99 */
100 ve.ui.MWEducationPopupWidget.prototype.onTargetMouseDown = function () {
101 if ( ve.init.target.openEducationPopup ) {
102 ve.init.target.openEducationPopup.popup.toggle( false );
103 ve.init.target.openEducationPopup.$pulsatingDot.removeClass( 'oo-ui-element-hidden' );
104 }
105 ve.init.target.openEducationPopup = this;
106
107 this.$pulsatingDot.addClass( 'oo-ui-element-hidden' );
108 this.popup.toggle( true );
109 this.popupCloseButton.focus();
110
111 if ( this.trackingName ) {
112 ve.track( 'activity.' + this.trackingName + 'EducationPopup', { action: 'show' } );
113 }
114 return false;
115 };
116
117 /**
118 * Click handler for the popup close button
119 */
120 ve.ui.MWEducationPopupWidget.prototype.onPopupCloseButtonClick = function () {
121 this.$target.off( 'mousedown', this.onTargetMouseDownHandler );
122 this.popup.toggle( false );
123
124 ve.init.target.openEducationPopup = null;
125 mw.libs.ve.stopShowingEducationPopups();
126
127 const mouseLeft = { which: OO.ui.MouseButtons.LEFT };
128 this.$target
129 .trigger( $.Event( 'mousedown', mouseLeft ) )
130 .trigger( $.Event( 'mouseup', mouseLeft ) )
131 .trigger( $.Event( 'click', mouseLeft ) );
132
133 };