Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import {LitElement, html, css} from 'lit-element'; |
| 6 | import 'elements/framework/mr-comment-content/mr-comment-content.js'; |
| 7 | import 'elements/chops/chops-timestamp/chops-timestamp.js'; |
| 8 | |
| 9 | /** |
| 10 | * `<mr-comment-table>` |
| 11 | * |
| 12 | * The list of comments for a Monorail Polymer profile. |
| 13 | * |
| 14 | */ |
| 15 | export class MrCommentTable extends LitElement { |
| 16 | /** @override */ |
| 17 | static get styles() { |
| 18 | return css` |
| 19 | .ellipsis { |
| 20 | max-width: 50%; |
| 21 | text-overflow: ellipsis; |
| 22 | overflow: hidden; |
| 23 | white-space: nowrap; |
| 24 | } |
| 25 | table { |
| 26 | word-wrap: break-word; |
| 27 | width: 100%; |
| 28 | } |
| 29 | tr { |
| 30 | font-size: var(--chops-main-font-size); |
| 31 | font-weight: normal; |
| 32 | text-align: left; |
| 33 | line-height: 180%; |
| 34 | } |
| 35 | td, th { |
| 36 | border-bottom: var(--chops-normal-border); |
| 37 | padding: 0.25em 16px; |
| 38 | } |
| 39 | td { |
| 40 | text-overflow: ellipsis; |
| 41 | } |
| 42 | th { |
| 43 | text-align: left; |
| 44 | } |
| 45 | .no-wrap { |
| 46 | white-space: nowrap; |
| 47 | } |
| 48 | `; |
| 49 | } |
| 50 | |
| 51 | /** @override */ |
| 52 | render() { |
| 53 | const comments = this._displayedComments(this.selectedDate, this.comments); |
| 54 | // TODO(zhangtiff): render deltas for comment changes. |
| 55 | return html` |
| 56 | <table cellspacing="0" cellpadding="0"> |
| 57 | <tbody> |
| 58 | <tr id="heading-row"> |
| 59 | <th>Date</th> |
| 60 | <th>Project</th> |
| 61 | <th>Comment</th> |
| 62 | <th>Issue Link</th> |
| 63 | </tr> |
| 64 | |
| 65 | ${comments && comments.length ? comments.map((comment) => html` |
| 66 | <tr id="row"> |
| 67 | <td class="no-wrap"> |
| 68 | <chops-timestamp |
| 69 | .timestamp=${comment.timestamp} |
| 70 | short |
| 71 | ></chops-timestamp> |
| 72 | </td> |
| 73 | <td>${comment.projectName}</td> |
| 74 | <td class="ellipsis"> |
| 75 | <mr-comment-content |
| 76 | .content=${this._truncateMessage(comment.content)} |
| 77 | ></mr-comment-content> |
| 78 | </td> |
| 79 | <td class="no-wrap"> |
| 80 | <a href="/p/${comment.projectName}/issues/detail?id=${comment.localId}"> |
| 81 | Issue ${comment.localId} |
| 82 | </a> |
| 83 | </td> |
| 84 | </tr> |
| 85 | `) : html` |
| 86 | <tr> |
| 87 | <td colspan="4"><i>No comments.</i></td> |
| 88 | </tr> |
| 89 | `} |
| 90 | </tbody> |
| 91 | </table> |
| 92 | `; |
| 93 | } |
| 94 | |
| 95 | /** @override */ |
| 96 | static get properties() { |
| 97 | return { |
| 98 | comments: {type: Array}, |
| 99 | selectedDate: {type: Number}, |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | /** @override */ |
| 104 | constructor() { |
| 105 | super(); |
| 106 | this.comments = []; |
| 107 | } |
| 108 | |
| 109 | _truncateMessage(message) { |
| 110 | return message && message.substring(0, message.indexOf('\n')); |
| 111 | } |
| 112 | |
| 113 | _displayedComments(selectedDate, comments) { |
| 114 | if (!selectedDate) { |
| 115 | return comments; |
| 116 | } else { |
| 117 | const computedComments = []; |
| 118 | if (!comments) return computedComments; |
| 119 | |
| 120 | for (let i = 0; i < comments.length; i++) { |
| 121 | if (comments[i].timestamp <= selectedDate && |
| 122 | comments[i].timestamp >= (selectedDate - 86400)) { |
| 123 | computedComments.push(comments[i]); |
| 124 | } |
| 125 | } |
| 126 | return computedComments; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | customElements.define('mr-comment-table', MrCommentTable); |