-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathve.ui.MWCategoryInputWidget.js
263 lines (234 loc) · 7.55 KB
/
ve.ui.MWCategoryInputWidget.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
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
/*!
* VisualEditor UserInterface MWCategoryInputWidget class.
*
* @copyright 2011-2018 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Creates an ve.ui.MWCategoryInputWidget object.
*
* @class
* @extends OO.ui.TextInputWidget
* @mixins OO.ui.mixin.LookupElement
*
* @constructor
* @param {ve.ui.MWCategoryWidget} categoryWidget
* @param {Object} [config] Configuration options
* @cfg {mw.Api} [api] API object to use, uses Target#getContentApi if not specified
*/
ve.ui.MWCategoryInputWidget = function VeUiMWCategoryInputWidget( categoryWidget, config ) {
// Config initialization
config = ve.extendObject( {
// @TMS - CommentedOut placeholder: ve.msg( 'visualeditor-dialog-meta-categories-input-placeholder' )
}, config );
// Parent constructor
ve.ui.MWCategoryInputWidget.super.call( this, config );
// Mixin constructors
OO.ui.mixin.LookupElement.call( this, config );
// Properties
this.categoryWidget = categoryWidget;
this.api = config.api || ve.init.target.getContentApi();
// Initialization
this.$element.addClass( 've-ui-mwCategoryInputWidget' );
this.lookupMenu.$element.addClass( 've-ui-mwCategoryInputWidget-menu' );
};
/* Inheritance */
OO.inheritClass( ve.ui.MWCategoryInputWidget, OO.ui.TextInputWidget );
OO.mixinClass( ve.ui.MWCategoryInputWidget, OO.ui.mixin.LookupElement );
/* Events */
/**
* @event choose
* A category was chosen
* @param {OO.ui.MenuOptionWidget} item Chosen item
*/
/* Methods */
/**
* @inheritdoc
*/
ve.ui.MWCategoryInputWidget.prototype.getLookupRequest = function () {
var title = mw.Title.newFromText( this.value );
if ( title && title.getNamespaceId() === mw.config.get( 'wgNamespaceIds' ).category ) {
title = title.getMainText();
} else {
title = this.value;
}
return this.api.get( {
action: 'query',
generator: 'allcategories',
gacmin: 1,
gacprefix: title,
prop: 'categoryinfo',
redirects: ''
} );
};
/**
* @inheritdoc
*/
ve.ui.MWCategoryInputWidget.prototype.getLookupCacheDataFromResponse = function ( data ) {
var result = [],
linkCacheUpdate = {},
query = data.query || {};
$.each( query.pages || [], function ( pageId, categoryPage ) {
result.push( mw.Title.newFromText( categoryPage.title ).getMainText() );
linkCacheUpdate[ categoryPage.title ] = {
missing: Object.prototype.hasOwnProperty.call( categoryPage, 'missing' ),
hidden: (
categoryPage.categoryinfo &&
Object.prototype.hasOwnProperty.call( categoryPage.categoryinfo, 'missing' )
)
};
} );
$.each( query.redirects || [], function ( index, redirect ) {
if ( !Object.prototype.hasOwnProperty.call( linkCacheUpdate, redirect.to ) ) {
linkCacheUpdate[ redirect.to ] = ve.init.platform.linkCache.getCached( redirect.to ) ||
{ missing: false, redirectFrom: [ redirect.from ] };
}
if (
linkCacheUpdate[ redirect.to ].redirectFrom &&
linkCacheUpdate[ redirect.to ].redirectFrom.indexOf( redirect.from ) === -1
) {
linkCacheUpdate[ redirect.to ].redirectFrom.push( redirect.from );
} else {
linkCacheUpdate[ redirect.to ].redirectFrom = [ redirect.from ];
}
} );
ve.init.platform.linkCache.set( linkCacheUpdate );
return result;
};
/**
* @inheritdoc
*/
ve.ui.MWCategoryInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
var widget = this,
exactMatch = false,
itemWidgets = [],
existingCategoryItems = [],
matchingCategoryItems = [],
hiddenCategoryItems = [],
newCategoryItems = [],
existingCategories = this.categoryWidget.getCategories(),
linkCacheUpdate = {},
canonicalQueryValue = mw.Title.newFromText( this.value ),
prefixedCanonicalQueryValue = mw.Title.newFromText(
this.value,
mw.config.get( 'wgNamespaceIds' ).category
);
prefixedCanonicalQueryValue = prefixedCanonicalQueryValue && prefixedCanonicalQueryValue.getPrefixedText();
// Invalid titles end up with canonicalQueryValue being null.
if ( canonicalQueryValue ) {
canonicalQueryValue = canonicalQueryValue.getMainText();
}
$.each( data, function ( index, suggestedCategory ) {
var suggestedCategoryTitle = mw.Title.newFromText(
suggestedCategory,
mw.config.get( 'wgNamespaceIds' ).category
).getPrefixedText(),
suggestedCacheEntry = ve.init.platform.linkCache.getCached( suggestedCategoryTitle );
if ( canonicalQueryValue === suggestedCategory ) {
exactMatch = true;
}
if ( !suggestedCacheEntry ) {
linkCacheUpdate[ suggestedCategoryTitle ] = { missing: false };
}
if (
existingCategories.indexOf( suggestedCategory ) === -1
) {
if ( suggestedCacheEntry && suggestedCacheEntry.hidden ) {
hiddenCategoryItems.push( suggestedCategory );
} else {
matchingCategoryItems.push( suggestedCategory );
}
}
} );
// Existing categories
$.each( existingCategories, function ( index, existingCategory ) {
if ( existingCategory === canonicalQueryValue ) {
exactMatch = true;
}
if ( index < existingCategories.length - 1 && existingCategory.lastIndexOf( canonicalQueryValue, 0 ) === 0 ) {
// Verify that item starts with category.value
existingCategoryItems.push( existingCategory );
}
} );
// New category
if ( !exactMatch && canonicalQueryValue ) {
newCategoryItems.push( canonicalQueryValue );
linkCacheUpdate[ prefixedCanonicalQueryValue ] = { missing: true };
}
ve.init.platform.linkCache.set( linkCacheUpdate );
// Add sections for non-empty groups. Each section consists of an id, a label and items
$.each( [
{
id: 'newCategory',
label: ve.msg( 'visualeditor-dialog-meta-categories-input-newcategorylabel' ),
items: newCategoryItems
},
{
id: 'inArticle',
label: ve.msg( 'visualeditor-dialog-meta-categories-input-movecategorylabel' ),
items: existingCategoryItems
},
{
id: 'matchingCategories',
label: ve.msg( 'visualeditor-dialog-meta-categories-input-matchingcategorieslabel' ),
items: matchingCategoryItems
},
{
id: 'hiddenCategories',
label: ve.msg( 'visualeditor-dialog-meta-categories-input-hiddencategorieslabel' ),
items: hiddenCategoryItems
}
], function ( index, sectionData ) {
if ( sectionData.items.length ) {
itemWidgets.push( new OO.ui.MenuSectionOptionWidget( {
data: sectionData.id,
label: sectionData.label
} ) );
$.each( sectionData.items, function ( index, categoryItem ) {
itemWidgets.push( widget.getCategoryWidgetFromName( categoryItem ) );
} );
}
} );
return itemWidgets;
};
/**
* @inheritdoc
*/
ve.ui.MWCategoryInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
this.emit( 'choose', item );
// Reset input
this.setValue( '' );
};
/**
* Take a category name and turn it into a menu item widget, following redirects.
*
* @method
* @param {string} name Category name
* @return {OO.ui.MenuOptionWidget} Menu item widget to be shown
*/
ve.ui.MWCategoryInputWidget.prototype.getCategoryWidgetFromName = function ( name ) {
var cachedData = ve.init.platform.linkCache.getCached( mw.Title.newFromText(
name,
mw.config.get( 'wgNamespaceIds' ).category
).getPrefixedText() ),
optionWidget, labelText;
if ( cachedData && cachedData.redirectFrom ) {
labelText = mw.Title.newFromText( cachedData.redirectFrom[ 0 ] ).getMainText();
optionWidget = new OO.ui.MenuOptionWidget( {
data: name,
autoFitLabel: false,
label: $( '<span>' )
.text( labelText )
.append( '<br>↳ ' )
.append( $( '<span>' ).text( mw.Title.newFromText( name ).getMainText() ) )
} );
} else {
labelText = name;
optionWidget = new OO.ui.MenuOptionWidget( {
data: name,
label: name
} );
}
optionWidget.$element.attr( 'title', labelText );
return optionWidget;
};