-
Notifications
You must be signed in to change notification settings - Fork 58
/
wikibase.EntityInitializer.js
133 lines (116 loc) · 3.43 KB
/
wikibase.EntityInitializer.js
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
/**
* @license GPL-2.0-or-later
*
* @author Adrian Heine <[email protected]>
* @author H. Snater < [email protected] >
*/
( function ( wb ) {
'use strict';
var config = require( './config.json' ),
serialization = require( 'wikibase.serialization' );
/**
* Entity initializer.
* Deserializes the entity passed to JavaScript via mw.config variable or
* as entity object promise.
*
* @constructor
*
* @param {string|Thenable} arg Config variable name or entity object promise
*
* @throws {Error} if required parameter is not specified properly.
*/
var EntityInitializer = wb.EntityInitializer = function ( arg ) {
var entityPromise;
if ( typeof arg === 'string' ) {
entityPromise = getFromConfig( arg );
} else if ( isThenable( arg ) ) {
entityPromise = arg;
} else {
throw new Error(
'Config variable name or entity promise needs to be specified'
);
}
this._entityPromise = entityPromise;
};
EntityInitializer.newFromEntityLoadedHook = function () {
var entityPromise = $.Deferred( ( deferred ) => {
mw.hook( 'wikibase.entityPage.entityLoaded' ).add( ( entity ) => {
deferred.resolve( entity );
} );
} ).promise();
return new EntityInitializer( entityPromise );
};
$.extend( EntityInitializer.prototype, {
/**
* @type {jQuery.Promise} Promise for serialized entity
*/
_entityPromise: null,
/**
* Retrieves an entity from mw.config.
*
* @return {Object} jQuery Promise
* Resolved parameters:
* - {wikibase.datamodel.Entity}
* No rejected parameters.
*/
getEntity: function () {
var self = this;
return this._entityPromise.then( ( entity ) => self._getDeserializer().then( ( entityDeserializer ) => entityDeserializer.deserialize( entity ) ) );
},
/**
* @return {Object} jQuery promise
* Resolved parameters:
* - {serialization.EntityDeserializer}
* No rejected parameters.
*/
_getDeserializer: function () {
var entityDeserializer = new serialization.EntityDeserializer(),
deferred = $.Deferred();
var entityTypes = config.entityTypes;
var modules = [];
var typeNames = [];
entityTypes.types.forEach( ( type ) => {
var deserializerFactoryFunction = entityTypes[ 'deserializer-factory-functions' ][ type ];
if ( deserializerFactoryFunction ) {
modules.push( deserializerFactoryFunction );
typeNames.push( type );
}
} );
mw.loader.using( modules, ( require ) => {
modules.forEach( ( module, index ) => {
entityDeserializer.registerStrategy(
// eslint-disable-next-line security/detect-non-literal-require
require( module )(),
typeNames[ index ]
);
} );
deferred.resolve( entityDeserializer );
} );
return deferred.promise();
}
} );
function isThenable( arg ) {
return typeof arg === 'object' && typeof arg.then === 'function';
}
/**
* Get entity from config
*
* @param configVarName
* @return {jQuery.Promise}
* Resolved parameters:
* - {object} Entity object
* No rejected parameters.
*/
function getFromConfig( configVarName ) {
return $.Deferred( ( deferred ) => {
mw.hook( 'wikipage.content' ).add( () => {
var serializedEntity = mw.config.get( configVarName );
if ( serializedEntity === null ) {
deferred.reject();
return;
}
deferred.resolve( JSON.parse( serializedEntity ) );
} );
} ).promise();
}
}( wikibase ) );