summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/MultimediaViewer/resources/mmv/ui/mmv.ui.dialog.js
blob: 6738b50be34379a2b95c90d08561d62851281486 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * This file is part of the MediaWiki extension MultimediaViewer.
 *
 * MultimediaViewer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * MultimediaViewer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MultimediaViewer.  If not, see <http://www.gnu.org/licenses/>.
 */

( function ( mw, $, oo ) {
	// Shortcut for prototype later
	var DP;

	/**
	 * Represents a dialog and the link to open it.
	 *
	 * @class mw.mmv.ui.Dialog
	 * @extends mw.mmv.ui.Element
	 * @param {jQuery} $container the element to which the dialog will be appended
	 * @param {jQuery} $openButton the button which opens the dialog. Only used for positioning.
	 * @param {mw.mmv.Config} config
	 */
	function Dialog( $container, $openButton, config ) {
		mw.mmv.ui.Element.call( this, $container );

		/** @property {boolean} isOpen Whether or not the dialog is open. */
		this.isOpen = false;

		/**
		 * @property {string[]} loadDependencies Dependencies to load before showing the dialog.
		 */
		this.loadDependencies = [];

		/**
		 * @property {string} eventPrefix Prefix specific to the class to be applied to events.
		 */
		this.eventPrefix = '';
		/** @property {mw.mmv.Config} config - */
		this.config = config;

		/** @property {jQuery} $openButton The click target which opens the dialog. */
		this.$openButton = $openButton;

		/** @type {jQuery} $dialog The main dialog container */
		this.$dialog = $( '<div>' )
			.addClass( 'mw-mmv-dialog' );

		/**
		 * @property {jQuery} $downArrow Tip of the dialog pointing to $openButton. Called
		 * downArrow for historical reasons although it does not point down anymore.
		 */
		this.$downArrow = $( '<div>' )
			.addClass( 'mw-mmv-dialog-down-arrow' )
			.appendTo( this.$dialog );

		this.initWarning();

		this.$dialog.appendTo( this.$container );
	}

	oo.inheritClass( Dialog, mw.mmv.ui.Element );
	DP = Dialog.prototype;

	/**
	 * Creates the DOM element that setWarning()/clearWarning() will operate on.
	 * @private
	 */
	DP.initWarning = function () {
		this.$warning = $( '<div>' )
			.addClass( 'mw-mmv-dialog-warning' )
			.hide()
			.click( function ( e ) {
				// prevent other click handlers such as the download CTA from intercepting clicks at the warning
				e.stopPropagation();
			} )
			.appendTo( this.$dialog );
	};

	/**
	 * Handles click on link that opens/closes the dialog.
	 *
	 * @param {jQuery.Event} openEvent Event object for the mmv-$dialog-open event.
	 * @param {jQuery.Event} e Event object for the click event.
	 * @return {boolean} False to cancel the default event
	 */
	DP.handleOpenCloseClick = function ( openEvent, e ) {
		var dialog = this;

		mw.loader.using( this.loadDependencies, function () {
			dialog.dependenciesLoaded = true;
			dialog.toggleDialog( e );
		}, function ( error ) {
			if ( window.console && window.console.error ) {
				window.console.error( 'mw.loader.using error when trying to load dialog dependencies', error );
			}
		} );

		return false;
	};

	/**
	 * Toggles the open state on the dialog.
	 *
	 * @param {jQuery.Event} [e] Event object when the close action is caused by a user
	 *   action, as opposed to closing the window or something.
	 */
	DP.toggleDialog = function ( e ) {
		if ( this.isOpen ) {
			this.closeDialog( e );
		} else {
			this.openDialog();
		}
	};

	/**
	 * Opens a dialog.
	 */
	DP.openDialog = function () {
		mw.mmv.actionLogger.log( this.eventPrefix + '-open' );

		this.startListeningToOutsideClick();
		this.$dialog.show();
		this.isOpen = true;
		this.$openButton.addClass( 'mw-mmv-dialog-open' );
	};

	/**
	 * Closes a dialog.
	 */
	DP.closeDialog = function () {
		if ( this.isOpen ) {
			mw.mmv.actionLogger.log( this.eventPrefix + '-close' );
		}

		this.stopListeningToOutsideClick();
		this.$dialog.hide();
		this.isOpen = false;
		this.$openButton.removeClass( 'mw-mmv-dialog-open' );
	};

	/**
	 * Sets up the event handler which closes the dialog when the user clicks outside.
	 */
	DP.startListeningToOutsideClick = function () {
		var dialog = this;

		this.outsideClickHandler = this.outsideClickHandler || function ( e ) {
			var $clickTarget = $( e.target );

			// Don't close the dialog if the click inside a dialog or on an navigation arrow
			if (
				$clickTarget.closest( dialog.$dialog ).length ||
				$clickTarget.closest( '.mw-mmv-next-image' ).length ||
				$clickTarget.closest( '.mw-mmv-prev-image' ).length ||
				e.which === 3
			) {
				return;
			}

			dialog.closeDialog();
			return false;
		};
		$( document ).on( 'click.mmv.' + this.eventPrefix, this.outsideClickHandler );
	};

	/**
	 * Removes the event handler set up by startListeningToOutsideClick().
	 */
	DP.stopListeningToOutsideClick = function () {
		$( document ).off( 'click.mmv.' + this.eventPrefix, this.outsideClickHandler );
	};

	/**
	 * Clears listeners.
	 */
	DP.unattach = function () {
		mw.mmv.ui.Element.prototype.unattach.call( this );

		this.stopListeningToOutsideClick();
	};

	/**
	 * @inheritdoc
	 */
	DP.empty = function () {
		this.closeDialog();
		this.clearWarning();
	};

	/**
	 * Displays a warning ribbon.
	 * @param {string} content Content of the warning (can be HTML,
	 *   setWarning does no escaping).
	 */
	DP.setWarning = function ( content ) {
		this.$warning
			.empty()
			.append( content )
			.show();
		this.$dialog.addClass( 'mw-mmv-warning-visible' );
	};

	/**
	 * Removes the warning ribbon.
	 */
	DP.clearWarning = function () {
		this.$warning.hide();
		this.$dialog.removeClass( 'mw-mmv-warning-visible' );
	};

	/**
	 * @param {mw.mmv.model.Image} image
	 * @return {string[]}
	 */
	DP.getImageWarnings = function ( image ) {
		var warnings = [];

		if ( image.deletionReason ) {
			warnings.push( mw.message( 'multimediaviewer-reuse-warning-deletion' ).plain() );
			// Don't inform about other warnings (they may be the cause of the deletion)
			return warnings;
		}

		if ( !image.license || image.license.needsAttribution() && !image.author && !image.attribution ) {
			warnings.push( mw.message( 'multimediaviewer-reuse-warning-noattribution' ).plain() );
		}

		if ( image.license && !image.license.isFree() ) {
			warnings.push( mw.message( 'multimediaviewer-reuse-warning-nonfree' ).plain() );
		}

		return warnings;
	};

	/**
	 * @param {mw.mmv.model.Image} image
	 */
	DP.showImageWarnings = function ( image ) {
		var warnings = this.getImageWarnings( image );

		if ( warnings.length > 0 ) {
			warnings.push( mw.message( 'multimediaviewer-reuse-warning-generic', image.descriptionUrl ).parse() );
			this.setWarning( warnings.join( '<br />' ) );
		} else {
			this.clearWarning();
		}
	};

	mw.mmv.ui.Dialog = Dialog;
}( mediaWiki, jQuery, OO ) );