| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
ve.ce.MWBlockImageNode = function VeCeMWBlockImageNode() { |
| 20 |
|
| 21 |
ve.ce.MWBlockImageNode.super.apply( this, arguments ); |
| 22 |
|
| 23 |
const type = this.model.getAttribute( 'type' ); |
| 24 |
const isError = this.model.getAttribute( 'isError' ); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
let $image, $focusable; |
| 33 |
|
| 34 |
if ( isError ) { |
| 35 |
$image = $( [] ); |
| 36 |
const $missingImage = $( '<span>' ) |
| 37 |
.addClass( 'mw-file-element mw-broken-media' ) |
| 38 |
.text( this.model.getAttribute( 'errorText' ) ); |
| 39 |
this.$a = $( '<a>' ) |
| 40 |
.addClass( 'new' ) |
| 41 |
.append( $missingImage ); |
| 42 |
$focusable = $missingImage; |
| 43 |
} else { |
| 44 |
$image = $( '<img>' ) |
| 45 |
.addClass( 'mw-file-element' ) |
| 46 |
.attr( 'src', this.getResolvedAttribute( 'src' ) ); |
| 47 |
this.$a = $( '<a>' ) |
| 48 |
.addClass( 'mw-file-description' ) |
| 49 |
.append( $image ); |
| 50 |
|
| 51 |
ve.setAttributeSafe( this.$a[ 0 ], 'href', this.getResolvedAttribute( 'href' ) || '', '#' ); |
| 52 |
$focusable = $image; |
| 53 |
} |
| 54 |
|
| 55 |
this.$element |
| 56 |
.prepend( this.$a ) |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
.addClass( 've-ce-mwBlockImageNode ve-ce-mwBlockImageNode-type-' + type ) |
| 63 |
|
| 64 |
|
| 65 |
.attr( 'typeof', this.model.getRdfa() ); |
| 66 |
|
| 67 |
|
| 68 |
ve.ce.MWImageNode.call( this, $focusable, $image ); |
| 69 |
|
| 70 |
|
| 71 |
this.updateSize(); |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
OO.inheritClass( ve.ce.MWBlockImageNode, ve.ce.BranchNode ); |
| 77 |
|
| 78 |
|
| 79 |
OO.mixinClass( ve.ce.MWBlockImageNode, ve.ce.GeneratedContentNode ); |
| 80 |
|
| 81 |
OO.mixinClass( ve.ce.MWBlockImageNode, ve.ce.MWImageNode ); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
ve.ce.MWBlockImageNode.static.name = 'mwBlockImage'; |
| 86 |
|
| 87 |
ve.ce.MWBlockImageNode.static.tagName = 'figure'; |
| 88 |
|
| 89 |
ve.ce.MWBlockImageNode.static.renderHtmlAttributes = false; |
| 90 |
|
| 91 |
ve.ce.MWBlockImageNode.static.autoFocus = false; |
| 92 |
|
| 93 |
ve.ce.MWBlockImageNode.static.cssClasses = { |
| 94 |
default: { |
| 95 |
left: 'mw-halign-left', |
| 96 |
right: 'mw-halign-right', |
| 97 |
center: 'mw-halign-center', |
| 98 |
none: 'mw-halign-none' |
| 99 |
}, |
| 100 |
none: { |
| 101 |
left: 'mw-halign-left', |
| 102 |
right: 'mw-halign-right', |
| 103 |
center: 'mw-halign-center', |
| 104 |
none: 'mw-halign-none' |
| 105 |
} |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
ve.ce.MWBlockImageNode.prototype.updateClasses = function ( oldAlign ) { |
| 116 |
const align = this.model.getAttribute( 'align' ); |
| 117 |
|
| 118 |
if ( oldAlign && oldAlign !== align ) { |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
this.$element |
| 123 |
.removeClass( this.getCssClass( 'none', oldAlign ) ) |
| 124 |
.removeClass( this.getCssClass( 'default', oldAlign ) ); |
| 125 |
} |
| 126 |
|
| 127 |
const type = this.model.getAttribute( 'type' ); |
| 128 |
const framed = type !== 'none' && type !== 'frameless'; |
| 129 |
const alignClass = this.getCssClass( framed ? 'default' : 'none', align ); |
| 130 |
this.$image.toggleClass( 've-ce-mwBlockImageNode-thumbimage', framed ); |
| 131 |
|
| 132 |
this.$element.addClass( alignClass ); |
| 133 |
|
| 134 |
|
| 135 |
this.$element.toggleClass( 'mw-image-border', !!this.model.getAttribute( 'borderImage' ) ); |
| 136 |
|
| 137 |
this.showHandles( { |
| 138 |
'mw-halign-right': [ 'sw' ], |
| 139 |
'mw-halign-left': [ 'se' ], |
| 140 |
'mw-halign-center': [ 'sw', 'se' ] |
| 141 |
|
| 142 |
}[ alignClass ] ); |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
ve.ce.MWBlockImageNode.prototype.updateSize = function ( dimensions ) { |
| 153 |
const isError = this.model.getAttribute( 'isError' ); |
| 154 |
|
| 155 |
if ( isError ) { |
| 156 |
this.$element.css( { width: '', height: '' } ); |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
if ( !dimensions ) { |
| 161 |
dimensions = { |
| 162 |
width: this.model.getAttribute( 'width' ), |
| 163 |
height: this.model.getAttribute( 'height' ) |
| 164 |
}; |
| 165 |
} |
| 166 |
|
| 167 |
this.$image.css( dimensions ); |
| 168 |
|
| 169 |
const type = this.model.getAttribute( 'type' ); |
| 170 |
const borderImage = this.model.getAttribute( 'borderImage' ); |
| 171 |
const hasBorderOrFrame = ( type !== 'none' && type !== 'frameless' ) || borderImage; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
this.$element.css( { |
| 176 |
width: dimensions.width + ( hasBorderOrFrame ? 2 : 0 ), |
| 177 |
height: hasBorderOrFrame ? 'auto' : dimensions.height |
| 178 |
} ); |
| 179 |
this.$element.toggleClass( 'mw-default-size', !!this.model.getAttribute( 'defaultSize' ) ); |
| 180 |
}; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
ve.ce.MWBlockImageNode.prototype.getCssClass = function ( type, alignment ) { |
| 190 |
|
| 191 |
|
| 192 |
if ( type === 'default' && alignment === 'default' ) { |
| 193 |
if ( this.getModel().getDocument().getDir() === 'rtl' ) { |
| 194 |
return 'mw-halign-left'; |
| 195 |
} else { |
| 196 |
return 'mw-halign-right'; |
| 197 |
} |
| 198 |
} else { |
| 199 |
return this.constructor.static.cssClasses[ type ][ alignment ]; |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
ve.ce.MWBlockImageNode.prototype.onSetup = function () { |
| 208 |
|
| 209 |
ve.ce.MWBlockImageNode.super.prototype.onSetup.call( this ); |
| 210 |
|
| 211 |
this.updateClasses(); |
| 212 |
}; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
ve.ce.MWBlockImageNode.prototype.onAttributeChange = function ( key, from, to ) { |
| 218 |
|
| 219 |
ve.ce.MWImageNode.prototype.onAttributeChange.apply( this, arguments ); |
| 220 |
|
| 221 |
if ( key === 'height' || key === 'width' ) { |
| 222 |
to = parseInt( to, 10 ); |
| 223 |
} |
| 224 |
|
| 225 |
if ( from !== to ) { |
| 226 |
switch ( key ) { |
| 227 |
case 'align': |
| 228 |
this.updateClasses( from ); |
| 229 |
break; |
| 230 |
case 'src': |
| 231 |
this.$image.attr( 'src', this.getResolvedAttribute( 'src' ) ); |
| 232 |
break; |
| 233 |
case 'width': |
| 234 |
this.updateSize( { |
| 235 |
width: to, |
| 236 |
height: this.model.getAttribute( 'height' ) |
| 237 |
} ); |
| 238 |
break; |
| 239 |
case 'height': |
| 240 |
this.updateSize( { |
| 241 |
width: this.model.getAttribute( 'width' ), |
| 242 |
height: to |
| 243 |
} ); |
| 244 |
break; |
| 245 |
case 'type': |
| 246 |
|
| 247 |
|
| 248 |
this.$element |
| 249 |
.removeClass( 've-ce-mwBlockImageNode-type-' + from ) |
| 250 |
.addClass( 've-ce-mwBlockImageNode-type-' + to ) |
| 251 |
.attr( 'typeof', this.model.getRdfa() ); |
| 252 |
|
| 253 |
this.updateClasses(); |
| 254 |
this.updateSize(); |
| 255 |
break; |
| 256 |
case 'borderImage': |
| 257 |
this.updateClasses(); |
| 258 |
this.updateSize(); |
| 259 |
break; |
| 260 |
|
| 261 |
case 'alt': |
| 262 |
if ( !to ) { |
| 263 |
this.$image.removeAttr( key ); |
| 264 |
} else { |
| 265 |
this.$image.attr( key, to ); |
| 266 |
} |
| 267 |
break; |
| 268 |
case 'mediaType': |
| 269 |
this.updateMediaType(); |
| 270 |
break; |
| 271 |
case 'defaultSize': |
| 272 |
this.$element.toggleClass( 'mw-default-size', to ); |
| 273 |
break; |
| 274 |
} |
| 275 |
} |
| 276 |
}; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
ve.ce.MWBlockImageNode.prototype.onResizableResizing = function ( dimensions ) { |
| 282 |
if ( !this.outline ) { |
| 283 |
ve.ce.ResizableNode.prototype.onResizableResizing.call( this, dimensions ); |
| 284 |
|
| 285 |
this.updateSize( dimensions ); |
| 286 |
} |
| 287 |
}; |
| 288 |
|
| 289 |
ve.ce.MWBlockImageNode.prototype.getDomPosition = function () { |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
const domNode = this.$element.last()[ 0 ]; |
| 295 |
return { |
| 296 |
node: domNode, |
| 297 |
offset: domNode.childNodes.length |
| 298 |
}; |
| 299 |
}; |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
ve.ce.nodeFactory.register( ve.ce.MWBlockImageNode ); |