| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
( function () { |
| 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 |
mw.widgets.UsersMultiselectWidget = function MwWidgetsUsersMultiselectWidget( config ) { |
| 37 |
|
| 38 |
config = Object.assign( { |
| 39 |
limit: 10, |
| 40 |
ipAllowed: false, |
| 41 |
ipRangeAllowed: false, |
| 42 |
ipRangeLimits: { |
| 43 |
IPv4: 16, |
| 44 |
IPv6: 32 |
| 45 |
}, |
| 46 |
excludeNamed: false, |
| 47 |
excludeTemp: false |
| 48 |
}, config ); |
| 49 |
|
| 50 |
|
| 51 |
mw.widgets.UsersMultiselectWidget.super.call( this, Object.assign( {}, config, {} ) ); |
| 52 |
|
| 53 |
|
| 54 |
OO.ui.mixin.PendingElement.call( this, Object.assign( {}, config, { $pending: this.$handle } ) ); |
| 55 |
|
| 56 |
|
| 57 |
this.limit = config.limit; |
| 58 |
this.ipAllowed = config.ipAllowed; |
| 59 |
this.ipRangeAllowed = config.ipRangeAllowed; |
| 60 |
this.ipRangeLimits = config.ipRangeLimits; |
| 61 |
this.excludeNamed = config.excludeNamed; |
| 62 |
this.excludeTemp = config.excludeTemp; |
| 63 |
|
| 64 |
if ( 'name' in config ) { |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
this.$hiddenInput = $( '<textarea>' ) |
| 69 |
.addClass( 'oo-ui-element-hidden' ) |
| 70 |
.attr( 'name', config.name ) |
| 71 |
.appendTo( this.$element ); |
| 72 |
|
| 73 |
this.updateHiddenInput(); |
| 74 |
|
| 75 |
this.$hiddenInput.prop( 'defaultValue', this.getSelectedUsernames().join( '\n' ) ); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
this.connect( this, { |
| 81 |
change: 'updateHiddenInput' |
| 82 |
} ); |
| 83 |
|
| 84 |
|
| 85 |
this.api = config.api || new mw.Api(); |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
OO.inheritClass( mw.widgets.UsersMultiselectWidget, OO.ui.MenuTagMultiselectWidget ); |
| 91 |
OO.mixinClass( mw.widgets.UsersMultiselectWidget, OO.ui.mixin.PendingElement ); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
mw.widgets.UsersMultiselectWidget.prototype.getSelectedUsernames = function () { |
| 101 |
return this.getValue(); |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
mw.widgets.UsersMultiselectWidget.prototype.updateMenuItems = function () { |
| 110 |
const inputValue = this.input.getValue(); |
| 111 |
|
| 112 |
if ( inputValue === this.inputValue ) { |
| 113 |
|
| 114 |
return; |
| 115 |
} else { |
| 116 |
this.inputValue = inputValue; |
| 117 |
} |
| 118 |
|
| 119 |
this.api.abort(); |
| 120 |
|
| 121 |
if ( inputValue.length > 0 ) { |
| 122 |
this.pushPending(); |
| 123 |
|
| 124 |
let isValidIp, isValidRange; |
| 125 |
if ( this.ipAllowed || this.ipRangeAllowed ) { |
| 126 |
isValidIp = mw.util.isIPAddress( inputValue, false ); |
| 127 |
isValidRange = !isValidIp && |
| 128 |
mw.util.isIPAddress( inputValue, true ) && |
| 129 |
this.validateIpRange( inputValue ); |
| 130 |
} |
| 131 |
|
| 132 |
if ( this.ipAllowed && isValidIp || this.ipRangeAllowed && isValidRange ) { |
| 133 |
this.menu.clearItems(); |
| 134 |
this.menu.addItems( [ |
| 135 |
new OO.ui.MenuOptionWidget( { |
| 136 |
data: inputValue, |
| 137 |
label: inputValue |
| 138 |
} ) |
| 139 |
] ); |
| 140 |
this.menu.toggle( true ); |
| 141 |
this.popPending(); |
| 142 |
} else { |
| 143 |
this.api.get( { |
| 144 |
action: 'query', |
| 145 |
list: 'allusers', |
| 146 |
auprefix: inputValue, |
| 147 |
aulimit: this.limit, |
| 148 |
auexcludenamed: this.excludeNamed, |
| 149 |
auexcludetemp: this.excludeTemp |
| 150 |
} ).done( ( response ) => { |
| 151 |
let suggestions = response.query.allusers; |
| 152 |
|
| 153 |
const selected = this.getSelectedUsernames(); |
| 154 |
|
| 155 |
|
| 156 |
suggestions = suggestions.map( ( user ) => { |
| 157 |
if ( selected.indexOf( user.name ) === -1 ) { |
| 158 |
return new OO.ui.MenuOptionWidget( { |
| 159 |
data: user.name, |
| 160 |
label: user.name, |
| 161 |
id: user.name |
| 162 |
} ); |
| 163 |
} |
| 164 |
return undefined; |
| 165 |
} ).filter( ( item ) => item !== undefined ); |
| 166 |
|
| 167 |
|
| 168 |
this.menu.clearItems(); |
| 169 |
this.menu.addItems( suggestions ); |
| 170 |
|
| 171 |
if ( suggestions.length ) { |
| 172 |
|
| 173 |
this.menu.$focusOwner.attr( 'aria-activedescendant', suggestions[ 0 ].$element.attr( 'id' ) ); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
this.menu.toggle( true ); |
| 178 |
|
| 179 |
this.popPending(); |
| 180 |
} ).fail( this.popPending.bind( this ) ); |
| 181 |
} |
| 182 |
|
| 183 |
} else { |
| 184 |
this.menu.clearItems(); |
| 185 |
} |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
mw.widgets.UsersMultiselectWidget.prototype.validateIpRange = function ( ipRange ) { |
| 194 |
ipRange = ipRange.split( '/' ); |
| 195 |
|
| 196 |
return mw.util.isIPv4Address( ipRange[ 0 ] ) && +ipRange[ 1 ] >= this.ipRangeLimits.IPv4 || |
| 197 |
mw.util.isIPv6Address( ipRange[ 0 ] ) && +ipRange[ 1 ] >= this.ipRangeLimits.IPv6; |
| 198 |
}; |
| 199 |
|
| 200 |
mw.widgets.UsersMultiselectWidget.prototype.onInputChange = function () { |
| 201 |
mw.widgets.UsersMultiselectWidget.super.prototype.onInputChange.apply( this, arguments ); |
| 202 |
|
| 203 |
this.updateMenuItems(); |
| 204 |
}; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
mw.widgets.UsersMultiselectWidget.prototype.updateHiddenInput = function () { |
| 213 |
if ( '$hiddenInput' in this ) { |
| 214 |
this.$hiddenInput.val( this.getSelectedUsernames().join( '\n' ) ); |
| 215 |
|
| 216 |
|
| 217 |
this.$hiddenInput.trigger( 'change' ); |
| 218 |
} |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
mw.widgets.UsersMultiselectWidget.prototype.onTagSelect = function () { |
| 228 |
if ( this.hasInput && !this.input.getValue() ) { |
| 229 |
OO.ui.TagMultiselectWidget.prototype.onTagSelect.apply( this, arguments ); |
| 230 |
} |
| 231 |
}; |
| 232 |
|
| 233 |
}() ); |