| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
ve.ce.MWTransclusionNode = function VeCeMWTransclusionNode( model, config ) { |
| 22 |
|
| 23 |
ve.ce.MWTransclusionNode.super.call( this, model, config ); |
| 24 |
|
| 25 |
|
| 26 |
ve.ce.GeneratedContentNode.call( this ); |
| 27 |
ve.ce.FocusableNode.call( this ); |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
OO.inheritClass( ve.ce.MWTransclusionNode, ve.ce.LeafNode ); |
| 33 |
|
| 34 |
OO.mixinClass( ve.ce.MWTransclusionNode, ve.ce.GeneratedContentNode ); |
| 35 |
OO.mixinClass( ve.ce.MWTransclusionNode, ve.ce.FocusableNode ); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
ve.ce.MWTransclusionNode.static.name = 'mwTransclusion'; |
| 40 |
|
| 41 |
ve.ce.MWTransclusionNode.static.primaryCommandName = 'transclusion'; |
| 42 |
|
| 43 |
ve.ce.MWTransclusionNode.static.iconWhenInvisible = 'puzzle'; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
ve.ce.MWTransclusionNode.static.getDescription = function ( model ) { |
| 56 |
return model.getPartsList() |
| 57 |
.map( ( part ) => { |
| 58 |
if ( part.templatePage ) { |
| 59 |
return mw.Title.newFromText( part.templatePage ) |
| 60 |
.getRelativeText( mw.config.get( 'wgNamespaceIds' ).template ); |
| 61 |
} |
| 62 |
|
| 63 |
return part.template || ''; |
| 64 |
} ) |
| 65 |
.filter( ( desc ) => desc ) |
| 66 |
.join( ve.msg( 'comma-separator' ) ); |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
ve.ce.MWTransclusionNode.static.getDescriptionDom = function ( model ) { |
| 81 |
const nodes = model.getPartsList() |
| 82 |
.map( ( part ) => { |
| 83 |
if ( part.templatePage ) { |
| 84 |
const title = mw.Title.newFromText( part.templatePage ); |
| 85 |
const link = document.createElement( 'a' ); |
| 86 |
link.textContent = title.getRelativeText( mw.config.get( 'wgNamespaceIds' ).template ); |
| 87 |
link.setAttribute( 'href', title.getUrl() ); |
| 88 |
return link; |
| 89 |
} |
| 90 |
|
| 91 |
return part.template ? document.createTextNode( part.template ) : null; |
| 92 |
} ) |
| 93 |
.filter( ( desc ) => desc ); |
| 94 |
const span = document.createElement( 'span' ); |
| 95 |
nodes.forEach( ( node, i ) => { |
| 96 |
if ( i ) { |
| 97 |
span.appendChild( document.createTextNode( ve.msg( 'comma-separator' ) ) ); |
| 98 |
} |
| 99 |
span.appendChild( node ); |
| 100 |
} ); |
| 101 |
ve.targetLinksToNewWindow( span ); |
| 102 |
return span; |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
ve.ce.MWTransclusionNode.static.filterRendering = function ( contentNodes ) { |
| 113 |
if ( !contentNodes.length ) { |
| 114 |
return []; |
| 115 |
} |
| 116 |
|
| 117 |
const whitespaceRegex = new RegExp( '^[' + ve.dm.Converter.static.whitespaceList + ']+$' ); |
| 118 |
|
| 119 |
|
| 120 |
contentNodes = contentNodes.filter( ( node ) => { |
| 121 |
const dataMw = node && |
| 122 |
node.nodeType === Node.ELEMENT_NODE && |
| 123 |
node.hasAttribute( 'data-mw' ) && |
| 124 |
JSON.parse( node.getAttribute( 'data-mw' ) ); |
| 125 |
|
| 126 |
return !dataMw || !dataMw.autoGenerated; |
| 127 |
} ); |
| 128 |
|
| 129 |
contentNodes.forEach( ( node ) => { |
| 130 |
if ( node.nodeType === Node.ELEMENT_NODE ) { |
| 131 |
mw.libs.ve.stripParsoidFallbackIds( node ); |
| 132 |
} |
| 133 |
} ); |
| 134 |
|
| 135 |
function isWhitespaceNode( node ) { |
| 136 |
return node && node.nodeType === Node.TEXT_NODE && whitespaceRegex.test( node.data ); |
| 137 |
} |
| 138 |
|
| 139 |
while ( isWhitespaceNode( contentNodes[ 0 ] ) ) { |
| 140 |
contentNodes.shift(); |
| 141 |
} |
| 142 |
while ( isWhitespaceNode( contentNodes[ contentNodes.length - 1 ] ) ) { |
| 143 |
contentNodes.pop(); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if ( contentNodes.length === 1 && contentNodes[ 0 ].nodeName.toLowerCase() === 'p' ) { |
| 149 |
contentNodes = Array.prototype.slice.call( contentNodes[ 0 ].childNodes ); |
| 150 |
} |
| 151 |
return contentNodes; |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
ve.ce.MWTransclusionNode.prototype.executeCommand = function () { |
| 158 |
const contextItems = this.focusableSurface.getSurface().getContext().items; |
| 159 |
const contextClicked = contextItems.some( ( contextItem ) => { |
| 160 |
if ( contextItem instanceof ve.ui.MWTransclusionContextItem ) { |
| 161 |
|
| 162 |
). |
| 163 |
contextItem.onEditButtonClick(); |
| 164 |
return true; |
| 165 |
} |
| 166 |
return false; |
| 167 |
} ); |
| 168 |
|
| 169 |
if ( contextClicked ) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
ve.ce.FocusableNode.prototype.executeCommand.apply( this, arguments ); |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
ve.ce.MWTransclusionNode.prototype.generateContents = function ( config ) { |
| 181 |
const deferred = ve.createDeferred(); |
| 182 |
const xhr = ve.init.target.parseWikitextFragment( |
| 183 |
( config && config.wikitext ) || this.model.getWikitext(), |
| 184 |
true, |
| 185 |
this.getModel().getDocument() |
| 186 |
) |
| 187 |
.done( this.onParseSuccess.bind( this, deferred ) ) |
| 188 |
.fail( this.onParseError.bind( this, deferred ) ); |
| 189 |
|
| 190 |
return deferred.promise( { abort: xhr.abort } ); |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
ve.ce.MWTransclusionNode.prototype.onParseSuccess = function ( deferred, response ) { |
| 200 |
if ( ve.getProp( response, 'visualeditor', 'result' ) !== 'success' ) { |
| 201 |
this.onParseError( deferred ); |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
const contentNodes = $.parseHTML( response.visualeditor.content, this.model && this.getModelHtmlDocument() ) || []; |
| 207 |
deferred.resolve( this.constructor.static.filterRendering( contentNodes ) ); |
| 208 |
}; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
ve.ce.MWTransclusionNode.prototype.render = function ( generatedContents ) { |
| 218 |
|
| 219 |
ve.ce.GeneratedContentNode.prototype.render.call( this, generatedContents ); |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
ve.ce.MWTransclusionNode.prototype.onSetup = function () { |
| 226 |
|
| 227 |
ve.ce.MWTransclusionNode.super.prototype.onSetup.apply( this, arguments ); |
| 228 |
|
| 229 |
|
| 230 |
this.$element.addClass( 've-ce-mwTransclusionNode' ); |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
ve.ce.MWTransclusionNode.prototype.getRenderedDomElements = function () { |
| 237 |
|
| 238 |
const elements = ve.ce.GeneratedContentNode.prototype.getRenderedDomElements.apply( this, arguments ); |
| 239 |
|
| 240 |
if ( this.model && this.getModelHtmlDocument() ) { |
| 241 |
ve.init.platform.linkCache.styleParsoidElements( |
| 242 |
$( elements ), |
| 243 |
this.getModelHtmlDocument() |
| 244 |
); |
| 245 |
} |
| 246 |
return elements; |
| 247 |
}; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
ve.ce.MWTransclusionNode.prototype.filterRenderedDomElements = function ( domElements ) { |
| 253 |
|
| 254 |
const selector = 'style:not([data-mw-deduplicate^="TemplateStyles:"]), link:not([rel~="mw-deduplicated-inline-style"][href^="mw-data:TemplateStyles:"])'; |
| 255 |
return $( domElements ).find( selector ).addBack( selector ).remove().end().end().toArray(); |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
ve.ce.MWTransclusionNode.prototype.onParseError = function ( deferred ) { |
| 265 |
deferred.reject(); |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
ve.ce.nodeFactory.register( ve.ce.MWTransclusionNode ); |