/* 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 MatrixClientPeg = require('matrix-react-sdk/lib/MatrixClientPeg'); var Modal = require('matrix-react-sdk/lib/Modal'); var sdk = require('matrix-react-sdk') var dis = require('matrix-react-sdk/lib/dispatcher'); module.exports = React.createClass({ displayName: 'RoomDirectory', getInitialState: function() { return { publicRooms: [], roomAlias: '', loading: true, } }, componentDidMount: function() { var self = this; MatrixClientPeg.get().publicRooms(function (err, data) { if (err) { self.setState({ loading: false }); console.error("Failed to get publicRooms: %s", JSON.stringify(err)); var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); Modal.createDialog(ErrorDialog, { title: "Failed to get public room list", description: err.message }); } else { self.setState({ publicRooms: data.chunk, loading: false, }); self.forceUpdate(); } }); }, joinRoom: function(roomId, shouldPeek) { var self = this; self.setState({ loading: true }); var joinOrPeekPromise; if (shouldPeek) { joinOrPeekPromise = MatrixClientPeg.get().peekInRoom(roomId); } else { joinOrPeekPromise = MatrixClientPeg.get().joinRoom(roomId); } joinOrPeekPromise.done(function() { dis.dispatch({ action: 'view_room', room_id: roomId }); }, function(err) { console.error("Failed to join room: %s", JSON.stringify(err)); console.error(err); var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); Modal.createDialog(ErrorDialog, { title: "Failed to join room", description: err.message }); }); }, getRows: function(filter) { if (!this.state.publicRooms) return []; var rooms = this.state.publicRooms.filter(function(a) { // FIXME: if incrementally typing, keep narrowing down the search set // incrementally rather than starting over each time. return (a.aliases[0].search(filter) >= 0 && a.num_joined_members > 0); }).sort(function(a,b) { return a.num_joined_members - b.num_joined_members; }); var rows = []; var self = this; var guestRead, guestJoin; for (var i = 0; i < rooms.length; i++) { var name = rooms[i].name || rooms[i].aliases[0]; guestRead = null; guestJoin = null; var shouldPeek = false; if (rooms[i].world_readable) { guestRead = ( World Readable ); if (MatrixClientPeg.get().isGuest() && !rooms[i].guest_can_join) { shouldPeek = true; } } if (rooms[i].guest_can_join) { guestJoin = ( Guests can join ); } // rows.unshift( { name } {guestRead} {guestJoin} { rooms[i].aliases[0] } { rooms[i].num_joined_members } { rooms[i].topic } ); } return rows; }, onKeyUp: function(ev) { this.forceUpdate(); this.setState({ roomAlias : this.refs.roomAlias.value }) if (ev.key == "Enter") { this.joinRoom(this.refs.roomAlias.value); } if (ev.key == "Down") { } }, render: function() { if (this.state.loading) { var Loader = sdk.getComponent("elements.Spinner"); return (
); } var RoomHeader = sdk.getComponent('rooms.RoomHeader'); return (
{ this.getRows(this.state.roomAlias) }
RoomAliasMembers
); } });