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 dis = require("../../../dispatcher");
|
|
|
|
var MatrixClientPeg = require('../../../MatrixClientPeg');
|
|
|
|
|
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,
|
|
|
|
editing: React.PropTypes.bool,
|
|
|
|
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-01-10 15:56:45 +03:00
|
|
|
componentWillReceiveProps: function(newProps) {
|
|
|
|
if (newProps.editing) {
|
|
|
|
var topic = this.props.room.currentState.getStateEvents('m.room.topic', '');
|
2016-01-10 21:46:08 +03:00
|
|
|
var name = this.props.room.currentState.getStateEvents('m.room.name', '');
|
2016-01-10 15:56:45 +03:00
|
|
|
|
|
|
|
this.setState({
|
2016-01-10 21:46:08 +03:00
|
|
|
name: name ? name.getContent().name : '',
|
2016-01-13 17:04:00 +03:00
|
|
|
defaultName: this.props.room.getDefaultRoomName(MatrixClientPeg.get().credentials.userId),
|
2016-01-10 15:56:45 +03:00
|
|
|
topic: topic ? topic.getContent().topic : '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-01-11 15:46:12 +03:00
|
|
|
componentDidUpdate: function() {
|
|
|
|
if (this.refs.topic) {
|
|
|
|
linkifyElement(this.refs.topic, linkifyMatrix.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-27 13:42:03 +03:00
|
|
|
onVideoClick: function(e) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'place_call',
|
|
|
|
type: e.shiftKey ? "screensharing" : "video",
|
|
|
|
room_id: this.props.room.roomId
|
|
|
|
});
|
|
|
|
},
|
2015-12-15 02:37:34 +03:00
|
|
|
|
2015-11-27 13:42:03 +03:00
|
|
|
onVoiceClick: function() {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'place_call',
|
|
|
|
type: "voice",
|
|
|
|
room_id: this.props.room.roomId
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-01-10 15:56:45 +03:00
|
|
|
onNameChanged: function(value) {
|
|
|
|
this.setState({ name : value });
|
|
|
|
},
|
|
|
|
|
|
|
|
onTopicChanged: function(value) {
|
|
|
|
this.setState({ topic : value });
|
2015-11-27 13:42:03 +03:00
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
changeAvatar.onFileSelected(ev).done(function() {
|
|
|
|
// dunno if the avatar changed, re-check it.
|
|
|
|
self._refreshFromServer();
|
|
|
|
}, function(err) {
|
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-11-27 13:42:03 +03:00
|
|
|
getRoomName: function() {
|
2016-01-10 15:56:45 +03:00
|
|
|
return this.state.name;
|
|
|
|
},
|
|
|
|
|
|
|
|
getTopic: function() {
|
|
|
|
return this.state.topic;
|
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() {
|
|
|
|
var EditableText = sdk.getComponent("elements.EditableText");
|
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");
|
2015-11-27 13:42:03 +03:00
|
|
|
|
|
|
|
var header;
|
|
|
|
if (this.props.simpleHeader) {
|
2015-12-18 03:37:56 +03:00
|
|
|
var cancel;
|
|
|
|
if (this.props.onCancelClick) {
|
2016-01-10 15:56:45 +03:00
|
|
|
cancel = <img className="mx_RoomHeader_simpleHeaderCancel" src="img/cancel.svg" onClick={ this.props.onCancelClick } alt="Close" width="18" height="18"/>
|
2015-12-18 03:37:56 +03:00
|
|
|
}
|
2015-11-27 13:42:03 +03:00
|
|
|
header =
|
|
|
|
<div className="mx_RoomHeader_wrapper">
|
|
|
|
<div className="mx_RoomHeader_simpleHeader">
|
|
|
|
{ this.props.simpleHeader }
|
2015-12-18 03:37:56 +03:00
|
|
|
{ cancel }
|
2015-11-27 13:42:03 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var name = null;
|
2015-12-11 05:58:59 +03:00
|
|
|
var searchStatus = null;
|
2015-11-27 13:42:03 +03:00
|
|
|
var topic_el = null;
|
|
|
|
var cancel_button = null;
|
|
|
|
var save_button = null;
|
|
|
|
var settings_button = null;
|
|
|
|
if (this.props.editing) {
|
2016-01-15 18:51:40 +03:00
|
|
|
|
|
|
|
// calculate permissions. XXX: this should be done on mount or something, and factored out with RoomSettings
|
|
|
|
var power_levels = this.props.room.currentState.getStateEvents('m.room.power_levels', '');
|
2016-01-17 06:59:31 +03:00
|
|
|
var events_levels = (power_levels ? power_levels.events : {}) || {};
|
2016-01-15 18:51:40 +03:00
|
|
|
var user_id = MatrixClientPeg.get().credentials.userId;
|
|
|
|
|
|
|
|
if (power_levels) {
|
|
|
|
power_levels = power_levels.getContent();
|
|
|
|
var default_user_level = parseInt(power_levels.users_default || 0);
|
|
|
|
var user_levels = power_levels.users || {};
|
|
|
|
var current_user_level = user_levels[user_id];
|
|
|
|
if (current_user_level == undefined) current_user_level = default_user_level;
|
|
|
|
} else {
|
|
|
|
var default_user_level = 0;
|
|
|
|
var user_levels = [];
|
|
|
|
var current_user_level = 0;
|
|
|
|
}
|
2016-01-17 06:59:31 +03:00
|
|
|
var state_default = parseInt((power_levels ? power_levels.state_default : 0) || 0);
|
2016-01-15 18:51:40 +03:00
|
|
|
|
2016-01-17 06:59:31 +03:00
|
|
|
var room_avatar_level = state_default;
|
2016-01-15 18:51:40 +03:00
|
|
|
if (events_levels['m.room.avatar'] !== undefined) {
|
|
|
|
room_avatar_level = events_levels['m.room.avatar'];
|
|
|
|
}
|
|
|
|
var can_set_room_avatar = current_user_level >= room_avatar_level;
|
|
|
|
|
2016-01-17 06:59:31 +03:00
|
|
|
var room_name_level = state_default;
|
2016-01-15 18:51:40 +03:00
|
|
|
if (events_levels['m.room.name'] !== undefined) {
|
|
|
|
room_name_level = events_levels['m.room.name'];
|
|
|
|
}
|
|
|
|
var can_set_room_name = current_user_level >= room_name_level;
|
|
|
|
|
2016-01-17 06:59:31 +03:00
|
|
|
var room_topic_level = state_default;
|
2016-01-15 18:51:40 +03:00
|
|
|
if (events_levels['m.room.topic'] !== undefined) {
|
|
|
|
room_topic_level = events_levels['m.room.topic'];
|
|
|
|
}
|
|
|
|
var can_set_room_topic = current_user_level >= room_topic_level;
|
2016-01-10 15:56:45 +03:00
|
|
|
|
2016-01-11 15:46:12 +03:00
|
|
|
var placeholderName = "Unnamed Room";
|
2016-01-13 17:04:00 +03:00
|
|
|
if (this.state.defaultName && this.state.defaultName !== '?') {
|
|
|
|
placeholderName += " (" + this.state.defaultName + ")";
|
2016-01-11 15:46:12 +03:00
|
|
|
}
|
2016-01-10 21:46:08 +03:00
|
|
|
|
2016-01-15 18:51:40 +03:00
|
|
|
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>
|
|
|
|
}
|
|
|
|
|
|
|
|
if (can_set_room_name) {
|
2016-01-10 15:56:45 +03:00
|
|
|
name =
|
|
|
|
<div className="mx_RoomHeader_name">
|
|
|
|
<EditableText
|
|
|
|
className="mx_RoomHeader_nametext mx_RoomHeader_editable"
|
|
|
|
placeholderClassName="mx_RoomHeader_placeholder"
|
2016-01-10 21:46:08 +03:00
|
|
|
placeholder={ placeholderName }
|
2016-01-10 15:56:45 +03:00
|
|
|
blurToCancel={ false }
|
|
|
|
onValueChanged={ this.onNameChanged }
|
|
|
|
initialValue={ this.state.name }/>
|
|
|
|
</div>
|
2016-01-15 18:51:40 +03:00
|
|
|
}
|
|
|
|
else {
|
2015-12-11 05:58:59 +03:00
|
|
|
var searchStatus;
|
2015-12-18 14:11:41 +03:00
|
|
|
// don't display the search count until the search completes and
|
2016-01-04 19:54:27 +03:00
|
|
|
// gives us a valid (possibly zero) searchCount.
|
2016-01-05 18:57:41 +03:00
|
|
|
if (this.props.searchInfo && this.props.searchInfo.searchCount !== undefined && this.props.searchInfo.searchCount !== null) {
|
2015-12-18 20:18:08 +03:00
|
|
|
searchStatus = <div className="mx_RoomHeader_searchStatus"> (~{ this.props.searchInfo.searchCount } results)</div>;
|
2015-12-11 05:58:59 +03:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:42:03 +03:00
|
|
|
name =
|
2016-01-11 21:44:36 +03:00
|
|
|
<div className="mx_RoomHeader_name" onClick={this.props.onSettingsClick}>
|
2015-12-12 20:29:53 +03:00
|
|
|
<div className="mx_RoomHeader_nametext" title={ this.props.room.name }>{ this.props.room.name }</div>
|
2015-12-11 05:58:59 +03:00
|
|
|
{ searchStatus }
|
2016-01-11 21:44:36 +03:00
|
|
|
<div className="mx_RoomHeader_settingsButton" title="Settings">
|
2016-01-06 05:11:07 +03:00
|
|
|
<TintableSvg src="img/settings.svg" width="12" height="12"/>
|
2015-11-27 13:42:03 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
2016-01-15 18:51:40 +03:00
|
|
|
}
|
2016-01-10 15:56:45 +03:00
|
|
|
|
2016-01-15 18:51:40 +03:00
|
|
|
if (can_set_room_topic) {
|
|
|
|
topic_el =
|
|
|
|
<EditableText
|
|
|
|
className="mx_RoomHeader_topic mx_RoomHeader_editable"
|
|
|
|
placeholderClassName="mx_RoomHeader_placeholder"
|
|
|
|
placeholder="Add a topic"
|
|
|
|
blurToCancel={ false }
|
|
|
|
onValueChanged={ this.onTopicChanged }
|
|
|
|
initialValue={ this.state.topic }/>
|
|
|
|
} else {
|
2016-01-10 15:56:45 +03:00
|
|
|
var topic = this.props.room.currentState.getStateEvents('m.room.topic', '');
|
2016-01-11 15:46:12 +03:00
|
|
|
if (topic) topic_el = <div className="mx_RoomHeader_topic" ref="topic" title={ topic.getContent().topic }>{ topic.getContent().topic }</div>;
|
2015-11-27 13:42:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var roomAvatar = null;
|
|
|
|
if (this.props.room) {
|
2016-01-15 18:51:40 +03:00
|
|
|
if (can_set_room_avatar) {
|
2016-01-15 17:22:17 +03:00
|
|
|
roomAvatar = (
|
2016-01-15 19:33:34 +03:00
|
|
|
<div className="mx_RoomHeader_avatarPicker">
|
|
|
|
<div onClick={ this.onAvatarPickerClick }>
|
|
|
|
<ChangeAvatar ref="changeAvatar" room={this.props.room} showUploadSection={false} width={48} height={48} />
|
|
|
|
</div>
|
2016-01-15 17:22:17 +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 }/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
roomAvatar = (
|
2016-01-15 18:23:41 +03:00
|
|
|
<div onClick={this.props.onSettingsClick}>
|
2016-01-18 13:49:45 +03:00
|
|
|
<RoomAvatar room={this.props.room} width={48} height={48}/>
|
2016-01-15 18:23:41 +03:00
|
|
|
</div>
|
2016-01-15 17:22:17 +03:00
|
|
|
);
|
|
|
|
}
|
2015-11-27 13:42:03 +03:00
|
|
|
}
|
|
|
|
|
2015-12-15 02:37:34 +03:00
|
|
|
var leave_button;
|
2015-12-13 16:49:28 +03:00
|
|
|
if (this.props.onLeaveClick) {
|
2015-12-15 02:37:34 +03:00
|
|
|
leave_button =
|
2016-01-04 01:34:56 +03:00
|
|
|
<div className="mx_RoomHeader_button mx_RoomHeader_leaveButton" onClick={this.props.onLeaveClick} title="Leave room">
|
2016-01-06 05:11:07 +03:00
|
|
|
<TintableSvg src="img/leave.svg" width="26" height="20"/>
|
2015-12-16 19:06:29 +03:00
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
var forget_button;
|
|
|
|
if (this.props.onForgetClick) {
|
|
|
|
forget_button =
|
2016-01-04 01:34:56 +03:00
|
|
|
<div className="mx_RoomHeader_button mx_RoomHeader_leaveButton" onClick={this.props.onForgetClick} title="Forget room">
|
2016-01-06 05:11:07 +03:00
|
|
|
<TintableSvg src="img/leave.svg" width="26" height="20"/>
|
2015-12-13 16:49:28 +03:00
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2016-01-10 15:56:45 +03:00
|
|
|
var right_row;
|
|
|
|
if (!this.props.editing) {
|
|
|
|
right_row =
|
|
|
|
<div className="mx_RoomHeader_rightRow">
|
|
|
|
{ forget_button }
|
|
|
|
{ leave_button }
|
|
|
|
<div className="mx_RoomHeader_button" onClick={this.props.onSearchClick} title="Search">
|
|
|
|
<TintableSvg src="img/search.svg" width="21" height="19"/>
|
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2015-11-27 13:42:03 +03:00
|
|
|
header =
|
|
|
|
<div className="mx_RoomHeader_wrapper">
|
2016-01-11 15:46:12 +03:00
|
|
|
<div className="mx_RoomHeader_leftRow">
|
2015-11-27 13:42:03 +03:00
|
|
|
<div className="mx_RoomHeader_avatar">
|
|
|
|
{ roomAvatar }
|
|
|
|
</div>
|
|
|
|
<div className="mx_RoomHeader_info">
|
|
|
|
{ name }
|
|
|
|
{ topic_el }
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{save_button}
|
2016-01-10 15:56:45 +03:00
|
|
|
{cancel_button}
|
|
|
|
{right_row}
|
2015-11-27 13:42:03 +03:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|