/* 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'; var React = require('react'); var sdk = require('../../../index'); var dis = require("../../../dispatcher"); var MatrixClientPeg = require('../../../MatrixClientPeg'); var linkify = require('linkifyjs'); var linkifyElement = require('linkifyjs/element'); var linkifyMatrix = require('../../../linkify-matrix'); linkifyMatrix(linkify); module.exports = React.createClass({ displayName: 'RoomHeader', propTypes: { room: React.PropTypes.object, editing: React.PropTypes.bool, onSettingsClick: React.PropTypes.func, onSaveClick: React.PropTypes.func, onSearchClick: React.PropTypes.func, onLeaveClick: React.PropTypes.func, }, getDefaultProps: function() { return { editing: false, onSettingsClick: function() {}, onSaveClick: function() {}, }; }, componentWillReceiveProps: function(newProps) { if (newProps.editing) { var topic = this.props.room.currentState.getStateEvents('m.room.topic', ''); var name = this.props.room.currentState.getStateEvents('m.room.name', ''); this.setState({ name: name ? name.getContent().name : '', defaultName: this.props.room.getDefaultRoomName(MatrixClientPeg.get().credentials.userId), topic: topic ? topic.getContent().topic : '', }); } }, componentDidUpdate: function() { if (this.refs.topic) { linkifyElement(this.refs.topic, linkifyMatrix.options); } }, onVideoClick: function(e) { dis.dispatch({ action: 'place_call', type: e.shiftKey ? "screensharing" : "video", room_id: this.props.room.roomId }); }, onVoiceClick: function() { dis.dispatch({ action: 'place_call', type: "voice", room_id: this.props.room.roomId }); }, onNameChanged: function(value) { this.setState({ name : value }); }, onTopicChanged: function(value) { this.setState({ topic : value }); }, 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 }); }); }, getRoomName: function() { return this.state.name; }, getTopic: function() { return this.state.topic; }, render: function() { var EditableText = sdk.getComponent("elements.EditableText"); var RoomAvatar = sdk.getComponent("avatars.RoomAvatar"); var ChangeAvatar = sdk.getComponent("settings.ChangeAvatar"); var TintableSvg = sdk.getComponent("elements.TintableSvg"); var header; if (this.props.simpleHeader) { var cancel; if (this.props.onCancelClick) { cancel = Close } header =
{ this.props.simpleHeader } { cancel }
} else { var name = null; var searchStatus = null; var topic_el = null; var cancel_button = null; var save_button = null; var settings_button = null; if (this.props.editing) { // 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', ''); var events_levels = power_levels.events || {}; 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; } var room_avatar_level = parseInt(power_levels.state_default || 0); 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; var room_name_level = parseInt(power_levels.state_default || 0); 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; var room_topic_level = parseInt(power_levels.state_default || 0); 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; var placeholderName = "Unnamed Room"; if (this.state.defaultName && this.state.defaultName !== '?') { placeholderName += " (" + this.state.defaultName + ")"; } save_button =
Save
cancel_button =
Cancel
} if (can_set_room_name) { name =
} else { 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 =
 (~{ this.props.searchInfo.searchCount } results)
; } name =
{ this.props.room.name }
{ searchStatus }
} if (can_set_room_topic) { topic_el = } else { var topic = this.props.room.currentState.getStateEvents('m.room.topic', ''); if (topic) topic_el =
{ topic.getContent().topic }
; } var roomAvatar = null; if (this.props.room) { if (can_set_room_avatar) { roomAvatar = (
); } else { roomAvatar = (
); } } var leave_button; if (this.props.onLeaveClick) { leave_button =
; } var forget_button; if (this.props.onForgetClick) { forget_button =
; } var right_row; if (!this.props.editing) { right_row =
{ forget_button } { leave_button }
; } header =
{ roomAvatar }
{ name } { topic_el }
{save_button} {cancel_button} {right_row}
} return (
{ header }
); }, });