2013-04-25 00:47:17 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor ContentEditable MWBlockImageNode class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2013-04-25 00:47:17 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentEditable MediaWiki image node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ce.BranchNode
|
2024-04-29 17:51:41 +00:00
|
|
|
* @mixes ve.ce.MWImageNode
|
2013-05-15 17:01:19 +00:00
|
|
|
*
|
2013-04-25 00:47:17 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.MWBlockImageNode} model Model to observe
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-04-25 00:47:17 +00:00
|
|
|
*/
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ce.MWBlockImageNode = function VeCeMWBlockImageNode() {
|
2013-04-25 00:47:17 +00:00
|
|
|
// Parent constructor
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ce.MWBlockImageNode.super.apply( this, arguments );
|
2013-04-25 00:47:17 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const type = this.model.getAttribute( 'type' );
|
|
|
|
const isError = this.model.getAttribute( 'isError' );
|
2013-06-19 22:58:13 +00:00
|
|
|
|
2017-01-15 02:23:53 +00:00
|
|
|
// DOM Hierarchy for MWBlockImageNode:
|
2014-08-16 17:44:49 +00:00
|
|
|
// <figure> this.$element (ve-ce-mwBlockImageNode-{type})
|
2013-12-18 21:22:41 +00:00
|
|
|
// <a> this.$a
|
2014-02-25 23:55:33 +00:00
|
|
|
// <img> this.$image
|
2018-02-28 23:43:18 +00:00
|
|
|
// <figcaption> ve.ce.MWImageCaptionNode
|
2013-11-01 06:13:09 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
let $image, $focusable;
|
2016-11-06 15:08:23 +00:00
|
|
|
// Build DOM:
|
2015-05-12 22:48:20 +00:00
|
|
|
if ( isError ) {
|
2016-11-06 15:08:23 +00:00
|
|
|
$image = $( [] );
|
2024-05-21 14:22:56 +00:00
|
|
|
const $missingImage = $( '<span>' )
|
2023-06-09 15:52:08 +00:00
|
|
|
.addClass( 'mw-file-element mw-broken-media' )
|
|
|
|
.text( this.model.getAttribute( 'errorText' ) );
|
2015-05-12 22:48:20 +00:00
|
|
|
this.$a = $( '<a>' )
|
|
|
|
.addClass( 'new' )
|
2021-01-27 09:03:38 +00:00
|
|
|
.append( $missingImage );
|
|
|
|
$focusable = $missingImage;
|
2015-05-12 22:48:20 +00:00
|
|
|
} else {
|
2016-11-06 15:08:23 +00:00
|
|
|
$image = $( '<img>' )
|
2023-06-09 15:52:08 +00:00
|
|
|
.addClass( 'mw-file-element' )
|
2016-11-06 15:08:23 +00:00
|
|
|
.attr( 'src', this.getResolvedAttribute( 'src' ) );
|
2015-05-12 22:48:20 +00:00
|
|
|
this.$a = $( '<a>' )
|
2023-06-09 15:52:08 +00:00
|
|
|
.addClass( 'mw-file-description' )
|
2016-11-06 15:08:23 +00:00
|
|
|
.append( $image );
|
2022-11-24 13:25:14 +00:00
|
|
|
// T322704
|
|
|
|
ve.setAttributeSafe( this.$a[ 0 ], 'href', this.getResolvedAttribute( 'href' ) || '', '#' );
|
2016-11-06 00:47:40 +00:00
|
|
|
$focusable = $image;
|
2015-05-12 22:48:20 +00:00
|
|
|
}
|
2013-05-17 19:40:12 +00:00
|
|
|
|
2014-08-16 17:44:49 +00:00
|
|
|
this.$element
|
2018-02-28 23:43:18 +00:00
|
|
|
.prepend( this.$a )
|
2020-04-09 13:33:54 +00:00
|
|
|
// The following classes are used here:
|
|
|
|
// * ve-ce-mwBlockImageNode-type-thumb
|
|
|
|
// * ve-ce-mwBlockImageNode-type-frame
|
|
|
|
// * ve-ce-mwBlockImageNode-type-frameless
|
|
|
|
// * ve-ce-mwBlockImageNode-type-none
|
2014-05-29 01:35:07 +00:00
|
|
|
.addClass( 've-ce-mwBlockImageNode ve-ce-mwBlockImageNode-type-' + type )
|
2013-11-01 06:13:09 +00:00
|
|
|
// 'typeof' should appear with the proper Parsoid-generated
|
|
|
|
// type. The model deals with converting it
|
2017-04-27 18:08:35 +00:00
|
|
|
.attr( 'typeof', this.model.getRdfa() );
|
2013-08-30 21:04:29 +00:00
|
|
|
|
2016-11-06 15:08:23 +00:00
|
|
|
// Mixin constructors
|
2016-11-06 00:47:40 +00:00
|
|
|
ve.ce.MWImageNode.call( this, $focusable, $image );
|
2016-11-06 15:08:23 +00:00
|
|
|
|
2020-02-14 01:23:00 +00:00
|
|
|
// Initialization
|
2013-12-18 21:22:41 +00:00
|
|
|
this.updateSize();
|
2013-04-25 00:47:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.ce.MWBlockImageNode, ve.ce.BranchNode );
|
2013-04-25 00:47:17 +00:00
|
|
|
|
2017-04-13 10:32:30 +00:00
|
|
|
// Need to mixin base class as well (T92540)
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.ce.MWBlockImageNode, ve.ce.GeneratedContentNode );
|
2013-06-27 18:24:26 +00:00
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.ce.MWBlockImageNode, ve.ce.MWImageNode );
|
2013-07-16 14:06:47 +00:00
|
|
|
|
2013-04-25 00:47:17 +00:00
|
|
|
/* Static Properties */
|
|
|
|
|
2013-05-28 11:31:41 +00:00
|
|
|
ve.ce.MWBlockImageNode.static.name = 'mwBlockImage';
|
2013-04-25 00:47:17 +00:00
|
|
|
|
2014-08-16 17:44:49 +00:00
|
|
|
ve.ce.MWBlockImageNode.static.tagName = 'figure';
|
2013-05-14 23:39:13 +00:00
|
|
|
|
|
|
|
ve.ce.MWBlockImageNode.static.renderHtmlAttributes = false;
|
|
|
|
|
2021-11-27 00:38:54 +00:00
|
|
|
ve.ce.MWBlockImageNode.static.autoFocus = false;
|
2013-06-07 00:51:00 +00:00
|
|
|
|
2013-06-19 22:58:13 +00:00
|
|
|
ve.ce.MWBlockImageNode.static.cssClasses = {
|
2019-01-08 17:00:09 +00:00
|
|
|
default: {
|
2014-08-22 20:50:48 +00:00
|
|
|
left: 'mw-halign-left',
|
|
|
|
right: 'mw-halign-right',
|
|
|
|
center: 'mw-halign-center',
|
|
|
|
none: 'mw-halign-none'
|
2013-06-19 22:58:13 +00:00
|
|
|
},
|
2014-08-22 20:50:48 +00:00
|
|
|
none: {
|
|
|
|
left: 'mw-halign-left',
|
|
|
|
right: 'mw-halign-right',
|
|
|
|
center: 'mw-halign-center',
|
|
|
|
none: 'mw-halign-none'
|
2013-06-19 22:58:13 +00:00
|
|
|
}
|
2013-05-14 23:39:13 +00:00
|
|
|
};
|
ve.Element refactor
Objectives:
* Move ve.ui.Element to ve.Element
* Make CE nodes inherit from ve.Element
Changes:
ve.ui.Element.js, ve.Element.js
* Move and rename
* Move ve.ui.get$$ to ve.Element.static.get$$
* Add static getDocument and getWindow methods
* Add instance getElementDocument and getElementWindow methods
* Add getTagName method, which by default reads the static tagName property, but when overridden can return a tag name based on other factors
*.php
* Updated file link
ve.ce.*Annotation.js, ve.ce.*Node.js, ve.ce.View.js, ve.ce.Document
* Added config options pass through
* Replaced passing elements through constructor with defining static tag names
* Added getTagName overrides where needed that derive tag name from model
* Refactore dom wrapper methods, now consistently using getTagName
ve.ce.Surface.js
* Removed static initialization (not needed)
ve.dm.Model.js, ve.ui.Window.js
* Added missing docs
ve.ui.GroupElement.js, ve.ui.Layout.js, ve.ui.Widget.js,
* Updated class name for elements
ve.ui.Frame.js, ve.ui.LookupInputWidget.js
* Updated location of get$$
ve.ui.js
* Move get$$ to ve.Element
ve.js
* Add auto-init of static properties to mixinClass
Change-Id: I39ae14966456903728e4d9e53f806ddce9ca2b70
2013-05-13 20:52:59 +00:00
|
|
|
|
2013-04-25 00:47:17 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2013-11-01 06:13:09 +00:00
|
|
|
/**
|
2013-12-18 21:22:41 +00:00
|
|
|
* Update CSS classes based on alignment and type
|
2013-11-01 06:13:09 +00:00
|
|
|
*
|
2013-12-18 21:22:41 +00:00
|
|
|
* @param {string} [oldAlign] The old alignment, for removing classes
|
2013-11-01 06:13:09 +00:00
|
|
|
*/
|
2013-12-18 21:22:41 +00:00
|
|
|
ve.ce.MWBlockImageNode.prototype.updateClasses = function ( oldAlign ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const align = this.model.getAttribute( 'align' );
|
2013-12-18 21:22:41 +00:00
|
|
|
|
|
|
|
if ( oldAlign && oldAlign !== align ) {
|
|
|
|
// Remove previous alignment
|
2020-04-09 13:33:54 +00:00
|
|
|
// See static.cssClasses
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
2014-08-16 17:44:49 +00:00
|
|
|
this.$element
|
2013-12-18 21:22:41 +00:00
|
|
|
.removeClass( this.getCssClass( 'none', oldAlign ) )
|
|
|
|
.removeClass( this.getCssClass( 'default', oldAlign ) );
|
2013-11-01 06:13:09 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const type = this.model.getAttribute( 'type' );
|
|
|
|
const framed = type !== 'none' && type !== 'frameless';
|
|
|
|
const alignClass = this.getCssClass( framed ? 'default' : 'none', align );
|
2023-07-07 08:24:13 +00:00
|
|
|
this.$image.toggleClass( 've-ce-mwBlockImageNode-thumbimage', framed );
|
2020-04-09 13:33:54 +00:00
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
2014-08-16 17:44:49 +00:00
|
|
|
this.$element.addClass( alignClass );
|
2014-01-15 22:26:15 +00:00
|
|
|
|
2014-02-27 17:39:19 +00:00
|
|
|
// Border
|
2014-08-16 17:44:49 +00:00
|
|
|
this.$element.toggleClass( 'mw-image-border', !!this.model.getAttribute( 'borderImage' ) );
|
2014-02-27 17:39:19 +00:00
|
|
|
|
2021-09-03 07:15:13 +00:00
|
|
|
this.showHandles( {
|
|
|
|
'mw-halign-right': [ 'sw' ],
|
|
|
|
'mw-halign-left': [ 'se' ],
|
|
|
|
'mw-halign-center': [ 'sw', 'se' ]
|
|
|
|
// Defaults to undefined
|
|
|
|
}[ alignClass ] );
|
2013-11-01 06:13:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-12-18 21:22:41 +00:00
|
|
|
* Redraw the image and its wrappers at the specified dimensions
|
2013-11-01 06:13:09 +00:00
|
|
|
*
|
2013-12-19 11:38:33 +00:00
|
|
|
* The current dimensions from the model are used if none are specified.
|
|
|
|
*
|
|
|
|
* @param {Object} [dimensions] Dimension object containing width & height
|
2013-11-01 06:13:09 +00:00
|
|
|
*/
|
2013-12-18 21:22:41 +00:00
|
|
|
ve.ce.MWBlockImageNode.prototype.updateSize = function ( dimensions ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const isError = this.model.getAttribute( 'isError' );
|
2018-02-28 23:11:07 +00:00
|
|
|
|
2019-02-15 18:28:03 +00:00
|
|
|
if ( isError ) {
|
|
|
|
this.$element.css( { width: '', height: '' } );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-18 21:22:41 +00:00
|
|
|
if ( !dimensions ) {
|
|
|
|
dimensions = {
|
2014-08-22 20:50:48 +00:00
|
|
|
width: this.model.getAttribute( 'width' ),
|
|
|
|
height: this.model.getAttribute( 'height' )
|
2013-12-18 21:22:41 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-01-26 20:09:40 +00:00
|
|
|
this.$image.css( dimensions );
|
2014-05-28 01:44:58 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const type = this.model.getAttribute( 'type' );
|
|
|
|
const borderImage = this.model.getAttribute( 'borderImage' );
|
|
|
|
const hasBorderOrFrame = ( type !== 'none' && type !== 'frameless' ) || borderImage;
|
2021-10-13 12:57:45 +00:00
|
|
|
|
2014-08-16 17:44:49 +00:00
|
|
|
// Make sure $element is sharing the dimensions, otherwise 'middle' and 'none'
|
2014-05-28 01:44:58 +00:00
|
|
|
// positions don't work properly
|
2014-08-16 17:44:49 +00:00
|
|
|
this.$element.css( {
|
2018-02-28 23:11:07 +00:00
|
|
|
width: dimensions.width + ( hasBorderOrFrame ? 2 : 0 ),
|
|
|
|
height: hasBorderOrFrame ? 'auto' : dimensions.height
|
2014-05-28 01:44:58 +00:00
|
|
|
} );
|
2014-08-16 17:44:49 +00:00
|
|
|
this.$element.toggleClass( 'mw-default-size', !!this.model.getAttribute( 'defaultSize' ) );
|
2013-11-01 06:13:09 +00:00
|
|
|
};
|
2013-12-18 21:22:41 +00:00
|
|
|
|
2013-07-24 22:31:46 +00:00
|
|
|
/**
|
|
|
|
* Get the right CSS class to use for alignment
|
2013-11-01 06:13:09 +00:00
|
|
|
*
|
2013-07-24 22:31:46 +00:00
|
|
|
* @param {string} type 'none' or 'default'
|
|
|
|
* @param {string} alignment 'left', 'right', 'center', 'none' or 'default'
|
2016-10-28 19:02:36 +00:00
|
|
|
* @return {string} CSS class
|
2013-07-24 22:31:46 +00:00
|
|
|
*/
|
|
|
|
ve.ce.MWBlockImageNode.prototype.getCssClass = function ( type, alignment ) {
|
2018-08-09 14:10:53 +00:00
|
|
|
// TODO use this.model.getAttribute( 'type' ) etc., see T54065
|
2013-07-24 22:31:46 +00:00
|
|
|
// Default is different between RTL and LTR wikis:
|
|
|
|
if ( type === 'default' && alignment === 'default' ) {
|
2014-07-21 16:44:09 +00:00
|
|
|
if ( this.getModel().getDocument().getDir() === 'rtl' ) {
|
2013-12-12 02:32:35 +00:00
|
|
|
return 'mw-halign-left';
|
2013-07-24 22:31:46 +00:00
|
|
|
} else {
|
2013-12-12 02:32:35 +00:00
|
|
|
return 'mw-halign-right';
|
2013-07-24 22:31:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.constructor.static.cssClasses[ type ][ alignment ];
|
2013-07-24 22:31:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-23 23:13:28 +00:00
|
|
|
/**
|
|
|
|
* Override the default onSetup to add direction-dependent
|
|
|
|
* classes to the image thumbnail.
|
|
|
|
*/
|
2013-10-17 22:09:48 +00:00
|
|
|
ve.ce.MWBlockImageNode.prototype.onSetup = function () {
|
2013-12-18 21:22:41 +00:00
|
|
|
// Parent method
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ce.MWBlockImageNode.super.prototype.onSetup.call( this );
|
2013-07-24 21:02:08 +00:00
|
|
|
|
2013-12-18 21:22:41 +00:00
|
|
|
this.updateClasses();
|
2013-07-23 23:13:28 +00:00
|
|
|
};
|
|
|
|
|
2013-10-11 12:33:53 +00:00
|
|
|
/**
|
2014-04-10 00:26:48 +00:00
|
|
|
* @inheritdoc
|
2013-10-11 12:33:53 +00:00
|
|
|
*/
|
2013-05-14 23:39:13 +00:00
|
|
|
ve.ce.MWBlockImageNode.prototype.onAttributeChange = function ( key, from, to ) {
|
2016-11-06 15:08:23 +00:00
|
|
|
// Mixin method
|
|
|
|
ve.ce.MWImageNode.prototype.onAttributeChange.apply( this, arguments );
|
|
|
|
|
2013-07-01 20:23:51 +00:00
|
|
|
if ( key === 'height' || key === 'width' ) {
|
|
|
|
to = parseInt( to, 10 );
|
|
|
|
}
|
|
|
|
|
2013-06-07 00:51:00 +00:00
|
|
|
if ( from !== to ) {
|
2013-06-19 02:31:01 +00:00
|
|
|
switch ( key ) {
|
|
|
|
case 'align':
|
2013-12-18 21:22:41 +00:00
|
|
|
this.updateClasses( from );
|
2013-06-19 02:31:01 +00:00
|
|
|
break;
|
|
|
|
case 'src':
|
2013-10-21 15:12:54 +00:00
|
|
|
this.$image.attr( 'src', this.getResolvedAttribute( 'src' ) );
|
2013-06-19 02:31:01 +00:00
|
|
|
break;
|
|
|
|
case 'width':
|
2013-12-21 22:37:54 +00:00
|
|
|
this.updateSize( {
|
2014-08-22 20:50:48 +00:00
|
|
|
width: to,
|
|
|
|
height: this.model.getAttribute( 'height' )
|
2013-12-21 22:37:54 +00:00
|
|
|
} );
|
|
|
|
break;
|
2013-06-30 04:53:50 +00:00
|
|
|
case 'height':
|
2013-12-21 22:37:54 +00:00
|
|
|
this.updateSize( {
|
2014-08-22 20:50:48 +00:00
|
|
|
width: this.model.getAttribute( 'width' ),
|
|
|
|
height: to
|
2013-12-21 22:37:54 +00:00
|
|
|
} );
|
2013-11-01 06:13:09 +00:00
|
|
|
break;
|
|
|
|
case 'type':
|
2020-04-09 13:33:54 +00:00
|
|
|
// See constructor for types used
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
2014-08-16 17:44:49 +00:00
|
|
|
this.$element
|
2013-12-18 21:22:41 +00:00
|
|
|
.removeClass( 've-ce-mwBlockImageNode-type-' + from )
|
|
|
|
.addClass( 've-ce-mwBlockImageNode-type-' + to )
|
2017-04-27 18:08:35 +00:00
|
|
|
.attr( 'typeof', this.model.getRdfa() );
|
2013-12-18 21:22:41 +00:00
|
|
|
|
|
|
|
this.updateClasses();
|
2018-02-28 23:11:07 +00:00
|
|
|
this.updateSize();
|
|
|
|
break;
|
|
|
|
case 'borderImage':
|
|
|
|
this.updateClasses();
|
|
|
|
this.updateSize();
|
2013-11-01 06:13:09 +00:00
|
|
|
break;
|
2013-12-18 21:22:41 +00:00
|
|
|
// Other image attributes if they exist
|
2013-11-01 06:13:09 +00:00
|
|
|
case 'alt':
|
2013-12-28 10:29:35 +00:00
|
|
|
if ( !to ) {
|
|
|
|
this.$image.removeAttr( key );
|
|
|
|
} else {
|
|
|
|
this.$image.attr( key, to );
|
|
|
|
}
|
2013-06-19 02:31:01 +00:00
|
|
|
break;
|
2019-04-28 20:56:13 +00:00
|
|
|
case 'mediaType':
|
|
|
|
this.updateMediaType();
|
|
|
|
break;
|
2014-02-20 05:11:04 +00:00
|
|
|
case 'defaultSize':
|
2014-08-16 17:44:49 +00:00
|
|
|
this.$element.toggleClass( 'mw-default-size', to );
|
2014-02-20 05:11:04 +00:00
|
|
|
break;
|
2013-06-07 00:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-28 19:02:36 +00:00
|
|
|
/**
|
|
|
|
* @param {Object} dimensions New dimensions
|
|
|
|
*/
|
2013-10-14 11:44:26 +00:00
|
|
|
ve.ce.MWBlockImageNode.prototype.onResizableResizing = function ( dimensions ) {
|
|
|
|
if ( !this.outline ) {
|
|
|
|
ve.ce.ResizableNode.prototype.onResizableResizing.call( this, dimensions );
|
2013-11-01 06:13:09 +00:00
|
|
|
|
2013-12-18 21:22:41 +00:00
|
|
|
this.updateSize( dimensions );
|
2013-10-14 11:44:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-28 23:43:18 +00:00
|
|
|
ve.ce.MWBlockImageNode.prototype.getDomPosition = function () {
|
|
|
|
// We need to override this because this.$element can have children other than renderings of child
|
|
|
|
// CE nodes (specifically, the image itself, this.$a), which throws the calculations out of whack.
|
|
|
|
// Luckily, MWBlockImageNode is very simple and can contain at most one other node: its caption,
|
|
|
|
// which is always inserted at the end.
|
2024-05-21 14:22:56 +00:00
|
|
|
const domNode = this.$element.last()[ 0 ];
|
2018-02-28 23:43:18 +00:00
|
|
|
return {
|
|
|
|
node: domNode,
|
|
|
|
offset: domNode.childNodes.length
|
|
|
|
};
|
2013-04-25 00:47:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ce.nodeFactory.register( ve.ce.MWBlockImageNode );
|