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 | 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 14x 14x 14x 14x 14x 14x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 2x 3x 3x 5x 1x 1x 2x 2x 2x 5x 5x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 4x 4x 4x 4x 4x 52x 52x 1x 1x 51x 51x 51x 52x 52x 10x 10x 10x 10x 10x 41x 41x 4x 4x 4x 126x 126x 126x 126x 126x 126x 126x 126x 126x 6x 6x 6x 6x 6x 13x 6x 6x 6x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 2x 2x 1x 1x 1x 1x 1x 1x 1x 126x 126x 126x 126x 126x 126x 126x 126x 126x 1x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 13x 13x 13x 9x 9x 6x 6x 6x 6x 6x 6x 6x 6x 9x 9x 9x 13x 13x 13x 126x 126x | /*!
* WikiLambda Vue editor: Wikidata Properties store module
*
* @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
* @license MIT
*/
const Constants = require( '../../../Constants.js' );
const LabelData = require( '../../classes/LabelData.js' );
const { isWikidataPropertyId } = require( '../../../utils/wikidataUtils.js' );
module.exports = {
state: {
properties: {},
scheduledProps: [],
scheduledPropsPromise: null
},
getters: {
/**
* Returns the Wikidata Property data given its Id,
* the fetch Promise if the fetch request is on the fly,
* or undefined if it hasn't been requested yet.
*
* @param {Object} state
* @return {Function}
*/
getPropertyData: function ( state ) {
/**
* @param {string} id
* @return {Object|Promise|undefined}
*/
const findPropertyData = ( id ) => state.properties[ id ];
return findPropertyData;
},
/**
* Returns a promise that resolves to the Wikidata Property data given its Id.
* If the property is already cached, returns a resolved promise.
* If the property is being fetched, returns the existing promise.
* If the property hasn't been requested, returns a rejected promise.
*
* @param {Object} state
* @return {Function}
*/
getPropertyDataAsync: function () {
/**
* @param {string} id
* @return {Promise<Object>}
*/
const getPropertyDataAsync = ( id ) => {
const propertyData = this.getPropertyData( id );
// If property is already cached (not a promise), return resolved promise
if ( propertyData && typeof propertyData.then !== 'function' ) {
return Promise.resolve( propertyData );
}
// If property is being fetched (is a promise), return that promise
if ( propertyData && typeof propertyData.then === 'function' ) {
return propertyData;
}
// If property hasn't been requested, return rejected promise
return Promise.reject( new Error( `Property ${ id } not found` ) );
};
return getPropertyDataAsync;
},
/**
* Returns the LabelData object built from the available
* labels in the data object of the selected Wikidata Property.
* If an Property is selected but it has no labels, returns
* LabelData object with the Wikidata Property id as its label.
* If no Wikidata Property is selected, returns undefined.
*
* @return {LabelData|undefined}
*/
getPropertyLabelData: function () {
/**
* @param {string} id The item ID
* @return {LabelData} The `LabelData` object containing label, language code, and directionality.
*/
const findPropertyLabelData = ( id ) => {
// If no selected Property, return undefined
if ( !id ) {
return undefined;
}
// If no propertyData yet, return Property Id
// Get best label from labels (if any)
const propertyData = this.getPropertyData( id );
const langs = propertyData ? Object.keys( propertyData.labels || {} ) : {};
if ( langs.length > 0 ) {
const label = langs.includes( this.getUserLangCode ) ?
propertyData.labels[ this.getUserLangCode ] :
propertyData.labels[ langs[ 0 ] ];
return new LabelData( id, label.value, null, label.language );
}
// Else, return Property Id as label
return new LabelData( id, id, null );
};
return findPropertyLabelData;
},
/**
* Returns the URL for a given property ID.
*
* @param {Object} state
* @return {Function}
*/
getPropertyUrl: function () {
/**
* @param {string} id
* @return {string|undefined}
*/
const findPropertyUrl = ( id ) => isWikidataPropertyId( id ) ?
`${ Constants.WIKIDATA_BASE_URL }/wiki/Property:${ id }` :
undefined;
return findPropertyUrl;
}
},
actions: {
/**
* Stores the Wikidata Property data indexed by its Id
*
* @param {Object} payload
* @param {string} payload.id
* @param {Object} payload.data
*/
setPropertyData: function ( payload ) {
// If payload.data is a promise, store it directly
if ( payload.data && typeof payload.data.then === 'function' ) {
this.properties[ payload.id ] = payload.data;
return;
}
// Select only subset of Wikidata Property data; title and labels
const unwrap = ( ( { title, labels } ) => ( { title, labels } ) );
this.properties[ payload.id ] = unwrap( payload.data );
},
/**
* Removes the properties for the given IDs
*
* @param {Object} payload
* @param {Array<string>} payload.ids - An array of Wikidata Property IDs
*/
resetPropertyData: function ( payload ) {
payload.ids.forEach( ( id ) => delete this.properties[ id ] );
},
/**
* Calls Wikidata Action API to fetch Wikidata Properties
* given their Ids.
*
* @param {Object} payload
* @param {Array} payload.ids - An array of Wikidata Property IDs to fetch.
* @return {Promise | undefined} - A promise that resolves to the fetched data.
*/
fetchProperties: function ( { ids } ) {
this.scheduledProps = [ ... new Set( [ ...this.scheduledProps, ...ids ] ) ];
if ( !this.scheduledPropsPromise ) {
this.scheduledPropsPromise = new Promise( ( resolve, reject ) => {
setTimeout( () => {
this.fetchWikidataEntitiesBatched( {
ids: this.scheduledProps,
getData: this.getPropertyData,
setData: this.setPropertyData,
resetData: this.resetPropertyData
} ).then( resolve, reject );
this.scheduledProps = [];
this.scheduledPropsPromise = null;
}, Constants.WIKIDATA_REQUEST_TIME_WINDOW );
} );
}
return this.scheduledPropsPromise;
}
}
};
|