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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 | 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 441x 441x 441x 441x 441x 441x 440x 440x 440x 439x 439x 440x 441x 441x 441x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 11x 11x 11x 11x 11x 11x 11x 11x 7x 7x 7x 7x 7x 7x 7x 7x 7x 41x 41x 41x 41x 41x 94x 94x 94x 94x 13x 13x 94x 94x 94x 94x 80x 80x 80x 80x 79x 79x 79x 79x 79x 79x 35x 35x 35x 35x 79x 79x 79x 43x 43x 43x 43x 43x 43x 43x 79x 79x 79x 1x 79x 78x 78x 80x 2x 2x 2x 41x 7x 7x 7x 7x 7x 7x 7x 7x 7x 42x 42x 42x 42x 42x 48x 2x 2x 46x 48x 2x 2x 44x 44x 48x 1x 1x 43x 43x 47x 22x 22x 21x 21x 21x 26x 18x 18x 31x 31x 31x 2x 48x 42x 7x 7x 7x 7x 7x 7x 7x 7x 107x 107x 107x 107x 107x 107x 107x 107x 34x 34x 73x 73x 107x 1x 1x 72x 72x 103x 113x 113x 5x 2x 113x 5x 5x 113x 107x 107x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 63x 63x 63x 63x 63x 76x 63x 7x 7x 7x 7x 7x 7x 7x 7x 7x 30x 30x 30x 30x 30x 32x 32x 30x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 121x 121x 121x 121x 121x 443x 443x 1x 1x 442x 442x 442x 443x 7x 7x 441x 441x 441x 441x 443x 121x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 10x 10x 10x 10x 10x 10x 13x 13x 12x 12x 12x 1x 13x 10x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 3x 3x 3x 3x 3x 1x 1x 3x 3x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 8x 8x 8x 8x 8x 49x 49x 1x 1x 49x 49x 49x 23x 23x 49x 26x 24x 24x 1x 49x 8x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 6x 6x 6x 6x 6x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 6x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 22x 22x 22x 22x 22x 21x 21x 1x 1x 20x 21x 1x 1x 19x 19x 21x 22x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 3x 3x 3x 3x 3x 3x 3x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 3x 3x 3x 3x 3x 3x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 650x 325x 325x 325x 325x 7x 7x 7x 7x 7x 7x 7x 7x 212x 205x 205x 7x 7x 7x 7x 7x 7x 7x 7x 7x 526x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 5x 5x 5x 5x 2x 2x 2x 3x 3x 5x 2x 2x 5x 5x 5x 5x 7x 7x 7x 7x 7x 7x 7x 7x 52x 52x 7x 7x 7x 7x 7x 7x 7x 7x 4x 4x 4x 4x 4x 4x 4x 4x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 15x 15x 15x 15x 15x 15x 15x 15x 15x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 284x 284x 284x 284x 284x 284x 284x 803x 803x 803x 803x 803x 803x 439x 803x 343x 343x 803x 803x 803x 169x 169x 284x 284x 284x 284x 284x 284x 284x 284x 40x 40x 284x 284x 284x 40x 40x 40x 325x 40x 40x 40x 40x 325x 40x 40x 40x 40x 284x 284x 284x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 207x 207x 207x 207x 207x 207x 207x 207x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 125x 125x 125x 125x 125x 297x 297x 297x 297x 297x 297x 297x 297x 297x 297x 297x 297x 297x 297x 125x 125x 125x 125x 125x 1x 1x 1x 1x 125x 1x 1x 1x 1x 125x 209x 209x 14x 14x 14x 14x 14x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 14x 14x 209x 209x 51x 51x 51x 51x 51x 51x 51x 209x 209x 209x 209x 209x 209x 209x 38x 38x 38x 38x 38x 38x 7x 7x 7x 7x 7x 7x 7x 7x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7x 7x | /*! * WikiLambda Vue editor: Vuex library module: fetch, store and * provide auxiliary data from other ZObejcts (labels, keys, etc.). * It also contains other helper getters to retrieve details of other * stored auxiliary objects such as functions, languages, etc. * * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt * @license MIT */ const Constants = require( '../../Constants.js' ), apiUtils = require( '../../mixins/api.js' ).methods, typeUtils = require( '../../mixins/typeUtils.js' ).methods, LabelData = require( '../classes/LabelData.js' ), convertTableToJson = require( '../../mixins/zobjectUtils.js' ).methods.convertTableToJson, hybridToCanonical = require( '../../mixins/schemata.js' ).methods.hybridToCanonical; const DEBOUNCE_ZOBJECT_LOOKUP_TIMEOUT = 300; let debounceZObjectLookup = null; module.exports = exports = { state: { /** * Collection of ZPersistent objects fetched * and indexed by their ZID. */ objects: {}, /** * Collection of LabelData object indexed by the identifier of * the ZKey, ZPersistentObject or ZArgumentDeclaration. */ labels: {}, /** * Collection of the requested zids and the resolving promises * zid: promise */ requests: {}, /** * Collection of enum types with all their selectable values */ enums: {}, /** * Map of the available language zids in the store indexed by language code */ languages: {} }, getters: { /** * Returns the string value of the language Iso code if the object * has been fetched and is stored in the state. * If not available, returns the input zid. * * @param {Object} state * @return {Function} */ getLanguageIsoCodeOfZLang: function ( state ) { /** * @param {string} zid * @return {string} */ function findLanguageCode( zid ) { if ( state.objects[ zid ] ) { const zobject = state.objects[ zid ][ Constants.Z_PERSISTENTOBJECT_VALUE ]; const ztype = zobject[ Constants.Z_OBJECT_TYPE ]; if ( ztype === Constants.Z_NATURAL_LANGUAGE ) { return zobject[ Constants.Z_NATURAL_LANGUAGE_ISO_CODE ]; } } return zid; } return findLanguageCode; }, /** * Returns the language zid given a language Iso code if the * object has been fetched and is stored in the state. * If not available, returns undefined. * * @param {Object} state * @return {Function} */ getLanguageZidOfCode: function ( state ) { /** * @param {string} code * @return {string|undefined} */ function findLanguageZid( code ) { return state.languages[ code ]; } return findLanguageZid; }, /** * Given a global ZKey (ZnKm) it returns a string that reflects * its expected value type (if any). Else it returns Z1. * * @param {Object} state * @return {Function} */ getExpectedTypeOfKey: function ( state ) { /** * @param {string|undefined} key * @return {string} */ return function ( key ) { // If the key is undefined, then this is the root object, // the expected type is always Z2/Persistent object type if ( key === undefined ) { return Constants.Z_PERSISTENTOBJECT; } // TODO (T324251): if this is an array index, (an integer), // should we return the expected type for the typed list? if ( typeUtils.isGlobalKey( key ) ) { let type; const zid = typeUtils.getZidOfGlobalKey( key ); if ( state.objects[ zid ] ) { const zobject = state.objects[ zid ][ Constants.Z_PERSISTENTOBJECT_VALUE ]; const ztype = zobject[ Constants.Z_OBJECT_TYPE ]; switch ( ztype ) { // Return the key value type if zid belongs to a type case Constants.Z_TYPE: // eslint-disable-next-line no-case-declarations const zkey = typeUtils.getKeyFromKeyList( key, zobject[ Constants.Z_TYPE_KEYS ] ); type = zkey ? zkey[ Constants.Z_KEY_TYPE ] : Constants.Z_OBJECT; break; // Return the argument type if the zid belongs to a function case Constants.Z_FUNCTION: // eslint-disable-next-line no-case-declarations const zarg = typeUtils.getArgFromArgList( key, zobject[ Constants.Z_FUNCTION_ARGUMENTS ] ); type = zarg ? zarg[ Constants.Z_ARGUMENT_TYPE ] : Constants.Z_OBJECT; break; // If not found, return Z1/ZObject (any) type default: return Constants.Z_OBJECT; } return type; } } // If key is a not found, a list index or a local key, return Z1/Object (any) type return Constants.Z_OBJECT; }; }, /** * Given a key, returns whether it is set to be an identity key * (Z3K4 field is set to true/Z41) * * @param {Object} state * @return {Function} */ isIdentityKey: function ( state ) { /** * @param {string|undefined} key * @return {boolean} */ return function ( key ) { if ( key === undefined ) { return false; } if ( !typeUtils.isGlobalKey( key ) ) { return false; } const zid = typeUtils.getZidOfGlobalKey( key ); if ( !state.objects[ zid ] ) { return false; } const zobject = state.objects[ zid ][ Constants.Z_PERSISTENTOBJECT_VALUE ]; if ( zobject[ Constants.Z_OBJECT_TYPE ] !== Constants.Z_TYPE ) { return false; } const zkey = typeUtils.getKeyFromKeyList( key, zobject[ Constants.Z_TYPE_KEYS ] ); const isIdentity = zkey[ Constants.Z_KEY_IS_IDENTITY ]; if ( !isIdentity ) { return false; } return ( ( isIdentity === Constants.Z_BOOLEAN_TRUE ) || ( isIdentity[ Constants.Z_BOOLEAN_IDENTITY ] === Constants.Z_BOOLEAN_TRUE ) ); }; }, /** * Given a type zid, returns whether it has an identity key * * @param {Object} state * @return {Function} */ isEnumType: function ( state ) { /** * @param {string} zid * @return {boolean} */ return function ( zid ) { if ( ( zid === undefined ) || ( Constants.EXCLUDE_FROM_ENUMS.includes( zid ) ) || ( !state.objects[ zid ] ) ) { return false; } const zobject = state.objects[ zid ][ Constants.Z_PERSISTENTOBJECT_VALUE ]; if ( zobject[ Constants.Z_OBJECT_TYPE ] !== Constants.Z_TYPE ) { return false; } const keys = zobject[ Constants.Z_TYPE_KEYS ].slice( 1 ); for ( const key of keys ) { const isIdentity = key[ Constants.Z_KEY_IS_IDENTITY ]; if ( isIdentity && ( ( isIdentity === Constants.Z_BOOLEAN_TRUE ) || ( isIdentity[ Constants.Z_BOOLEAN_IDENTITY ] === Constants.Z_BOOLEAN_TRUE ) ) ) { return true; } } return false; }; }, /** * Given a type zid, returns whether it has an identity key, * excluding the builtin enum types (Boolean/Z40), as they * have builtin components that require special cases. * * @param {Object} _state * @param {Object} getters * @return {Function} */ isCustomEnum: function ( _state, getters ) { /** * @param {string} zid * @return {boolean} */ return function ( zid ) { return getters.isEnumType( zid ) && !Constants.BUILTIN_ENUMS.includes( zid ); }; }, /** * Returns the persisted object for a given ZID if that was * fetched from the DB and saved in the state. Else returns undefined * * @param {Object} state * @return {Function} */ getStoredObject: function ( state ) { /** * @param {string} zid of the ZPersistentObject * @return {Object|undefined} persisted ZObject */ function findPersistedObject( zid ) { return state.objects[ zid ]; } return findPersistedObject; }, /** * Returns the LabelData of the ID of a ZKey, ZPersistentObject or ZArgumentDeclaration. * The label is in the user selected language, if available, or else in the closest fallback. * If not available, returns a new LabelData object with the input zid as the label. * * This getter must make sure that there's always a LabelData object return, so that the * caller components can safely access the LabelData properties. * * @param {Object} state * @param {Object} getters * @return {Function} */ getLabelData: function ( state, getters ) { /** * @param {string} id * @return {LabelData} */ function findLabel( id ) { // If the requested language is 'qqx', return (zid) as the label if ( getters.getUserRequestedLang === 'qqx' && id ) { return new LabelData( id, `(${ id })`, getters.getUserLangZid, getters.getUserLangCode ); } // If label data is still not present in the library, return zid as the label const labelData = state.labels[ id ]; if ( !labelData ) { return new LabelData( id, id, getters.getUserLangZid, getters.getUserLangCode ); } // Enrich label data with language code and directionality labelData.setLangCode( getters.getLanguageIsoCodeOfZLang( labelData.lang ) ); return labelData; } return findLabel; }, /** * Returns the array of implementations or tests connected to a persisted * Function stored in the library, given the Function Zid and the key * identifying the object list (tests or implementations) * * @param {Object} state * @return {Function} */ getConnectedObjects: function ( state ) { /** * @param {string} zid * @param {string} key * @return {Array} */ function findConnectedObjects( zid, key ) { const func = state.objects[ zid ]; if ( func ) { const imps = func[ Constants.Z_PERSISTENTOBJECT_VALUE ][ key ]; return imps ? imps.slice( 1 ) : []; } return []; } return findConnectedObjects; }, /** * Returns the function zid given an implementation zid * by returning the value in its Z14K1 field * * @param {Object} state * @return {Function} */ getFunctionZidOfImplementation: function ( state ) { /** * @param {string} zid * @return {string|undefined} */ function findImplementationFunction( zid ) { const implementation = state.objects[ zid ]; if ( !implementation ) { return undefined; } return implementation[ Constants.Z_PERSISTENTOBJECT_VALUE ][ Constants.Z_IMPLEMENTATION_FUNCTION ]; } return findImplementationFunction; }, /** * Returns the type of an implementation stored in the * global state, given its Zid. The type will be * composition/Z14K2, built-in/Z14K4, or code/Z14K3. * If the implementation Zid is unknown returns undefined. * * @param {Object} state * @return {Function} */ getTypeOfImplementation: function ( state ) { /** * @param {string} zid * @return {string | undefined} */ function findImplementationType( zid ) { let implementation = state.objects[ zid ]; if ( !implementation ) { return undefined; } implementation = implementation[ Constants.Z_PERSISTENTOBJECT_VALUE ]; if ( Constants.Z_IMPLEMENTATION_COMPOSITION in implementation ) { return Constants.Z_IMPLEMENTATION_COMPOSITION; } if ( Constants.Z_IMPLEMENTATION_BUILT_IN in implementation ) { return Constants.Z_IMPLEMENTATION_BUILT_IN; } if ( Constants.Z_IMPLEMENTATION_CODE in implementation ) { return Constants.Z_IMPLEMENTATION_CODE; } return undefined; } return findImplementationType; }, /** * Returns the language code of an implementation stored * in the global state, given its Zid. If the implementation * is not of type code (but composition or built-in) returns * undefined. * * @param {Object} state * @return {Function} */ getLanguageOfImplementation: function ( state ) { /** * @param {string} zid * @return {string | undefined} */ function findImplementationLanguage( zid ) { let implementation = state.objects[ zid ]; if ( !implementation ) { return undefined; } implementation = implementation[ Constants.Z_PERSISTENTOBJECT_VALUE ]; if ( Constants.Z_IMPLEMENTATION_CODE in implementation ) { // If code is literal: return literal if ( typeUtils.isTruthyOrEqual( implementation, [ Constants.Z_IMPLEMENTATION_CODE, Constants.Z_CODE_LANGUAGE, Constants.Z_PROGRAMMING_LANGUAGE_CODE ] ) ) { return implementation[ Constants.Z_IMPLEMENTATION_CODE ][ Constants.Z_CODE_LANGUAGE ][ Constants.Z_PROGRAMMING_LANGUAGE_CODE ]; } // Else, code is reference: return zid return implementation[ Constants.Z_IMPLEMENTATION_CODE ][ Constants.Z_CODE_LANGUAGE ]; } return undefined; } return findImplementationLanguage; }, /** * Given a function Zid, it inspects its function definition * stored in the state and returns an array of its arguments. * It returns undefined if the function is not available or the * zid does not belong to a valid function. * * @param {Object} state * @return {Function} */ getInputsOfFunctionZid: function ( state ) { /** * @param {string} zid * @return {Array} */ function findInputs( zid ) { const func = state.objects[ zid ]; if ( func === undefined ) { return []; } const obj = func[ Constants.Z_PERSISTENTOBJECT_VALUE ]; if ( obj[ Constants.Z_OBJECT_TYPE ] !== Constants.Z_FUNCTION ) { return []; } // Remove benjamin type item return obj[ Constants.Z_FUNCTION_ARGUMENTS ].slice( 1 ); } return findInputs; }, /** * Returns the values of an enum with a given ZID * * @param {Object} state * @return {Function} */ getEnumValues: function ( state ) { /** * @param {string} zid * @return {Array} */ function findEnum( zid ) { const enumObj = state.enums[ zid ]; return enumObj && enumObj.data instanceof Array ? enumObj.data : []; } return findEnum; }, /** * Returns the enum of a given ZID * * @param {Object} state * @return {Object} */ getEnum: function ( state ) { /** * @param {string} zid * @return {Array} */ function findEnum( zid ) { return state.enums[ zid ]; } return findEnum; } }, mutations: { /** * Set request state for each zid * * @param {Object} state * @param {Object} payload * @param {string} payload.zid * @param {Promise} payload.request */ setZidRequest: function ( state, payload ) { if ( payload.request ) { state.requests[ payload.zid ] = payload.request; } else { delete state.requests[ payload.zid ]; } }, /** * Add zid info to the state * * @param {Object} state * @param {Object} payload */ setStoredObject: function ( state, payload ) { if ( !( payload.zid in state.objects ) || payload.forceUpdate ) { state.objects[ payload.zid ] = payload.info; } }, /** * Save the LabelData object for a given ID * of a ZPersistentObject, ZKey or ZArgumentDeclaration. * * @param {Object} state * @param {LabelData} labelData */ setLabel: function ( state, labelData ) { state.labels[ labelData.zid ] = labelData; }, /** * @param {Object} state * @param {Object} payload * @param {string} payload.zid * @param {Array | Promise} payload.data * @param {number | undefined} payload.continue */ setEnumData: function ( state, payload ) { const zid = payload.zid; const data = payload.data; const continueVal = payload.continue; // Initialize the enum object if it does not exist with a Promise or Array if ( !state.enums[ zid ] ) { state.enums[ zid ] = { data }; return; } // Ensure data is an array before concatenation if ( !Array.isArray( state.enums[ zid ].data ) ) { state.enums[ zid ].data = []; } // Append new data and update continuation value state.enums[ zid ].data = state.enums[ zid ].data.concat( data ); state.enums[ zid ].continue = continueVal; }, /** * @param {Object} state * @param {Object} payload * @param {string} payload.code * @param {string} payload.zid */ setLanguageCode: function ( state, payload ) { state.languages[ payload.code ] = payload.zid; } }, actions: { /** * Updates the stored object in the Library with the current ZObject * * @param {Object} context */ updateStoredObject: function ( context ) { const zobject = hybridToCanonical( convertTableToJson( context.getters.getZObjectTable ) ); const zid = context.getters.getCurrentZObjectId; context.commit( 'setStoredObject', { zid, info: zobject, forceUpdate: true } ); }, /** * Performs a Lookup call to the database to retrieve all * ZObject references that match a given input and type. * This is used in selectors such as ZObjectSelector or the * language selector of the About widget. * * @param {Object} context Vuex context object * @param {number} payload Object containing input(string) and type * @return {Promise} */ lookupZObjectLabels: function ( context, payload ) { // Add user language code to the payload payload.language = context.getters.getUserLangCode; return new Promise( ( resolve ) => { clearTimeout( debounceZObjectLookup ); debounceZObjectLookup = setTimeout( () => apiUtils.searchLabels( payload ).then( ( data ) => resolve( data.labels ) ), DEBOUNCE_ZOBJECT_LOOKUP_TIMEOUT ); } ); }, /** * Fetches all values stored of a given enum type. * * @param {Object} context * @param {Object} payload * @param {string} payload.type - The ZID of the enum type * @param {boolean} payload.isContinue - Whether to continue fetching the enum * @return {Promise<Object>|undefined} - Promise resolving to: * @property {Array<Object>} labels - The search results * @property {number|null} continue - The token to continue the search or null if no more results */ fetchEnumValues: function ( context, payload ) { const enumObject = context.getters.getEnum( payload.type ); const continueVal = enumObject ? enumObject.continue : undefined; // If the enum has already been fetched and we are not continuing, do nothing // If the enum has been fetched and we are continuing, but there are no more values, do nothing if ( ( enumObject && !payload.isContinue ) || ( payload.isContinue && continueVal === null ) ) { return; } const promise = apiUtils.searchLabels( { input: '', type: payload.type, limit: Constants.API_ENUMS_LIMIT, language: context.getters.getUserLangCode, continue: continueVal } ).then( ( data ) => { // Set values when the request is completed context.commit( 'setEnumData', { zid: payload.type, data: data.labels, continue: data.continue } ); return data; } ).catch( () => { // Set empty array when the request fails, so we could retry context.commit( 'setEnumData', { zid: payload.type, data: [] } ); } ); if ( !payload.isContinue ) { // Initialize the enum with the pending promise context.commit( 'setEnumData', { zid: payload.type, data: promise } ); } return promise; }, /** * Orchestrates the calls to wikilambdaload_zobject api to fetch * a given set of ZIDs. This method takes care of the following requirements: * * * Zids are requested in batches of max 50 items. * * Zids are only requested once. * * Every zid is stored along with their request while it's being fetched. * * Once it's fetched, the request is cleared. * * The returning promise only resolves when all of the batches have returned. * * @param {Object} context * @param {Object} payload * @param {Array} payload.zids array of zids to fetch * @return {Promise} */ fetchZids: function ( context, payload ) { let requestZids = []; const allPromises = []; const { zids = [] } = payload; zids.forEach( ( zid ) => { // Ignore if: // * Zid is Z0 // * Zid has already been fetched if ( zid && ( zid !== Constants.NEW_ZID_PLACEHOLDER ) && !( zid in context.state.objects ) && !( zid in context.state.requests ) ) { requestZids.push( zid ); } // Capture pending promise to await if: // * Zid is waiting to be fetched if ( zid in context.state.requests ) { allPromises.push( context.state.requests[ zid ] ); } } ); // Keep only unique values requestZids = [ ...new Set( requestZids ) ]; // Batch zids in groups of max 50 items const batches = []; for ( let i = 0; i < requestZids.length; i += Constants.API_REQUEST_ITEMS_LIMIT ) { batches.push( requestZids.slice( i, i + Constants.API_REQUEST_ITEMS_LIMIT ) ); } // For each batch, generate a Promise for ( const batch of batches ) { const batchPromise = context.dispatch( 'performFetchZids', { zids: batch } ).then( () => { // Once it's back, unset active request batch.forEach( ( zid ) => { context.commit( 'setZidRequest', { zid, request: null } ); } ); } ); // Set active request batch.forEach( ( zid ) => { context.commit( 'setZidRequest', { zid, request: batchPromise } ); } ); // Collect batch promises allPromises.push( batchPromise ); } // Return pending and new promises for all the requested zids return Promise.all( allPromises ); }, /** * Calls the api wikilambdaload_zobjects with a set of Zids and * with or without language property. The language will always be * requested so that the backend takes care of the language ballback * logic. The only moment in wich we will not specify a language * property is when requesting the root ZObject on initialization * * Once received the response, stores the full object in the objects * array and the labels for the Zids, ZKey and ZArgument ids in the * labels store object. The labels returned are already in the * preferred language (or closest fallback available). * * @param {Object} context * @param {Object} payload * @return {Promise} */ performFetchZids: function ( context, payload ) { return new Promise( ( resolve ) => { apiUtils.fetchZObjects( { zids: payload.zids.join( '|' ), language: context.getters.getUserLangCode, dependencies: true } ).then( ( response ) => { const requestedZids = payload.zids; const returnedZids = Object.keys( response ); const dependentZids = []; returnedZids.forEach( ( zid ) => { // If the requested zid returned error, do nothing if ( !( 'success' in response[ zid ] ) ) { return; } // 1. State mutation: // Add zObject to the state objects array const persistentObject = response[ zid ].data; context.commit( 'setStoredObject', { zid: zid, info: persistentObject } ); // 2. State mutation: // Add zObject label in user's selected language const multiStr = persistentObject[ Constants.Z_PERSISTENTOBJECT_LABEL ][ Constants.Z_MULTILINGUALSTRING_VALUE ].slice( 1 ); // The returned multilingual strings will only contain one monolingual string // (or none) as they have already been filtered by the back-end to the given // language or any of its available fallbacks. if ( multiStr.length === 1 ) { const labelData = new LabelData( zid, multiStr[ 0 ][ Constants.Z_MONOLINGUALSTRING_VALUE ], multiStr[ 0 ][ Constants.Z_MONOLINGUALSTRING_LANGUAGE ] ); dependentZids.push( multiStr[ 0 ][ Constants.Z_MONOLINGUALSTRING_LANGUAGE ] ); context.commit( 'setLabel', labelData ); } // 3. State mutation: // Add the key or argument labels from the selected language to the store let objects; const objectValue = persistentObject[ Constants.Z_PERSISTENTOBJECT_VALUE ]; const zType = ( typeof objectValue === 'object' ) ? objectValue[ Constants.Z_OBJECT_TYPE ] : undefined; switch ( zType ) { case Constants.Z_TYPE: // If the zObject is a type, get all key labels // and commit to the store objects = objectValue[ Constants.Z_TYPE_KEYS ].slice( 1 ); objects.forEach( ( key ) => { const keyLabels = key[ Constants.Z_KEY_LABEL ][ Constants.Z_MULTILINGUALSTRING_VALUE ].slice( 1 ); if ( keyLabels.length === 1 ) { const labelData = new LabelData( key[ Constants.Z_KEY_ID ], keyLabels[ 0 ][ Constants.Z_MONOLINGUALSTRING_VALUE ], keyLabels[ 0 ][ Constants.Z_MONOLINGUALSTRING_LANGUAGE ] ); dependentZids.push( keyLabels[ 0 ][ Constants.Z_MONOLINGUALSTRING_LANGUAGE ] ); context.commit( 'setLabel', labelData ); } } ); // If the zObject is a type, get all parser and renderer functions // and commit to the store if ( Constants.Z_TYPE_RENDERER in objectValue ) { const renderer = objectValue[ Constants.Z_TYPE_RENDERER ]; dependentZids.push( renderer ); context.commit( 'setRenderer', { type: zid, renderer } ); } if ( Constants.Z_TYPE_PARSER in objectValue ) { const parser = objectValue[ Constants.Z_TYPE_PARSER ]; dependentZids.push( parser ); context.commit( 'setParser', { type: zid, parser } ); } break; case Constants.Z_FUNCTION: // If the zObject is a function, get all argument // declaration labels and commit to the store objects = objectValue[ Constants.Z_FUNCTION_ARGUMENTS ].slice( 1 ); objects.forEach( ( arg ) => { const argLabels = arg[ Constants.Z_ARGUMENT_LABEL ][ Constants.Z_MULTILINGUALSTRING_VALUE ].slice( 1 ); if ( argLabels.length === 1 ) { const labelData = new LabelData( arg[ Constants.Z_ARGUMENT_KEY ], argLabels[ 0 ][ Constants.Z_MONOLINGUALSTRING_VALUE ], argLabels[ 0 ][ Constants.Z_MONOLINGUALSTRING_LANGUAGE ] ); dependentZids.push( argLabels[ 0 ][ Constants.Z_MONOLINGUALSTRING_LANGUAGE ] ); context.commit( 'setLabel', labelData ); } } ); break; case Constants.Z_NATURAL_LANGUAGE: // If the zObject is a natural langugae, save the // code and the zid in the store for easy access context.commit( 'setLanguageCode', { code: objectValue[ Constants.Z_NATURAL_LANGUAGE_ISO_CODE ], zid } ); break; default: // Do nothing } // Make sure that we fetch all languages stored in the labels library context.dispatch( 'fetchZids', { zids: [ ...new Set( dependentZids ) ] } ); } ); // performFetch must resolve to the list of requested zids resolve( requestedZids ); } ); } ); }, /** * Pre-fetch information of the Zids most commonly used within the UI * * @param {Object} context * @return {Promise} */ prefetchZids: function ( context ) { const zids = [ Constants.Z_OBJECT, Constants.Z_PERSISTENTOBJECT, Constants.Z_MULTILINGUALSTRING, Constants.Z_MONOLINGUALSTRING, Constants.Z_KEY, Constants.Z_TYPE, Constants.Z_STRING, Constants.Z_FUNCTION, Constants.Z_FUNCTION_CALL, Constants.Z_REFERENCE, Constants.Z_BOOLEAN, Constants.Z_BOOLEAN_TRUE, Constants.Z_BOOLEAN_FALSE, Constants.Z_IMPLEMENTATION, context.getters.getUserLangZid, Constants.Z_TYPED_LIST, Constants.Z_ARGUMENT_REFERENCE, Constants.Z_NATURAL_LANGUAGE, ... Constants.SUGGESTIONS.LANGUAGES, ... Constants.SUGGESTIONS.TYPES ]; return context.dispatch( 'fetchZids', { zids: zids } ); } } }; |