|
| 1 | +/* |
| 2 | +Copyright 2015, 2016 OpenMarket Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +var q = require("q"); |
| 20 | +var React = require('react'); |
| 21 | +var classNames = require('classnames'); |
| 22 | +var MatrixClientPeg = require('matrix-react-sdk/lib/MatrixClientPeg'); |
| 23 | +var dis = require('matrix-react-sdk/lib/dispatcher'); |
| 24 | + |
| 25 | +module.exports = React.createClass({ |
| 26 | + displayName: 'RoomTagContextMenu', |
| 27 | + |
| 28 | + propTypes: { |
| 29 | + room: React.PropTypes.object.isRequired, |
| 30 | + /* callback called when the menu is dismissed */ |
| 31 | + onFinished: React.PropTypes.func, |
| 32 | + }, |
| 33 | + |
| 34 | + getInitialState: function() { |
| 35 | + return { |
| 36 | + isFavourite: this.props.room.tags.hasOwnProperty("m.favourite"), |
| 37 | + isLowPriority: this.props.room.tags.hasOwnProperty("m.lowpriority"), |
| 38 | + }; |
| 39 | + }, |
| 40 | + |
| 41 | + _toggleTag: function(tagNameOn, tagNameOff) { |
| 42 | + var self = this; |
| 43 | + const roomId = this.props.room.roomId; |
| 44 | + var cli = MatrixClientPeg.get(); |
| 45 | + if (!cli.isGuest()) { |
| 46 | + q.delay(500).then(function() { |
| 47 | + if (tagNameOff !== null && tagNameOff !== undefined) { |
| 48 | + cli.deleteRoomTag(roomId, tagNameOff).finally(function() { |
| 49 | + // Close the context menu |
| 50 | + if (self.props.onFinished) { |
| 51 | + self.props.onFinished(); |
| 52 | + }; |
| 53 | + }).fail(function(err) { |
| 54 | + var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); |
| 55 | + Modal.createDialog(ErrorDialog, { |
| 56 | + title: "Failed to remove tag " + tagNameOff + " from room", |
| 57 | + description: err.toString() |
| 58 | + }); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + if (tagNameOn !== null && tagNameOn !== undefined) { |
| 63 | + // If the tag ordering meta data is required, it is added by |
| 64 | + // the RoomSubList when it sorts its rooms |
| 65 | + cli.setRoomTag(roomId, tagNameOn, {}).finally(function() { |
| 66 | + // Close the context menu |
| 67 | + if (self.props.onFinished) { |
| 68 | + self.props.onFinished(); |
| 69 | + }; |
| 70 | + }).fail(function(err) { |
| 71 | + var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); |
| 72 | + Modal.createDialog(ErrorDialog, { |
| 73 | + title: "Failed to add tag " + tagNameOn + " to room", |
| 74 | + description: err.toString() |
| 75 | + }); |
| 76 | + }); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + }, |
| 81 | + |
| 82 | + _onClickFavourite: function() { |
| 83 | + // Tag room as 'Favourite' |
| 84 | + if (!this.state.isFavourite && this.state.isLowPriority) { |
| 85 | + this.setState({ |
| 86 | + isFavourite: true, |
| 87 | + isLowPriority: false, |
| 88 | + }); |
| 89 | + this._toggleTag("m.favourite", "m.lowpriority"); |
| 90 | + } else if (this.state.isFavourite) { |
| 91 | + this.setState({isFavourite: false}); |
| 92 | + this._toggleTag(null, "m.favourite"); |
| 93 | + } else if (!this.state.isFavourite) { |
| 94 | + this.setState({isFavourite: true}); |
| 95 | + this._toggleTag("m.favourite"); |
| 96 | + } |
| 97 | + }, |
| 98 | + |
| 99 | + _onClickLowPriority: function() { |
| 100 | + // Tag room as 'Low Priority' |
| 101 | + if (!this.state.isLowPriority && this.state.isFavourite) { |
| 102 | + this.setState({ |
| 103 | + isFavourite: false, |
| 104 | + isLowPriority: true, |
| 105 | + }); |
| 106 | + this._toggleTag("m.lowpriority", "m.favourite"); |
| 107 | + } else if (this.state.isLowPriority) { |
| 108 | + this.setState({isLowPriority: false}); |
| 109 | + this._toggleTag(null, "m.lowpriority"); |
| 110 | + } else if (!this.state.isLowPriority) { |
| 111 | + this.setState({isLowPriority: true}); |
| 112 | + this._toggleTag("m.lowpriority"); |
| 113 | + } |
| 114 | + }, |
| 115 | + |
| 116 | + _onClickLeave: function() { |
| 117 | + // Leave room |
| 118 | + dis.dispatch({ |
| 119 | + action: 'leave_room', |
| 120 | + room_id: this.props.room.roomId, |
| 121 | + }); |
| 122 | + |
| 123 | + // Close the context menu |
| 124 | + if (this.props.onFinished) { |
| 125 | + this.props.onFinished(); |
| 126 | + }; |
| 127 | + }, |
| 128 | + |
| 129 | + render: function() { |
| 130 | + var myUserId = MatrixClientPeg.get().credentials.userId; |
| 131 | + var myMember = this.props.room.getMember(myUserId); |
| 132 | + |
| 133 | + var favouriteClasses = classNames({ |
| 134 | + 'mx_RoomTagContextMenu_field': true, |
| 135 | + 'mx_RoomTagContextMenu_fieldSet': this.state.isFavourite, |
| 136 | + 'mx_RoomTagContextMenu_fieldDisabled': false, |
| 137 | + }); |
| 138 | + |
| 139 | + var lowPriorityClasses = classNames({ |
| 140 | + 'mx_RoomTagContextMenu_field': true, |
| 141 | + 'mx_RoomTagContextMenu_fieldSet': this.state.isLowPriority, |
| 142 | + 'mx_RoomTagContextMenu_fieldDisabled': false, |
| 143 | + }); |
| 144 | + |
| 145 | + var leaveClasses = classNames({ |
| 146 | + 'mx_RoomTagContextMenu_field': true, |
| 147 | + 'mx_RoomTagContextMenu_fieldSet': false, |
| 148 | + 'mx_RoomTagContextMenu_fieldDisabled': false, |
| 149 | + }); |
| 150 | + |
| 151 | + return ( |
| 152 | + <div> |
| 153 | + <div className={ favouriteClasses } onClick={this._onClickFavourite} > |
| 154 | + <img className="mx_RoomTagContextMenu_icon" src="img/icon_context_fave.svg" width="15" height="15" /> |
| 155 | + <img className="mx_RoomTagContextMenu_icon_set" src="img/icon_context_fave_on.svg" width="15" height="15" /> |
| 156 | + Favourite |
| 157 | + </div> |
| 158 | + <div className={ lowPriorityClasses } onClick={this._onClickLowPriority} > |
| 159 | + <img className="mx_RoomTagContextMenu_icon" src="img/icon_context_low.svg" width="15" height="15" /> |
| 160 | + <img className="mx_RoomTagContextMenu_icon_set" src="img/icon_context_low_on.svg" width="15" height="15" /> |
| 161 | + Low Priority |
| 162 | + </div> |
| 163 | + <hr className="mx_RoomTagContextMenu_separator" /> |
| 164 | + <div className={ leaveClasses } onClick={(myMember && myMember.membership === "join") ? this._onClickLeave : null} > |
| 165 | + <img className="mx_RoomTagContextMenu_icon" src="img/icon_context_delete.svg" width="15" height="15" /> |
| 166 | + Leave |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + ); |
| 170 | + } |
| 171 | +}); |
0 commit comments