Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ce/nodes/ve.ce.MWInlineImageNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable MWInlineImageNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * ContentEditable MediaWiki image node.
10 *
11 * @class
12 * @extends ve.ce.LeafNode
13 * @mixes ve.ce.MWImageNode
14 *
15 * @constructor
16 * @param {ve.dm.MWInlineImageNode} model Model to observe
17 * @param {Object} [config] Configuration options
18 */
19 ve.ce.MWInlineImageNode = function VeCeMWInlineImageNode( model, config ) {
20 let $image;
21 let hasHref = false;
22
23 if ( model.getAttribute( 'isError' ) ) {
24 this.$element = $( '<a>' )
25 .addClass( 'new' )
26 .append(
27 $( '<span>' )
28 .addClass( 'mw-file-element mw-broken-media' )
29 .text( model.getAttribute( 'errorText' ) )
30 );
31 $image = $( [] );
32 } else {
33 $image = $( '<img>' ).addClass( 'mw-file-element' );
34 if ( model.getAttribute( 'href' ) ) {
35 hasHref = true;
36 this.$element = $( '<a>' ).addClass( 'mw-file-description' );
37 $image.appendTo( this.$element );
38 } else {
39 this.$element = $image;
40 }
41 }
42
43 // Parent constructor
44 // this.$element has already been created and styled, so pass through as config.$element
45 // The constructor will add more classes to this.$element, such as ve-ce-leafNode
46 ve.ce.MWInlineImageNode.super.call( this, model, ve.extendObject( {}, config, { $element: this.$element } ) );
47
48 // Mixin constructors
49 ve.ce.MWImageNode.call( this, this.$element, $image );
50
51 $image
52 .attr( 'src', this.getResolvedAttribute( 'src' ) )
53 .attr( 'width', this.model.getAttribute( 'width' ) )
54 .attr( 'height', this.model.getAttribute( 'height' ) );
55
56 if ( hasHref ) {
57 // T322704
58 ve.setAttributeSafe( this.$element[ 0 ], 'href', this.getResolvedAttribute( 'href' ) || '', '#' );
59 }
60
61 this.showHandles( [ this.$element.css( 'direction' ) === 'rtl' ? 'sw' : 'se' ] );
62
63 this.updateClasses();
64
65 // DOM changes
66 this.$element.addClass( 've-ce-mwInlineImageNode' );
67 };
68
69 /* Inheritance */
70
71 OO.inheritClass( ve.ce.MWInlineImageNode, ve.ce.LeafNode );
72
73 // Need to mixin base class as well (T92540)
74 OO.mixinClass( ve.ce.MWInlineImageNode, ve.ce.GeneratedContentNode );
75
76 OO.mixinClass( ve.ce.MWInlineImageNode, ve.ce.MWImageNode );
77
78 /* Static Properties */
79
80 ve.ce.MWInlineImageNode.static.name = 'mwInlineImage';
81
82 /* Methods */
83
84 /**
85 * Update CSS classes based on current attributes
86 */
87 ve.ce.MWInlineImageNode.prototype.updateClasses = function () {
88 const valign = this.model.getAttribute( 'valign' );
89
90 // Border
91 this.$element.toggleClass( 'mw-image-border', !!this.model.getAttribute( 'borderImage' ) );
92
93 // default size
94 this.$element.toggleClass( 'mw-default-size', !!this.model.getAttribute( 'defaultSize' ) );
95
96 // valign
97 if ( valign !== 'default' ) {
98 this.$image.css( 'vertical-align', valign );
99 }
100 };
101
102 /**
103 * @inheritdoc
104 */
105 ve.ce.MWInlineImageNode.prototype.onAttributeChange = function ( key, from, to ) {
106 // Mixin method
107 ve.ce.MWImageNode.prototype.onAttributeChange.apply( this, arguments );
108
109 if ( key === 'height' || key === 'width' ) {
110 to = parseInt( to, 10 );
111 }
112
113 if ( from !== to ) {
114 switch ( key ) {
115 // TODO: 'align', 'src', 'valign', 'border'
116 case 'width':
117 this.$image.css( 'width', to );
118 break;
119 case 'height':
120 this.$image.css( 'height', to );
121 break;
122 case 'mediaType':
123 this.updateMediaType();
124 break;
125 }
126 this.updateClasses();
127 }
128 };
129
130 /* Registration */
131
132 ve.ce.nodeFactory.register( ve.ce.MWInlineImageNode );