|
| 1 | +// Import Third-party Dependencies |
| 2 | +import { LitElement, html, css, nothing } from "lit"; |
| 3 | +import { repeat } from "lit/directives/repeat.js"; |
| 4 | + |
| 5 | +// Import Internal Dependencies |
| 6 | +import * as utils from "../../common/utils.js"; |
| 7 | +import "../icon/icon.js"; |
| 8 | + |
| 9 | +/** |
| 10 | + * @typedef {Object} PackageMetadata |
| 11 | + * @property {string} spec - Package spec (e.g. "package@1.0.0") |
| 12 | + * @property {string} scanType - Type of scan ("cwd" for local) |
| 13 | + * @property {string} locationOnDisk - Path to the package on disk |
| 14 | + * @property {number} lastUsedAt - Timestamp of last usage |
| 15 | + * @property {string | null} integrity - Package integrity hash |
| 16 | + */ |
| 17 | + |
| 18 | +class PackageNavigation extends LitElement { |
| 19 | + static styles = css` |
| 20 | + b,p { |
| 21 | + margin: 0; |
| 22 | + padding: 0; |
| 23 | + border: 0; |
| 24 | + font: inherit; |
| 25 | + font-size: 100%; |
| 26 | + } |
| 27 | +
|
| 28 | + :host { |
| 29 | + z-index: 30; |
| 30 | + display: flex; |
| 31 | + justify-content: center; |
| 32 | + align-items: center; |
| 33 | + height: 30px; |
| 34 | + left: 50px; |
| 35 | + padding-left: 20px; |
| 36 | + max-width: calc(100vw - 70px); |
| 37 | + box-sizing: border-box; |
| 38 | + background: var(--primary); |
| 39 | + box-shadow: 2px 1px 10px #26107f7a; |
| 40 | + } |
| 41 | +
|
| 42 | + :host-context(body.dark) { |
| 43 | + background: var(--dark-theme-primary-color); |
| 44 | + } |
| 45 | +
|
| 46 | + .packages { |
| 47 | + height: 30px; |
| 48 | + display: flex; |
| 49 | + background: var(--primary); |
| 50 | + } |
| 51 | +
|
| 52 | + .packages > .package { |
| 53 | + height: 30px; |
| 54 | + font-family: mononoki; |
| 55 | + display: flex; |
| 56 | + align-items: center; |
| 57 | + background: linear-gradient(to right, rgb(55 34 175 / 100%) 0%, rgb(87 74 173 / 100%) 50%, rgb(59 110 205) 100%); |
| 58 | + padding: 0 10px; |
| 59 | + border-right: 2px solid #0f041a; |
| 60 | + text-shadow: 1px 1px 10px #000; |
| 61 | + color: #def7ff; |
| 62 | + } |
| 63 | +
|
| 64 | + :host-context(body.dark) .packages > .package { |
| 65 | + background: linear-gradient(to right, rgb(11 3 31 / 100%) 0%, rgb(11 3 31 / 80%) 50%, rgb(11 3 31 / 60%) 100%); |
| 66 | + } |
| 67 | +
|
| 68 | + .packages > .package > * { |
| 69 | + transform: skewX(20deg); |
| 70 | + } |
| 71 | +
|
| 72 | + .packages > .package:first-child { |
| 73 | + padding-left: 10px; |
| 74 | + } |
| 75 | +
|
| 76 | + .packages > .package:not(.active):hover { |
| 77 | + background: linear-gradient(to right, rgb(55 34 175 / 100%) 1%, rgb(68 121 218) 100%); |
| 78 | + color: #defff9; |
| 79 | + cursor: pointer; |
| 80 | + } |
| 81 | +
|
| 82 | + :host-context(body.dark) .packages > .package:not(.active):hover { |
| 83 | + background: linear-gradient(to right, rgb(11 3 31 / 70%) 1%, rgb(11 3 31 / 50%) 100%); |
| 84 | + } |
| 85 | +
|
| 86 | + .packages > .package.active { |
| 87 | + background: linear-gradient(to right, rgb(55 34 175 / 100%) 0%, rgb(87 74 173 / 100%) 50%, rgb(59 110 205) 100%); |
| 88 | + } |
| 89 | +
|
| 90 | + .packages > .package.active > b { |
| 91 | + background: var(--secondary); |
| 92 | + } |
| 93 | +
|
| 94 | + .packages > .package.active > .remove { |
| 95 | + display: block; |
| 96 | + } |
| 97 | +
|
| 98 | + .packages > .package > b:last-of-type:not(:first-of-type) { |
| 99 | + background: #f57c00; |
| 100 | + } |
| 101 | +
|
| 102 | + .packages > .package > b { |
| 103 | + font-weight: bold; |
| 104 | + font-size: 12px; |
| 105 | + margin-left: 5px; |
| 106 | + background: var(--secondary-darker); |
| 107 | + padding: 3px 5px; |
| 108 | + border-radius: 2px; |
| 109 | + font-family: Roboto; |
| 110 | + letter-spacing: 1px; |
| 111 | + } |
| 112 | +
|
| 113 | + .add { |
| 114 | + height: 30px; |
| 115 | + font-size: 20px; |
| 116 | + border: none; |
| 117 | + background: var(--secondary-darker); |
| 118 | + cursor: pointer; |
| 119 | + padding: 0 7px; |
| 120 | + transition: 0.2s all ease; |
| 121 | + color: #def7ff; |
| 122 | + } |
| 123 | +
|
| 124 | + .add:hover { |
| 125 | + background: var(--secondary); |
| 126 | + cursor: pointer; |
| 127 | + } |
| 128 | +
|
| 129 | + .add > i { |
| 130 | + transform: skewX(20deg); |
| 131 | + } |
| 132 | +
|
| 133 | + button.remove { |
| 134 | + display: none; |
| 135 | + border: none; |
| 136 | + position: relative; |
| 137 | + cursor: pointer; |
| 138 | + color: #fff5dc; |
| 139 | + background: #ff3434e2; |
| 140 | + margin-left: 10px; |
| 141 | + border-radius: 50%; |
| 142 | + line-height: 16px; |
| 143 | + text-shadow: 1px 1px 10px #000; |
| 144 | + font-weight: bold; |
| 145 | + width: 20px; |
| 146 | + } |
| 147 | +
|
| 148 | + button.remove:hover { |
| 149 | + cursor: pointer; |
| 150 | + background: #ff5353e2; |
| 151 | + } |
| 152 | + `; |
| 153 | + |
| 154 | + static properties = { |
| 155 | + /** |
| 156 | + * Array of package metadata objects |
| 157 | + * @type {PackageMetadata[]} |
| 158 | + */ |
| 159 | + metadata: { type: Array }, |
| 160 | + /** |
| 161 | + * Currently active package spec |
| 162 | + * @type {string} |
| 163 | + */ |
| 164 | + activePackage: { type: String } |
| 165 | + }; |
| 166 | + |
| 167 | + constructor() { |
| 168 | + super(); |
| 169 | + /** @type {PackageMetadata[]} */ |
| 170 | + this.metadata = []; |
| 171 | + /** @type {string} */ |
| 172 | + this.activePackage = ""; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Check if there are at least 2 packages |
| 177 | + * @returns {boolean} |
| 178 | + */ |
| 179 | + get #hasAtLeast2Packages() { |
| 180 | + return this.metadata.length > 1; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Handle click on a package to select it |
| 185 | + * @param {string} spec |
| 186 | + */ |
| 187 | + #handlePackageClick(spec) { |
| 188 | + if (this.activePackage !== spec) { |
| 189 | + window.socket.commands.search(spec); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Handle click on remove button |
| 195 | + * @param {Event} event |
| 196 | + * @param {string} packageName |
| 197 | + */ |
| 198 | + #handleRemoveClick(event, packageName) { |
| 199 | + event.stopPropagation(); |
| 200 | + window.socket.commands.remove(packageName); |
| 201 | + } |
| 202 | + |
| 203 | + #handleAddClick() { |
| 204 | + window.navigation.setNavByName("search--view"); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Render a single package element |
| 209 | + * @param {PackageMetadata} param0 |
| 210 | + * @returns {import("lit").TemplateResult} |
| 211 | + */ |
| 212 | + #renderPackage({ spec, scanType }) { |
| 213 | + const isLocal = scanType === "cwd"; |
| 214 | + const { name, version } = utils.parseNpmSpec(spec); |
| 215 | + const isActive = spec === this.activePackage; |
| 216 | + |
| 217 | + return html` |
| 218 | + <div |
| 219 | + class="package ${isActive ? "active" : ""}" |
| 220 | + data-name="${spec}" |
| 221 | + @click="${() => this.#handlePackageClick(spec)}" |
| 222 | + > |
| 223 | + <p>${name}</p> |
| 224 | + <b>v${version}</b> |
| 225 | + ${isLocal ? html`<b>local</b>` : nothing} |
| 226 | + ${this.#hasAtLeast2Packages |
| 227 | + ? html` |
| 228 | + <button |
| 229 | + class="remove" |
| 230 | + @click="${(e) => this.#handleRemoveClick(e, spec)}" |
| 231 | + > |
| 232 | + x |
| 233 | + </button> |
| 234 | + ` |
| 235 | + : nothing} |
| 236 | + </div> |
| 237 | + `; |
| 238 | + } |
| 239 | + |
| 240 | + render() { |
| 241 | + if (this.metadata.length === 0) { |
| 242 | + return nothing; |
| 243 | + } |
| 244 | + |
| 245 | + return html` |
| 246 | + <div class="packages"> |
| 247 | + ${repeat( |
| 248 | + this.metadata, |
| 249 | + (pkg) => pkg.spec, |
| 250 | + (pkg) => this.#renderPackage(pkg) |
| 251 | + )} |
| 252 | + </div> |
| 253 | + <button class="add" @click="${this.#handleAddClick}"> |
| 254 | + <p>+</p> |
| 255 | + </button> |
| 256 | + `; |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +customElements.define("package-navigation", PackageNavigation); |
0 commit comments