summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/resources/controller/uw.controller.Deed.js
blob: 6c875f9f8c99f72e5aad9dc9d26a722c304375e5 (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
/*
 * 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 ) {
	/**
	 * Deed step controller.
	 *
	 * @class
	 * @extends uw.controller.Step
	 * @param {mw.Api} api
	 * @param {Object} config UploadWizard config object.
	 */
	uw.controller.Deed = function UWControllerDeed( api, config ) {
		uw.controller.Step.call(
			this,
			new uw.ui.Deed(),
			api,
			config
		);

		this.stepName = 'deeds';

		this.deeds = {};
	};

	OO.inheritClass( uw.controller.Deed, uw.controller.Step );

	uw.controller.Deed.prototype.moveNext = function () {
		var
			deedController = this,
			valid, fields, validityPromises;

		if ( !this.deedChooser ) {
			uw.controller.Step.prototype.moveNext.call( this );
			return;
		}

		valid = this.deedChooser.valid();
		if ( valid ) {
			fields = this.deedChooser.deed.getFields();
			validityPromises = fields.map( function ( fieldLayout ) {
				// Update any error/warning messages
				return fieldLayout.checkValidity( true );
			} );
			if ( validityPromises.length === 1 ) {
				// validityPromises will hold all promises for all uploads;
				// adding a bogus promise (no warnings & errors) to
				// ensure $.when always resolves with an array of multiple
				// results (if there's just 1, it would otherwise have just
				// that one's arguments, instead of a multi-dimensional array
				// of upload warnings & failures)
				validityPromises.push( $.Deferred().resolve( [], [] ).promise() );
			}

			$.when.apply( $, validityPromises ).then( function () {
				// `arguments` will be an array of all fields, with their warnings & errors
				// e.g. `[[something], []], [[], [something]]` for 2 fields, where the first one has
				// a warning and the last one an error

				// TODO Handle warnings with a confirmation dialog

				var i;
				for ( i = 0; i < arguments.length; i++ ) {
					if ( arguments[ i ][ 1 ].length ) {
						// One of the fields has errors; refuse to proceed!
						return;
					}
				}

				uw.controller.Step.prototype.moveNext.call( deedController );
			} );
		}
	};

	/**
	 * Move to this step.
	 *
	 * @param {mw.UploadWizardUpload[]} uploads
	 */
	uw.controller.Deed.prototype.load = function ( uploads ) {
		var customDeed, previousDeed, fromStepName,
			showDeed = false;

		$.each( uploads, function ( i, upload ) {
			fromStepName = upload.state;
			if ( !upload.file.fromURL ) {
				showDeed = true;
				return false;
			}
		} );

		uw.controller.Step.prototype.load.call( this, uploads );

		// If all of the uploads are from URLs, then we know the licenses
		// already, we don't need this step.
		if ( !showDeed ) {
			uw.eventFlowLogger.logSkippedStep( this.stepName );
			// this is a bit of a hack: when images from flickr are uploaded, we
			// don't get to choose the license anymore, and this step will be
			// skipped ... but we could reach this step from either direction
			if ( fromStepName === 'details' ) {
				this.movePrevious();
			} else {
				this.moveNext();
			}
			return;
		}

		// grab a serialized copy of previous deeds' details (if any)
		if ( this.deedChooser ) {
			previousDeed = this.deedChooser.getSerialized();
		}

		this.deeds = mw.UploadWizard.getLicensingDeeds( this.uploads, this.config );

		// if we have multiple uploads, also give them the option to set
		// licenses individually
		if ( this.uploads.length > 1 && this.shouldShowIndividualDeed( this.config ) ) {
			customDeed = new uw.deed.Custom( this.config );
			this.deeds[ customDeed.name ] = customDeed;
		}

		this.deedChooser = new mw.UploadWizardDeedChooser(
			this.config,
			'#mwe-upwiz-deeds',
			this.deeds,
			this.uploads
		);

		$( '<div>' )
			.insertBefore( this.deedChooser.$selector.find( '.mwe-upwiz-deed-ownwork' ) )
			.msg( 'mwe-upwiz-deeds-macro-prompt', this.uploads.length, mw.user );

		$.each( uploads, function ( i, upload ) {
			// Add previews and details to the DOM
			if ( !upload.file.fromURL ) {
				upload.deedPreview = new uw.ui.DeedPreview( upload );
			}
		} );

		this.deedChooser.onLayoutReady();

		// restore the previous input (if any) for all deeds
		if ( previousDeed ) {
			this.deedChooser.setSerialized( previousDeed );
		}
	};

	/**
	 * Check whether we should give the user the option to choose licenses for
	 * individual files on the details step.
	 *
	 * @private
	 * @param {Object} config
	 * @return {boolean}
	 */
	uw.controller.Deed.prototype.shouldShowIndividualDeed = function ( config ) {
		var ownWork;

		if ( config.licensing.ownWorkDefault === 'choice' ) {
			return true;
		} else if ( config.licensing.ownWorkDefault === 'own' ) {
			ownWork = config.licensing.ownWork;
			return ownWork.licenses.length > 1;
		} else {
			return true; // TODO: might want to have similar behaviour here
		}
	};

	/**
	 * @param {UploadWizardUpload} upload
	 */
	uw.controller.Deed.prototype.removeUpload = function ( upload ) {
		uw.controller.Step.prototype.removeUpload.call( this, upload );

		if ( upload.deedPreview ) {
			upload.deedPreview.remove();
		}
	};

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