|
| 1 | +/* |
| 2 | +Copyright 2015 OpenMarket Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +var React = require('react'); |
| 20 | + |
| 21 | +var MImageTileController = require("../../../../src/controllers/molecules/MImageTile"); |
| 22 | + |
| 23 | +var MatrixClientPeg = require('../../../../src/MatrixClientPeg'); |
| 24 | + |
| 25 | +module.exports = React.createClass({ |
| 26 | + displayName: 'MImageTile', |
| 27 | + mixins: [MImageTileController], |
| 28 | + |
| 29 | + thumbHeight: function(fullWidth, fullHeight, thumbWidth, thumbHeight) { |
| 30 | + if (!fullWidth || !fullHeight) { |
| 31 | + // Cannot calculate thumbnail height for image: missing w/h in metadata. We can't even |
| 32 | + // log this because it's spammy |
| 33 | + return undefined; |
| 34 | + } |
| 35 | + if (fullWidth < thumbWidth && fullHeight < thumbHeight) { |
| 36 | + // no scaling needs to be applied |
| 37 | + return fullHeight; |
| 38 | + } |
| 39 | + var widthMulti = thumbWidth / fullWidth; |
| 40 | + var heightMulti = thumbHeight / fullHeight; |
| 41 | + if (widthMulti < heightMulti) { |
| 42 | + // width is the dominant dimension so scaling will be fixed on that |
| 43 | + return Math.floor(widthMulti * fullHeight); |
| 44 | + } |
| 45 | + else { |
| 46 | + // height is the dominant dimension so scaling will be fixed on that |
| 47 | + return Math.floor(heightMulti * fullHeight); |
| 48 | + } |
| 49 | + }, |
| 50 | + |
| 51 | + render: function() { |
| 52 | + var content = this.props.mxEvent.getContent(); |
| 53 | + var cli = MatrixClientPeg.get(); |
| 54 | + |
| 55 | + var thumbHeight = null; |
| 56 | + if (content.info) thumbHeight = this.thumbHeight(content.info.w, content.info.h, 320, 240); |
| 57 | + |
| 58 | + var imgStyle = {}; |
| 59 | + if (thumbHeight) imgStyle['height'] = thumbHeight; |
| 60 | + |
| 61 | + return ( |
| 62 | + <span className="mx_MImageTile"> |
| 63 | + <a href={cli.mxcUrlToHttp(content.url)} target="_blank"> |
| 64 | + <img src={cli.mxcUrlToHttp(content.url, 320, 240)} alt={content.body} style={imgStyle} /> |
| 65 | + </a> |
| 66 | + </span> |
| 67 | + ); |
| 68 | + }, |
| 69 | +}); |
0 commit comments