You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.4 KiB
JavaScript

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.pagePrefix = config.pagePrefix || "";
this.loadedPageList = [];
};
OO.inheritClass( isekai.ui.CreatePageInputWidget, OO.ui.TextInputWidget );
OO.mixinClass( isekai.ui.CreatePageInputWidget, OO.ui.mixin.LookupElement );
isekai.ui.CreatePageInputWidget.prototype.setPagePrefix = function ( pagePrefix ) {
this.pagePrefix = pagePrefix;
this.requestCache = {}; // Clear cache
}
/**
* @inheritdoc
*/
isekai.ui.CreatePageInputWidget.prototype.getLookupRequest = function () {
var
value = this.getValue(),
deferred = $.Deferred();
var api = this.api;
var _this = this;
var pageName = this.pagePrefix + value;
this.getValidity().then( function () {
api.get( {
action: "opensearch",
search: pageName,
} ).done( function (data) {
if ( Array.isArray(data) && data.length > 0 ) {
var pageList = data[1];
pageList = pageList.map( function (pageTitle) {
return pageTitle.substring( _this.pagePrefix.length );
} );
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
} )
} );
};