Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 193x 1x 1x 1x 1x 1x 1x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 44x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 54x 54x 54x 1x 1x 1x 1x 1x 1x 1x 1x 1x 58x 1x 1x 1x 1x 1x 1x 1x 1x 51x 2x 2x 49x 49x 49x 49x 51x 12x 12x 12x 51x 29x 29x 29x 51x 8x 51x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 142x 142x 142x 142x 142x 142x 41x 41x 41x 1x 1x 1x 1x 1x 1x 1x 1x 106x 1x 1x 1x 1x 1x 1x 1x 1x 38x 114x 114x 114x 38x 1x 1x 1x 1x 1x 1x 1x 1x 33x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 34x 8x 8x 26x 34x 6x 34x 8x 34x 10x 34x 2x 34x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 17x 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 10x 10x 10x 10x 10x 10x 10x 9x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 11x 11x 11x 11x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 9x 9x 9x 9x 9x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 152x 152x 152x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 2x 2x 12x 12x 14x 12x 12x 12x 12x 12x 12x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 1x 1x 37x 37x 4x 4x 37x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | <!-- WikiLambda Vue interface module for selecting any ZObject, with lookup on name. Receives an input parameter to filter the type of ZObjects that it will search and display (e.g. Z4 for selecting only types) @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt @license MIT --> <template> <span class="ext-wikilambda-app-object-selector" data-testid="z-object-selector"> <cdx-select v-if="isEnum" :selected="selectedValue" :disabled="disabled" :menu-items="enumValues" :menu-config="selectConfig" @update:selected="onSelect" @load-more="loadMoreValues" ></cdx-select> <cdx-lookup v-else v-model:input-value="inputValue" :selected="selectedValue" :disabled="disabled" :placeholder="lookupPlaceholder" :menu-items="menuItems" :menu-config="lookupConfig" :end-icon="lookupIcon" :status="errorLookupStatus" data-testid="z-object-selector-lookup" @update:selected="onSelect" @update:input-value="onInput" @blur="onBlur" > <template #no-results> {{ $i18n( 'wikilambda-zobjectselector-no-results' ).text() }} </template> </cdx-lookup> <div v-if="hasFieldErrors" class="ext-wikilambda-app-object-selector__errors" > <cdx-message v-for="( error, index ) in fieldErrors" :key="'field-error-' + rowId + '-' + index" :type="error.type" :inline="true" > <!-- eslint-disable vue/no-v-html --> <div v-html="getErrorMessage( error )"></div> </cdx-message> </div> </span> </template> <script> const { CdxLookup, CdxSelect, CdxMessage } = require( '@wikimedia/codex' ); const { defineComponent } = require( 'vue' ); const Constants = require( '../../Constants.js' ), errorUtils = require( '../../mixins/errorUtils.js' ), typeUtils = require( '../../mixins/typeUtils.js' ), { mapActions, mapGetters } = require( 'vuex' ), icons = require( '../../../lib/icons.json' ); module.exports = exports = defineComponent( { name: 'wl-z-object-selector', components: { 'cdx-message': CdxMessage, 'cdx-select': CdxSelect, 'cdx-lookup': CdxLookup }, mixins: [ errorUtils, typeUtils ], props: { rowId: { type: Number, default: 0 }, selectedZid: { type: String, default: '' }, disabled: { type: Boolean, required: false, default: false }, type: { type: String, default: '' }, returnType: { type: String, default: '' }, strictType: { type: Boolean, required: false, default: false }, placeholder: { type: String, default: '' }, /** * List of Zids to exclude from selection and from * the lookup results. Must be in uppercase. */ excludeZids: { type: Array, default: function () { return []; }, required: false } }, emits: [ 'input' ], data: function () { return { inputValue: '', lookupResults: [], lookupConfig: { boldLabel: true, searchQuery: '', visibleItemLimit: 5 }, lookupDelayTimer: null, lookupDelayMs: 300, selectConfig: { visibleItemLimit: 5 } }; }, computed: Object.assign( mapGetters( [ 'getLabelData', 'getEnumValues', 'isEnumType', 'getUserRequestedLang' ] ), { /** * Value model for the internal codex lookup component. It * must be null or the value (Zid) of the selected MenuItem. * * @return {string} */ selectedValue: function () { return this.selectedZid || null; }, /** * Human readable label for the selected Zid in the * user language or closest fallback. If the label is * not available it returns the input. * * @return {string} */ selectedLabel: function () { return this.selectedZid ? this.getLabelData( this.selectedZid ).label : ''; }, /** * Returns lookupResults if there are any items, * else returns the suggestions for the type. * * @return {Array} */ menuItems: function () { return this.lookupResults.length > 0 ? this.lookupResults : this.suggestions; }, /** * Returns the computed suggestions for the given type. * * @return {Array} */ suggestions: function () { if ( !this.type ) { return []; } let title = ''; let suggestedZids = []; switch ( this.type ) { case Constants.Z_NATURAL_LANGUAGE: title = this.$i18n( 'wikilambda-object-selector-suggested-languages' ).text(); suggestedZids = Constants.SUGGESTIONS.LANGUAGES; break; case Constants.Z_TYPE: title = this.$i18n( 'wikilambda-object-selector-suggested-types' ).text(); suggestedZids = Constants.SUGGESTIONS.TYPES; break; default: return []; } const suggestions = []; // Add dropdown title suggestions.push( { label: title, value: 'suggestion', disabled: true } ); // Add one item per suggested Zid suggestedZids.forEach( ( zid ) => { suggestions.push( { value: zid, label: this.getLabelData( zid ).label, description: this.getLabelData( this.type ).label, class: 'ext-wikilambda-app-object-selector__suggestion' } ); } ); return suggestions; }, /** * Returns whether the type is an enumeration * * @return {boolean} */ isEnum: function () { return this.isEnumType( this.type ); }, /** * Returns the menu items for the enumeration value selector * * @return {Array} */ enumValues: function () { return this.getEnumValues( this.type ).map( ( item ) => { const value = item.page_title; const label = this.getLabelOrZid( value, item.label ); return { value, label }; } ); }, /** * Icon to display at the end of the Codex Lookup selector * * @return {string} */ lookupIcon: function () { return icons.cdxIconExpand; }, /** * Returns the placeholder for the Lookup selector, either * the value passed as an input prop, or a built-in message * depending on the type. * * @return {string} */ lookupPlaceholder: function () { if ( this.placeholder ) { return this.placeholder; } switch ( this.type ) { case Constants.Z_FUNCTION: return this.$i18n( 'wikilambda-function-typeselector-label' ).text(); case Constants.Z_NATURAL_LANGUAGE: return this.$i18n( 'wikilambda-editor-label-addlanguage-label' ).text(); case Constants.Z_TYPE: return this.$i18n( 'wikilambda-typeselector-label' ).text(); default: return this.$i18n( 'wikilambda-zobjectselector-label' ).text(); } }, /** * Status property for the Lookup component (ValidateStatusType). * Can take the values 'default' or 'error': * https://doc.wikimedia.org/codex/latest/components/types-and-constants.html#validationstatustype * * @return {string} */ errorLookupStatus: function () { return this.hasFieldErrors ? 'error' : 'default'; } } ), methods: Object.assign( {}, mapActions( [ 'lookupZObjectLabels', 'fetchEnumValues', 'fetchZids' ] ), { /** * Load more values for the enumeration selector * * @return {void} */ loadMoreValues: function () { this.fetchEnumValues( { type: this.type, isContinue: true } ); }, /** * Whether is in the input list of Zids excluded from selection. * Al the zids in the excludeZids input property must be uppercase. * * @param {string} zid * @return {boolean} */ isExcludedZid: function ( zid ) { return ( this.excludeZids.includes( zid ) || this.isDisallowedType( zid ) ); }, /** * Whether is in the built-in list of Zids excluded from selection. * * @param {string} zid * @return {boolean} */ isDisallowedType: function ( zid ) { return ( ( this.type === Constants.Z_TYPE ) && Constants.EXCLUDE_FROM_SELECTOR.includes( zid ) ); }, /** * Handle get zObject lookup. * update lookup results with label and update in store. * * @param {string} input */ getLookupResults: function ( input ) { this.lookupZObjectLabels( { input, type: this.type || undefined, returnType: this.returnType || undefined, strictType: this.strictType } ).then( ( payload ) => { // If the string searched has changed, do not show the search result if ( !this.inputValue.includes( input ) ) { return; } const zids = []; this.lookupConfig.searchQuery = input; this.lookupResults = []; // Update lookupResults list if ( payload && payload.length > 0 ) { payload.forEach( ( result ) => { // Set up codex MenuItem options // https://doc.wikimedia.org/codex/latest/components/demos/menu-item.html const value = result.page_title; const label = this.getLabelOrZid( value, result.label ); const description = this.getLabelOrZid( result.page_type, result.type_label ); const supportingText = ( label !== result.match_label ) ? `(${ result.match_label })` : ''; // If return type is set, reflect mode with icon let icon; if ( this.returnType ) { icon = ( result.page_type === this.type ) ? icons.cdxIconInstance : icons.cdxIconFunction; } // Exclude everything in the exclude Zids and disallowed types lists if ( !this.isExcludedZid( value ) ) { this.lookupResults.push( { value, label, description, supportingText, icon } ); } // Gather all zids to request them for the data store zids.push( value ); } ); // Once lookupResults are gathered, fetch and collect all the data; // fetchZids makes sure that only the missing zids are requested this.fetchZids( { zids } ); } } ); }, /** * Returns the value when the user requested language is 'qqx', * otherwise returns the label. * * @param {string} value * @param {string} label * @return {Object} */ getLabelOrZid: function ( value, label ) { // If the requested language is 'qqx', return (value/zid) as the label if ( this.getUserRequestedLang === 'qqx' ) { return `(${ value })`; } return label; }, /** * Clears the ZObjectSelector lookup results. * This doesn't clear the component TextInput. */ clearResults: function () { this.lookupResults = []; }, /** * On field input, perform a backend lookup and set the lookupResults * array. When searching for a Zid, validate and select. * * @param {string} input */ onInput: function ( input ) { this.clearFieldErrors(); // If empty input, clear and exit if ( !input ) { this.clearResults(); return; } // Just search if more than one characters if ( input.length < 2 ) { return; } // Search after 300 ms clearTimeout( this.lookupDelayTimer ); this.lookupDelayTimer = setTimeout( () => { this.getLookupResults( input ); }, this.lookupDelayMs ); }, /** * Model update event, sets the value of the field * either with an empty value or with a selected value * from the menu. * * @param {string | null} value */ onSelect: function ( value ) { // T374246: update:selected events are emitted with null value // whenever input changes, so we need to exit early whenever // selected value is null, instead of setting the value to empty // for now. When Codex fixes this issue, we'll be able to remove // the following lines and restore the clear behavior. if ( value === null ) { return; } // If the already selected value is selected again, exit early if ( this.selectedValue === value ) { return; } // If we select a new value, clear errors and emit input event // Once the parent responds to the input event and updates the // selected value, the computed property selectedValue will be // updated. this.clearFieldErrors(); this.$emit( 'input', value || '' ); }, /** * On Lookup blur, make sure that the input and the selected values * are synchronized: If there is something written in the input but * there is nothing selected, clear the input. If the input is empty, * clear the selection */ onBlur: function () { // On blur, these are the possible states: // * inputValue is empty // * inputValue is non-empty // * selectedValue is empty // * selectedValue is not empty // This means that the possible cases are: // 1. Nothing is selected previously, and we blur with empty text field // 2. Nothing is selected previously, and we blur with string in the input: // 2.a. The field text doesn't match any lookup options // E.g. we write "str", nothing that we want comes in the lookup, // and we exit the field. // 2.b. The field text matches one lookup option. // E.g. we write "String", we see "String" in the lookup and we exit // thinking that writing the option will be enough // 3. Something is selected previously, and we blur with empty text field // 4. Something is selected previously, and we blur with non-empty text field // 4.a. The field text doesn't match any lookup options // 4.b. The field text matches one lookup option // Logic: // Nothing selected: // * match?: set selectedValue // * no match?: clear inputValue // Something selected: // * match?: set selectedValue // * no match?: set inputValue to selectedLabel const match = this.lookupResults.find( ( option ) => option.label === this.inputValue ); if ( match ) { // Select new value this.onSelect( match.value ); } else { // Reset to old value this.inputValue = this.selectedLabel; this.lookupConfig.searchQuery = this.selectedLabel; } } } ), watch: { /** * When label of selected Zid is updated, updates * the input value shown in the field. * * @param {string} label */ selectedLabel: function ( label ) { this.inputValue = label; }, /** * When isEnum flag changes (due to a type change, or * due to delayed retrieval of type data), it initializes * the fetching of the enum values. * * @param {boolean} value */ isEnum: function ( value ) { if ( value ) { this.fetchEnumValues( { type: this.type } ); } } }, mounted: function () { this.inputValue = this.selectedLabel; if ( this.isEnum ) { this.fetchEnumValues( { type: this.type } ); } } } ); </script> <style lang="less"> @import '../../ext.wikilambda.app.variables.less'; .ext-wikilambda-app-object-selector { // Style the custom chevron icon to be smaller .cdx-text-input__end-icon { width: @size-icon-x-small; height: @size-icon-x-small; min-width: @size-icon-x-small; min-height: @size-icon-x-small; } .ext-wikilambda-app-object-selector__errors { margin-top: @spacing-50; width: max-content; } .ext-wikilambda-app-object-selector__suggestion { // Override the default Codex menu item weight .cdx-menu-item__text__label, .cdx-search-result-title { font-weight: @font-weight-normal; } } } </style> |