Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/nodes/ve.ce.ImageNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable ImageNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * ContentEditable image node.
9 *
10 * @class
11 * @abstract
12 * @mixes ve.ce.FocusableNode
13 * @mixes ve.ce.ResizableNode
14 *
15 * @constructor
16 * @param {jQuery} $figure Image or figure element
17 * @param {jQuery} [$image] Actual image element, if $figure is just a container
18 * @param {Object} [config] Configuration options
19 */
20 ve.ce.ImageNode = function VeCeImageNode( $figure, $image, config ) {
21 config = ve.extendObject( {
22 enforceMax: false,
23 minDimensions: { width: 1, height: 1 },
24 $bounding: this.$element
25 }, config );
26
27 this.$figure = $figure;
28 this.$image = $image || $figure;
29
30 // Mixin constructors
31 ve.ce.FocusableNode.call( this, this.$figure, config );
32 ve.ce.ResizableNode.call( this, this.$image, config );
33
34 // Events
35 this.$image.on( 'load', this.onLoad.bind( this ) );
36 this.model.connect( this, { attributeChange: 'onAttributeChange' } );
37
38 // Initialization
39 this.$element.addClass( 've-ce-imageNode' );
40 };
41
42 /* Inheritance */
43
44 OO.mixinClass( ve.ce.ImageNode, ve.ce.FocusableNode );
45
46 OO.mixinClass( ve.ce.ImageNode, ve.ce.ResizableNode );
47
48 /* Static Methods */
49
50 // eslint-disable-next-line jsdoc/require-param, jsdoc/require-returns
51 /**
52 * @see ve.ce.Node
53 */
54 ve.ce.ImageNode.static.getDescription = function ( model ) {
55 return model.getAttribute( 'src' );
56 };
57
58 /* Methods */
59
60 /**
61 * Update the rendering of the 'align', src', 'width' and 'height' attributes
62 * when they change in the model.
63 *
64 * @param {string} key Attribute key
65 * @param {string} from Old value
66 * @param {string} to New value
67 */
68 ve.ce.ImageNode.prototype.onAttributeChange = function ( key, from, to ) {
69 switch ( key ) {
70 case 'src':
71 this.$image.prop( 'src', this.getResolvedAttribute( 'src' ) );
72 break;
73
74 case 'width':
75 case 'height':
76 this.$image.css( key, to !== null ? to : '' );
77 break;
78 }
79 };
80
81 /**
82 * Handle the image load
83 *
84 * @param {jQuery.Event} e Load event
85 */
86 ve.ce.ImageNode.prototype.onLoad = function () {
87 if ( !this.model ) {
88 // This node has probably been destroyed. (Currently there's no easy way for
89 // a mixin class to disconnect listeners on destroy)
90 return;
91 }
92 this.setOriginalDimensions( {
93 width: this.$image.prop( 'naturalWidth' ),
94 height: this.$image.prop( 'naturalHeight' )
95 } );
96 };