| 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 |
const ItemModel = function MwRcfiltersDmItemModel( param, config ) { |
| 27 |
config = config || {}; |
| 28 |
|
| 29 |
|
| 30 |
OO.EventEmitter.call( this ); |
| 31 |
|
| 32 |
this.param = param; |
| 33 |
this.namePrefix = config.namePrefix || 'item_'; |
| 34 |
this.name = this.namePrefix + param; |
| 35 |
|
| 36 |
this.label = config.label || this.name; |
| 37 |
this.labelPrefixKey = config.labelPrefixKey; |
| 38 |
this.description = config.description || ''; |
| 39 |
this.setValue( config.value || config.selected ); |
| 40 |
|
| 41 |
this.identifiers = config.identifiers || []; |
| 42 |
|
| 43 |
|
| 44 |
this.cssClass = config.cssClass; |
| 45 |
this.helpLink = config.helpLink; |
| 46 |
this.highlightColor = config.defaultHighlightColor || null; |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
OO.initClass( ItemModel ); |
| 52 |
OO.mixinClass( ItemModel, OO.EventEmitter ); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
ItemModel.prototype.getState = function () { |
| 71 |
return { |
| 72 |
selected: this.isSelected() |
| 73 |
}; |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
ItemModel.prototype.getName = function () { |
| 82 |
return this.name; |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
ItemModel.prototype.getLabelMessageKey = function ( inverted ) { |
| 92 |
if ( this.labelPrefixKey ) { |
| 93 |
if ( typeof this.labelPrefixKey === 'string' ) { |
| 94 |
return this.labelPrefixKey; |
| 95 |
} |
| 96 |
return this.labelPrefixKey[ |
| 97 |
|
| 98 |
|
| 99 |
inverted && this.isSelected() ? |
| 100 |
'inverted' : 'default' |
| 101 |
]; |
| 102 |
} |
| 103 |
return null; |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
ItemModel.prototype.getParamName = function () { |
| 112 |
return this.param; |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
ItemModel.prototype.getStateMessage = function () { |
| 121 |
|
| 122 |
return this.getDescription(); |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
ItemModel.prototype.getLabel = function () { |
| 131 |
return this.label; |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
ItemModel.prototype.getDescription = function () { |
| 140 |
return this.description; |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
ItemModel.prototype.getDefault = function () { |
| 149 |
return this.default; |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
ItemModel.prototype.isSelected = function () { |
| 158 |
return !!this.value; |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
ItemModel.prototype.toggleSelected = function ( isSelected ) { |
| 168 |
isSelected = isSelected === undefined ? !this.isSelected() : isSelected; |
| 169 |
this.setValue( isSelected ); |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
ItemModel.prototype.getValue = function () { |
| 178 |
return this.value; |
| 179 |
}; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
ItemModel.prototype.coerceValue = function ( value ) { |
| 188 |
return this.getGroupModel().getType() === 'any_value' ? value : !!value; |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
ItemModel.prototype.setValue = function ( newValue ) { |
| 197 |
newValue = this.coerceValue( newValue ); |
| 198 |
if ( this.value !== newValue ) { |
| 199 |
this.value = newValue; |
| 200 |
this.emit( 'update' ); |
| 201 |
} |
| 202 |
}; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
ItemModel.prototype.setHighlightColor = function ( highlightColor ) { |
| 210 |
if ( !this.isHighlightSupported() ) { |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
if ( !this.highlightColor && !highlightColor ) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
if ( this.highlightColor !== highlightColor ) { |
| 219 |
this.highlightColor = highlightColor; |
| 220 |
this.emit( 'update' ); |
| 221 |
} |
| 222 |
}; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
ItemModel.prototype.clearHighlightColor = function () { |
| 228 |
this.setHighlightColor( null ); |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
ItemModel.prototype.getHighlightColor = function () { |
| 237 |
return this.highlightColor; |
| 238 |
}; |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
ItemModel.prototype.getCssClass = function () { |
| 247 |
return this.cssClass; |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
ItemModel.prototype.getHelpLink = function () { |
| 257 |
return this.helpLink; |
| 258 |
}; |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
ItemModel.prototype.getIdentifiers = function () { |
| 266 |
return this.identifiers; |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
ItemModel.prototype.isHighlightSupported = function () { |
| 275 |
return !!this.getCssClass() && !OO.ui.isMobile(); |
| 276 |
}; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
ItemModel.prototype.isHighlighted = function () { |
| 284 |
return !!this.getHighlightColor(); |
| 285 |
}; |
| 286 |
|
| 287 |
module.exports = ItemModel; |