|
| 1 | +import {css, customElement, html, LitElement, property, TemplateResult} from "lit-element"; |
| 2 | +import {translate, translateUnsafeHTML} from "lit-translate"; |
| 3 | +import {Dialog} from "@material/mwc-dialog"; |
| 4 | + |
| 5 | +import {saveDocumentToFile} from "../file.js"; |
| 6 | +import {getOpenScdElement} from "./foundation.js"; |
| 7 | + |
| 8 | +import {CompasUserInfoService} from "../compas-services/CompasUserInfoService.js"; |
| 9 | + |
| 10 | +@customElement('compas-session-expiring-dialog') |
| 11 | +export class CompasSessionExpiringDialogElement extends LitElement { |
| 12 | + @property({ type: Number }) |
| 13 | + expiringSessionWarning: number = 10 * 60 * 1000; |
| 14 | + @property({ type: Number }) |
| 15 | + expiredSessionMessage: number = 15 * 60 * 1000; |
| 16 | + |
| 17 | + private expiringSessionWarningTimer: NodeJS.Timeout | null = null; |
| 18 | + |
| 19 | + static getElement(): CompasSessionExpiringDialogElement { |
| 20 | + return (<CompasSessionExpiringDialogElement>getOpenScdElement() |
| 21 | + .shadowRoot!.querySelector('compas-session-expiring-dialog')); |
| 22 | + } |
| 23 | + |
| 24 | + resetTimer(): void { |
| 25 | + if (this.expiringSessionWarningTimer) { |
| 26 | + clearTimeout(this.expiringSessionWarningTimer); |
| 27 | + } |
| 28 | + this.expiringSessionWarningTimer = setTimeout(showExpiringSessionWarning, this.expiringSessionWarning); |
| 29 | + } |
| 30 | + |
| 31 | + private getDialog(): Dialog { |
| 32 | + return <Dialog>this.shadowRoot!.querySelector('mwc-dialog[id="compasSessionExpiringDialog"]'); |
| 33 | + } |
| 34 | + |
| 35 | + show(): void { |
| 36 | + const expiringDialog = this.getDialog(); |
| 37 | + if (expiringDialog && !expiringDialog.open) { |
| 38 | + expiringDialog.show(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + close(): void { |
| 43 | + const expiringDialog = this.getDialog(); |
| 44 | + if (expiringDialog && expiringDialog.open) { |
| 45 | + expiringDialog.close(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + render(): TemplateResult { |
| 50 | + return html` |
| 51 | + <mwc-dialog id="compasSessionExpiringDialog" |
| 52 | + heading="${translate('compas.session.headingExpiring')}" |
| 53 | + scrimClickAction=""> |
| 54 | + <div>${translateUnsafeHTML('compas.session.explainExpiring', |
| 55 | + {timeTillExpire: ((this.expiredSessionMessage - this.expiringSessionWarning) / 60 / 1000), |
| 56 | + expiringSessionWarning: (this.expiringSessionWarning / 60 / 1000)})}</div> |
| 57 | + <mwc-button slot="primaryAction" dialogAction="close"> |
| 58 | + ${translate('compas.session.continue')} |
| 59 | + </mwc-button> |
| 60 | + </mwc-dialog> |
| 61 | + `; |
| 62 | + } |
| 63 | + |
| 64 | + static styles = css` |
| 65 | + #compasSessionExpiringDialog { |
| 66 | + --mdc-dialog-max-width: 800px; |
| 67 | + } |
| 68 | + ` |
| 69 | +} |
| 70 | + |
| 71 | +@customElement('compas-session-expired-dialog') |
| 72 | +export class CompasSessionExpiredDialogElement extends LitElement { |
| 73 | + @property({ type: Document }) |
| 74 | + doc: XMLDocument | null = null; |
| 75 | + @property({ type: String }) |
| 76 | + docName = ''; |
| 77 | + @property({ type: Number }) |
| 78 | + expiredSessionMessage: number = 15 * 60 * 1000; |
| 79 | + |
| 80 | + private expiredSessionMessageTimer: NodeJS.Timeout | null = null; |
| 81 | + |
| 82 | + static getElement(): CompasSessionExpiredDialogElement { |
| 83 | + return (<CompasSessionExpiredDialogElement>getOpenScdElement() |
| 84 | + .shadowRoot!.querySelector('compas-session-expired-dialog')); |
| 85 | + } |
| 86 | + |
| 87 | + resetTimer(): void { |
| 88 | + if (this.expiredSessionMessageTimer) { |
| 89 | + clearTimeout(this.expiredSessionMessageTimer); |
| 90 | + } |
| 91 | + this.expiredSessionMessageTimer = setTimeout(showExpiredSessionMessage, this.expiredSessionMessage); |
| 92 | + } |
| 93 | + |
| 94 | + private getDialog(): Dialog { |
| 95 | + return <Dialog>this.shadowRoot!.querySelector('mwc-dialog[id="compasSessionExpiredDialog"]'); |
| 96 | + } |
| 97 | + |
| 98 | + show(): void { |
| 99 | + const expiringDialog = this.getDialog(); |
| 100 | + if (expiringDialog && !expiringDialog.open) { |
| 101 | + expiringDialog.show(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + close(): void { |
| 106 | + const expiringDialog = this.getDialog(); |
| 107 | + if (expiringDialog && expiringDialog.open) { |
| 108 | + expiringDialog.close(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + save(): void { |
| 113 | + saveDocumentToFile(this.doc, this.docName); |
| 114 | + } |
| 115 | + |
| 116 | + render(): TemplateResult { |
| 117 | + const expiredSessionMessage = (this.expiredSessionMessage / 60 / 1000); |
| 118 | + return html` |
| 119 | + <mwc-dialog id="compasSessionExpiredDialog" |
| 120 | + heading="${translate('compas.session.headingExpired')}" |
| 121 | + scrimClickAction="" |
| 122 | + escapeKeyAction="""> |
| 123 | + <div>${(this.doc == null) ? |
| 124 | + translateUnsafeHTML('compas.session.explainExpiredWithoutProject', |
| 125 | + {expiredSessionMessage: expiredSessionMessage}) : |
| 126 | + translateUnsafeHTML('compas.session.explainExpiredWithProject', |
| 127 | + {expiredSessionMessage: expiredSessionMessage}) } |
| 128 | + </div> |
| 129 | + ${(this.doc != null) ? |
| 130 | + html `<mwc-button slot="primaryAction" |
| 131 | + @click=${() => this.save()} |
| 132 | + ?disabled=${this.doc == null}> |
| 133 | + ${translate('compas.session.saveProject')} |
| 134 | + </mwc-button>` : |
| 135 | + html `` } |
| 136 | + </mwc-dialog> |
| 137 | + `; |
| 138 | + } |
| 139 | + |
| 140 | + static styles = css` |
| 141 | + #compasSessionExpiredDialog { |
| 142 | + --mdc-dialog-max-width: 1024px; |
| 143 | + } |
| 144 | + ` |
| 145 | +} |
| 146 | + |
| 147 | +export function renderCompasSessionDialogs(doc: Document | null, docName: string): TemplateResult { |
| 148 | + return html ` |
| 149 | + <compas-session-expiring-dialog></compas-session-expiring-dialog> |
| 150 | + <compas-session-expired-dialog .doc="${doc}" .docName="${docName}"></compas-session-expired-dialog> |
| 151 | + `; |
| 152 | +} |
| 153 | + |
| 154 | +let pingTimer: NodeJS.Timeout | null = null; |
| 155 | + |
| 156 | +async function executeKeepAlivePing() { |
| 157 | + await CompasUserInfoService().ping() |
| 158 | + .finally(() => pingTimer = null) |
| 159 | +} |
| 160 | + |
| 161 | +function schedulePing() { |
| 162 | + if (!pingTimer) { |
| 163 | + // Every minute we will send a Ping to the CoMPAS Services while the user is still active. |
| 164 | + // This to keep the connection alive so long the user is working. |
| 165 | + pingTimer = setTimeout(executeKeepAlivePing, (60 * 1000)); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +function showExpiringSessionWarning() { |
| 170 | + CompasSessionExpiringDialogElement.getElement().show(); |
| 171 | +} |
| 172 | + |
| 173 | +function showExpiredSessionMessage() { |
| 174 | + CompasSessionExpiringDialogElement.getElement().close(); |
| 175 | + CompasSessionExpiredDialogElement.getElement().show(); |
| 176 | + unregisterEvents(); |
| 177 | +} |
| 178 | + |
| 179 | +export function resetTimer() { |
| 180 | + CompasSessionExpiringDialogElement.getElement().resetTimer(); |
| 181 | + CompasSessionExpiredDialogElement.getElement().resetTimer(); |
| 182 | + schedulePing(); |
| 183 | +} |
| 184 | + |
| 185 | +function unregisterEvents() { |
| 186 | + window.removeEventListener('click', resetTimer); |
| 187 | + window.removeEventListener('keydown', resetTimer); |
| 188 | +} |
| 189 | + |
| 190 | +function registerEvents() { |
| 191 | + window.addEventListener('click', resetTimer); |
| 192 | + window.addEventListener('keydown', resetTimer); |
| 193 | +} |
| 194 | + |
| 195 | +export function setSessionTimeouts(sessionWarning: number, sessionExpires: number): void { |
| 196 | + const expiringSessionWarning = sessionWarning * 60 * 1000; |
| 197 | + const expiredSessionMessage = sessionExpires * 60 * 1000; |
| 198 | + |
| 199 | + CompasSessionExpiringDialogElement.getElement().expiringSessionWarning = expiringSessionWarning; |
| 200 | + CompasSessionExpiringDialogElement.getElement().expiredSessionMessage = expiredSessionMessage; |
| 201 | + CompasSessionExpiredDialogElement.getElement().expiredSessionMessage = expiredSessionMessage; |
| 202 | + resetTimer(); |
| 203 | + registerEvents(); |
| 204 | +} |
0 commit comments