summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/resources/uw.CopyMetadataWidget.js
blob: 8fd42336dafc4e3396d1ae30eed4cbd995034759 (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
( function ( mw, uw, $, OO ) {

	/**
	 * Metadata copier in UploadWizard's "Details" step form.
	 *
	 * @extends OO.ui.Widget
	 * @constructor
	 * @param {Object} [config] Configuration options
	 * @cfg {mw.UploadWizardUpload} copyFrom Upload to copy the details from
	 * @cfg {mw.UploadWizardUpload[]} copyTo Uploads to copy the details to
	 */
	uw.CopyMetadataWidget = function UWCopyMetadataWidget( config ) {
		var metadataType, defaultStatus, copyMetadataMsg,
			checkboxes = [],
			$copyMetadataWrapperDiv = $( '<div>' ),
			$copyMetadataDiv = $( '<div>' );

		uw.CopyMetadataWidget.parent.call( this );

		this.copyFrom = config.copyFrom;
		this.copyTo = config.copyTo;
		this.savedSerializedData = [];

		for ( metadataType in uw.CopyMetadataWidget.static.copyMetadataTypes ) {
			if ( uw.CopyMetadataWidget.static.copyMetadataTypes.hasOwnProperty( metadataType ) ) {
				defaultStatus = uw.CopyMetadataWidget.static.copyMetadataTypes[ metadataType ];
				// mwe-upwiz-copy-title, mwe-upwiz-copy-caption, mwe-upwiz-copy-description,
				// mwe-upwiz-copy-date, mwe-upwiz-copy-categories, mwe-upwiz-copy-location,
				// mwe-upwiz-copy-other
				copyMetadataMsg = mw.message( 'mwe-upwiz-copy-' + metadataType ).text();

				checkboxes.push( new OO.ui.CheckboxMultioptionWidget( {
					data: metadataType,
					label: copyMetadataMsg,
					selected: defaultStatus
				} ) );
			}
		}

		this.$success = $( '<span>' );
		this.checkboxesWidget = new OO.ui.CheckboxMultiselectWidget( {
			items: checkboxes
		} );
		this.copyButton = new OO.ui.ButtonWidget( {
			label: mw.message( 'mwe-upwiz-copy-metadata-button' ).text(),
			flags: [ 'progressive' ]
		} );
		this.undoButton = new OO.ui.ButtonWidget( {
			label: mw.message( 'mwe-upwiz-copy-metadata-button-undo' ).text()
		} );

		this.checkboxesWidget.connect( this, {
			select: 'onCheckboxesSelect'
		} );
		this.copyButton.connect( this, {
			click: 'onCopyClick'
		} );
		this.undoButton.connect( this, {
			click: 'onUndoClick'
		} );

		this.undoButton.toggle( false );
		$copyMetadataDiv.append(
			this.checkboxesWidget.$element,
			this.copyButton.$element,
			this.undoButton.$element,
			this.$success
		);

		$copyMetadataWrapperDiv
			.append(
				$( '<a>' ).text( mw.msg( 'mwe-upwiz-copy-metadata' ) )
					.addClass( 'mwe-upwiz-details-copy-metadata mw-collapsible-toggle mw-collapsible-arrow' ),
				$copyMetadataDiv.addClass( 'mw-collapsible-content' )
			)
			.makeCollapsible( { collapsed: true } );

		this.$element
			.addClass( 'mwe-upwiz-copyMetadataWidget' )
			.append( $copyMetadataWrapperDiv );
	};
	OO.inheritClass( uw.CopyMetadataWidget, OO.ui.Widget );

	/**
	 * Metadata which we can copy over to other details objects.
	 *
	 * Object with key: metadata name and value: boolean value indicating default checked status
	 *
	 * @property {Object}
	 * @static
	 */
	uw.CopyMetadataWidget.static.copyMetadataTypes = {
		title: true,
		caption: true,
		description: true,
		date: false,
		categories: true,
		location: false,
		other: true
	};

	/**
	 * Checkbox multiselect widget select event handler.
	 *
	 * @private
	 */
	uw.CopyMetadataWidget.prototype.onCheckboxesSelect = function () {
		this.copyButton.setDisabled( this.checkboxesWidget.findSelectedItemsData().length === 0 );
	};

	/**
	 * Button click event handler.
	 *
	 * @private
	 */
	uw.CopyMetadataWidget.prototype.onCopyClick = function () {
		var metadataTypes = this.checkboxesWidget.findSelectedItemsData();
		this.copyMetadata( metadataTypes );

		this.undoButton.toggle( true );
		this.$success
			.text( mw.message( 'mwe-upwiz-copied-metadata' ).text() )
			.show()
			.fadeOut( 5000, 'linear' );
	};

	/**
	 * Button click event handler.
	 *
	 * @private
	 */
	uw.CopyMetadataWidget.prototype.onUndoClick = function () {
		this.restoreMetadata();

		this.undoButton.toggle( false );
		this.$success
			.text( mw.message( 'mwe-upwiz-undid-metadata' ).text() )
			.show()
			.fadeOut( 5000, 'linear' );
	};

	/**
	 * Copy metadata from the first upload to other uploads.
	 *
	 * @param {string[]} metadataTypes Types to copy, as defined in the copyMetadataTypes property
	 */
	uw.CopyMetadataWidget.prototype.copyMetadata = function ( metadataTypes ) {
		var titleZero, matches, i,
			uploads = this.copyTo,
			sourceUpload = this.copyFrom,
			serialized = sourceUpload.details.getSerialized(),
			// Values to copy
			sourceValue = {},
			// Checks for extra behaviors
			copyingTitle = false,
			copyingOther = false;

		// Filter serialized data to only the types we want to copy
		metadataTypes.forEach( function ( type ) {
			sourceValue[ type ] = serialized[ type ];
			copyingTitle = copyingTitle || type === 'title';
			copyingOther = copyingOther || type === 'other';
		} );

		if ( copyingOther ) {
			// Campaign fields are grouped with this, hmph
			sourceValue.campaigns = serialized.campaigns;
		}

		if ( copyingTitle ) {
			titleZero = sourceValue.title.title;
			// Add number suffix to first title if no numbering present
			matches = titleZero.match( /(\D+)(\d{1,3})(\D*)$/ );
			if ( matches === null ) {
				titleZero = titleZero + ' 01';
			}
		}

		// And apply
		for ( i = 0; i < uploads.length; i++ ) {
			if ( copyingTitle ) {
				// Overwrite remaining title inputs with first title + increment of rightmost
				// number in the title. Note: We ignore numbers with more than three digits, because these
				// are more likely to be years ("Wikimania 2011 Celebration") or other non-sequence
				// numbers.
				sourceValue.title.title = titleZero.replace( /(\D+)(\d{1,3})(\D*)$/,
					// eslint-disable-next-line no-loop-func
					function ( str, m1, m2, m3 ) {
						var newstr = String( +m2 + i );
						return m1 + new Array( m2.length + 1 - newstr.length )
							.join( '0' ) + newstr + m3;
					}
				);
			}

			this.savedSerializedData[ i ] = uploads[ i ].details.getSerialized();
			uploads[ i ].details.setSerialized( sourceValue );
		}
	};

	/**
	 * Restore previously saved metadata that we backed up when copying.
	 */
	uw.CopyMetadataWidget.prototype.restoreMetadata = function () {
		var i,
			uploads = this.copyTo;

		for ( i = 0; i < uploads.length; i++ ) {
			uploads[ i ].details.setSerialized( this.savedSerializedData[ i ] );
		}
	};

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