增加创建页面自动补全
parent
7242b6f12c
commit
29e0b574b7
@ -0,0 +1,83 @@
|
||||
if ( !window.isekai ) {
|
||||
window.isekai = {
|
||||
ui: {}
|
||||
};
|
||||
}
|
||||
if ( !window.isekai.ui ) {
|
||||
window.isekai.ui = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Input for create page and auto complete parent page
|
||||
* @class
|
||||
* @extends OO.ui.TextInputWidget
|
||||
* @mixins OO.ui.mixin.LookupElement
|
||||
*
|
||||
* @constructor
|
||||
* @param {Object} config Configuration options
|
||||
*/
|
||||
isekai.ui.CreatePageInputWidget = function IsekaiUiCreatePageInputWidget( config ) {
|
||||
// Parent constructor
|
||||
OO.ui.TextInputWidget.call( this, config );
|
||||
// Mixin constructors
|
||||
OO.ui.mixin.LookupElement.call( this, config );
|
||||
|
||||
this.api = new mw.Api();
|
||||
|
||||
this.loadedPageList = [];
|
||||
};
|
||||
OO.inheritClass( isekai.ui.CreatePageInputWidget, OO.ui.TextInputWidget );
|
||||
OO.mixinClass( isekai.ui.CreatePageInputWidget, OO.ui.mixin.LookupElement );
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
isekai.ui.CreatePageInputWidget.prototype.getLookupRequest = function () {
|
||||
var
|
||||
value = this.getValue(),
|
||||
deferred = $.Deferred();
|
||||
var api = this.api;
|
||||
var _this = this;
|
||||
this.getValidity().then( function () {
|
||||
api.get( {
|
||||
action: "opensearch",
|
||||
search: value,
|
||||
} ).done( function (data) {
|
||||
if ( Array.isArray(data) && data.length > 0 ) {
|
||||
var pageList = data[1];
|
||||
|
||||
deferred.resolve( pageList );
|
||||
|
||||
// Set cache
|
||||
pageList.forEach( function (pageTitle) {
|
||||
if ( !_this.loadedPageList.includes( pageTitle ) ) {
|
||||
_this.loadedPageList.push( pageTitle );
|
||||
}
|
||||
} );
|
||||
} else {
|
||||
deferred.resolve( [] );
|
||||
}
|
||||
} );
|
||||
}, function () {
|
||||
// No results when the input contains invalid content
|
||||
deferred.resolve( [] );
|
||||
} );
|
||||
return deferred.promise( { abort: function () {} } );
|
||||
};
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
isekai.ui.CreatePageInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
|
||||
return response || [];
|
||||
};
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
isekai.ui.CreatePageInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
|
||||
return ( data || [] ).map( function (one) {
|
||||
return new OO.ui.MenuOptionWidget( {
|
||||
data: one + "/",
|
||||
label: one
|
||||
} )
|
||||
} );
|
||||
};
|
Loading…
Reference in New Issue