Debian Codegraph @

extensions/MultimediaViewer/resources/mmv/ui/mmv.ui.dialog.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MultimediaViewer.
3 *
4 * MultimediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MultimediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const UiElement = require( './mmv.ui.js' );
19
20 /**
21 * Represents a dialog and the link to open it.
22 */
23 class Dialog extends UiElement {
24 /**
25 * @param {jQuery} $container the element to which the dialog will be appended
26 * @param {jQuery} $openButton the button which opens the dialog. Only used for positioning.
27 */
28 constructor( $container, $openButton ) {
29 super( $container );
30
31 /** @property {boolean} isOpen Whether or not the dialog is open. */
32 this.isOpen = false;
33
34 /**
35 * @property {string} eventPrefix Prefix specific to the class to be applied to events.
36 */
37 this.eventPrefix = '';
38
39 /** @property {jQuery} $openButton The click target which opens the dialog. */
40 this.$openButton = $openButton;
41
42 /** @property {jQuery} $dialog The main dialog container */
43 this.$dialog = $( '<div>' )
44 .addClass( 'mw-mmv-dialog' );
45
46 /**
47 * @property {jQuery} $downArrow Tip of the dialog pointing to $openButton. Called
48 * downArrow for historical reasons although it does not point down anymore.
49 */
50 this.$downArrow = $( '<div>' )
51 .addClass( 'mw-mmv-dialog-down-arrow' )
52 .appendTo( this.$dialog );
53
54 this.initWarning();
55
56 this.$dialog.appendTo( this.$container );
57 }
58
59 /**
60 * Creates the DOM element that setWarning()/clearWarning() will operate on.
61 *
62 * @private
63 */
64 initWarning() {
65 this.$warning = $( '<div>' )
66 .addClass( 'mw-mmv-dialog-warning' )
67 .hide()
68 .on( 'click', ( e ) => {
69 // prevent other click handlers such as the download CTA from intercepting clicks at the warning
70 e.stopPropagation();
71 } )
72 .appendTo( this.$dialog );
73 }
74
75 /**
76 * Handles click on link that opens/closes the dialog.
77 *
78 * @param {jQuery.Event} openEvent Event object for the mmv-$dialog-open event.
79 * @param {jQuery.Event} e Event object for the click event.
80 * @return {boolean} False to cancel the default event
81 */
82 handleOpenCloseClick( openEvent, e ) {
83 this.toggleDialog( e );
84 return false;
85 }
86
87 /**
88 * Toggles the open state on the dialog.
89 *
90 * @param {jQuery.Event} [e] Event object when the close action is caused by a user
91 * action, as opposed to closing the window or something.
92 */
93 toggleDialog( e ) {
94 if ( this.isOpen ) {
95 this.closeDialog( e );
96 } else {
97 this.openDialog();
98 }
99 }
100
101 /**
102 * Opens a dialog.
103 */
104 openDialog() {
105 this.startListeningToOutsideClick();
106 this.$dialog.show();
107 this.isOpen = true;
108 this.$openButton.addClass( 'mw-mmv-dialog-open' );
109 }
110
111 /**
112 * Closes a dialog.
113 */
114 closeDialog() {
115 this.stopListeningToOutsideClick();
116 this.$dialog.hide();
117 this.isOpen = false;
118 this.$openButton.removeClass( 'mw-mmv-dialog-open' );
119 }
120
121 /**
122 * Sets up the event handler which closes the dialog when the user clicks outside.
123 */
124 startListeningToOutsideClick() {
125 this.outsideClickHandler = this.outsideClickHandler || ( ( e ) => {
126 const $clickTarget = $( e.target );
127
128 // Don't close the dialog if the click inside a dialog or on an navigation arrow
129 if (
130 $clickTarget.closest( this.$dialog ).length ||
131 $clickTarget.closest( '.mw-mmv-next-image' ).length ||
132 $clickTarget.closest( '.mw-mmv-prev-image' ).length ||
133 e.which === 3
134 ) {
135 return;
136 }
137
138 this.closeDialog();
139 return false;
140 } );
141 $( document ).on( `click.mmv.${ this.eventPrefix }`, this.outsideClickHandler );
142 }
143
144 /**
145 * Removes the event handler set up by startListeningToOutsideClick().
146 */
147 stopListeningToOutsideClick() {
148 $( document ).off( `click.mmv.${ this.eventPrefix }`, this.outsideClickHandler );
149 }
150
151 /**
152 * Clears listeners.
153 */
154 unattach() {
155 super.unattach();
156
157 this.stopListeningToOutsideClick();
158 }
159
160 /**
161 * @inheritdoc
162 */
163 empty() {
164 this.closeDialog();
165 this.clearWarning();
166 }
167
168 /**
169 * Displays a warning ribbon.
170 *
171 * @param {string} content Content of the warning (can be HTML,
172 * setWarning does no escaping).
173 */
174 setWarning( content ) {
175 this.$warning
176 .empty()
177 .append( content )
178 .show();
179 this.$dialog.addClass( 'mw-mmv-warning-visible' );
180 }
181
182 /**
183 * Removes the warning ribbon.
184 */
185 clearWarning() {
186 this.$warning.hide();
187 this.$dialog.removeClass( 'mw-mmv-warning-visible' );
188 }
189
190 /**
191 * @param {ImageModel} image
192 * @return {string[]}
193 */
194 getImageWarnings( image ) {
195 const warnings = [];
196
197 if ( image.deletionReason ) {
198 warnings.push( mw.message( 'multimediaviewer-reuse-warning-deletion' ).plain() );
199 // Don't inform about other warnings (they may be the cause of the deletion)
200 return warnings;
201 }
202
203 if ( !image.license || image.license.needsAttribution() && !image.author && !image.attribution ) {
204 warnings.push( mw.message( 'multimediaviewer-reuse-warning-noattribution' ).plain() );
205 }
206
207 if ( image.license && !image.license.isFree() ) {
208 warnings.push( mw.message( 'multimediaviewer-reuse-warning-nonfree' ).plain() );
209 }
210
211 return warnings;
212 }
213
214 /**
215 * @param {ImageModel} image
216 */
217 showImageWarnings( image ) {
218 const warnings = this.getImageWarnings( image );
219
220 if ( warnings.length > 0 ) {
221 warnings.push( mw.message( 'multimediaviewer-reuse-warning-generic', image.descriptionUrl ).parse() );
222 this.setWarning( warnings.join( '<br />' ) );
223 } else {
224 this.clearWarning();
225 }
226 }
227 }
228
229 module.exports = Dialog;