summaryrefslogtreecommitdiff
path: root/ui/app/components/main_panel/account_list.js
blob: bb3263aaa65ff7f4da7c2d096dc2c5c82c28e8e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import React from 'react'
import {Button, ButtonGroup, ButtonToolbar, Glyphicon} from 'react-bootstrap'

import App from 'app'
import Account from 'models/account'
import Confirmation from 'components/confirmation'
import AppButtons from './app_buttons'
import bitmask from 'lib/bitmask'

export default class AccountList extends React.Component {

  static get defaultProps() {return{
    account: null,
    accounts: [],
    onAdd: null,
    onRemove: null,
    onSelect: null
  }}

  constructor(props) {
    super(props)

    this.state = {
      mode: 'expanded',
      showRemoveConfirmation: false,
      activeVpnDomain: null,
      activeVpnStatus: null
    }

    // prebind:
    this.select = this.select.bind(this)
    this.add    = this.add.bind(this)
    this.remove = this.remove.bind(this)
    this.askRemove = this.askRemove.bind(this)
    this.cancelRemove = this.cancelRemove.bind(this)
    this.expand = this.expand.bind(this)
    this.collapse = this.collapse.bind(this)
    this.statusEvent = this.statusEvent.bind(this)
  }

  componentWillMount() {
    // Listen to state changes so we can update the vpn status icon
    bitmask.events.register("VPN_STATUS_CHANGED", 'account list update', this.statusEvent)
    this.updateStatus()
  }

  componentWillUnmount() {
    bitmask.events.unregister("VPN_STATUS_CHANGED", 'acconut list update')
  }

  statusEvent() {
    this.updateStatus()
  }

  /*
   * Simple check to update the VPN status icon. We assume that if it's not up or
   * connecting, it's down.
   */
  updateStatus() {
    bitmask.vpn.status().then(
      vpn => {
        this.setState({'activeVpnDomain': vpn.domain})
        if (vpn.status == "on") {
          this.setState({'activeVpnState': "up"})
        } else if (vpn.status == "starting") {
          this.setState({'activeVpnState': "connecting"})
        } else {
          this.setState({'activeVpnState': "down"})
        }
      }
    )
  }

  select(e) {
    let account = this.props.accounts.find(
      account => account.id == e.currentTarget.dataset.id
    )
    if (this.props.onSelect) {
      this.props.onSelect(account)
    }
  }

  add() {
    App.show('wizard')
  }

  remove() {
    this.setState({showRemoveConfirmation: false})
    if (this.props.onRemove) {
      this.props.onRemove(this.props.account)
    }
  }

  askRemove() {
    this.setState({showRemoveConfirmation: true})
  }
  cancelRemove() {
    this.setState({showRemoveConfirmation: false})
  }

  expand() {
    this.setState({mode: 'expanded'})
  }

  collapse() {
    this.setState({mode: 'collapsed'})
  }

  render() {
    let style = {}
    let expandButton = null
    let plusminusButtons = null
    let removeModal = null

    if (this.state.mode == 'expanded') {
      expandButton = (
        <Button onClick={this.collapse} className="expander btn-inverse btn-flat pull-right">
          <Glyphicon glyph="triangle-left" />
        </Button>
      )
      let removeDisabled = !this.props.account || this.props.account.authenticated
      plusminusButtons = (
        <ButtonGroup style={style}>
          <Button onClick={this.add} className="btn-inverse">
            <Glyphicon glyph="plus" />
          </Button>
          <Button disabled={removeDisabled} onClick={this.askRemove} className="btn-inverse">
            <Glyphicon glyph="minus" />
          </Button>
        </ButtonGroup>
      )
    } else {
      style.width = '60px'
      expandButton = (
        <Button onClick={this.expand} className="expander btn-inverse btn-flat pull-right">
          <Glyphicon glyph="triangle-right" />
        </Button>
      )
    }

    if (this.state.showRemoveConfirmation) {
      let domain = this.props.account.domain
      let title = `Are you sure you wish to remove ${domain}?`
      removeModal = (
        <Confirmation title={title} onAccept={this.remove} onCancel={this.cancelRemove} />
      )
    }

    let items = this.props.accounts.map((account, i) => {
      let className = account == this.props.account ? 'active' : 'inactive'
      let icon = null;
      if (account.domain === this.state.activeVpnDomain){
        icon = <Glyphicon glyph="lock" />
      }
      return (
        <li key={i} className={className} onClick={this.select} data-id={account.id}>
          <div className="account-details">
            <span className="username">{account.userpart}</span>
            <span className="domain">{account.domain}</span>
          </div>
          <div className={`vpn-status ${this.state.activeVpnState}`}>
            {icon}
          </div>
          <span className="arc top"></span>
          <span className="arc bottom"></span>
        </li>
      )
    })

    expandButton = null // for now, disable expand  button

    return (
      <div className="accounts" style={style}>
        <AppButtons />
        <ul>
          {items}
        </ul>
        <ButtonToolbar>
          {plusminusButtons}
          {expandButton}
        </ButtonToolbar>
        {removeModal}
      </div>
    )
  }


}