| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
( function () { |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
mw.widgets.MediaResultWidget = function MwWidgetsMediaResultWidget( config ) { |
| 25 |
|
| 26 |
config = config || {}; |
| 27 |
|
| 28 |
|
| 29 |
mw.widgets.MediaResultWidget.super.call( this, config ); |
| 30 |
|
| 31 |
|
| 32 |
this.setRowHeight( config.rowHeight || 150 ); |
| 33 |
this.maxRowWidth = config.maxRowWidth || 500; |
| 34 |
this.minWidth = config.minWidth || this.maxRowWidth / 5; |
| 35 |
this.maxWidth = config.maxWidth || this.maxRowWidth * 2 / 3; |
| 36 |
|
| 37 |
this.imageDimensions = {}; |
| 38 |
|
| 39 |
this.isAudio = this.data.mediatype === 'AUDIO'; |
| 40 |
|
| 41 |
|
| 42 |
this.thumbUrl = this.data.thumburl; |
| 43 |
this.src = null; |
| 44 |
this.row = null; |
| 45 |
|
| 46 |
this.$thumb = $( '<img>' ) |
| 47 |
.addClass( 'mw-widget-mediaResultWidget-thumbnail' ) |
| 48 |
.attr( 'alt', '' ) |
| 49 |
.on( { |
| 50 |
load: this.onThumbnailLoad.bind( this ), |
| 51 |
error: this.onThumbnailError.bind( this ) |
| 52 |
} ); |
| 53 |
this.$overlay = $( '<div>' ) |
| 54 |
.addClass( 'mw-widget-mediaResultWidget-overlay' ); |
| 55 |
|
| 56 |
this.calculateSizing( this.data ); |
| 57 |
|
| 58 |
|
| 59 |
this.setLabel( new mw.Title( this.data.title ).getNameText() ); |
| 60 |
this.$label.addClass( 'mw-widget-mediaResultWidget-nameLabel' ); |
| 61 |
|
| 62 |
this.$element |
| 63 |
.addClass( 'mw-widget-mediaResultWidget ve-ui-texture-pending' ) |
| 64 |
.prepend( this.$thumb, this.$overlay ); |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
OO.inheritClass( mw.widgets.MediaResultWidget, OO.ui.OptionWidget ); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
mw.widgets.MediaResultWidget.static.resizeToBoundingBox = function ( imageDimensions, boundingBox ) { |
| 75 |
let newDimensions = OO.copy( imageDimensions ); |
| 76 |
const scale = Math.min( |
| 77 |
boundingBox.height / imageDimensions.height, |
| 78 |
boundingBox.width / imageDimensions.width |
| 79 |
); |
| 80 |
|
| 81 |
if ( scale < 1 ) { |
| 82 |
|
| 83 |
newDimensions = { |
| 84 |
width: Math.floor( newDimensions.width * scale ), |
| 85 |
height: Math.floor( newDimensions.height * scale ) |
| 86 |
}; |
| 87 |
} |
| 88 |
return newDimensions; |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
mw.widgets.MediaResultWidget.prototype.onThumbnailLoad = function () { |
| 93 |
this.$thumb.first().addClass( 've-ui-texture-transparency' ); |
| 94 |
this.$element |
| 95 |
.addClass( 'mw-widget-mediaResultWidget-done' ) |
| 96 |
.removeClass( 've-ui-texture-pending' ); |
| 97 |
}; |
| 98 |
|
| 99 |
mw.widgets.MediaResultWidget.prototype.onThumbnailError = function () { |
| 100 |
this.$thumb.last() |
| 101 |
.css( 'background-image', '' ) |
| 102 |
.addClass( 've-ui-texture-alert' ); |
| 103 |
this.$element |
| 104 |
.addClass( 'mw-widget-mediaResultWidget-error' ) |
| 105 |
.removeClass( 've-ui-texture-pending' ); |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
mw.widgets.MediaResultWidget.prototype.calculateSizing = function ( originalDimensions, boundingBox ) { |
| 115 |
boundingBox = boundingBox || {}; |
| 116 |
|
| 117 |
let imageDimensions; |
| 118 |
if ( this.isAudio ) { |
| 119 |
|
| 120 |
|
| 121 |
imageDimensions = { |
| 122 |
width: 120, |
| 123 |
height: 120 |
| 124 |
}; |
| 125 |
} else { |
| 126 |
|
| 127 |
imageDimensions = this.constructor.static.resizeToBoundingBox( |
| 128 |
|
| 129 |
{ |
| 130 |
width: originalDimensions.width || originalDimensions.thumbwidth, |
| 131 |
height: originalDimensions.height || originalDimensions.thumbwidth |
| 132 |
}, |
| 133 |
|
| 134 |
{ |
| 135 |
width: boundingBox.width || this.getImageMaxWidth(), |
| 136 |
height: boundingBox.height || this.getRowHeight() |
| 137 |
} |
| 138 |
); |
| 139 |
} |
| 140 |
this.imageDimensions = imageDimensions; |
| 141 |
|
| 142 |
this.$thumb.css( this.imageDimensions ); |
| 143 |
|
| 144 |
|
| 145 |
const wrapperPadding = this.calculateWrapperPadding( this.imageDimensions ); |
| 146 |
this.$element.css( wrapperPadding ); |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
mw.widgets.MediaResultWidget.prototype.lazyLoad = function () { |
| 154 |
if ( !this.hasSrc() ) { |
| 155 |
this.src = this.thumbUrl; |
| 156 |
this.$thumb.attr( 'src', this.thumbUrl ); |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
mw.widgets.MediaResultWidget.prototype.getDimensions = function () { |
| 166 |
return this.dimensions; |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
mw.widgets.MediaResultWidget.prototype.resizeThumb = function ( resizeFactor ) { |
| 175 |
const imageOriginalWidth = this.imageDimensions.width, |
| 176 |
wrapperWidth = this.$element.width(); |
| 177 |
|
| 178 |
this.setRowHeight( Math.ceil( this.getRowHeight() * resizeFactor ) ); |
| 179 |
|
| 180 |
const boundingBox = { |
| 181 |
width: Math.ceil( this.imageDimensions.width * resizeFactor ), |
| 182 |
height: this.getRowHeight() |
| 183 |
}; |
| 184 |
|
| 185 |
this.calculateSizing( this.data, boundingBox ); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
if ( imageOriginalWidth < wrapperWidth ) { |
| 190 |
boundingBox.width = wrapperWidth * resizeFactor; |
| 191 |
} |
| 192 |
this.$element.css( this.calculateWrapperPadding( boundingBox ) ); |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
mw.widgets.MediaResultWidget.prototype.calculateWrapperPadding = function ( thumbDimensions ) { |
| 202 |
const css = { |
| 203 |
height: this.rowHeight, |
| 204 |
width: thumbDimensions.width, |
| 205 |
lineHeight: this.getRowHeight() + 'px' |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
if ( thumbDimensions.width < this.minWidth ) { |
| 210 |
css.width = this.minWidth; |
| 211 |
} |
| 212 |
|
| 213 |
return css; |
| 214 |
}; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
mw.widgets.MediaResultWidget.prototype.getRowHeight = function () { |
| 222 |
return this.rowHeight; |
| 223 |
}; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
mw.widgets.MediaResultWidget.prototype.setRowHeight = function ( rowHeight ) { |
| 231 |
this.rowHeight = rowHeight; |
| 232 |
}; |
| 233 |
|
| 234 |
mw.widgets.MediaResultWidget.prototype.setImageMaxWidth = function ( width ) { |
| 235 |
this.maxWidth = width; |
| 236 |
}; |
| 237 |
mw.widgets.MediaResultWidget.prototype.getImageMaxWidth = function () { |
| 238 |
return this.maxWidth; |
| 239 |
}; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
mw.widgets.MediaResultWidget.prototype.setRow = function ( row ) { |
| 247 |
this.row = row; |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
mw.widgets.MediaResultWidget.prototype.getRow = function () { |
| 256 |
return this.row; |
| 257 |
}; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
mw.widgets.MediaResultWidget.prototype.hasSrc = function () { |
| 265 |
return !!this.src; |
| 266 |
}; |
| 267 |
}() ); |