summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/resources/ui/steps/uw.ui.Details.js
blob: 0f27b24ec0237c16ac9ce16ade6cf40367b92f15 (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
/*
 * This file is part of the MediaWiki extension UploadWizard.
 *
 * UploadWizard 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.
 *
 * UploadWizard 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 UploadWizard.  If not, see <http://www.gnu.org/licenses/>.
 */

( function ( mw, $, uw, OO ) {
	/**
	 * Represents the UI for the wizard's Details step.
	 *
	 * @class uw.ui.Details
	 * @extends uw.ui.Step
	 * @constructor
	 */
	uw.ui.Details = function UWUIDetails() {
		var details = this;

		function startDetails() {
			details.emit( 'start-details' );
		}

		uw.ui.Step.call(
			this,
			'details'
		);

		this.$errorCount = $( '<div>' )
			.attr( 'id', 'mwe-upwiz-details-error-count' );
		this.$warningCount = $( '<div>' )
			.attr( 'id', 'mwe-upwiz-details-warning-count' );

		this.nextButton = new OO.ui.ButtonWidget( {
			label: mw.message( 'mwe-upwiz-next-details' ).text(),
			flags: [ 'progressive', 'primary' ]
		} ).on( 'click', startDetails );

		this.nextButtonDespiteFailures = new OO.ui.ButtonWidget( {
			label: mw.message( 'mwe-upwiz-next-file-despite-failures' ).text(),
			flags: [ 'progressive' ]
		} ).on( 'click', function () {
			details.emit( 'finalize-details-after-removal' );
		} );

		this.retryButtonSomeFailed = new OO.ui.ButtonWidget( {
			label: mw.message( 'mwe-upwiz-file-retry' ).text(),
			flags: [ 'progressive', 'primary' ]
		} ).on( 'click', startDetails );

		this.retryButtonAllFailed = new OO.ui.ButtonWidget( {
			label: mw.message( 'mwe-upwiz-file-retry' ).text(),
			flags: [ 'progressive', 'primary' ]
		} ).on( 'click', startDetails );

		this.$buttons.append( this.$errorCount, this.$warningCount );
		this.addPreviousButton();
		this.addNextButton();
	};

	OO.inheritClass( uw.ui.Details, uw.ui.Step );

	uw.ui.Details.prototype.load = function ( uploads ) {
		uw.ui.Step.prototype.load.call( this, uploads );

		if ( uploads.filter( this.needsPatentAgreement.bind( this ) ).length > 0 ) {
			this.$div.prepend(
				$( '<div>' )
					.addClass( 'mwe-upwiz-patent-weapon-policy ui-corner-all' )
					.append(
						$( '<p>' ).append( mw.msg( 'mwe-upwiz-patent-weapon-policy' ) ),
						$( '<p>' ).append(
							$( '<a>' )
								.text( mw.msg( 'mwe-upwiz-patent-weapon-policy-link' ) )
								.attr( { target: '_blank', href: mw.UploadWizard.config.patents.url.weapons } )
						)
					)
			);
		}

		this.$div.prepend(
			$( '<div>' )
				.attr( 'id', 'mwe-upwiz-macro-files' )
				.addClass( 'mwe-upwiz-filled-filelist ui-corner-all' )
		);

		// set default buttons visibility (can be altered in controller later)
		this.$div.find( '.mwe-upwiz-file-next-some-failed' ).hide();
		this.$div.find( '.mwe-upwiz-file-next-all-failed' ).hide();
		this.$div.find( '.mwe-upwiz-file-next-all-ok' ).show();
	};

	uw.ui.Details.prototype.addNextButton = function () {
		var ui = this;

		this.nextButtonPromise.done( function () {
			ui.$buttons.append(
				$( '<div>' )
					.addClass( 'mwe-upwiz-file-next-all-ok mwe-upwiz-file-endchoice' )
					.append( ui.nextButton.$element )
			);

			ui.$buttons.append(
				$( '<div>' )
					.addClass( 'mwe-upwiz-file-next-some-failed mwe-upwiz-file-endchoice' )
					.append(
						new OO.ui.HorizontalLayout( {
							items: [
								new OO.ui.LabelWidget( {
									label: mw.message( 'mwe-upwiz-file-some-failed' ).text()
								} ),
								ui.nextButtonDespiteFailures,
								ui.retryButtonSomeFailed
							]
						} ).$element
					)
			);

			ui.$buttons.append(
				$( '<div>' )
					.addClass( 'mwe-upwiz-file-next-all-failed mwe-upwiz-file-endchoice' )
					.append(
						new OO.ui.HorizontalLayout( {
							items: [
								new OO.ui.LabelWidget( {
									label: mw.message( 'mwe-upwiz-file-all-failed' ).text()
								} ),
								ui.retryButtonAllFailed
							]
						} ).$element
					)
			);
		} );
	};

	/**
	 * Hide buttons for moving to the next step.
	 */
	uw.ui.Details.prototype.hideEndButtons = function () {
		this.$errorCount.empty();
		this.$warningCount.empty();
		this.$div
			.find( '.mwe-upwiz-buttons .mwe-upwiz-file-endchoice' )
			.hide();
	};

	/**
	 * Disable edits to the details.
	 */
	uw.ui.Details.prototype.disableEdits = function () {
		this.$div
			.find( '.mwe-upwiz-data' )
			.morphCrossfade( '.mwe-upwiz-submitting' );

		this.previousButton.$element.hide();
		this.$div.find( '.mwe-upwiz-patent-weapon-policy' ).hide();
	};

	/**
	 * Re-enabled edits to the details.
	 */
	uw.ui.Details.prototype.enableEdits = function () {
		this.previousButton.$element.show();
		this.$div.find( '.mwe-upwiz-patent-weapon-policy' ).show();
	};

	/**
	 * Show errors in the form.
	 * The details page can be vertically long so sometimes it is not obvious there are errors above. This counts them and puts the count
	 * right next to the submit button, so it should be obvious to the user they need to fix things.
	 * This is a bit of a hack. We should already know how many errors there are, and where.
	 * This method also opens up "more info" if the form has errors.
	 */
	uw.ui.Details.prototype.showErrors = function () {
		var $errorElements = this.$div
				// TODO Evil
				.find( '.oo-ui-fieldLayout-messages-error' ),
			errorCount = $errorElements.length;

		// Open "more info" if that part of the form has errors
		$errorElements.each( function () {
			var $collapsibleWrapper = $( this ).closest( '.mwe-more-details' );
			if ( $collapsibleWrapper.length ) {
				$collapsibleWrapper.data( 'mw-collapsible' ).expand();
			}
		} );

		if ( errorCount > 0 ) {
			// Errors supersede warnings, so stop any animating to the warnings before we animate to the errors
			$( 'html, body' ).stop();

			this.$errorCount
				.msg( 'mwe-upwiz-details-error-count', errorCount, this.uploads.length )
				// TODO The IconWidget and 'warning' flag is specific to MediaWiki theme, looks weird in Apex
				.prepend( new OO.ui.IconWidget( { icon: 'alert', flags: [ 'warning' ] } ).$element, ' ' );
			// Scroll to the first error
			$( 'html, body' ).animate( { scrollTop: $( $errorElements[ 0 ] ).offset().top - 50 }, 'slow' );
		} else {
			this.$errorCount.empty();
		}
	};

	/**
	 * Show warnings in the form.
	 * See #showErrors for details.
	 */
	uw.ui.Details.prototype.showWarnings = function () {
		var $warningElements = this.$div
				// TODO Evil
				.find( '.oo-ui-fieldLayout-messages-notice' ),
			warningCount = $warningElements.length;

		// Open "more info" if that part of the form has warnings
		$warningElements.each( function () {
			var $collapsibleWrapper = $( this ).closest( '.mwe-more-details' );
			if ( $collapsibleWrapper.length ) {
				$collapsibleWrapper.data( 'mw-collapsible' ).expand();
			}
		} );

		if ( warningCount > 0 ) {
			this.$warningCount
				.msg( 'mwe-upwiz-details-warning-count', warningCount, this.uploads.length )
				// TODO The IconWidget is specific to MediaWiki theme, looks weird in Apex
				.prepend( new OO.ui.IconWidget( { icon: 'info' } ).$element, ' ' );
			// Scroll to the first warning
			$( 'html, body' ).animate( { scrollTop: $( $warningElements[ 0 ] ).offset().top - 50 }, 'slow' );
		} else {
			this.$warningCount.empty();
		}
	};

	/**
	 * @param {mw.UploadWizardUpload} upload
	 * @return {boolean}
	 */
	uw.ui.Details.prototype.needsPatentAgreement = function ( upload ) {
		var extensions = mw.UploadWizard.config.patents.extensions;

		return $.inArray( upload.title.getExtension().toLowerCase(), extensions ) >= 0;
	};

}( mediaWiki, jQuery, mediaWiki.uploadWizard, OO ) );