| 1 |
mw.editcheck.EditCheckFactory = function MWEditEditCheckFactory() { |
| 2 |
|
| 3 |
mw.editcheck.EditCheckFactory.super.call( this, this.arguments ); |
| 4 |
|
| 5 |
this.checksByListener = { |
| 6 |
onDocumentChange: [], |
| 7 |
onBeforeSave: [] |
| 8 |
}; |
| 9 |
}; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
OO.inheritClass( mw.editcheck.EditCheckFactory, OO.Factory ); |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
mw.editcheck.EditCheckFactory.prototype.register = function ( constructor, name ) { |
| 18 |
name = name || ( constructor.static && constructor.static.name ); |
| 19 |
|
| 20 |
if ( typeof name !== 'string' || name === '' ) { |
| 21 |
throw new Error( 'Check names must be strings and must not be empty' ); |
| 22 |
} |
| 23 |
if ( !( constructor.prototype instanceof mw.editcheck.BaseEditCheck ) ) { |
| 24 |
throw new Error( 'Checks must be subclasses of mw.editcheck.BaseEditCheck' ); |
| 25 |
} |
| 26 |
if ( this.lookup( name ) === constructor ) { |
| 27 |
|
| 28 |
|
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
mw.editcheck.EditCheckFactory.super.prototype.register.call( this, constructor, name ); |
| 34 |
|
| 35 |
if ( constructor.prototype.onDocumentChange ) { |
| 36 |
this.checksByListener.onDocumentChange.push( name ); |
| 37 |
} |
| 38 |
if ( constructor.prototype.onBeforeSave ) { |
| 39 |
this.checksByListener.onBeforeSave.push( name ); |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
mw.editcheck.EditCheckFactory.prototype.getNamesByListener = function ( listener ) { |
| 50 |
if ( !this.checksByListener[ listener ] ) { |
| 51 |
throw new Error( `Unknown listener '${ listener }'` ); |
| 52 |
} |
| 53 |
return this.checksByListener[ listener ]; |
| 54 |
}; |
| 55 |
|
| 56 |
mw.editcheck.EditCheckFactory.prototype.createAllByListener = function ( listener, surfaceModel ) { |
| 57 |
const newChecks = []; |
| 58 |
this.getNamesByListener( listener ).forEach( ( checkName ) => { |
| 59 |
const check = this.create( checkName, mw.editcheck.config[ checkName ] ); |
| 60 |
if ( !check.canBeShown() ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
const actions = check[ listener ]( surfaceModel ); |
| 64 |
if ( actions.length > 0 ) { |
| 65 |
ve.batchPush( newChecks, actions ); |
| 66 |
} |
| 67 |
} ); |
| 68 |
newChecks.sort( |
| 69 |
( a, b ) => a.highlight.getSelection().getCoveringRange().start - b.highlight.getSelection().getCoveringRange().start |
| 70 |
); |
| 71 |
return newChecks; |
| 72 |
}; |
| 73 |
|
| 74 |
mw.editcheck.editCheckFactory = new mw.editcheck.EditCheckFactory(); |