-
Notifications
You must be signed in to change notification settings - Fork 58
/
Entity.js
110 lines (94 loc) · 2.61 KB
/
Entity.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
( function ( wb, vv ) {
'use strict';
var MODULE = wb.experts,
PARENT = vv.experts.StringValue;
/**
* `valueview` `Expert` for specifying a reference to a Wikibase `Entity`.
*
* @class wikibase.experts.Entity
* @extends jQuery.valueview.experts.StringValue
* @abstract
* @uses jQuery.wikibase.entityselector
* @license GPL-2.0-or-later
* @author Daniel Werner < [email protected] >
*/
var SELF = MODULE.Entity = vv.expert( 'wikibaseentity', PARENT, {
/**
* @inheritdoc
*
* @throws {Error} when called because this `Expert` is meant to be abstract.
*/
_init: function () {
throw new Error( 'Abstract Entity id expert cannot be instantiated directly' );
},
/**
* @protected
*/
_initEntityExpert: function () {
PARENT.prototype._init.call( this );
// FIXME: Use SuggestedStringValue
var notifier = this._viewNotifier,
self = this,
repoConfig = mw.config.get( 'wbRepo' ),
repoApiUrl = repoConfig.url + repoConfig.scriptPath + '/api.php';
this._initEntityselector( repoApiUrl );
var value = this.viewState().value(),
entityId = value && value.getSerialization();
this.$input.data( 'entityselector' ).selectedEntity( entityId );
this.$input
.on( 'eachchange.' + this.uiBaseClass, function ( e ) {
$( this ).data( 'entityselector' ).repositionMenu();
} )
.on( 'entityselectorselected.' + this.uiBaseClass, ( e ) => {
self._resizeInput();
notifier.notify( 'change' );
} );
},
/**
* Initializes a `jQuery.wikibase.entityselector` instance on the `Expert`'s input element.
*
* @abstract
* @protected
*
* @param {string} repoApiUrl
*/
_initEntityselector: function ( repoApiUrl ) {
this.$input.entityselector( {
url: repoApiUrl,
type: this.constructor.TYPE,
selectOnAutocomplete: true
} );
},
/**
* @inheritdoc
*/
destroy: function () {
// Prevent error when issuing destroy twice:
if ( this.$input ) {
// The entityselector may have already been destroyed by a parent component:
var entityselector = this.$input.data( 'entityselector' );
if ( entityselector ) {
entityselector.destroy();
}
}
PARENT.prototype.destroy.call( this );
},
/**
* @inheritdoc
*
* @return {string}
*/
rawValue: function () {
var entitySelector = this.$input.data( 'entityselector' ),
selectedEntity = entitySelector.selectedEntity();
return selectedEntity ? selectedEntity.id : '';
}
} );
/**
* `Entity` type this `Expert` supports.
*
* @property {string} [TYPE=null]
* @static
*/
SELF.TYPE = null;
}( wikibase, $.valueview ) );