/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ import { html } from "chrome://global/content/vendor/lit.all.mjs"; import { MozLitElement } from "chrome://global/content/lit-utils.mjs"; const lazy = {}; ChromeUtils.defineESModuleGetters(lazy, { DownloadUtils: "resource://gre/modules/DownloadUtils.sys.mjs", }); /** * ModelFilesView */ export class ModelFilesView extends MozLitElement { static properties = { models: { type: Array }, }; static events = { delete: "MlModelDelete", }; constructor() { super(); this.models = []; } dispatch(event, detail) { this.dispatchEvent( new CustomEvent(event, { detail, bubbles: true, composed: true }) ); } /** * Convert a timestamp to a string * * @param {number} ts * @returns {string} - The string representation of the timestamp */ ts2str(ts) { return ts ? new Date(ts).toLocaleString() : "-"; } /** * Handle the delete model click event and remove the model from the list * * @param {mlmodel} model */ handleDeleteModelClick(model) { this.dispatch(ModelFilesView.events.delete, { ...model, }); } removeModel(model) { this.models = this.models.filter(modelToCheck => { return ( modelToCheck.name !== model.name || modelToCheck.revision != model.revision ); }); } /** * Render the rows of the table * * @param {Array} files * @returns {Array} - Array of lit-html TemplateResult rows */ renderRows(files) { const rows = files.map(file => { const size = parseInt( file.headers.fileSize || file.headers["Content-Length"] || 0 ); return html`
| ${lazy.DownloadUtils.getTransferTotal( model.metadata.totalSize )} | ${this.ts2str(model.metadata.lastUsed)} | ${this.ts2str(model.metadata.updateDate)} |