EndpointLink.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React, { PropTypes } from 'react';
  15. import Location from '../../core/Location';
  16. import Reflux from 'reflux';
  17. import ConnectionStore from '../../stores/ConnectionsStore';
  18. function isLeftClickEvent(event) {
  19. return event.button === 0;
  20. }
  21. function isModifiedEvent(event) {
  22. return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
  23. }
  24. class EndpointLink extends Reflux.Component {
  25. static propTypes = {
  26. connectionId: PropTypes.string
  27. };
  28. constructor(props) {
  29. super(props)
  30. this.store = ConnectionStore;
  31. this.state = {
  32. connection: null
  33. }
  34. }
  35. componentWillMount() {
  36. super.componentWillMount.call(this)
  37. }
  38. componentDidMount() {
  39. this.componentWillReceiveProps(this.props)
  40. }
  41. componentWillReceiveProps(props) {
  42. if (this.state && this.state.connections) {
  43. this.state.connections.forEach(connection => {
  44. if (connection.id == props.connectionId) {
  45. this.setState({ connection: connection })
  46. }
  47. })
  48. }
  49. }
  50. handleClick = (event) => {
  51. let allowTransition = true;
  52. let clickResult;
  53. if (this.props && this.props.onClick) {
  54. clickResult = this.props.onClick(event);
  55. }
  56. if (isModifiedEvent(event) || !isLeftClickEvent(event)) {
  57. return;
  58. }
  59. if (clickResult === false || event.defaultPrevented === true) {
  60. allowTransition = false;
  61. }
  62. event.preventDefault();
  63. if (allowTransition) {
  64. const link = event.currentTarget;
  65. if (this.props && this.props.to) {
  66. Location.push(this.props.to);
  67. } else {
  68. Location.push({ pathname: link.pathname, search: link.search });
  69. }
  70. }
  71. };
  72. render() {
  73. const { to, ...props } = this.props; // eslint-disable-line no-use-before-define
  74. if (this.state && this.state.connection) {
  75. return (<a
  76. href={Location.createHref(`/cloud-endpoints/${this.state.connection.id}`)}
  77. {...props}
  78. onClick={this.handleClick}
  79. >{this.state.connection.name}</a>)
  80. } else {
  81. return <div><span className="taskIcon ERROR" />Endpoint not found</div>
  82. }
  83. }
  84. }
  85. export default EndpointLink;