summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.ItemModel.js
blob: d1e40cad90abafe8a7537ed5a6858c743120bf2a (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
259
260
261
262
263
264
265
266
267
268
269
( function ( mw ) {
	/**
	 * RCFilter base item model
	 *
	 * @mixins OO.EventEmitter
	 *
	 * @constructor
	 * @param {string} param Filter param name
	 * @param {Object} config Configuration object
	 * @cfg {string} [label] The label for the filter
	 * @cfg {string} [description] The description of the filter
	 * @cfg {string|Object} [labelPrefixKey] An i18n key defining the prefix label for this
	 *  group. If the prefix has 'invert' state, the parameter is expected to be an object
	 *  with 'default' and 'inverted' as keys.
	 * @cfg {boolean} [active=true] The filter is active and affecting the result
	 * @cfg {boolean} [selected] The item is selected
	 * @cfg {*} [value] The value of this item
	 * @cfg {string} [namePrefix='item_'] A prefix to add to the param name to act as a unique
	 *  identifier
	 * @cfg {string} [cssClass] The class identifying the results that match this filter
	 * @cfg {string[]} [identifiers] An array of identifiers for this item. They will be
	 *  added and considered in the view.
	 * @cfg {string} [defaultHighlightColor] If set, highlight this filter by default with this color
	 */
	mw.rcfilters.dm.ItemModel = function MwRcfiltersDmItemModel( param, config ) {
		config = config || {};

		// Mixin constructor
		OO.EventEmitter.call( this );

		this.param = param;
		this.namePrefix = config.namePrefix || 'item_';
		this.name = this.namePrefix + param;

		this.label = config.label || this.name;
		this.labelPrefixKey = config.labelPrefixKey;
		this.description = config.description || '';
		this.setValue( config.value || config.selected );

		this.identifiers = config.identifiers || [];

		// Highlight
		this.cssClass = config.cssClass;
		this.highlightColor = config.defaultHighlightColor;
	};

	/* Initialization */

	OO.initClass( mw.rcfilters.dm.ItemModel );
	OO.mixinClass( mw.rcfilters.dm.ItemModel, OO.EventEmitter );

	/* Events */

	/**
	 * @event update
	 *
	 * The state of this filter has changed
	 */

	/* Methods */

	/**
	 * Return the representation of the state of this item.
	 *
	 * @return {Object} State of the object
	 */
	mw.rcfilters.dm.ItemModel.prototype.getState = function () {
		return {
			selected: this.isSelected()
		};
	};

	/**
	 * Get the name of this filter
	 *
	 * @return {string} Filter name
	 */
	mw.rcfilters.dm.ItemModel.prototype.getName = function () {
		return this.name;
	};

	/**
	 * Get the message key to use to wrap the label. This message takes the label as a parameter.
	 *
	 * @param {boolean} inverted Whether this item should be considered inverted
	 * @return {string|null} Message key, or null if no message
	 */
	mw.rcfilters.dm.ItemModel.prototype.getLabelMessageKey = function ( inverted ) {
		if ( this.labelPrefixKey ) {
			if ( typeof this.labelPrefixKey === 'string' ) {
				return this.labelPrefixKey;
			}
			return this.labelPrefixKey[
				// Only use inverted-prefix if the item is selected
				// Highlight-only an inverted item makes no sense
				inverted && this.isSelected() ?
					'inverted' : 'default'
			];
		}
		return null;
	};

	/**
	 * Get the param name or value of this filter
	 *
	 * @return {string} Filter param name
	 */
	mw.rcfilters.dm.ItemModel.prototype.getParamName = function () {
		return this.param;
	};

	/**
	 * Get the message representing the state of this model.
	 *
	 * @return {string} State message
	 */
	mw.rcfilters.dm.ItemModel.prototype.getStateMessage = function () {
		// Display description
		return this.getDescription();
	};

	/**
	 * Get the label of this filter
	 *
	 * @return {string} Filter label
	 */
	mw.rcfilters.dm.ItemModel.prototype.getLabel = function () {
		return this.label;
	};

	/**
	 * Get the description of this filter
	 *
	 * @return {string} Filter description
	 */
	mw.rcfilters.dm.ItemModel.prototype.getDescription = function () {
		return this.description;
	};

	/**
	 * Get the default value of this filter
	 *
	 * @return {boolean} Filter default
	 */
	mw.rcfilters.dm.ItemModel.prototype.getDefault = function () {
		return this.default;
	};

	/**
	 * Get the selected state of this filter
	 *
	 * @return {boolean} Filter is selected
	 */
	mw.rcfilters.dm.ItemModel.prototype.isSelected = function () {
		return !!this.value;
	};

	/**
	 * Toggle the selected state of the item
	 *
	 * @param {boolean} [isSelected] Filter is selected
	 * @fires update
	 */
	mw.rcfilters.dm.ItemModel.prototype.toggleSelected = function ( isSelected ) {
		isSelected = isSelected === undefined ? !this.isSelected() : isSelected;
		this.setValue( isSelected );
	};

	/**
	 * Get the value
	 *
	 * @return {*}
	 */
	mw.rcfilters.dm.ItemModel.prototype.getValue = function () {
		return this.value;
	};

	/**
	 * Convert a given value to the appropriate representation based on group type
	 *
	 * @param {*} value
	 * @return {*}
	 */
	mw.rcfilters.dm.ItemModel.prototype.coerceValue = function ( value ) {
		return this.getGroupModel().getType() === 'any_value' ? value : !!value;
	};

	/**
	 * Set the value
	 *
	 * @param {*} newValue
	 */
	mw.rcfilters.dm.ItemModel.prototype.setValue = function ( newValue ) {
		newValue = this.coerceValue( newValue );
		if ( this.value !== newValue ) {
			this.value = newValue;
			this.emit( 'update' );
		}
	};

	/**
	 * Set the highlight color
	 *
	 * @param {string|null} highlightColor
	 */
	mw.rcfilters.dm.ItemModel.prototype.setHighlightColor = function ( highlightColor ) {
		if ( !this.isHighlightSupported() ) {
			return;
		}

		if ( this.highlightColor !== highlightColor ) {
			this.highlightColor = highlightColor;
			this.emit( 'update' );
		}
	};

	/**
	 * Clear the highlight color
	 */
	mw.rcfilters.dm.ItemModel.prototype.clearHighlightColor = function () {
		this.setHighlightColor( null );
	};

	/**
	 * Get the highlight color, or null if none is configured
	 *
	 * @return {string|null}
	 */
	mw.rcfilters.dm.ItemModel.prototype.getHighlightColor = function () {
		return this.highlightColor;
	};

	/**
	 * Get the CSS class that matches changes that fit this filter
	 * or null if none is configured
	 *
	 * @return {string|null}
	 */
	mw.rcfilters.dm.ItemModel.prototype.getCssClass = function () {
		return this.cssClass;
	};

	/**
	 * Get the item's identifiers
	 *
	 * @return {string[]}
	 */
	mw.rcfilters.dm.ItemModel.prototype.getIdentifiers = function () {
		return this.identifiers;
	};

	/**
	 * Check if the highlight feature is supported for this filter
	 *
	 * @return {boolean}
	 */
	mw.rcfilters.dm.ItemModel.prototype.isHighlightSupported = function () {
		return !!this.getCssClass();
	};

	/**
	 * Check if the filter is currently highlighted
	 *
	 * @return {boolean}
	 */
	mw.rcfilters.dm.ItemModel.prototype.isHighlighted = function () {
		return !!this.getHighlightColor();
	};
}( mediaWiki ) );