2017-10-14 23:37:47 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Travis Ralston
|
|
|
|
|
|
|
|
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 * as sdk from "../../../index";
|
|
|
|
import MatrixClientPeg from "../../../MatrixClientPeg";
|
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
|
|
|
import Presence from "../../../Presence";
|
|
|
|
import dispatcher from "../../../dispatcher";
|
2017-10-15 04:13:46 +03:00
|
|
|
import UserSettingsStore from "../../../UserSettingsStore";
|
2017-10-15 06:44:07 +03:00
|
|
|
import * as ContextualMenu from "../../structures/ContextualMenu";
|
2017-10-14 23:37:47 +03:00
|
|
|
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'MemberPresenceAvatar',
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
member: React.PropTypes.object.isRequired,
|
|
|
|
width: React.PropTypes.number,
|
|
|
|
height: React.PropTypes.number,
|
|
|
|
resizeMethod: React.PropTypes.string,
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
width: 40,
|
|
|
|
height: 40,
|
|
|
|
resizeMethod: 'crop',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
const presenceState = this.props.member.user.presence;
|
2017-10-15 06:44:07 +03:00
|
|
|
const presenceMessage = this.props.member.user.presenceStatusMsg;
|
2017-10-14 23:37:47 +03:00
|
|
|
return {
|
|
|
|
status: presenceState,
|
2017-10-15 06:44:07 +03:00
|
|
|
message: presenceMessage,
|
2017-10-14 23:37:47 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillMount: function() {
|
|
|
|
MatrixClientPeg.get().on("User.presence", this.onUserPresence);
|
|
|
|
this.dispatcherRef = dispatcher.register(this.onAction);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
if (MatrixClientPeg.get()) {
|
|
|
|
MatrixClientPeg.get().removeListener("User.presence", this.onUserPresence);
|
|
|
|
}
|
|
|
|
dispatcher.unregister(this.dispatcherRef);
|
|
|
|
},
|
|
|
|
|
|
|
|
onAction: function(payload) {
|
|
|
|
if (payload.action !== "self_presence_updated") return;
|
|
|
|
if (this.props.member.userId !== MatrixClientPeg.get().getUserId()) return;
|
|
|
|
this.setState({
|
|
|
|
status: payload.statusInfo.presence,
|
|
|
|
message: payload.statusInfo.status_msg,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onUserPresence: function(event, user) {
|
|
|
|
if (user.userId !== MatrixClientPeg.get().getUserId()) return;
|
|
|
|
this.setState({
|
|
|
|
status: user.presence,
|
|
|
|
message: user.presenceStatusMsg,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-10-15 06:44:07 +03:00
|
|
|
onStatusChange: function(newStatus) {
|
|
|
|
console.log(this.state);
|
|
|
|
console.log(newStatus);
|
|
|
|
},
|
2017-10-14 23:37:47 +03:00
|
|
|
|
2017-10-15 06:44:07 +03:00
|
|
|
onClick: function(e) {
|
|
|
|
const PresenceContextMenu = sdk.getComponent('context_menus.PresenceContextMenu');
|
|
|
|
const elementRect = e.target.getBoundingClientRect();
|
|
|
|
|
|
|
|
// The window X and Y offsets are to adjust position when zoomed in to page
|
|
|
|
const x = (elementRect.left + window.pageXOffset) - (elementRect.width / 2) + 3;
|
|
|
|
const chevronOffset = 12;
|
|
|
|
let y = elementRect.top + (elementRect.height / 2) + window.pageYOffset;
|
|
|
|
y = y - (chevronOffset + 4); // where 4 is 1/4 the height of the chevron
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
ContextualMenu.createMenu(PresenceContextMenu, {
|
|
|
|
chevronOffset: chevronOffset,
|
|
|
|
chevronFace: 'bottom',
|
|
|
|
left: x,
|
|
|
|
top: y,
|
|
|
|
menuWidth: 300,
|
|
|
|
currentStatus: this.state.status,
|
|
|
|
onChange: this.onStatusChange,
|
2017-10-14 23:37:47 +03:00
|
|
|
});
|
2017-10-15 06:44:07 +03:00
|
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
// const presenceState = this.props.member.user.presence;
|
|
|
|
// const presenceLastActiveAgo = this.props.member.user.lastActiveAgo;
|
|
|
|
// const presenceLastTs = this.props.member.user.lastPresenceTs;
|
|
|
|
// const presenceCurrentlyActive = this.props.member.user.currentlyActive;
|
|
|
|
// const presenceMessage = this.props.member.user.presenceStatusMsg;
|
2017-10-14 23:37:47 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
const MemberAvatar = sdk.getComponent("avatars.MemberAvatar");
|
|
|
|
|
|
|
|
let onClickFn = null;
|
|
|
|
if (this.props.member.userId === MatrixClientPeg.get().getUserId()) {
|
|
|
|
onClickFn = this.onClick;
|
|
|
|
}
|
|
|
|
|
|
|
|
const avatarNode = (
|
|
|
|
<MemberAvatar member={this.props.member} width={this.props.width} height={this.props.height}
|
2017-10-15 05:23:50 +03:00
|
|
|
resizeMethod={this.props.resizeMethod} />
|
2017-10-14 23:37:47 +03:00
|
|
|
);
|
2017-10-15 04:13:46 +03:00
|
|
|
let statusNode = (
|
2017-10-15 05:23:50 +03:00
|
|
|
<span className={"mx_MemberPresenceAvatar_status mx_MemberPresenceAvatar_status_" + this.state.status} />
|
2017-10-14 23:37:47 +03:00
|
|
|
);
|
|
|
|
|
2017-10-15 04:13:46 +03:00
|
|
|
// LABS: Disable presence management functions for now
|
|
|
|
if (!UserSettingsStore.isFeatureEnabled("feature_presence_management")) {
|
|
|
|
statusNode = null;
|
|
|
|
onClickFn = null;
|
|
|
|
}
|
|
|
|
|
2017-10-14 23:37:47 +03:00
|
|
|
let avatar = (
|
|
|
|
<div className="mx_MemberPresenceAvatar">
|
2017-10-15 05:23:50 +03:00
|
|
|
{ avatarNode }
|
|
|
|
{ statusNode }
|
2017-10-14 23:37:47 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
if (onClickFn) {
|
|
|
|
avatar = (
|
|
|
|
<AccessibleButton onClick={onClickFn} className="mx_MemberPresenceAvatar" element="div">
|
2017-10-15 05:23:50 +03:00
|
|
|
{ avatarNode }
|
|
|
|
{ statusNode }
|
2017-10-14 23:37:47 +03:00
|
|
|
</AccessibleButton>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return avatar;
|
|
|
|
},
|
|
|
|
});
|