summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki.rcfilters/mw.rcfilters.js
blob: c62d6f25abde5deca386c1167b9f35ab142b8c95 (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
( function ( mw ) {
	/**
	 * @class
	 * @singleton
	 */
	mw.rcfilters = {
		dm: {},
		ui: {},
		utils: {
			addArrayElementsUnique: function ( arr, elements ) {
				elements = Array.isArray( elements ) ? elements : [ elements ];

				elements.forEach( function ( element ) {
					if ( arr.indexOf( element ) === -1 ) {
						arr.push( element );
					}
				} );

				return arr;
			},
			normalizeParamOptions: function ( givenOptions, legalOptions ) {
				var result = [];

				if ( givenOptions.indexOf( 'all' ) > -1 ) {
					// If anywhere in the values there's 'all', we
					// treat it as if only 'all' was selected.
					// Example: param=valid1,valid2,all
					// Result: param=all
					return [ 'all' ];
				}

				// Get rid of any dupe and invalid parameter, only output
				// valid ones
				// Example: param=valid1,valid2,invalid1,valid1
				// Result: param=valid1,valid2
				givenOptions.forEach( function ( value ) {
					if (
						legalOptions.indexOf( value ) > -1 &&
						result.indexOf( value ) === -1
					) {
						result.push( value );
					}
				} );

				return result;
			}
		}
	};
}( mediaWiki ) );