2 * MediaWiki Widgets - StashedFileWidget class.
4 * @copyright 2011-2016 MediaWiki Widgets Team and others; see mediawiki.widgets/AUTHORS.txt
5 * @license The MIT License (MIT); see mediawiki.widgets/LICENSE.txt
10 * @classdesc Accepts a stashed file and displays the information for purposes of
11 * publishing the file at the behest of the user.
13 * Note that this widget will not finish an upload for you. Use {@link mw.Upload}
14 * and {@link mw.Upload#setFilekey}, then {@link mw.Upload#finishStashUpload} to accomplish
18 * const widget = new mw.widgets.StashedFileWidget( {
19 * filekey: '12r9e4rugeec.ddtmmp.1.jpg',
22 * widget.getValue(); // '12r9e4rugeec.ddtmmp.1.jpg'
23 * widget.setValue( '12r9epfbnskk.knfiy7.1.jpg' );
24 * widget.getValue(); // '12r9epfbnskk.knfiy7.1.jpg'
26 * @class mw.widgets.StashedFileWidget
27 * @extends OO.ui.Widget
29 * @param {Object} config Configuration options
30 * @param {string} [config.filekey] The filekey of the stashed file.
31 * @param {Object} [config.api] API to use for thumbnails.
33 mw
.widgets
.StashedFileWidget
= function MWWStashedFileWidget( config
) {
35 config
.api
= new mw
.Api();
39 mw
.widgets
.StashedFileWidget
.super.call( this, config
);
42 OO
.ui
.mixin
.IconElement
.call( this, config
);
43 OO
.ui
.mixin
.LabelElement
.call( this, config
);
44 OO
.ui
.mixin
.PendingElement
.call( this, config
);
47 this.api
= config
.api
;
48 this.$info
= $( '<span>' );
49 this.setValue( config
.filekey
);
50 this.$label
.addClass( 'mw-widgets-stashedFileWidget-label' );
52 .addClass( 'mw-widgets-stashedFileWidget-info' )
53 .append( this.$icon
, this.$label
);
55 this.$thumbnail
= $( '<div>' ).addClass( 'mw-widgets-stashedFileWidget-thumbnail' );
56 this.setPendingElement( this.$thumbnail
);
58 this.$thumbContain
= $( '<div>' )
59 .addClass( 'mw-widgets-stashedFileWidget-thumbnail-container' )
60 .append( this.$thumbnail
, this.$info
);
63 .addClass( 'mw-widgets-stashedFileWidget' )
64 .append( this.$thumbContain
);
69 OO
.inheritClass( mw
.widgets
.StashedFileWidget
, OO
.ui
.Widget
);
70 OO
.mixinClass( mw
.widgets
.StashedFileWidget
, OO
.ui
.mixin
.IconElement
);
71 OO
.mixinClass( mw
.widgets
.StashedFileWidget
, OO
.ui
.mixin
.LabelElement
);
72 OO
.mixinClass( mw
.widgets
.StashedFileWidget
, OO
.ui
.mixin
.PendingElement
);
75 * Get the current filekey.
77 * @return {string|null}
79 mw
.widgets
.StashedFileWidget
.prototype.getValue = function () {
86 * @param {string|null} filekey
88 mw
.widgets
.StashedFileWidget
.prototype.setValue = function ( filekey
) {
89 if ( filekey
!== this.filekey
) {
90 this.filekey
= filekey
;
92 this.emit( 'change', this.filekey
);
96 mw
.widgets
.StashedFileWidget
.prototype.updateUI = function () {
97 let $label
, $filetype
;
100 this.$element
.removeClass( 'mw-widgets-stashedFileWidget-empty' );
102 $filetype
= $( '<span>' )
103 .addClass( 'mw-widgets-stashedFileWidget-fileType' );
107 .addClass( 'mw-widgets-stashedFileWidget-filekey' )
108 .text( this.filekey
)
111 this.setLabel( $label
);
114 this.loadAndGetImageUrl().done( ( url
, mime
) => {
115 this.$thumbnail
.css( 'background-image', 'url( ' + url
+ ' )' );
117 $filetype
.text( mime
);
118 this.setLabel( $label
);
121 this.$thumbnail
.append(
122 new OO
.ui
.IconWidget( {
124 classes
: [ 'mw-widgets-stashedFileWidget-noThumbnail-icon' ]
131 this.$element
.addClass( 'mw-widgets-stashedFileWidget-empty' );
136 mw
.widgets
.StashedFileWidget
.prototype.loadAndGetImageUrl = function () {
137 const filekey
= this.filekey
;
140 return this.api
.get( {
142 prop
: 'stashimageinfo',
144 siiprop
: [ 'size', 'url', 'mime' ],
146 } ).then( ( data
) => {
147 const sii
= data
.query
.stashimageinfo
[ 0 ];
149 return $.Deferred().resolve( sii
.thumburl
, sii
.mime
);
153 return $.Deferred().reject( 'No filekey' );