2015-06-23 18:41:25 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015 OpenMarket Ltd
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
2015-06-19 14:53:48 +03:00
|
|
|
var MatrixClientPeg = require("../../MatrixClientPeg");
|
2015-07-06 20:09:19 +03:00
|
|
|
var React = require("react");
|
2015-07-08 16:34:26 +03:00
|
|
|
var q = require("q");
|
|
|
|
var ContentMessages = require("../../ContentMessages");
|
2015-06-12 19:34:17 +03:00
|
|
|
|
2015-06-24 20:15:34 +03:00
|
|
|
var dis = require("../../dispatcher");
|
|
|
|
|
2015-06-24 15:28:44 +03:00
|
|
|
var PAGINATE_SIZE = 20;
|
2015-06-25 16:36:24 +03:00
|
|
|
var INITIAL_SIZE = 100;
|
2015-06-24 15:28:44 +03:00
|
|
|
|
2015-07-06 20:09:19 +03:00
|
|
|
var ComponentBroker = require('../../ComponentBroker');
|
|
|
|
|
|
|
|
var tileTypes = {
|
|
|
|
'm.room.message': ComponentBroker.get('molecules/MessageTile'),
|
|
|
|
'm.room.member': ComponentBroker.get('molecules/MRoomMemberTile')
|
|
|
|
};
|
|
|
|
|
2015-06-19 14:53:48 +03:00
|
|
|
module.exports = {
|
2015-06-09 19:40:42 +03:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2015-07-14 20:46:15 +03:00
|
|
|
room: this.props.roomId ? MatrixClientPeg.get().getRoom(this.props.roomId) : null,
|
2015-06-25 16:36:24 +03:00
|
|
|
messageCap: INITIAL_SIZE
|
2015-06-09 19:40:42 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-12 20:17:54 +03:00
|
|
|
componentWillMount: function() {
|
2015-06-24 20:15:34 +03:00
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
2015-06-12 20:17:54 +03:00
|
|
|
MatrixClientPeg.get().on("Room.timeline", this.onRoomTimeline);
|
2015-06-18 13:23:35 +03:00
|
|
|
this.atBottom = true;
|
2015-06-12 20:17:54 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount: function() {
|
2015-07-07 13:00:02 +03:00
|
|
|
if (this.refs.messageWrapper) {
|
|
|
|
var messageWrapper = this.refs.messageWrapper.getDOMNode();
|
2015-07-08 16:34:26 +03:00
|
|
|
messageWrapper.removeEventListener('drop', this.onDrop);
|
2015-07-07 13:00:02 +03:00
|
|
|
}
|
2015-06-24 20:15:34 +03:00
|
|
|
dis.unregister(this.dispatcherRef);
|
2015-06-16 16:37:15 +03:00
|
|
|
if (MatrixClientPeg.get()) {
|
|
|
|
MatrixClientPeg.get().removeListener("Room.timeline", this.onRoomTimeline);
|
|
|
|
}
|
2015-06-12 20:17:54 +03:00
|
|
|
},
|
|
|
|
|
2015-06-24 20:15:34 +03:00
|
|
|
onAction: function(payload) {
|
|
|
|
switch (payload.action) {
|
|
|
|
case 'message_sent':
|
|
|
|
this.setState({
|
|
|
|
room: MatrixClientPeg.get().getRoom(this.props.roomId)
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-18 13:23:35 +03:00
|
|
|
// MatrixRoom still showing the messages from the old room?
|
|
|
|
// Set the key to the room_id. Sadly you can no longer get at
|
|
|
|
// the key from inside the component, or we'd check this in code.
|
|
|
|
/*componentWillReceiveProps: function(props) {
|
|
|
|
},*/
|
2015-06-12 20:01:38 +03:00
|
|
|
|
2015-06-12 20:17:54 +03:00
|
|
|
onRoomTimeline: function(ev, room, toStartOfTimeline) {
|
2015-06-25 19:52:40 +03:00
|
|
|
if (!this.isMounted()) return;
|
|
|
|
|
2015-06-24 15:28:44 +03:00
|
|
|
// ignore anything that comes in whilst pagingating: we get one
|
|
|
|
// event for each new matrix event so this would cause a huge
|
|
|
|
// number of UI updates. Just update the UI when the paginate
|
|
|
|
// call returns.
|
|
|
|
if (this.state.paginating) return;
|
|
|
|
|
2015-06-23 13:36:19 +03:00
|
|
|
// no point handling anything while we're waiting for the join to finish:
|
|
|
|
// we'll only be showing a spinner.
|
|
|
|
if (this.state.joining) return;
|
2015-06-16 13:08:27 +03:00
|
|
|
if (room.roomId != this.props.roomId) return;
|
2015-06-22 16:48:58 +03:00
|
|
|
|
2015-07-07 13:00:02 +03:00
|
|
|
if (this.refs.messageWrapper) {
|
|
|
|
var messageWrapper = this.refs.messageWrapper.getDOMNode();
|
|
|
|
this.atBottom = messageWrapper.scrollHeight - messageWrapper.scrollTop <= messageWrapper.clientHeight;
|
2015-06-22 16:48:58 +03:00
|
|
|
}
|
2015-06-12 20:17:54 +03:00
|
|
|
this.setState({
|
2015-06-16 13:08:27 +03:00
|
|
|
room: MatrixClientPeg.get().getRoom(this.props.roomId)
|
2015-06-12 20:17:54 +03:00
|
|
|
});
|
2015-06-24 15:28:44 +03:00
|
|
|
|
|
|
|
if (toStartOfTimeline && !this.state.paginating) {
|
|
|
|
this.fillSpace();
|
|
|
|
}
|
2015-06-12 20:17:54 +03:00
|
|
|
},
|
|
|
|
|
2015-06-18 13:23:35 +03:00
|
|
|
componentDidMount: function() {
|
2015-07-07 13:00:02 +03:00
|
|
|
if (this.refs.messageWrapper) {
|
|
|
|
var messageWrapper = this.refs.messageWrapper.getDOMNode();
|
2015-07-06 20:29:24 +03:00
|
|
|
|
2015-07-08 16:34:26 +03:00
|
|
|
messageWrapper.addEventListener('drop', this.onDrop);
|
|
|
|
messageWrapper.addEventListener('dragover', this.onDragOver);
|
2015-07-06 20:29:24 +03:00
|
|
|
|
2015-07-07 13:00:02 +03:00
|
|
|
messageWrapper.scrollTop = messageWrapper.scrollHeight;
|
2015-06-24 15:28:44 +03:00
|
|
|
|
|
|
|
this.fillSpace();
|
2015-06-22 16:48:58 +03:00
|
|
|
}
|
2015-06-18 13:23:35 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
componentDidUpdate: function() {
|
2015-07-07 13:00:02 +03:00
|
|
|
if (!this.refs.messageWrapper) return;
|
2015-06-24 15:28:44 +03:00
|
|
|
|
2015-07-07 13:00:02 +03:00
|
|
|
var messageWrapper = this.refs.messageWrapper.getDOMNode();
|
2015-06-24 15:28:44 +03:00
|
|
|
|
|
|
|
if (this.state.paginating && !this.waiting_for_paginate) {
|
2015-07-07 13:00:02 +03:00
|
|
|
var heightGained = messageWrapper.scrollHeight - this.oldScrollHeight;
|
|
|
|
messageWrapper.scrollTop += heightGained;
|
2015-06-24 15:28:44 +03:00
|
|
|
this.oldScrollHeight = undefined;
|
|
|
|
if (!this.fillSpace()) {
|
|
|
|
this.setState({paginating: false});
|
|
|
|
}
|
|
|
|
} else if (this.atBottom) {
|
2015-07-07 13:00:02 +03:00
|
|
|
messageWrapper.scrollTop = messageWrapper.scrollHeight;
|
2015-06-18 13:23:35 +03:00
|
|
|
}
|
2015-06-22 16:48:58 +03:00
|
|
|
},
|
|
|
|
|
2015-06-24 15:28:44 +03:00
|
|
|
fillSpace: function() {
|
2015-07-07 13:00:02 +03:00
|
|
|
var messageWrapper = this.refs.messageWrapper.getDOMNode();
|
|
|
|
if (messageWrapper.scrollTop < messageWrapper.clientHeight && this.state.room.oldState.paginationToken) {
|
2015-06-24 15:28:44 +03:00
|
|
|
this.setState({paginating: true});
|
|
|
|
|
2015-07-07 13:00:02 +03:00
|
|
|
this.oldScrollHeight = messageWrapper.scrollHeight;
|
2015-06-24 15:28:44 +03:00
|
|
|
|
2015-06-25 16:36:24 +03:00
|
|
|
if (this.state.messageCap < this.state.room.timeline.length) {
|
|
|
|
this.waiting_for_paginate = false;
|
|
|
|
var cap = Math.min(this.state.messageCap + PAGINATE_SIZE, this.state.room.timeline.length);
|
|
|
|
this.setState({messageCap: cap, paginating: true});
|
|
|
|
} else {
|
|
|
|
this.waiting_for_paginate = true;
|
|
|
|
var cap = this.state.messageCap + PAGINATE_SIZE;
|
|
|
|
this.setState({messageCap: cap, paginating: true});
|
|
|
|
var that = this;
|
|
|
|
MatrixClientPeg.get().scrollback(this.state.room, PAGINATE_SIZE).finally(function() {
|
|
|
|
that.waiting_for_paginate = false;
|
2015-06-25 19:52:40 +03:00
|
|
|
if (that.isMounted()) {
|
|
|
|
that.setState({
|
|
|
|
room: MatrixClientPeg.get().getRoom(that.props.roomId)
|
|
|
|
});
|
|
|
|
}
|
2015-06-25 16:36:24 +03:00
|
|
|
// wait and set paginating to false when the component updates
|
2015-06-24 15:28:44 +03:00
|
|
|
});
|
2015-06-25 16:36:24 +03:00
|
|
|
}
|
|
|
|
|
2015-06-24 15:28:44 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2015-06-22 16:48:58 +03:00
|
|
|
onJoinButtonClicked: function(ev) {
|
2015-06-23 13:36:19 +03:00
|
|
|
var that = this;
|
2015-06-22 16:48:58 +03:00
|
|
|
MatrixClientPeg.get().joinRoom(this.props.roomId).then(function() {
|
2015-06-23 13:36:19 +03:00
|
|
|
that.setState({
|
|
|
|
joining: false,
|
|
|
|
room: MatrixClientPeg.get().getRoom(that.props.roomId)
|
2015-06-22 16:48:58 +03:00
|
|
|
});
|
|
|
|
}, function(error) {
|
2015-06-23 13:36:19 +03:00
|
|
|
that.setState({
|
|
|
|
joining: false,
|
2015-06-22 16:48:58 +03:00
|
|
|
joinError: error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.setState({
|
|
|
|
joining: true
|
|
|
|
});
|
2015-06-24 15:28:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onMessageListScroll: function(ev) {
|
2015-07-07 13:00:02 +03:00
|
|
|
if (this.refs.messageWrapper) {
|
|
|
|
var messageWrapper = this.refs.messageWrapper.getDOMNode();
|
|
|
|
this.atBottom = messageWrapper.scrollHeight - messageWrapper.scrollTop <= messageWrapper.clientHeight;
|
2015-06-24 15:28:44 +03:00
|
|
|
}
|
|
|
|
if (!this.state.paginating) this.fillSpace();
|
2015-07-06 20:09:19 +03:00
|
|
|
},
|
|
|
|
|
2015-07-08 16:34:26 +03:00
|
|
|
onDragOver: function(ev) {
|
2015-07-07 13:00:02 +03:00
|
|
|
ev.stopPropagation();
|
2015-07-08 16:34:26 +03:00
|
|
|
ev.preventDefault();
|
2015-07-07 13:00:02 +03:00
|
|
|
|
2015-07-08 16:34:26 +03:00
|
|
|
ev.dataTransfer.dropEffect = 'none';
|
|
|
|
|
|
|
|
var items = ev.dataTransfer.items;
|
|
|
|
if (items.length == 1) {
|
|
|
|
if (items[0].kind == 'file') {
|
|
|
|
ev.dataTransfer.dropEffect = 'copy';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onDrop: function(ev) {
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
var files = ev.dataTransfer.files;
|
|
|
|
|
|
|
|
if (files.length == 1) {
|
|
|
|
ContentMessages.sendContentToRoom(
|
|
|
|
files[0], this.props.roomId, MatrixClientPeg.get()
|
|
|
|
).progress(function(ev) {
|
|
|
|
//console.log("Upload: "+ev.loaded+" / "+ev.total);
|
|
|
|
}).done(undefined, function() {
|
|
|
|
// display error message
|
|
|
|
});
|
|
|
|
}
|
2015-07-07 13:00:02 +03:00
|
|
|
},
|
|
|
|
|
2015-07-06 20:09:19 +03:00
|
|
|
getEventTiles: function() {
|
|
|
|
var ret = [];
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
for (var i = this.state.room.timeline.length-1; i >= 0 && count < this.state.messageCap; --i) {
|
|
|
|
var mxEv = this.state.room.timeline[i];
|
|
|
|
var TileType = tileTypes[mxEv.getType()];
|
2015-07-14 02:25:51 +03:00
|
|
|
var continuation = false;
|
2015-07-15 06:16:38 +03:00
|
|
|
var last = false;
|
|
|
|
if (i == this.state.room.timeline.length - 1) {
|
|
|
|
last = true;
|
|
|
|
}
|
2015-07-14 02:25:51 +03:00
|
|
|
if (i > 0 &&
|
|
|
|
count < this.state.messageCap - 1 &&
|
|
|
|
this.state.room.timeline[i].sender &&
|
|
|
|
this.state.room.timeline[i - 1].sender &&
|
|
|
|
this.state.room.timeline[i].sender.userId ===
|
|
|
|
this.state.room.timeline[i - 1].sender.userId)
|
|
|
|
{
|
|
|
|
continuation = true;
|
|
|
|
}
|
2015-07-06 20:09:19 +03:00
|
|
|
if (!TileType) continue;
|
|
|
|
ret.unshift(
|
2015-07-15 06:16:38 +03:00
|
|
|
<TileType key={mxEv.getId()} mxEvent={mxEv} continuation={continuation} last={last}/>
|
2015-07-06 20:09:19 +03:00
|
|
|
);
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
return ret;
|
2015-06-18 13:23:35 +03:00
|
|
|
}
|
2015-06-19 14:53:48 +03:00
|
|
|
};
|
2015-06-09 19:40:42 +03:00
|
|
|
|