This repository was archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathLibraryItemView.tsx
176 lines (160 loc) · 6.49 KB
/
LibraryItemView.tsx
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as React from "react";
import { Button, DropdownButton, MenuItem } from "react-bootstrap";
import { versionCompare } from "../../../common/sharedUtilities/utils";
import * as API from "../actions/api";
import * as util from "../utils/util";
interface ILibraryProps extends React.Props<any> {
library: any;
installLibrary: (libraryName, version, callback) => void;
uninstallLibrary: (libraryName, libraryPath, callback) => void;
}
interface ILibraryState {
version: string;
isInstalling: boolean;
isUninstalling: boolean;
}
export default class LibraryItemView extends React.Component<ILibraryProps, ILibraryState> {
private renderedElement = null;
private library = null;
private previousState = { version: "", isInstalling: false, isUninstalling: false};
constructor(props) {
super(props);
this.state = {
version: props.library.installed ? "" : props.library.versions[0],
isInstalling: false,
isUninstalling: false,
};
this.versionUpdate = this.versionUpdate.bind(this);
}
public render() {
const compareKeys = ["name", "installed", "version"];
// Cache render element to improve perfomance.
if (!this.renderedElement || !util.shallowEqual(this.library, this.props.library, compareKeys)
|| !util.shallowEqual(this.previousState, this.state)) {
this.library = this.props.library;
this.previousState = { ...this.state };
this.renderedElement = this._render();
}
return this.renderedElement;
}
private _render() {
return (<div className="listitem theme-listitem">
{ this.buildLibrarySectionHeader(this.props.library) }
{ this.buildLibrarySectionBody(this.props.library) }
{ this.buildLibrarySectionButtons(this.props.library) }
</div>);
}
private versionUpdate(eventKey: any, event?: React.SyntheticEvent<{}>): void {
this.setState({
version: eventKey,
});
}
private openLink(url) {
API.openLink(url);
}
private installLibrary(libraryName, version) {
this.setState({
isInstalling: true,
});
this.props.installLibrary(libraryName, version, () => {
this.setState({
isInstalling: false,
});
});
}
private uninstallLibrary(libraryName, libraryPath) {
this.setState({
isUninstalling: true,
});
this.props.uninstallLibrary(libraryName, libraryPath, () => {
this.setState({
isUninstalling: false,
});
});
}
private addLibPath(srcPath) {
API.addLibPath(srcPath);
}
private buildLibrarySectionHeader(lib) {
return (<div><span className="listitem-header">{lib.name}</span>
{
lib.builtIn && (<span> Built-In </span>)
}
{
lib.author && (<span className="listitem-author"> by <span className="listitem-header">{lib.author}</span></span>)
}
{
lib.installed && (
<span className="listitem-author"> Version {lib.version || "Unknown"} <span className="listitem-installed-header">
INSTALLED</span>
</span>
)
}
</div>);
}
private buildLibrarySectionBody(lib) {
return (<div>
<span className="listitem-header">{lib.sentence}</span> {lib.paragraph}
{
lib.website && (<div><a className="help-link" onClick={() => this.openLink(lib.website)}>More info</a></div>)
}
</div>);
}
private buildLibrarySectionButtons(lib) {
return (<div className="listitem-footer">
{
this.state.isInstalling && (<div className="toolbar-mask theme-bgcolor">Installing...</div>)
}
{
this.state.isUninstalling && (<div className="toolbar-mask theme-bgcolor">Removing</div>)
}
{
lib.installed && (
<div className="right-side">
{
lib.supported && (
<Button className="operation-btn" onClick={() => this.addLibPath(lib.srcPath)}>Include Library</Button>
)
}
{
lib.versions && lib.versions.length && versionCompare(lib.versions[0], lib.version) > 0 && (
<Button className="operation-btn" onClick={() => this.installLibrary(lib.name, lib.versions[0])}>Update</Button>
)
}
{
!lib.builtIn && (
<Button className="operation-btn"onClick={() => this.uninstallLibrary(lib.name, lib.installedPath)}>Remove</Button>
)
}
</div>
)
}
{
lib.versions && lib.versions.length > 1 && (
<div className="left-side">
<DropdownButton id="versionselector" title={this.state.version || "Select version"}
placeholder="Select version" onSelect={this.versionUpdate}>
{ lib.versions.map((v, index) => {
if (v === lib.version) {
return "";
}
return (<MenuItem key={index} eventKey={v} active={v === this.state.version}>{v}</MenuItem>);
})}
</DropdownButton>
<Button className="operation-btn" disabled={!this.state.version}
onClick={() => this.installLibrary(lib.name, this.state.version)}>Install</Button>
</div>
)
}
{
lib.versions && lib.versions.length === 1 && !lib.installed && (
<div className="left-side">
<Button className="operation-btn" onClick={() => this.installLibrary(lib.name, lib.versions[0])}>Install</Button>
</div>
)
}
</div>);
}
}