2 * MediaWiki Widgets - StashedFileWidget class.
4 * @copyright 2011-2016 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
7 ( function ( $, mw
, OO
) {
10 * Accepts a stashed file and displays the information for purposes of
11 * publishing the file at the behest of the user.
14 * var widget = new mw.widgets.StashedFileWidget( {
15 * filekey: '12r9e4rugeec.ddtmmp.1.jpg',
18 * widget.getValue(); // '12r9e4rugeec.ddtmmp.1.jpg'
19 * widget.setValue( '12r9epfbnskk.knfiy7.1.jpg' );
20 * widget.getValue(); // '12r9epfbnskk.knfiy7.1.jpg'
22 * Note that this widget will not finish an upload for you. Use mw.Upload
23 * and mw.Upload#setFilekey, then mw.Upload#finishStashUpload to accomplish
26 * @class mw.widgets.StashedFileWidget
27 * @extends OO.ui.Widget
32 * @param {Object} config Configuration options
33 * @cfg {string} filekey The filekey of the stashed file.
34 * @cfg {Object} [api] API to use for thumbnails.
36 mw
.widgets
.StashedFileWidget
= function MWWStashedFileWidget( config
) {
38 config
.api
= new mw
.Api();
42 mw
.widgets
.StashedFileWidget
.parent
.call( this, config
);
45 OO
.ui
.mixin
.IconElement
.call( this, config
);
46 OO
.ui
.mixin
.LabelElement
.call( this, config
);
47 OO
.ui
.mixin
.PendingElement
.call( this, config
);
50 this.api
= config
.api
;
51 this.$info
= $( '<span>' );
52 this.setValue( config
.filekey
);
53 this.$label
.addClass( 'mw-widgets-stashedFileWidget-label' );
55 .addClass( 'mw-widgets-stashedFileWidget-info' )
56 .append( this.$icon
, this.$label
);
58 this.$thumbnail
= $( '<div>' ).addClass( 'mw-widgets-stashedFileWidget-thumbnail' );
59 this.setPendingElement( this.$thumbnail
);
61 this.$thumbContain
= $( '<div>' )
62 .addClass( 'mw-widgets-stashedFileWidget-thumbnail-container' )
63 .append( this.$thumbnail
, this.$info
);
66 .addClass( 'mw-widgets-stashedFileWidget' )
67 .append( this.$thumbContain
);
72 OO
.inheritClass( mw
.widgets
.StashedFileWidget
, OO
.ui
.Widget
);
73 OO
.mixinClass( mw
.widgets
.StashedFileWidget
, OO
.ui
.mixin
.IconElement
);
74 OO
.mixinClass( mw
.widgets
.StashedFileWidget
, OO
.ui
.mixin
.LabelElement
);
75 OO
.mixinClass( mw
.widgets
.StashedFileWidget
, OO
.ui
.mixin
.PendingElement
);
78 * Get the current filekey.
80 * @return {string|null}
82 mw
.widgets
.StashedFileWidget
.prototype.getValue = function () {
89 * @param {string|null} filekey
91 mw
.widgets
.StashedFileWidget
.prototype.setValue = function ( filekey
) {
92 if ( filekey
!== this.filekey
) {
93 this.filekey
= filekey
;
95 this.emit( 'change', this.filekey
);
99 mw
.widgets
.StashedFileWidget
.prototype.updateUI = function () {
100 var $label
, $filetype
;
102 if ( this.filekey
) {
103 this.$element
.removeClass( 'mw-widgets-stashedFileWidget-empty' );
105 $filetype
= $( '<span>' )
106 .addClass( 'mw-widgets-stashedFileWidget-fileType' );
110 .addClass( 'mw-widgets-stashedFileWidget-filekey' )
111 .text( this.filekey
)
114 this.setLabel( $label
);
117 this.loadAndGetImageUrl().done( function ( url
, mime
) {
118 this.$thumbnail
.css( 'background-image', 'url( ' + url
+ ' )' );
120 $filetype
.text( mime
);
121 this.setLabel( $label
);
123 }.bind( this ) ).fail( function () {
124 this.$thumbnail
.append(
125 new OO
.ui
.IconWidget( {
127 classes
: [ 'mw-widgets-stashedFileWidget-noThumbnail-icon' ]
130 }.bind( this ) ).always( function () {
134 this.$element
.addClass( 'mw-widgets-stashedFileWidget-empty' );
139 mw
.widgets
.StashedFileWidget
.prototype.loadAndGetImageUrl = function () {
140 var filekey
= this.filekey
;
143 return this.api
.get( {
145 prop
: 'stashimageinfo',
147 siiprop
: [ 'size', 'url', 'mime' ],
149 } ).then( function ( data
) {
150 var sii
= data
.query
.stashimageinfo
[ 0 ];
152 return $.Deferred().resolve( sii
.thumburl
, sii
.mime
);
156 return $.Deferred().reject( 'No filekey' );
158 }( jQuery
, mediaWiki
, OO
) );