Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable MWImageNode 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 * @abstract
13 * @extends ve.ce.GeneratedContentNode
14 * @mixes ve.ce.FocusableNode
15 * @mixes ve.ce.MWResizableNode
16 *
17 * @constructor
18 * @param {jQuery} $focusable Focusable part of the node
19 * @param {jQuery} $image Image part of the node
20 * @param {Object} [config] Configuration options
21 */
22 ve.ce.MWImageNode = function VeCeMWImageNode( $focusable, $image, config ) {
23 config = ve.extendObject( {
24 enforceMax: false,
25 minDimensions: { width: 1, height: 1 },
26 $bounding: this.$element
27 }, config );
28
29 // Properties
30 this.$image = $image;
31 // Parent constructor triggers render so this must precede it
32 this.renderedDimensions = null;
33
34 // Parent constructor
35 ve.ce.GeneratedContentNode.call( this );
36
37 // Mixin constructors
38 ve.ce.FocusableNode.call( this, $focusable, config );
39 ve.ce.MWResizableNode.call( this, this.$image, config );
40
41 // Events
42 this.model.connect( this, { attributeChange: 'onAttributeChange' } );
43
44 // Initialization
45 this.updateMediaType();
46 };
47
48 /* Inheritance */
49
50 OO.inheritClass( ve.ce.MWImageNode, ve.ce.GeneratedContentNode );
51
52 OO.mixinClass( ve.ce.MWImageNode, ve.ce.FocusableNode );
53
54 // Need to mixin base class as well (T92540)
55 OO.mixinClass( ve.ce.MWImageNode, ve.ce.ResizableNode );
56
57 OO.mixinClass( ve.ce.MWImageNode, ve.ce.MWResizableNode );
58
59 /* Static Properties */
60
61 ve.ce.MWImageNode.static.primaryCommandName = 'media';
62
63 /* Static Methods */
64
65 /**
66 * @inheritdoc ve.ce.Node
67 */
68 ve.ce.MWImageNode.static.getDescription = function ( model ) {
69 const title = new mw.Title( model.getFilename() );
70 return title.getMainText();
71 };
72
73 /* Methods */
74
75 /**
76 * Update the rendering of the 'align', src', 'width' and 'height' attributes
77 * when they change in the model.
78 *
79 * @param {string} key Attribute key
80 * @param {string} from Old value
81 * @param {string} to New value
82 */
83 ve.ce.MWImageNode.prototype.onAttributeChange = function () {
84 this.update();
85 };
86
87 /**
88 * @inheritdoc ve.ce.GeneratedContentNode
89 */
90 ve.ce.MWImageNode.prototype.onGeneratedContentNodeUpdate = function () {
91 // Do nothing to avoid re-rendering every time the caption is changed.
92 // Call update inside onAttributeChange instead.
93 };
94
95 /**
96 * @inheritdoc ve.ce.GeneratedContentNode
97 */
98 ve.ce.MWImageNode.prototype.generateContents = function () {
99 const model = this.getModel(),
100 height = model.getAttribute( 'height' ),
101 mwData = model.getAttribute( 'mw' ) || {},
102 deferred = ve.createDeferred();
103 let width = model.getAttribute( 'width' );
104
105 // If the current rendering is larger don't fetch a new image, just let the browser resize
106 if ( this.renderedDimensions && this.renderedDimensions.width > width ) {
107 return deferred.reject().promise();
108 }
109
110 let params;
111 if ( mwData.thumbtime !== undefined ) {
112 params = 'seek=' + mwData.thumbtime;
113 } else if ( mwData.page !== undefined ) {
114 params = 'page' + mwData.page + '-' + width + 'px';
115 // Don't send width twice
116 width = undefined;
117 }
118
119 const xhr = ve.init.target.getContentApi( this.getModel().getDocument() ).get( {
120 action: 'query',
121 prop: 'imageinfo',
122 iiprop: 'url',
123 iiurlwidth: width,
124 iiurlheight: height,
125 iiurlparam: params,
126 titles: this.getModel().getFilename()
127 } )
128 .done( this.onParseSuccess.bind( this, deferred ) )
129 .fail( this.onParseError.bind( this, deferred ) );
130
131 return deferred.promise( { abort: xhr.abort } );
132 };
133
134 /**
135 * Handle a successful response from the parser for the image src.
136 *
137 * @param {jQuery.Deferred} deferred The Deferred object created by generateContents
138 * @param {Object} response Response data
139 */
140 ve.ce.MWImageNode.prototype.onParseSuccess = function ( deferred, response ) {
141 const thumburl = ve.getProp( response.query.pages[ 0 ], 'imageinfo', 0, 'thumburl' );
142 if ( thumburl ) {
143 deferred.resolve( thumburl );
144 } else {
145 deferred.reject();
146 }
147 };
148
149 /**
150 * @inheritdoc ve.ce.GeneratedContentNode
151 */
152 ve.ce.MWImageNode.prototype.render = function ( generatedContents ) {
153 this.$image.attr( 'src', generatedContents );
154 // As we only re-render when the image is larger than last rendered size
155 // this will always be the largest ever rendering
156 this.renderedDimensions = ve.copy( this.model.getScalable().getCurrentDimensions() );
157 if ( this.live ) {
158 this.afterRender();
159 }
160 };
161
162 /**
163 * Handle an unsuccessful response from the parser for the image src.
164 *
165 * @param {jQuery.Deferred} deferred The promise object created by generateContents
166 * @param {Object} response Response data
167 */
168 ve.ce.MWImageNode.prototype.onParseError = function ( deferred ) {
169 deferred.reject();
170 };
171
172 /**
173 * Update rendering when media type changes
174 */
175 ve.ce.MWImageNode.prototype.updateMediaType = function () {
176 this.$image.toggleClass( 've-ce-mwImageNode-audioPlayer', this.model.getMediaType() === 'AUDIO' );
177 };