2015-11-27 13:42:03 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-27 13:42:03 +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 sdk = require('../../../index');
|
|
|
|
var MatrixClientPeg = require('../../../MatrixClientPeg');
|
2016-01-22 14:48:26 +03:00
|
|
|
var Modal = require("../../../Modal");
|
2015-11-27 13:42:03 +03:00
|
|
|
|
2016-01-11 15:46:12 +03:00
|
|
|
var linkify = require('linkifyjs');
|
|
|
|
var linkifyElement = require('linkifyjs/element');
|
|
|
|
var linkifyMatrix = require('../../../linkify-matrix');
|
|
|
|
|
|
|
|
linkifyMatrix(linkify);
|
|
|
|
|
2015-11-27 13:42:03 +03:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'RoomHeader',
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
room: React.PropTypes.object,
|
2016-03-01 21:23:57 +03:00
|
|
|
oobData: React.PropTypes.object,
|
2015-11-27 13:42:03 +03:00
|
|
|
editing: React.PropTypes.bool,
|
2016-04-12 20:01:49 +03:00
|
|
|
saving: React.PropTypes.bool,
|
2016-09-13 14:18:22 +03:00
|
|
|
collapsedRhs: React.PropTypes.bool,
|
2015-11-27 13:42:03 +03:00
|
|
|
onSettingsClick: React.PropTypes.func,
|
|
|
|
onSaveClick: React.PropTypes.func,
|
2015-12-13 16:49:28 +03:00
|
|
|
onSearchClick: React.PropTypes.func,
|
|
|
|
onLeaveClick: React.PropTypes.func,
|
2015-11-27 13:42:03 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
editing: false,
|
|
|
|
onSettingsClick: function() {},
|
|
|
|
onSaveClick: function() {},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-03-29 18:31:13 +03:00
|
|
|
componentDidMount: function() {
|
|
|
|
var cli = MatrixClientPeg.get();
|
|
|
|
cli.on("RoomState.events", this._onRoomStateEvents);
|
2016-04-15 11:59:23 +03:00
|
|
|
|
|
|
|
// When a room name occurs, RoomState.events is fired *before*
|
|
|
|
// room.name is updated. So we have to listen to Room.name as well as
|
|
|
|
// RoomState.events.
|
|
|
|
if (this.props.room) {
|
|
|
|
this.props.room.on("Room.name", this._onRoomNameChange);
|
|
|
|
}
|
2016-03-29 18:31:13 +03:00
|
|
|
},
|
|
|
|
|
2016-01-11 15:46:12 +03:00
|
|
|
componentDidUpdate: function() {
|
|
|
|
if (this.refs.topic) {
|
|
|
|
linkifyElement(this.refs.topic, linkifyMatrix.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-29 18:31:13 +03:00
|
|
|
componentWillUnmount: function() {
|
2016-04-15 11:59:23 +03:00
|
|
|
if (this.props.room) {
|
|
|
|
this.props.room.removeListener("Room.name", this._onRoomNameChange);
|
|
|
|
}
|
2016-03-29 18:31:13 +03:00
|
|
|
var cli = MatrixClientPeg.get();
|
|
|
|
if (cli) {
|
|
|
|
cli.removeListener("RoomState.events", this._onRoomStateEvents);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onRoomStateEvents: function(event, state) {
|
|
|
|
if (!this.props.room || event.getRoomId() != this.props.room.roomId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// redisplay the room name, topic, etc.
|
|
|
|
this.forceUpdate();
|
|
|
|
},
|
|
|
|
|
2016-04-15 11:59:23 +03:00
|
|
|
_onRoomNameChange: function(room) {
|
|
|
|
this.forceUpdate();
|
|
|
|
},
|
|
|
|
|
2016-01-15 17:22:17 +03:00
|
|
|
onAvatarPickerClick: function(ev) {
|
|
|
|
if (this.refs.file_label) {
|
|
|
|
this.refs.file_label.click();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onAvatarSelected: function(ev) {
|
|
|
|
var self = this;
|
|
|
|
var changeAvatar = this.refs.changeAvatar;
|
|
|
|
if (!changeAvatar) {
|
|
|
|
console.error("No ChangeAvatar found to upload image to!");
|
|
|
|
return;
|
|
|
|
}
|
2016-01-22 14:48:26 +03:00
|
|
|
changeAvatar.onFileSelected(ev).catch(function(err) {
|
2016-01-15 17:22:17 +03:00
|
|
|
var errMsg = (typeof err === "string") ? err : (err.error || "");
|
|
|
|
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
|
|
|
title: "Error",
|
|
|
|
description: "Failed to set avatar. " + errMsg
|
|
|
|
});
|
2016-01-22 14:48:26 +03:00
|
|
|
}).done();
|
2016-04-15 11:59:23 +03:00
|
|
|
},
|
2016-01-15 17:22:17 +03:00
|
|
|
|
2016-03-29 17:21:16 +03:00
|
|
|
/**
|
|
|
|
* After editing the settings, get the new name for the room
|
|
|
|
*
|
|
|
|
* Returns undefined if we didn't let the user edit the room name
|
|
|
|
*/
|
|
|
|
getEditedName: function() {
|
|
|
|
var newName;
|
|
|
|
if (this.refs.nameEditor) {
|
|
|
|
newName = this.refs.nameEditor.getRoomName();
|
|
|
|
}
|
|
|
|
return newName;
|
2016-01-10 15:56:45 +03:00
|
|
|
},
|
|
|
|
|
2016-03-29 17:21:16 +03:00
|
|
|
/**
|
|
|
|
* After editing the settings, get the new topic for the room
|
|
|
|
*
|
|
|
|
* Returns undefined if we didn't let the user edit the room topic
|
|
|
|
*/
|
|
|
|
getEditedTopic: function() {
|
|
|
|
var newTopic;
|
|
|
|
if (this.refs.topicEditor) {
|
|
|
|
newTopic = this.refs.topicEditor.getTopic();
|
|
|
|
}
|
|
|
|
return newTopic;
|
2015-11-27 13:42:03 +03:00
|
|
|
},
|
2016-01-05 06:34:52 +03:00
|
|
|
|
2015-11-27 13:42:03 +03:00
|
|
|
render: function() {
|
2016-01-15 17:22:17 +03:00
|
|
|
var RoomAvatar = sdk.getComponent("avatars.RoomAvatar");
|
|
|
|
var ChangeAvatar = sdk.getComponent("settings.ChangeAvatar");
|
2016-01-06 05:11:07 +03:00
|
|
|
var TintableSvg = sdk.getComponent("elements.TintableSvg");
|
2016-08-11 05:25:12 +03:00
|
|
|
const EmojiText = sdk.getComponent('elements.EmojiText');
|
2015-11-27 13:42:03 +03:00
|
|
|
|
|
|
|
var header;
|
2016-03-29 14:51:46 +03:00
|
|
|
var name = null;
|
|
|
|
var searchStatus = null;
|
|
|
|
var topic_el = null;
|
|
|
|
var cancel_button = null;
|
2016-04-12 20:01:49 +03:00
|
|
|
var spinner = null;
|
2016-03-29 14:51:46 +03:00
|
|
|
var save_button = null;
|
|
|
|
var settings_button = null;
|
|
|
|
if (this.props.editing) {
|
|
|
|
|
|
|
|
// calculate permissions. XXX: this should be done on mount or something
|
|
|
|
var user_id = MatrixClientPeg.get().credentials.userId;
|
|
|
|
|
|
|
|
var can_set_room_name = this.props.room.currentState.maySendStateEvent(
|
|
|
|
'm.room.name', user_id
|
|
|
|
);
|
|
|
|
var can_set_room_avatar = this.props.room.currentState.maySendStateEvent(
|
|
|
|
'm.room.avatar', user_id
|
|
|
|
);
|
|
|
|
var can_set_room_topic = this.props.room.currentState.maySendStateEvent(
|
|
|
|
'm.room.topic', user_id
|
|
|
|
);
|
|
|
|
var can_set_room_name = this.props.room.currentState.maySendStateEvent(
|
|
|
|
'm.room.name', user_id
|
|
|
|
);
|
|
|
|
|
|
|
|
save_button = <div className="mx_RoomHeader_textButton" onClick={this.props.onSaveClick}>Save</div>
|
|
|
|
cancel_button = <div className="mx_RoomHeader_cancelButton" onClick={this.props.onCancelClick}><img src="img/cancel.svg" width="18" height="18" alt="Cancel"/> </div>
|
|
|
|
}
|
|
|
|
|
2016-04-12 20:01:49 +03:00
|
|
|
if (this.props.saving) {
|
|
|
|
var Spinner = sdk.getComponent("elements.Spinner");
|
|
|
|
spinner = <div className="mx_RoomHeader_spinner"><Spinner/></div>;
|
|
|
|
}
|
|
|
|
|
2016-03-29 14:51:46 +03:00
|
|
|
if (can_set_room_name) {
|
2016-03-29 17:21:16 +03:00
|
|
|
var RoomNameEditor = sdk.getComponent("rooms.RoomNameEditor");
|
|
|
|
name = <RoomNameEditor ref="nameEditor" room={this.props.room} />
|
2015-11-27 13:42:03 +03:00
|
|
|
}
|
|
|
|
else {
|
2016-03-29 14:51:46 +03:00
|
|
|
var searchStatus;
|
|
|
|
// don't display the search count until the search completes and
|
|
|
|
// gives us a valid (possibly zero) searchCount.
|
|
|
|
if (this.props.searchInfo && this.props.searchInfo.searchCount !== undefined && this.props.searchInfo.searchCount !== null) {
|
|
|
|
searchStatus = <div className="mx_RoomHeader_searchStatus"> (~{ this.props.searchInfo.searchCount } results)</div>;
|
2016-01-15 18:51:40 +03:00
|
|
|
}
|
|
|
|
|
2016-03-29 14:51:46 +03:00
|
|
|
// XXX: this is a bit inefficient - we could just compare room.name for 'Empty room'...
|
|
|
|
var settingsHint = false;
|
|
|
|
var members = this.props.room ? this.props.room.getJoinedMembers() : undefined;
|
|
|
|
if (members) {
|
|
|
|
if (members.length === 1 && members[0].userId === MatrixClientPeg.get().credentials.userId) {
|
|
|
|
var name = this.props.room.currentState.getStateEvents('m.room.name', '');
|
|
|
|
if (!name || !name.getContent().name) {
|
|
|
|
settingsHint = true;
|
2016-01-21 03:16:10 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-29 14:51:46 +03:00
|
|
|
}
|
2016-01-21 03:16:10 +03:00
|
|
|
|
2016-03-29 14:51:46 +03:00
|
|
|
var roomName = 'Join Room';
|
|
|
|
if (this.props.oobData && this.props.oobData.name) {
|
|
|
|
roomName = this.props.oobData.name;
|
|
|
|
} else if (this.props.room) {
|
|
|
|
roomName = this.props.room.name;
|
|
|
|
}
|
2016-03-01 21:23:57 +03:00
|
|
|
|
2016-07-05 07:46:17 +03:00
|
|
|
|
2016-03-29 14:51:46 +03:00
|
|
|
name =
|
|
|
|
<div className="mx_RoomHeader_name" onClick={this.props.onSettingsClick}>
|
2016-08-09 22:01:51 +03:00
|
|
|
<EmojiText element="div" className={ "mx_RoomHeader_nametext " + (settingsHint ? "mx_RoomHeader_settingsHint" : "") } title={ roomName }>{roomName}</EmojiText>
|
2016-03-29 14:51:46 +03:00
|
|
|
{ searchStatus }
|
2016-08-09 22:01:51 +03:00
|
|
|
</div>;
|
2016-03-29 14:51:46 +03:00
|
|
|
}
|
2016-01-10 15:56:45 +03:00
|
|
|
|
2016-03-29 14:51:46 +03:00
|
|
|
if (can_set_room_topic) {
|
2016-03-29 17:21:16 +03:00
|
|
|
var RoomTopicEditor = sdk.getComponent("rooms.RoomTopicEditor");
|
|
|
|
topic_el = <RoomTopicEditor ref="topicEditor" room={this.props.room} />
|
2016-03-29 14:51:46 +03:00
|
|
|
} else {
|
2016-03-29 17:21:16 +03:00
|
|
|
var topic;
|
|
|
|
if (this.props.room) {
|
|
|
|
var ev = this.props.room.currentState.getStateEvents('m.room.topic', '');
|
|
|
|
if (ev) {
|
|
|
|
topic = ev.getContent().topic;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (topic) {
|
|
|
|
topic_el = <div className="mx_RoomHeader_topic" ref="topic" title={ topic }>{ topic }</div>;
|
|
|
|
}
|
2016-03-29 14:51:46 +03:00
|
|
|
}
|
2015-11-27 13:42:03 +03:00
|
|
|
|
2016-03-29 14:51:46 +03:00
|
|
|
var roomAvatar = null;
|
|
|
|
if (can_set_room_avatar) {
|
|
|
|
roomAvatar = (
|
|
|
|
<div className="mx_RoomHeader_avatarPicker">
|
|
|
|
<div onClick={ this.onAvatarPickerClick }>
|
|
|
|
<ChangeAvatar ref="changeAvatar" room={this.props.room} showUploadSection={false} width={48} height={48} />
|
2016-03-01 21:23:57 +03:00
|
|
|
</div>
|
2016-03-29 14:51:46 +03:00
|
|
|
<div className="mx_RoomHeader_avatarPicker_edit">
|
|
|
|
<label htmlFor="avatarInput" ref="file_label">
|
|
|
|
<img src="img/camera.svg"
|
|
|
|
alt="Upload avatar" title="Upload avatar"
|
|
|
|
width="17" height="15" />
|
|
|
|
</label>
|
|
|
|
<input id="avatarInput" type="file" onChange={ this.onAvatarSelected }/>
|
2016-03-01 21:23:57 +03:00
|
|
|
</div>
|
2016-03-29 14:51:46 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (this.props.room || (this.props.oobData && this.props.oobData.name)) {
|
|
|
|
roomAvatar = (
|
|
|
|
<div onClick={this.props.onSettingsClick}>
|
|
|
|
<RoomAvatar room={this.props.room} width={48} height={48} oobData={this.props.oobData} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2015-11-27 13:42:03 +03:00
|
|
|
|
2016-07-13 18:02:18 +03:00
|
|
|
var settings_button;
|
|
|
|
if (this.props.onSettingsClick) {
|
|
|
|
settings_button =
|
2016-07-14 14:05:00 +03:00
|
|
|
<div className="mx_RoomHeader_button" onClick={this.props.onSettingsClick} title="Settings">
|
2016-07-13 18:02:18 +03:00
|
|
|
<TintableSvg src="img/icons-settings-room.svg" width="16" height="16"/>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2016-08-10 18:34:49 +03:00
|
|
|
// var leave_button;
|
|
|
|
// if (this.props.onLeaveClick) {
|
|
|
|
// leave_button =
|
|
|
|
// <div className="mx_RoomHeader_button" onClick={this.props.onLeaveClick} title="Leave room">
|
|
|
|
// <TintableSvg src="img/leave.svg" width="26" height="20"/>
|
|
|
|
// </div>;
|
|
|
|
// }
|
2015-12-16 19:06:29 +03:00
|
|
|
|
2016-03-29 14:51:46 +03:00
|
|
|
var forget_button;
|
|
|
|
if (this.props.onForgetClick) {
|
|
|
|
forget_button =
|
2016-07-14 13:43:33 +03:00
|
|
|
<div className="mx_RoomHeader_button" onClick={this.props.onForgetClick} title="Forget room">
|
2016-03-29 14:51:46 +03:00
|
|
|
<TintableSvg src="img/leave.svg" width="26" height="20"/>
|
|
|
|
</div>;
|
|
|
|
}
|
2015-12-13 16:49:28 +03:00
|
|
|
|
2016-04-15 20:45:40 +03:00
|
|
|
var rightPanel_buttons;
|
2016-09-13 14:18:22 +03:00
|
|
|
if (this.props.collapsedRhs) {
|
|
|
|
rightPanel_buttons =
|
|
|
|
<div className="mx_RoomHeader_button" onClick={this.props.onSearchClick} title="Search">
|
|
|
|
<TintableSvg src="img/icons-search.svg" width="35" height="35"/>
|
|
|
|
</div>
|
2016-04-15 20:45:40 +03:00
|
|
|
}
|
|
|
|
|
2016-03-29 14:51:46 +03:00
|
|
|
var right_row;
|
|
|
|
if (!this.props.editing) {
|
2016-04-15 11:59:23 +03:00
|
|
|
right_row =
|
2016-03-29 14:51:46 +03:00
|
|
|
<div className="mx_RoomHeader_rightRow">
|
2016-07-13 18:02:18 +03:00
|
|
|
{ settings_button }
|
2016-03-29 14:51:46 +03:00
|
|
|
{ forget_button }
|
|
|
|
<div className="mx_RoomHeader_button" onClick={this.props.onSearchClick} title="Search">
|
2016-07-14 13:43:33 +03:00
|
|
|
<TintableSvg src="img/icons-search.svg" width="35" height="35"/>
|
2016-03-29 14:51:46 +03:00
|
|
|
</div>
|
2016-04-15 20:45:40 +03:00
|
|
|
{ rightPanel_buttons }
|
2016-03-29 14:51:46 +03:00
|
|
|
</div>;
|
|
|
|
}
|
2016-01-10 15:56:45 +03:00
|
|
|
|
2016-03-29 14:51:46 +03:00
|
|
|
header =
|
|
|
|
<div className="mx_RoomHeader_wrapper">
|
|
|
|
<div className="mx_RoomHeader_leftRow">
|
|
|
|
<div className="mx_RoomHeader_avatar">
|
|
|
|
{ roomAvatar }
|
|
|
|
</div>
|
|
|
|
<div className="mx_RoomHeader_info">
|
|
|
|
{ name }
|
|
|
|
{ topic_el }
|
2015-11-27 13:42:03 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
2016-04-12 20:01:49 +03:00
|
|
|
{spinner}
|
2016-03-29 14:51:46 +03:00
|
|
|
{save_button}
|
|
|
|
{cancel_button}
|
|
|
|
{right_row}
|
|
|
|
</div>;
|
2015-11-27 13:42:03 +03:00
|
|
|
|
|
|
|
return (
|
2016-01-15 18:23:41 +03:00
|
|
|
<div className={ "mx_RoomHeader " + (this.props.editing ? "mx_RoomHeader_editing" : "") }>
|
2015-11-27 13:42:03 +03:00
|
|
|
{ header }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|