| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
ve.ui.MWDefinedTransclusionContextItem = function VeUiMWDefinedTransclusionContextItem() { |
| 46 |
|
| 47 |
ve.ui.MWDefinedTransclusionContextItem.super.apply( this, arguments ); |
| 48 |
|
| 49 |
this.tool = this.constructor.static.getMatchedTool( this.model ); |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
OO.inheritClass( ve.ui.MWDefinedTransclusionContextItem, ve.ui.MWTransclusionContextItem ); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
ve.ui.MWDefinedTransclusionContextItem.static.name = null; |
| 59 |
|
| 60 |
ve.ui.MWDefinedTransclusionContextItem.static.toolDefinitions = ( function () { |
| 61 |
let tools; |
| 62 |
|
| 63 |
try { |
| 64 |
|
| 65 |
tools = JSON.parse( mw.message( 'visualeditor-template-tools-definition.json' ).plain() ); |
| 66 |
} catch ( e ) {} |
| 67 |
|
| 68 |
return tools || {}; |
| 69 |
}() ); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
ve.ui.MWDefinedTransclusionContextItem.static.template = null; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
ve.ui.MWDefinedTransclusionContextItem.static.isCompatibleWith = function ( model ) { |
| 86 |
|
| 87 |
return ve.ui.MWDefinedTransclusionContextItem.super.static.isCompatibleWith.apply( this, arguments ) && |
| 88 |
!!this.getMatchedTool( model ); |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
ve.ui.MWDefinedTransclusionContextItem.static.getToolsByTitle = function () { |
| 97 |
if ( !this.toolsByTitle ) { |
| 98 |
let toolsByTitle; |
| 99 |
this.toolsByTitle = toolsByTitle = {}; |
| 100 |
( this.toolDefinitions[ this.name ] || [] ).forEach( ( template ) => { |
| 101 |
const titles = Array.isArray( template.title ) ? template.title : [ template.title ]; |
| 102 |
|
| 103 |
titles.forEach( ( title ) => { |
| 104 |
toolsByTitle[ mw.Title.newFromText( title, mw.config.get( 'wgNamespaceIds' ).template ).getPrefixedText() ] = template; |
| 105 |
} ); |
| 106 |
} ); |
| 107 |
} |
| 108 |
return this.toolsByTitle; |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
ve.ui.MWDefinedTransclusionContextItem.static.getMatchedTool = function ( model ) { |
| 118 |
const resource = ve.getProp( model.getAttribute( 'mw' ), 'parts', 0, 'template', 'target', 'href' ); |
| 119 |
if ( resource ) { |
| 120 |
const title = mw.Title.newFromText( mw.libs.ve.normalizeParsoidResourceName( resource ) ).getPrefixedText(); |
| 121 |
return this.getToolsByTitle()[ title ] || null; |
| 122 |
} |
| 123 |
return null; |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
ve.ui.MWDefinedTransclusionContextItem.prototype.getCanonicalParam = function ( name ) { |
| 133 |
const params = this.tool.params || {}; |
| 134 |
|
| 135 |
if ( Object.prototype.hasOwnProperty.call( params, name ) ) { |
| 136 |
const aliases = Array.isArray( params[ name ] ) ? params[ name ] : [ params[ name ] ]; |
| 137 |
|
| 138 |
for ( let i = 0; i < aliases.length; i++ ) { |
| 139 |
const value = ve.getProp( this.model.getAttribute( 'mw' ), 'parts', 0, 'template', 'params', aliases[ i ], 'wt' ); |
| 140 |
if ( value ) { |
| 141 |
return value; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
return null; |
| 146 |
}; |