forked from OutlineFoundation/outline-apps
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver_card.ts
More file actions
272 lines (254 loc) · 8.41 KB
/
server_card.ts
File metadata and controls
272 lines (254 loc) · 8.41 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
Copyright 2021 The Outline Authors
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.
*/
import {computed, customElement, property} from '@polymer/decorators';
import {html, PolymerElement} from '@polymer/polymer';
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin';
import {ServerConnectionState} from './server_connection_viz';
@customElement('server-card') export class ServerCard extends LegacyElementMixin
(PolymerElement) {
static template = html`
<style>
:host {
display: block;
}
:focus {
/* Disable outline for focused elements; mainly affects the iOS WebView,
* where elements stay highlighted after user interaction.
*/
outline: 0 !important;
}
paper-card {
width: 100%;
}
paper-menu-button {
color: #5f6368;
}
paper-item {
white-space: nowrap;
}
paper-item:not([disabled]) {
cursor: pointer;
}
.card-header {
display: flex;
}
.card-content {
text-align: center;
padding: 10% 0;
}
.card-header server-connection-viz {
padding: 16px 0 0 16px;
}
#serverInfo {
flex: 1;
padding: 16px 0 0 16px;
font-size: 20px;
/* Make the sever name and address copyable */
-webkit-user-select: text; /* Safari */
-ms-user-select: text; /* IE/Edge */
user-select: text; /* Chrome */
}
#serverName {
line-height: 32px;
word-break: break-word;
}
#serverAddress {
color: rgba(0, 0, 0, 0.54);
font-size: small;
overflow: hidden;
text-overflow: ellipsis;
}
#server-visualization-button {
background: none;
border-radius: 100px;
margin: 0;
padding: 3px 3px 0;
}
.status-message {
color: var(--disabled-text-color);
font-size: small;
font-weight: normal;
margin: 12px 0;
text-transform: capitalize;
}
.card-actions {
display: flex;
align-items: center;
border-radius: 0 0 2px 2px;
border-top: none;
}
.expanded .card-actions {
background-color: var(--paper-grey-50);
border-top: 1px solid #e8e8e8;
}
#connectButton {
color: #2fbea5;
font-weight: bold;
}
#connectButton[disabled] {
color: var(--disabled-text-color);
background: transparent;
}
#errorMessage {
color: #f44336;
margin-right: auto;
}
@media (max-width: 360px) {
#serverInfo {
font-size: 18px;
}
#serverName {
line-height: 24px;
}
}
@media (min-height: 600px) {
.card-content {
padding: 20% 0;
}
}
</style>
<paper-card class\$="[[expandedClassName]]">
<div class="card-header">
<server-connection-viz
state="[[state]]"
root-path="[[rootPath]]"
hidden\$="[[expanded]]"
></server-connection-viz>
<div id="serverInfo">
<div id="serverName">[[localizedServerName]]</div>
<div id="serverAddress">[[serverAddress]]</div>
</div>
<paper-menu-button horizontal-align="right" close-on-activate="true">
<paper-icon-button
icon="icons:more-vert"
slot="dropdown-trigger"
></paper-icon-button>
<paper-listbox
id="menu"
slot="dropdown-content"
on-iron-activate="onMenuItemPressed"
attr-for-selected="name"
>
<paper-item name="rename">[[localize('server-rename')]]</paper-item>
<paper-item name="forget">[[localize('server-forget')]]</paper-item>
</paper-listbox>
</paper-menu-button>
</div>
<div class="card-content" hidden\$="[[!expanded]]">
<div>
<paper-button
id="server-visualization-button"
on-tap="onConnectToggled"
disabled\$="[[connectButtonDisabled]]"
noink=""
>
<server-connection-viz
state="[[state]]"
root-path="[[rootPath]]"
expanded=""
></server-connection-viz>
</paper-button>
</div>
<div class\$="status-message [[state]]">[[statusMessage]]</div>
</div>
<div class="card-actions">
<div id="errorMessage">[[errorMessage]]</div>
<paper-button
id="connectButton"
on-tap="onConnectToggled"
disabled\$="[[connectButtonDisabled]]"
>
[[connectButtonLabel]]
</paper-button>
</div>
</paper-card>
`;
@property({type: Boolean}) disabled: boolean;
@property({type: String}) errorMessage: string;
@property({type: Boolean}) expanded = false;
@property({type: Boolean}) isOutlineServer = true;
@property({type: String}) rootPath: string;
@property({type: String}) serverAddress: string;
@property({type: String}) serverId: string;
@property({type: String}) serverName: string;
@property({type: String}) state: ServerConnectionState = ServerConnectionState.DISCONNECTED;
// Need to declare localize function passed in from parent, or else
// localize() calls within the template won't be updated.
// @polymer/decorators doesn't support Function constructors...
@property({type: Object}) localize: (messageId: string) => string;
@computed('serverName', 'isOutlineServer', 'localize')
get localizedServerName() {
if (this.serverName.length) {
return this.serverName;
}
return this.localize(this.isOutlineServer ? 'server-default-name-outline' : 'server-default-name');
}
@computed('state', 'localize')
get statusMessage() {
if (!this.localize) return '';
switch (this.state) {
case ServerConnectionState.CONNECTING:
return this.localize('connecting-server-state');
case ServerConnectionState.CONNECTED:
return this.localize('connected-server-state');
case ServerConnectionState.RECONNECTING:
return this.localize('reconnecting-server-state');
case ServerConnectionState.DISCONNECTING:
return this.localize('disconnecting-server-state');
case ServerConnectionState.DISCONNECTED:
default:
return this.localize('disconnected-server-state');
}
}
@computed('state', 'localize')
get connectButtonLabel() {
if (!this.localize) return '';
switch (this.state) {
case ServerConnectionState.CONNECTING:
case ServerConnectionState.CONNECTED:
case ServerConnectionState.RECONNECTING:
return this.localize('disconnect-button-label');
case ServerConnectionState.DISCONNECTING:
case ServerConnectionState.DISCONNECTED:
default:
return this.localize('connect-button-label');
}
}
@computed('state')
get connectButtonDisabled() {
return (
this.disabled || this.state === ServerConnectionState.CONNECTING ||
this.state === ServerConnectionState.DISCONNECTING);
}
@computed('expanded')
get expandedClassName() {
return this.expanded ? 'expanded' : '';
}
protected onConnectToggled() {
const {serverId} = this;
this.state === ServerConnectionState.DISCONNECTED ? this.fire('ConnectPressed', {serverId}) :
this.fire('DisconnectPressed', {serverId});
}
protected onMenuItemPressed({detail: {selected}}: CustomEvent) {
const {serverName, serverId} = this;
if (selected === 'forget') {
this.fire('ForgetPressed', {serverId});
} else if (selected === 'rename') {
this.fire('ShowServerRename', {serverName, serverId});
}
// This can leave the pressed paper-item in the selected state,
// causing it to get selected styling (e.g. font-weight: bold),
// so explicitly deselect it:
this.async(() => (this.$.menu as HTMLInputElement).select());
}
}