/* Copyright 2015, 2016 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'; import React from 'react'; import classNames from 'classnames'; import sdk from '../../../index'; import { _t } from '../../../languageHandler'; import MatrixClientPeg from '../../../MatrixClientPeg'; import Modal from "../../../Modal"; import dis from "../../../dispatcher"; import RateLimitedFunc from '../../../ratelimitedfunc'; import * as linkify from 'linkifyjs'; import linkifyElement from 'linkifyjs/element'; import linkifyMatrix from '../../../linkify-matrix'; import AccessibleButton from '../elements/AccessibleButton'; import {CancelButton} from './SimpleRoomHeader'; linkifyMatrix(linkify); module.exports = React.createClass({ displayName: 'RoomHeader', propTypes: { room: React.PropTypes.object, oobData: React.PropTypes.object, editing: React.PropTypes.bool, saving: React.PropTypes.bool, inRoom: React.PropTypes.bool, collapsedRhs: React.PropTypes.bool, onSettingsClick: React.PropTypes.func, onSaveClick: React.PropTypes.func, onSearchClick: React.PropTypes.func, onLeaveClick: React.PropTypes.func, }, getDefaultProps: function() { return { editing: false, inRoom: false, onSaveClick: function() {}, }; }, componentDidMount: function() { const cli = MatrixClientPeg.get(); cli.on("RoomState.events", this._onRoomStateEvents); // 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); } }, componentDidUpdate: function() { if (this.refs.topic) { linkifyElement(this.refs.topic, linkifyMatrix.options); } }, componentWillUnmount: function() { if (this.props.room) { this.props.room.removeListener("Room.name", this._onRoomNameChange); } const 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._rateLimitedUpdate(); }, _rateLimitedUpdate: new RateLimitedFunc(function() { /* eslint-disable babel/no-invalid-this */ this.forceUpdate(); }, 500), _onRoomNameChange: function(room) { this.forceUpdate(); }, onAvatarPickerClick: function(ev) { if (this.refs.file_label) { this.refs.file_label.click(); } }, onAvatarSelected: function(ev) { const changeAvatar = this.refs.changeAvatar; if (!changeAvatar) { console.error("No ChangeAvatar found to upload image to!"); return; } changeAvatar.onFileSelected(ev).catch(function(err) { const errMsg = (typeof err === "string") ? err : (err.error || ""); const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); console.error("Failed to set avatar: " + errMsg); Modal.createDialog(ErrorDialog, { title: _t("Error"), description: _t("Failed to set avatar."), }); }).done(); }, onShowRhsClick: function(ev) { dis.dispatch({ action: 'show_right_panel' }); }, /** * After editing the settings, get the new name for the room * * @return {?string} newName or undefined if we didn't let the user edit the room name */ getEditedName: function() { let newName; if (this.refs.nameEditor) { newName = this.refs.nameEditor.getRoomName(); } return newName; }, /** * After editing the settings, get the new topic for the room * * @return {?string} newTopic or undefined if we didn't let the user edit the room topic */ getEditedTopic: function() { let newTopic; if (this.refs.topicEditor) { newTopic = this.refs.topicEditor.getTopic(); } return newTopic; }, render: function() { const RoomAvatar = sdk.getComponent("avatars.RoomAvatar"); const ChangeAvatar = sdk.getComponent("settings.ChangeAvatar"); const TintableSvg = sdk.getComponent("elements.TintableSvg"); const EmojiText = sdk.getComponent('elements.EmojiText'); let name = null; let searchStatus = null; let topicElement = null; let cancelButton = null; let spinner = null; let saveButton = null; let settingsButton = null; let canSetRoomName; let canSetRoomAvatar; let canSetRoomTopic; if (this.props.editing) { // calculate permissions. XXX: this should be done on mount or something const userId = MatrixClientPeg.get().credentials.userId; canSetRoomName = this.props.room.currentState.maySendStateEvent('m.room.name', userId); canSetRoomAvatar = this.props.room.currentState.maySendStateEvent('m.room.avatar', userId); canSetRoomTopic = this.props.room.currentState.maySendStateEvent('m.room.topic', userId); saveButton = ( {_t("Save")} ); } if (this.props.onCancelClick) { cancelButton = ; } if (this.props.saving) { const Spinner = sdk.getComponent("elements.Spinner"); spinner =
; } if (canSetRoomName) { const RoomNameEditor = sdk.getComponent("rooms.RoomNameEditor"); name = ; } else { // 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 =
  { _t("(~%(count)s results)", { count: this.props.searchInfo.searchCount }) }
; } // XXX: this is a bit inefficient - we could just compare room.name for 'Empty room'... let settingsHint = false; const members = this.props.room ? this.props.room.getJoinedMembers() : undefined; if (members) { if (members.length === 1 && members[0].userId === MatrixClientPeg.get().credentials.userId) { const nameEvent = this.props.room.currentState.getStateEvents('m.room.name', ''); if (!nameEvent || !nameEvent.getContent().name) { settingsHint = true; } } } let roomName = _t("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; } const emojiTextClasses = classNames('mx_RoomHeader_nametext', { mx_RoomHeader_settingsHint: settingsHint }); name =
{ roomName } { searchStatus }
; } if (canSetRoomTopic) { const RoomTopicEditor = sdk.getComponent("rooms.RoomTopicEditor"); topicElement = ; } else { let topic; if (this.props.room) { const ev = this.props.room.currentState.getStateEvents('m.room.topic', ''); if (ev) { topic = ev.getContent().topic; } } if (topic) { topicElement =
{ topic }
; } } let roomAvatar = null; if (canSetRoomAvatar) { roomAvatar = (
); } else if (this.props.room || (this.props.oobData && this.props.oobData.name)) { roomAvatar = (
); } if (this.props.onSettingsClick) { settingsButton = ; } // var leave_button; // if (this.props.onLeaveClick) { // leave_button = //
// //
; // } let forgetButton; if (this.props.onForgetClick) { forgetButton = ; } let searchButton; if (this.props.onSearchClick && this.props.inRoom) { searchButton = ; } let rightPanelButtons; if (this.props.collapsedRhs) { rightPanelButtons = ; } let rightRow; if (!this.props.editing) { rightRow =
{ settingsButton } { forgetButton } { searchButton } { rightPanelButtons }
; } return (
{ roomAvatar }
{ name } { topicElement }
{spinner} {saveButton} {cancelButton} {rightRow}
); }, });