import { LitElement, html, css } from 'lit-element'; import { wrapCss } from './misc'; import prettyBytes from 'pretty-bytes'; import fasCopy from '@fortawesome/fontawesome-free/svgs/regular/copy.svg'; import fasArrowUp from '@fortawesome/fontawesome-free/svgs/solid/angle-double-up.svg'; import fasArrowDown from '@fortawesome/fontawesome-free/svgs/solid/angle-double-down.svg'; import fasSearch from '@fortawesome/fontawesome-free/svgs/solid/search.svg'; // =========================================================================== class CollIndex extends LitElement { constructor() { super(); this.colls = []; this.filteredColls = []; this.sortedColls = []; this.query = ""; this.hideHeader = localStorage.getItem("index:hideHeader") === "1"; this._deleting = {}; } static get sortKeys() { return [ {key: "title", name: "Title"}, {key: "sourceUrl", name: "Source"}, {key: "ctime", name: "Date Loaded"}, {key: "size", name: "Total Size"} ]; } static get properties() { return { colls: { type: Array }, query: { type: String }, filteredColls: { type: Array }, sortedColls: { type: Array }, hideHeader: { type: Boolean }, _deleting: { type: Object } } } firstUpdated() { this.loadColls(); } updated(changedProperties) { if (changedProperties.has("hideHeader")) { localStorage.setItem("index:hideHeader", this.hideHeader ? "1" : "0"); } if (changedProperties.has("colls") || changedProperties.has("query")) { this.filter(); } } filter() { if (!this.query) { this.filteredColls = this.colls; return; } this.filteredColls = []; for (const coll of this.colls) { if (coll.sourceUrl.indexOf(this.query) >= 0 || coll.filename.indexOf(this.query) >= 0 || (coll.title && coll.title.indexOf(this.query) >= 0)) { this.filteredColls.push(coll); } } } async loadColls() { const resp = await fetch("./wabac/api/index"); try { const json = await resp.json(); this.colls = json.colls.map((coll) => { coll.title = coll.title || coll.filename; return coll; }); } catch (e) { // likely no sw registered yet } this._deleting = {}; } async onDeleteColl(event) { event.preventDefault(); event.stopPropagation(); if (!this.sortedColls) { return; } const index = Number(event.currentTarget.getAttribute("data-coll-index")); const coll = this.sortedColls[index]; if (!coll || this._deleting[coll.sourceUrl]) { return; } this._deleting[coll.sourceUrl] = true; this.requestUpdate(); const resp = await fetch(`./wabac/api/${coll.id}`, {method: 'DELETE'}); if (resp.status === 200) { const json = await resp.json(); this.colls = json.colls; } return false; } static get styles() { return wrapCss(css` :host { overflow-y: auto; } .size { margin-right: 20px; } .extra-padding { padding: 2em; } .no-top-padding { padding-top: 1.0em; } button.is-loading { line-height: 1.5em; height: 1.5em; border: 0px; background-color: transparent !important; width: auto; } nav.panel.is-light { margin-bottom: 2em; } fa-icon { vertical-align: middle; } .copy { color: black; margin: 0px; margin: 0; line-height: 0.4em; padding: 6px; border-radius: 10px; display: none; position: absolute; } .copy:active { background-color: lightgray; } .sort-header { padding: 0.3rem 0.3rem 0.3rem 0; display: flex; flex-direction: row; flex-flow: row wrap; } .sort-header .control { flex: auto; padding-left: 0.3rem; width: initial; } wr-sorter { padding: 0.3rem; } a.button.is-small.collapse { border-radius: 6px; } .icon.is-left { margin-left: 0.5rem; } .columns { width: 100vw; } .column { word-break: break-word; position: relative; } .col-title:hover { } .col-title a { display: block; height: 100%; } .column:hover > .copy, .source-text:hover + .copy, .copy:hover { display: inline; } .coll-block { position: relative; } .delete { position: absolute; top: 10px; right: 10px; } .minihead { font-size: 10px; font-weight: bold; } #sort-select::after { display: none; } header { transform: translate(0px, 0px); transition: all 0.5s ease 0s; visibility: visible; display: flex; flex-direction: column; } header.closed { transform: translate(0, -100%); transition: all 0.5s ease 0s; visibility: visible; height: 269px; margin-top: -269px; } `); } render() { return html`
this.hideHeader = !this.hideHeader} class="collapse button is-small"> ${this.hideHeader ? 'Expand' : 'Collapse'}
`; } onCopy(event, sourceUrl) { event.preventDefault(); event.stopPropagation(); navigator.clipboard.writeText(sourceUrl); return false; } } customElements.define("wr-coll-index", CollIndex); export { CollIndex };