| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
mw.widgets.SelectWithInputWidget = function MwWidgetsSelectWithInputWidget( config ) { |
| 46 |
|
| 47 |
config = Object.assign( { or: false, required: false }, config ); |
| 48 |
|
| 49 |
|
| 50 |
this.textinput = new OO.ui.TextInputWidget( config.textinput ); |
| 51 |
this.dropdowninput = new OO.ui.DropdownInputWidget( config.dropdowninput ); |
| 52 |
this.or = config.or; |
| 53 |
this.required = config.required; |
| 54 |
|
| 55 |
|
| 56 |
this.dropdowninput.on( 'change', this.onChange.bind( this ) ); |
| 57 |
this.textinput.on( 'change', () => { |
| 58 |
this.emit( 'change', this.getValue() ); |
| 59 |
} ); |
| 60 |
|
| 61 |
|
| 62 |
mw.widgets.SelectWithInputWidget.super.call( this, config ); |
| 63 |
|
| 64 |
|
| 65 |
this.$element |
| 66 |
.addClass( 'mw-widget-selectWithInputWidget' ) |
| 67 |
.append( |
| 68 |
this.dropdowninput.$element, |
| 69 |
this.textinput.$element |
| 70 |
); |
| 71 |
this.onChange(); |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
OO.inheritClass( mw.widgets.SelectWithInputWidget, OO.ui.Widget ); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
mw.widgets.SelectWithInputWidget.static.reusePreInfuseDOM = function ( node, config ) { |
| 83 |
config = mw.widgets.SelectWithInputWidget.super.static.reusePreInfuseDOM( node, config ); |
| 84 |
config.dropdowninput = OO.ui.DropdownInputWidget.static.reusePreInfuseDOM( |
| 85 |
$( node ).find( '.oo-ui-dropdownInputWidget' ), |
| 86 |
config.dropdowninput |
| 87 |
); |
| 88 |
config.textinput = OO.ui.TextInputWidget.static.reusePreInfuseDOM( |
| 89 |
$( node ).find( '.oo-ui-textInputWidget' ), |
| 90 |
config.textinput |
| 91 |
); |
| 92 |
return config; |
| 93 |
}; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
mw.widgets.SelectWithInputWidget.static.gatherPreInfuseState = function ( node, config ) { |
| 99 |
const state = mw.widgets.SelectWithInputWidget.super.static.gatherPreInfuseState( node, config ); |
| 100 |
state.dropdowninput = OO.ui.DropdownInputWidget.static.gatherPreInfuseState( |
| 101 |
$( node ).find( '.oo-ui-dropdownInputWidget' ), |
| 102 |
config.dropdowninput |
| 103 |
); |
| 104 |
state.textinput = OO.ui.TextInputWidget.static.gatherPreInfuseState( |
| 105 |
$( node ).find( '.oo-ui-textInputWidget' ), |
| 106 |
config.textinput |
| 107 |
); |
| 108 |
return state; |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
mw.widgets.SelectWithInputWidget.prototype.restorePreInfuseState = function ( state ) { |
| 117 |
mw.widgets.SelectWithInputWidget.super.prototype.restorePreInfuseState.call( this, state ); |
| 118 |
this.dropdowninput.restorePreInfuseState( state.dropdowninput ); |
| 119 |
this.textinput.restorePreInfuseState( state.textinput ); |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
mw.widgets.SelectWithInputWidget.prototype.setDisabled = function ( disabled ) { |
| 126 |
const textinputIsHidden = this.or && this.dropdowninput.getValue() !== 'other'; |
| 127 |
mw.widgets.SelectWithInputWidget.super.prototype.setDisabled.call( this, disabled ); |
| 128 |
this.dropdowninput.setDisabled( disabled ); |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
this.textinput.setDisabled( textinputIsHidden || disabled ); |
| 133 |
|
| 134 |
if ( this.required ) { |
| 135 |
this.textinput.setRequired( !this.textinput.isDisabled() ); |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
mw.widgets.SelectWithInputWidget.prototype.setValue = function ( value ) { |
| 145 |
let selectable = false; |
| 146 |
|
| 147 |
if ( this.or ) { |
| 148 |
if ( value !== 'other' ) { |
| 149 |
selectable = !!this.dropdowninput.dropdownWidget.getMenu().findItemFromData( value ); |
| 150 |
} |
| 151 |
|
| 152 |
if ( selectable ) { |
| 153 |
this.dropdowninput.setValue( value ); |
| 154 |
this.textinput.setValue( undefined ); |
| 155 |
} else { |
| 156 |
this.dropdowninput.setValue( 'other' ); |
| 157 |
this.textinput.setValue( value ); |
| 158 |
} |
| 159 |
|
| 160 |
this.emit( 'change', value ); |
| 161 |
} |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
mw.widgets.SelectWithInputWidget.prototype.getValue = function () { |
| 170 |
if ( this.or ) { |
| 171 |
if ( this.dropdowninput.getValue() !== 'other' ) { |
| 172 |
return this.dropdowninput.getValue(); |
| 173 |
} |
| 174 |
|
| 175 |
return this.textinput.getValue(); |
| 176 |
} else { |
| 177 |
return ''; |
| 178 |
} |
| 179 |
}; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
mw.widgets.SelectWithInputWidget.prototype.onChange = function ( value ) { |
| 188 |
if ( this.or ) { |
| 189 |
value = value || this.dropdowninput.getValue(); |
| 190 |
this.textinput.$element.toggle( value === 'other' ); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
this.textinput.setDisabled( value !== 'other' || this.isDisabled() ); |
| 195 |
} |
| 196 |
|
| 197 |
this.emit( 'change', this.getValue() ); |
| 198 |
}; |
| 199 |
|
| 200 |
}() ); |