| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
ve.ui.MWCategoryInputWidget = function VeUiMWCategoryInputWidget( categoryWidget, config ) { |
| 22 |
|
| 23 |
config = ve.extendObject( { |
| 24 |
placeholder: ve.msg( 'visualeditor-dialog-meta-categories-input-placeholder' ) |
| 25 |
}, config ); |
| 26 |
|
| 27 |
|
| 28 |
ve.ui.MWCategoryInputWidget.super.call( this, config ); |
| 29 |
|
| 30 |
|
| 31 |
OO.ui.mixin.LookupElement.call( this, config ); |
| 32 |
|
| 33 |
|
| 34 |
this.categoryWidget = categoryWidget; |
| 35 |
this.api = config.api || ve.init.target.getContentApi(); |
| 36 |
|
| 37 |
this.$input.attr( 'aria-label', ve.msg( 'visualeditor-dialog-meta-categories-input-placeholder' ) ); |
| 38 |
|
| 39 |
|
| 40 |
this.$element.addClass( 've-ui-mwCategoryInputWidget' ); |
| 41 |
this.lookupMenu.$element.addClass( 've-ui-mwCategoryInputWidget-menu' ); |
| 42 |
this.lookupMenu.$element.attr( 'aria-label', ve.msg( 'visualeditor-dialog-meta-categories-data-label' ) ); |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
OO.inheritClass( ve.ui.MWCategoryInputWidget, OO.ui.TextInputWidget ); |
| 48 |
|
| 49 |
OO.mixinClass( ve.ui.MWCategoryInputWidget, OO.ui.mixin.LookupElement ); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
ve.ui.MWCategoryInputWidget.prototype.getLookupRequest = function () { |
| 66 |
let title = mw.Title.newFromText( this.value ); |
| 67 |
if ( title && title.getNamespaceId() === mw.config.get( 'wgNamespaceIds' ).category ) { |
| 68 |
title = title.getMainText(); |
| 69 |
} else { |
| 70 |
title = this.value; |
| 71 |
} |
| 72 |
return this.api.get( { |
| 73 |
action: 'query', |
| 74 |
generator: 'allcategories', |
| 75 |
gacmin: 1, |
| 76 |
gacprefix: title, |
| 77 |
prop: 'categoryinfo', |
| 78 |
redirects: '' |
| 79 |
} ); |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
ve.ui.MWCategoryInputWidget.prototype.getLookupCacheDataFromResponse = function ( data ) { |
| 86 |
const result = [], |
| 87 |
linkCacheUpdate = {}, |
| 88 |
query = data.query || {}; |
| 89 |
|
| 90 |
( query.pages || [] ).forEach( ( categoryPage ) => { |
| 91 |
result.push( mw.Title.newFromText( categoryPage.title ).getMainText() ); |
| 92 |
linkCacheUpdate[ categoryPage.title ] = { |
| 93 |
missing: Object.prototype.hasOwnProperty.call( categoryPage, 'missing' ), |
| 94 |
hidden: ( |
| 95 |
categoryPage.categoryinfo && |
| 96 |
Object.prototype.hasOwnProperty.call( categoryPage.categoryinfo, 'missing' ) |
| 97 |
) |
| 98 |
}; |
| 99 |
} ); |
| 100 |
|
| 101 |
( query.redirects || [] ).forEach( ( redirect ) => { |
| 102 |
if ( !Object.prototype.hasOwnProperty.call( linkCacheUpdate, redirect.to ) ) { |
| 103 |
linkCacheUpdate[ redirect.to ] = ve.init.platform.linkCache.getCached( redirect.to ) || |
| 104 |
{ missing: false, redirectFrom: [ redirect.from ] }; |
| 105 |
} |
| 106 |
if ( |
| 107 |
linkCacheUpdate[ redirect.to ].redirectFrom && |
| 108 |
linkCacheUpdate[ redirect.to ].redirectFrom.indexOf( redirect.from ) === -1 |
| 109 |
) { |
| 110 |
linkCacheUpdate[ redirect.to ].redirectFrom.push( redirect.from ); |
| 111 |
} else { |
| 112 |
linkCacheUpdate[ redirect.to ].redirectFrom = [ redirect.from ]; |
| 113 |
} |
| 114 |
} ); |
| 115 |
|
| 116 |
ve.init.platform.linkCache.set( linkCacheUpdate ); |
| 117 |
|
| 118 |
return result; |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
ve.ui.MWCategoryInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) { |
| 125 |
const itemWidgets = [], |
| 126 |
existingCategoryItems = [], |
| 127 |
matchingCategoryItems = [], |
| 128 |
hiddenCategoryItems = [], |
| 129 |
newCategoryItems = [], |
| 130 |
existingCategories = this.categoryWidget.getCategories(), |
| 131 |
linkCacheUpdate = {}; |
| 132 |
|
| 133 |
let canonicalQueryValue = mw.Title.newFromText( this.value ), |
| 134 |
prefixedCanonicalQueryValue = mw.Title.newFromText( |
| 135 |
this.value, |
| 136 |
mw.config.get( 'wgNamespaceIds' ).category |
| 137 |
); |
| 138 |
|
| 139 |
prefixedCanonicalQueryValue = prefixedCanonicalQueryValue && prefixedCanonicalQueryValue.getPrefixedText(); |
| 140 |
|
| 141 |
|
| 142 |
if ( canonicalQueryValue ) { |
| 143 |
canonicalQueryValue = canonicalQueryValue.getMainText(); |
| 144 |
} |
| 145 |
|
| 146 |
let exactMatch = false; |
| 147 |
data.forEach( ( suggestedCategory ) => { |
| 148 |
const suggestedCategoryTitle = mw.Title.newFromText( |
| 149 |
suggestedCategory, |
| 150 |
mw.config.get( 'wgNamespaceIds' ).category |
| 151 |
).getPrefixedText(), |
| 152 |
suggestedCacheEntry = ve.init.platform.linkCache.getCached( suggestedCategoryTitle ); |
| 153 |
if ( canonicalQueryValue === suggestedCategory ) { |
| 154 |
exactMatch = true; |
| 155 |
} |
| 156 |
if ( !suggestedCacheEntry ) { |
| 157 |
linkCacheUpdate[ suggestedCategoryTitle ] = { missing: false }; |
| 158 |
} |
| 159 |
if ( |
| 160 |
existingCategories.indexOf( suggestedCategory ) === -1 |
| 161 |
) { |
| 162 |
if ( suggestedCacheEntry && suggestedCacheEntry.hidden ) { |
| 163 |
hiddenCategoryItems.push( suggestedCategory ); |
| 164 |
} else { |
| 165 |
matchingCategoryItems.push( suggestedCategory ); |
| 166 |
} |
| 167 |
} |
| 168 |
} ); |
| 169 |
|
| 170 |
|
| 171 |
existingCategories.forEach( ( existingCategory, i ) => { |
| 172 |
if ( existingCategory === canonicalQueryValue ) { |
| 173 |
exactMatch = true; |
| 174 |
} |
| 175 |
if ( i < existingCategories.length - 1 && existingCategory.lastIndexOf( canonicalQueryValue, 0 ) === 0 ) { |
| 176 |
|
| 177 |
existingCategoryItems.push( existingCategory ); |
| 178 |
} |
| 179 |
} ); |
| 180 |
|
| 181 |
|
| 182 |
if ( !exactMatch && canonicalQueryValue ) { |
| 183 |
newCategoryItems.push( canonicalQueryValue ); |
| 184 |
linkCacheUpdate[ prefixedCanonicalQueryValue ] = { missing: true }; |
| 185 |
} |
| 186 |
|
| 187 |
ve.init.platform.linkCache.set( linkCacheUpdate ); |
| 188 |
|
| 189 |
|
| 190 |
[ |
| 191 |
{ |
| 192 |
id: 'newCategory', |
| 193 |
label: ve.msg( 'visualeditor-dialog-meta-categories-input-newcategorylabel' ), |
| 194 |
items: newCategoryItems |
| 195 |
}, |
| 196 |
{ |
| 197 |
id: 'inArticle', |
| 198 |
label: ve.msg( 'visualeditor-dialog-meta-categories-input-movecategorylabel' ), |
| 199 |
items: existingCategoryItems |
| 200 |
}, |
| 201 |
{ |
| 202 |
id: 'matchingCategories', |
| 203 |
label: ve.msg( 'visualeditor-dialog-meta-categories-input-matchingcategorieslabel' ), |
| 204 |
items: matchingCategoryItems |
| 205 |
}, |
| 206 |
{ |
| 207 |
id: 'hiddenCategories', |
| 208 |
label: ve.msg( 'visualeditor-dialog-meta-categories-input-hiddencategorieslabel' ), |
| 209 |
items: hiddenCategoryItems |
| 210 |
} |
| 211 |
].forEach( ( sectionData ) => { |
| 212 |
if ( sectionData.items.length ) { |
| 213 |
itemWidgets.push( new OO.ui.MenuSectionOptionWidget( { |
| 214 |
data: sectionData.id, |
| 215 |
label: sectionData.label |
| 216 |
} ) ); |
| 217 |
sectionData.items.forEach( ( categoryItem ) => { |
| 218 |
itemWidgets.push( this.getCategoryWidgetFromName( categoryItem ) ); |
| 219 |
} ); |
| 220 |
} |
| 221 |
} ); |
| 222 |
|
| 223 |
return itemWidgets; |
| 224 |
}; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
ve.ui.MWCategoryInputWidget.prototype.onLookupMenuChoose = function ( item ) { |
| 231 |
this.emit( 'choose', item ); |
| 232 |
|
| 233 |
|
| 234 |
this.setValue( '' ); |
| 235 |
}; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
ve.ui.MWCategoryInputWidget.prototype.getCategoryWidgetFromName = function ( name ) { |
| 244 |
const cachedData = ve.init.platform.linkCache.getCached( mw.Title.newFromText( |
| 245 |
name, |
| 246 |
mw.config.get( 'wgNamespaceIds' ).category |
| 247 |
).getPrefixedText() ); |
| 248 |
let optionWidget, labelText; |
| 249 |
if ( cachedData && cachedData.redirectFrom ) { |
| 250 |
labelText = mw.Title.newFromText( cachedData.redirectFrom[ 0 ] ).getMainText(); |
| 251 |
optionWidget = new OO.ui.MenuOptionWidget( { |
| 252 |
data: name, |
| 253 |
autoFitLabel: false, |
| 254 |
label: $( '<span>' ) |
| 255 |
.text( labelText ) |
| 256 |
.append( |
| 257 |
$( '<br>' ), |
| 258 |
$( document.createTextNode( '↳ ' ) ), |
| 259 |
$( '<span>' ).text( mw.Title.newFromText( name ).getMainText() ) |
| 260 |
) |
| 261 |
} ); |
| 262 |
} else { |
| 263 |
labelText = name; |
| 264 |
optionWidget = new OO.ui.MenuOptionWidget( { |
| 265 |
data: name, |
| 266 |
label: name |
| 267 |
} ); |
| 268 |
} |
| 269 |
optionWidget.$element.attr( 'title', labelText ); |
| 270 |
return optionWidget; |
| 271 |
}; |