2024-03-14 17:17:36 +00:00
|
|
|
mw.editcheck.EditCheckFactory = function MWEditEditCheckFactory() {
|
|
|
|
// Parent constructor
|
|
|
|
mw.editcheck.EditCheckFactory.super.call( this, this.arguments );
|
|
|
|
|
|
|
|
this.checksByListener = {
|
|
|
|
onDocumentChange: [],
|
|
|
|
onBeforeSave: []
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( mw.editcheck.EditCheckFactory, OO.Factory );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2024-11-19 13:41:38 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2024-03-14 17:17:36 +00:00
|
|
|
mw.editcheck.EditCheckFactory.prototype.register = function ( constructor, name ) {
|
|
|
|
name = name || ( constructor.static && constructor.static.name );
|
|
|
|
|
|
|
|
if ( typeof name !== 'string' || name === '' ) {
|
|
|
|
throw new Error( 'Check names must be strings and must not be empty' );
|
|
|
|
}
|
|
|
|
if ( !( constructor.prototype instanceof mw.editcheck.BaseEditCheck ) ) {
|
|
|
|
throw new Error( 'Checks must be subclasses of mw.editcheck.BaseEditCheck' );
|
|
|
|
}
|
|
|
|
if ( this.lookup( name ) === constructor ) {
|
|
|
|
// Don't allow double registration as it would create duplicate
|
|
|
|
// entries in various caches.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent method
|
|
|
|
mw.editcheck.EditCheckFactory.super.prototype.register.call( this, constructor, name );
|
|
|
|
|
|
|
|
if ( constructor.prototype.onDocumentChange ) {
|
|
|
|
this.checksByListener.onDocumentChange.push( name );
|
|
|
|
}
|
|
|
|
if ( constructor.prototype.onBeforeSave ) {
|
|
|
|
this.checksByListener.onBeforeSave.push( name );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of registered command names.
|
|
|
|
*
|
|
|
|
* @param {string} listener Listener name, 'onDocumentChange', 'onBeforeSave'
|
|
|
|
* @return {string[]}
|
|
|
|
*/
|
|
|
|
mw.editcheck.EditCheckFactory.prototype.getNamesByListener = function ( listener ) {
|
|
|
|
if ( !this.checksByListener[ listener ] ) {
|
|
|
|
throw new Error( `Unknown listener '${ listener }'` );
|
|
|
|
}
|
|
|
|
return this.checksByListener[ listener ];
|
|
|
|
};
|
|
|
|
|
2024-11-19 13:41:38 +00:00
|
|
|
/**
|
|
|
|
* Create all checks actions for a given listener
|
|
|
|
*
|
|
|
|
* TODO: Rename to createAllActionsByListener
|
|
|
|
*
|
|
|
|
* @param {string} listener Listener name
|
|
|
|
* @param {ve.dm.Surface} surfaceModel Surface model
|
|
|
|
* @return {mw.editcheck.EditCheckActions[]} Actions, sorted by range
|
|
|
|
*/
|
2024-10-01 11:31:17 +00:00
|
|
|
mw.editcheck.EditCheckFactory.prototype.createAllByListener = function ( listener, surfaceModel ) {
|
2024-03-14 17:17:36 +00:00
|
|
|
const newChecks = [];
|
|
|
|
this.getNamesByListener( listener ).forEach( ( checkName ) => {
|
|
|
|
const check = this.create( checkName, mw.editcheck.config[ checkName ] );
|
2024-09-04 15:23:43 +00:00
|
|
|
if ( !check.canBeShown() ) {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-01 11:31:17 +00:00
|
|
|
const actions = check[ listener ]( surfaceModel );
|
2024-03-14 17:17:36 +00:00
|
|
|
if ( actions.length > 0 ) {
|
|
|
|
ve.batchPush( newChecks, actions );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
newChecks.sort(
|
2024-11-05 11:41:50 +00:00
|
|
|
( a, b ) => a.highlights[ 0 ].getSelection().getCoveringRange().start - b.highlights[ 0 ].getSelection().getCoveringRange().start
|
2024-03-14 17:17:36 +00:00
|
|
|
);
|
|
|
|
return newChecks;
|
|
|
|
};
|
|
|
|
|
|
|
|
mw.editcheck.editCheckFactory = new mw.editcheck.EditCheckFactory();
|