| 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 |
ve.ui.LanguageInputWidget = function VeUiLanguageInputWidget( config ) { |
| 26 |
|
| 27 |
config = config || {}; |
| 28 |
|
| 29 |
|
| 30 |
ve.ui.LanguageInputWidget.super.call( this, config ); |
| 31 |
|
| 32 |
|
| 33 |
this.lang = null; |
| 34 |
this.dir = null; |
| 35 |
this.setReadOnly( !!config.readOnly ); |
| 36 |
|
| 37 |
this.dialogs = config.dialogManager || new ve.ui.WindowManager( { factory: ve.ui.windowFactory } ); |
| 38 |
this.availableLanguages = config.availableLanguages; |
| 39 |
|
| 40 |
this.findLanguageButton = new OO.ui.ButtonWidget( { |
| 41 |
classes: [ 've-ui-languageInputWidget-findLanguageButton' ], |
| 42 |
icon: 'ellipsis' |
| 43 |
} ); |
| 44 |
this.findLanguageButton.$button.attr( 'aria-label', ve.msg( 'visualeditor-dialog-language-search-title' ) ); |
| 45 |
this.selectedLanguageLabel = new OO.ui.LabelWidget( { |
| 46 |
classes: [ 've-ui-languageInputWidget-selectedLanguageLabel' ], |
| 47 |
label: ve.msg( 'visualeditor-languageinspector-widget-changelang' ) |
| 48 |
} ); |
| 49 |
this.languageCodeTextInput = new OO.ui.TextInputWidget( { |
| 50 |
classes: [ 've-ui-languageInputWidget-languageCodeTextInput' ] |
| 51 |
} ); |
| 52 |
this.languageCodeTextInput.$input.attr( 'aria-label', ve.msg( 'visualeditor-languageinspector-widget-label-langcode' ) ); |
| 53 |
|
| 54 |
this.directionSelect = new OO.ui.ButtonSelectWidget( { |
| 55 |
classes: [ 've-ui-languageInputWidget-directionSelect' ] |
| 56 |
} ); |
| 57 |
this.directionLabel = new OO.ui.LabelWidget( { |
| 58 |
classes: [ 've-ui-languageInputWidget-directionLabel' ], |
| 59 |
label: ve.msg( 'visualeditor-languageinspector-widget-label-direction' ) |
| 60 |
} ); |
| 61 |
|
| 62 |
const $language = $( '<div>' ).addClass( 've-ui-languageInputWidget-languageInput' ); |
| 63 |
$language.append( |
| 64 |
this.findLanguageButton.$element |
| 65 |
); |
| 66 |
if ( !config.hideCodeInput ) { |
| 67 |
$language.prepend( this.languageCodeTextInput.$element ); |
| 68 |
} |
| 69 |
this.findLanguageButton.$element.before( this.selectedLanguageLabel.$element ); |
| 70 |
|
| 71 |
|
| 72 |
this.findLanguageButton.connect( this, { click: 'onFindLanguageButtonClick' } ); |
| 73 |
this.languageCodeTextInput.connect( this, { change: 'onChange' } ); |
| 74 |
this.directionSelect.connect( this, { select: 'onChange' } ); |
| 75 |
|
| 76 |
|
| 77 |
const dirItems = [ |
| 78 |
new OO.ui.ButtonOptionWidget( { |
| 79 |
data: 'rtl', |
| 80 |
icon: 'textDirRTL' |
| 81 |
} ), |
| 82 |
new OO.ui.ButtonOptionWidget( { |
| 83 |
data: 'ltr', |
| 84 |
icon: 'textDirLTR' |
| 85 |
} ) |
| 86 |
]; |
| 87 |
const dirInput = ( config.dirInput === undefined ) ? 'auto' : config.dirInput; |
| 88 |
|
| 89 |
if ( dirInput === 'auto' ) { |
| 90 |
dirItems.splice( |
| 91 |
1, 0, new OO.ui.ButtonOptionWidget( { |
| 92 |
data: null, |
| 93 |
label: ve.msg( 'visualeditor-dialog-language-auto-direction' ) |
| 94 |
} ) |
| 95 |
); |
| 96 |
} |
| 97 |
this.directionSelect.addItems( dirItems ); |
| 98 |
$( OO.ui.getTeleportTarget() ).append( this.dialogs.$element ); |
| 99 |
|
| 100 |
this.$element |
| 101 |
.addClass( 've-ui-languageInputWidget' ) |
| 102 |
.append( $language ); |
| 103 |
|
| 104 |
if ( dirInput !== 'none' ) { |
| 105 |
this.$element.append( this.directionLabel.$element, this.directionSelect.$element ); |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
OO.inheritClass( ve.ui.LanguageInputWidget, OO.ui.Widget ); |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
ve.ui.LanguageInputWidget.prototype.onFindLanguageButtonClick = function () { |
| 127 |
this.dialogs.openWindow( 'languageSearch', { |
| 128 |
availableLanguages: this.availableLanguages, |
| 129 |
$returnFocusTo: null |
| 130 |
} ).closing.then( ( data ) => { |
| 131 |
data = data || {}; |
| 132 |
if ( data.action === 'done' ) { |
| 133 |
this.setLangAndDir( data.lang, data.dir ); |
| 134 |
} |
| 135 |
} ); |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
ve.ui.LanguageInputWidget.prototype.onChange = function () { |
| 142 |
if ( this.updating ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
const selectedItem = this.directionSelect.findSelectedItem(); |
| 147 |
this.setLangAndDir( |
| 148 |
this.languageCodeTextInput.getValue(), |
| 149 |
selectedItem ? selectedItem.getData() : null |
| 150 |
); |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
ve.ui.LanguageInputWidget.prototype.setLangAndDir = function ( lang, dir ) { |
| 165 |
if ( lang === this.lang && dir === this.dir ) { |
| 166 |
|
| 167 |
return this; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
this.updating = true; |
| 172 |
if ( lang || dir ) { |
| 173 |
lang = lang || ''; |
| 174 |
this.languageCodeTextInput.setValue( lang ); |
| 175 |
this.selectedLanguageLabel.setLabel( |
| 176 |
ve.init.platform.hasLanguageCode( lang.toLowerCase() ) ? |
| 177 |
ve.init.platform.getLanguageName( lang.toLowerCase() ) : |
| 178 |
ve.msg( 'visualeditor-languageinspector-widget-changelang' ) |
| 179 |
); |
| 180 |
this.directionSelect.selectItemByData( dir ); |
| 181 |
} else { |
| 182 |
this.languageCodeTextInput.setValue( '' ); |
| 183 |
this.selectedLanguageLabel.setLabel( |
| 184 |
ve.msg( 'visualeditor-languageinspector-widget-changelang' ) |
| 185 |
); |
| 186 |
this.directionSelect.selectItem( null ); |
| 187 |
} |
| 188 |
|
| 189 |
this.selectedLanguageLabel.setTitle( this.selectedLanguageLabel.$label.text() ); |
| 190 |
this.updating = false; |
| 191 |
|
| 192 |
this.lang = lang; |
| 193 |
this.dir = dir; |
| 194 |
this.emit( 'change', lang, dir ); |
| 195 |
return this; |
| 196 |
}; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
ve.ui.LanguageInputWidget.prototype.getLang = function () { |
| 204 |
return this.lang; |
| 205 |
}; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
ve.ui.LanguageInputWidget.prototype.getDir = function () { |
| 213 |
return this.dir; |
| 214 |
}; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
ve.ui.LanguageInputWidget.prototype.updateControlsDisabled = function () { |
| 224 |
const disabled = this.isDisabled() || this.isReadOnly(); |
| 225 |
if ( this.findLanguageButton ) { |
| 226 |
this.findLanguageButton.setDisabled( disabled ); |
| 227 |
} |
| 228 |
if ( this.directionSelect ) { |
| 229 |
this.directionSelect.setDisabled( disabled ); |
| 230 |
} |
| 231 |
return this; |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
ve.ui.LanguageInputWidget.prototype.setDisabled = function ( disabled ) { |
| 242 |
|
| 243 |
ve.ui.LanguageInputWidget.super.prototype.setDisabled.call( this, disabled ); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if ( this.languageCodeTextInput ) { |
| 248 |
this.languageCodeTextInput.setDisabled( disabled ); |
| 249 |
} |
| 250 |
this.updateControlsDisabled(); |
| 251 |
return this; |
| 252 |
}; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
ve.ui.LanguageInputWidget.prototype.isReadOnly = function () { |
| 260 |
return this.readOnly; |
| 261 |
}; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
ve.ui.LanguageInputWidget.prototype.setReadOnly = function ( readOnly ) { |
| 271 |
this.readOnly = readOnly; |
| 272 |
if ( this.languageCodeTextInput ) { |
| 273 |
this.languageCodeTextInput.setReadOnly( readOnly ); |
| 274 |
} |
| 275 |
this.updateControlsDisabled(); |
| 276 |
return this; |
| 277 |
}; |