2015-11-27 18:02:32 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-27 18:02:32 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var React = require('react');
|
|
|
|
var filesize = require('filesize');
|
|
|
|
|
|
|
|
var MatrixClientPeg = require('../../../MatrixClientPeg');
|
2016-04-02 02:36:19 +03:00
|
|
|
var ImageUtils = require('../../../ImageUtils');
|
2015-11-27 18:02:32 +03:00
|
|
|
var Modal = require('../../../Modal');
|
|
|
|
var sdk = require('../../../index');
|
2016-01-05 06:59:04 +03:00
|
|
|
var dis = require("../../../dispatcher");
|
2015-11-27 18:02:32 +03:00
|
|
|
|
|
|
|
module.exports = React.createClass({
|
2015-11-30 18:19:43 +03:00
|
|
|
displayName: 'MImageBody',
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2016-02-22 20:19:04 +03:00
|
|
|
propTypes: {
|
|
|
|
/* the MatrixEvent to show */
|
|
|
|
mxEvent: React.PropTypes.object.isRequired,
|
|
|
|
|
|
|
|
/* callback called when images in events are loaded */
|
2016-04-02 02:36:19 +03:00
|
|
|
onWidgetLoad: React.PropTypes.func,
|
2015-11-27 18:02:32 +03:00
|
|
|
},
|
|
|
|
|
2015-12-08 22:49:16 +03:00
|
|
|
onClick: function onClick(ev) {
|
2015-11-27 18:02:32 +03:00
|
|
|
if (ev.button == 0 && !ev.metaKey) {
|
|
|
|
ev.preventDefault();
|
|
|
|
var content = this.props.mxEvent.getContent();
|
|
|
|
var httpUrl = MatrixClientPeg.get().mxcUrlToHttp(content.url);
|
|
|
|
var ImageView = sdk.getComponent("elements.ImageView");
|
2015-12-08 22:49:16 +03:00
|
|
|
var params = {
|
2015-11-27 18:02:32 +03:00
|
|
|
src: httpUrl,
|
2015-12-08 22:49:16 +03:00
|
|
|
mxEvent: this.props.mxEvent
|
|
|
|
};
|
|
|
|
|
|
|
|
if (content.info) {
|
|
|
|
params.width = content.info.w;
|
|
|
|
params.height = content.info.h;
|
2016-04-04 02:18:18 +03:00
|
|
|
params.size = content.info.size;
|
2015-12-08 22:49:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Modal.createDialog(ImageView, params, "mx_Dialog_lightbox");
|
2015-11-27 18:02:32 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_isGif: function() {
|
|
|
|
var content = this.props.mxEvent.getContent();
|
|
|
|
return (content && content.info && content.info.mimetype === "image/gif");
|
|
|
|
},
|
|
|
|
|
|
|
|
onImageEnter: function(e) {
|
|
|
|
if (!this._isGif()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var imgElement = e.target;
|
|
|
|
imgElement.src = MatrixClientPeg.get().mxcUrlToHttp(
|
|
|
|
this.props.mxEvent.getContent().url
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
onImageLeave: function(e) {
|
|
|
|
if (!this._isGif()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var imgElement = e.target;
|
|
|
|
imgElement.src = this._getThumbUrl();
|
|
|
|
},
|
|
|
|
|
|
|
|
_getThumbUrl: function() {
|
|
|
|
var content = this.props.mxEvent.getContent();
|
2016-02-18 22:09:58 +03:00
|
|
|
return MatrixClientPeg.get().mxcUrlToHttp(content.url, 800, 600);
|
2015-11-27 18:02:32 +03:00
|
|
|
},
|
|
|
|
|
2016-03-24 03:13:32 +03:00
|
|
|
componentDidMount: function() {
|
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
|
|
|
this.fixupHeight();
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
dis.unregister(this.dispatcherRef);
|
|
|
|
},
|
|
|
|
|
|
|
|
onAction: function(payload) {
|
|
|
|
if (payload.action === "timeline_resize") {
|
|
|
|
this.fixupHeight();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fixupHeight: function() {
|
2016-03-24 19:58:27 +03:00
|
|
|
if (!this.refs.image) {
|
2016-03-29 02:46:51 +03:00
|
|
|
console.warn("Refusing to fix up height on MImageBody with no image element");
|
2016-03-24 19:58:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-27 18:02:32 +03:00
|
|
|
var content = this.props.mxEvent.getContent();
|
|
|
|
|
|
|
|
var thumbHeight = null;
|
2016-03-24 19:58:27 +03:00
|
|
|
var timelineWidth = this.refs.body.offsetWidth;
|
2016-03-24 04:12:51 +03:00
|
|
|
var maxHeight = 600; // let images take up as much width as they can so long as the height doesn't exceed 600px.
|
|
|
|
// the alternative here would be 600*timelineWidth/800; to scale them down to fit inside a 4:3 bounding box
|
2016-03-24 03:13:32 +03:00
|
|
|
|
2016-03-24 19:58:27 +03:00
|
|
|
//console.log("trying to fit image into timelineWidth of " + this.refs.body.offsetWidth + " or " + this.refs.body.clientWidth);
|
2016-04-02 02:36:19 +03:00
|
|
|
if (content.info) {
|
|
|
|
thumbHeight = ImageUtils.thumbHeight(content.info.w, content.info.h, timelineWidth, maxHeight);
|
|
|
|
}
|
2016-03-24 19:58:27 +03:00
|
|
|
this.refs.image.style.height = thumbHeight + "px";
|
2016-03-29 02:46:51 +03:00
|
|
|
// console.log("Image height now", thumbHeight);
|
2016-03-24 03:13:32 +03:00
|
|
|
},
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2016-03-24 03:13:32 +03:00
|
|
|
render: function() {
|
|
|
|
var TintableSvg = sdk.getComponent("elements.TintableSvg");
|
|
|
|
var content = this.props.mxEvent.getContent();
|
|
|
|
var cli = MatrixClientPeg.get();
|
2015-11-27 18:02:32 +03:00
|
|
|
|
|
|
|
var thumbUrl = this._getThumbUrl();
|
|
|
|
if (thumbUrl) {
|
|
|
|
return (
|
2016-03-24 19:58:27 +03:00
|
|
|
<span className="mx_MImageBody" ref="body">
|
2015-11-27 18:02:32 +03:00
|
|
|
<a href={cli.mxcUrlToHttp(content.url)} onClick={ this.onClick }>
|
2016-03-24 19:58:27 +03:00
|
|
|
<img className="mx_MImageBody_thumbnail" src={thumbUrl} ref="image"
|
2016-03-24 03:13:32 +03:00
|
|
|
alt={content.body}
|
2015-11-27 18:02:32 +03:00
|
|
|
onMouseEnter={this.onImageEnter}
|
2016-04-02 02:36:19 +03:00
|
|
|
onMouseLeave={this.onImageLeave} />
|
2015-11-27 18:02:32 +03:00
|
|
|
</a>
|
2016-01-03 03:11:11 +03:00
|
|
|
<div className="mx_MImageBody_download">
|
2015-11-27 18:02:32 +03:00
|
|
|
<a href={cli.mxcUrlToHttp(content.url)} target="_blank">
|
2016-01-06 05:11:07 +03:00
|
|
|
<TintableSvg src="img/download.svg" width="12" height="14"/>
|
2015-11-27 18:02:32 +03:00
|
|
|
Download {content.body} ({ content.info && content.info.size ? filesize(content.info.size) : "Unknown size" })
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
} else if (content.body) {
|
|
|
|
return (
|
2016-01-03 03:11:11 +03:00
|
|
|
<span className="mx_MImageBody">
|
2015-11-27 18:02:32 +03:00
|
|
|
Image '{content.body}' cannot be displayed.
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
2016-01-03 03:11:11 +03:00
|
|
|
<span className="mx_MImageBody">
|
2015-11-27 18:02:32 +03:00
|
|
|
This image cannot be displayed.
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|