Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 | 126x 298x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 993x 993x 993x 993x 993x 993x 993x 993x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 862x 862x 105x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 126x 303x 303x 209x 209x 209x 209x 209x 209x 209x 303x 303x 222x 222x 222x 126x 126x 126x 993x 993x 993x 993x 993x 993x 703x 993x 303x 993x 243x 243x 243x 126x 993x 993x 993x 993x 310x 310x 310x 310x 310x 243x 243x 208x 208x 243x 243x 243x 243x 243x 243x 243x 243x 243x 243x 310x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 310x 310x 242x 385x 385x 310x 68x 68x 126x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 4x 4x 126x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 126x 126x 9x 8x 8x 126x 13x 13x 13x 13x 13x 13x 13x 13x 13x 126x 2x 10x 10x 10x 10x 10x 2x 10x 126x 8x 8x 151x 151x 151x 8x 8x 126x 183x 183x 126x 10x 10x 4x 4x 4x 4x 4x 4x 10x 3x 3x 10x 7x 7x 7x 126x 34x 34x 34x 34x 17x 15x 15x 17x 2x 2x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 89x 89x 89x 126x 126x 122x 122x 122x 122x 122x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x | import { property } from 'lit/decorators.js';
import { TemplateResult, html, css, PropertyValues } from 'lit';
import { Button } from '../display/Button';
import { CustomEventType } from '../interfaces';
import { styleMap } from 'lit-html/directives/style-map.js';
import { getClasses } from '../utils';
import { ResizeElement } from '../ResizeElement';
export enum ButtonType {
PRIMARY = 'primary',
SECONDARY = 'secondary',
DESTRUCTIVE = 'destructive'
}
export class DialogButton {
name?: string;
id?: string;
details?: any;
type?: string;
closes?: boolean;
}
export class Dialog extends ResizeElement {
public static readonly UNSAVED_CHANGES_MESSAGE =
'You have unsaved changes. Are you sure you want to discard them?';
static get widths(): { [size: string]: string } {
return {
small: '400px',
medium: '600px',
large: '655px',
xlarge: '800px'
};
}
static get styles() {
return css`
:host {
position: absolute;
z-index: 10000;
font-family: var(--font-family);
background: white;
}
.flex-grow {
flex-grow: 1;
}
.flex {
display: flex;
flex-direction: column;
width: 100%;
position: relative;
left: 0px;
top: 0px;
align-items: center;
height: 100vh;
}
.mobile .flex {
height: 100%;
position: fixed;
}
.mobile .grow-top {
flex-grow: 0;
}
.mobile .grow-bottom {
flex-grow: 0;
}
.grow-top {
flex-grow: 1;
}
.grow-bottom {
flex-grow: 3;
}
.bottom-padding {
padding: 3rem;
}
.dialog-mask {
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
opacity: 0;
position: fixed;
top: 0px;
left: 0px;
transition: opacity linear calc(var(--transition-speed) / 2ms);
pointer-events: none;
}
.mobile.dialog-mask .dialog-container {
border-radius: 0px;
}
.dialog-mask .dialog-container {
position: relative;
transition:
transform var(--transition-speed) ease-in-out,
opacity ease-in-out calc(var(--transition-speed) - 50ms);
border-radius: var(--curvature);
box-shadow: 0px 0px 2px 4px rgba(0, 0, 0, 0.06);
overflow: hidden;
transform: scale(0.9) translatey(2em);
background: white;
margin: auto;
display: flex;
flex-direction: column;
}
.dialog-body {
background: #fff;
overflow-y: auto;
overflow-x: hidden;
flex-grow: 1;
}
.dialog-mask.dialog-open {
opacity: 1;
pointer-events: auto;
}
.dialog-mask.dialog-open .dialog-container {
top: inherit;
}
.dialog-mask.dialog-animation-end .dialog-container {
transform: scale(1) translate(0, 0) !important;
}
.dialog-mask.dialog-ready .dialog-container {
transform: none;
}
.dialog-mask.dialog-loading .dialog-container {
margin-top: -10000px;
}
.header-text {
display: flex;
flex-direction: row;
align-items: center;
font-size: 20px;
padding: 12px 20px var(--dialog-header-padding-bottom, 12px);
color: var(--header-text);
background: var(--header-bg);
}
/* "flat" variant: header/footer share the body's white background
and lose their inner padding so the dialog reads as one
continuous surface. Use for content-focused dialogs that don't
need the tinted chrome. */
:host([variant='flat']) .header-text {
background: #fff;
color: var(--color-text);
padding-bottom: 0;
}
:host([variant='flat']) .dialog-footer {
background: #fff;
padding-top: 0;
}
.header-text .title {
flex-grow: 1;
}
.header-text .status {
font-size: 0.6em;
font-weight: bold;
}
.dialog-footer {
background: var(--dialog-footer-background, var(--color-primary-light));
padding: var(--dialog-footer-padding-top, 10px) 10px 10px;
display: flex;
flex-flow: row;
align-items: center;
}
temba-button {
margin-left: 10px;
}
.dialog-body temba-loading {
position: absolute;
right: 12px;
margin-top: -30px;
padding-bottom: 9px;
display: none;
}
#page-loader {
text-align: center;
display: block;
position: relative;
opacity: 0;
margin: auto;
margin-top: 30px;
width: 154px;
transition: opacity calc(var(--transition-speed) * 5ms) ease-in
calc(var(--transition-speed * 2));
visibility: hidden;
}
.dialog-mask.dialog-loading #page-loader {
opacity: 1;
visibility: visible;
}
#submit-loader {
flex-grow: 1;
text-align: right;
}
`;
}
@property({ type: Boolean })
open: boolean;
@property()
header: string;
@property()
body: string;
@property({ type: Boolean })
submitting: boolean;
@property({ type: Boolean })
destructive: boolean;
@property({ type: Boolean })
disabled: boolean;
@property({ type: Boolean })
loading: boolean;
@property({ type: Boolean })
hideOnClick: boolean;
@property({ type: String, reflect: true })
variant: string;
@property({ type: Boolean })
noFocus: boolean;
@property()
size = 'medium';
@property({ type: String })
primaryButtonName = 'Ok';
@property({ type: String })
cancelButtonName = 'Cancel';
@property({ type: String })
width = null;
@property()
submittingName = 'Saving';
@property()
animationEnd: boolean;
@property()
ready: boolean;
@property({ type: Array })
buttons: DialogButton[] = [];
@property({ attribute: false })
onButtonClicked: (button: Button) => void;
// when set, consulted instead of the built-in edit tracking to decide
// whether escape should confirm before dismissing — for owners that can
// compare actual values (and so ignore edits that were reverted)
@property({ attribute: false })
checkForChanges?: () => boolean;
@property({ type: Number })
originX: number | null = null;
@property({ type: Number })
originY: number | null = null;
// whether any input/change has bubbled from our content since opening;
// the default signal that escape would discard the user's edits
private contentEdited = false;
// elements the user has actually interacted with (pointer or key) since the
// dialog opened; components fire synthetic change events on programmatic
// value changes (initialization of preset values, async option resolution),
// so an input/change only counts as an edit when it originates from an
// element the user has touched
private interactedElements = new WeakSet<EventTarget>();
scrollOffset: any = 0;
public constructor() {
super();
}
private updateButtons() {
this.buttons = [];
if (this.cancelButtonName) {
this.buttons.push({
name: this.cancelButtonName,
type: ButtonType.SECONDARY,
closes: true
});
}
if (this.primaryButtonName) {
this.buttons.push({
name: this.primaryButtonName,
type: ButtonType.PRIMARY
});
}
}
public willUpdate(changes: PropertyValues) {
super.willUpdate(changes);
if (changes.has('cancelButtonName') || changes.has('primaryButtonName')) {
this.updateButtons();
}
if (changes.has('open') && this.open) {
if (this.originX == null && this.originY == null) {
// Default animation (no origin): set animationEnd before render
this.animationEnd = true;
}
}
}
public updated(changes: Map<string, any>) {
super.updated(changes);
if (changes.has('open')) {
this.contentEdited = false;
this.interactedElements = new WeakSet<EventTarget>();
const body = document.querySelector('body');
if (this.open) {
if (this.originX != null && this.originY != null) {
// Spring-from-origin animation: measure final position, then
// set initial transform at click point and transition to center
const ox = this.originX;
const oy = this.originY;
this.originX = null;
this.originY = null;
requestAnimationFrame(() => {
const container = this.shadowRoot?.querySelector(
'.dialog-container'
) as HTMLElement;
if (container) {
const rect = container.getBoundingClientRect();
const cx = rect.left + rect.width / 2;
const cy = rect.top + rect.height / 2;
const dx = ox - cx;
const dy = oy - cy;
// Disable transition so we can set the start position instantly
container.style.transition = 'none';
container.style.transform = `translate(${dx}px, ${dy}px) scale(0.2)`;
// Force reflow to register the start position
container.getBoundingClientRect();
// Re-enable transition and trigger animation to final position
container.style.transition = '';
this.animationEnd = true;
window.setTimeout(() => {
this.ready = true;
this.animationEnd = false;
container.style.transform = '';
}, 400);
}
});
} else {
// Default animation (no origin): animationEnd already set in willUpdate
window.setTimeout(() => {
this.ready = true;
this.animationEnd = false;
}, 400);
}
this.scrollOffset = -document.documentElement.scrollTop;
body.style.position = 'fixed';
body.style.overflowY = 'scroll';
body.style.top = this.scrollOffset + 'px';
body.style.width = '100%';
body.style.overflowY = 'hidden';
} else {
body.style.position = '';
body.style.overflowY = '';
body.style.width = '';
body.style.marginRight = '';
body.style.paddingRight = '0px';
window.scrollTo(0, parseInt(this.scrollOffset || '0') * -1);
}
// make sure our buttons aren't in progress on show
if (this.open && !changes.get('open')) {
this.shadowRoot
.querySelectorAll('temba-button')
.forEach((button: Button) => {
if (button) button.submitting = false;
});
} else {
window.setTimeout(() => {
this.ready = false;
}, 400);
}
}
}
public focusFirstInput(): void {
window.setTimeout(() => {
let input = this.querySelector(
'temba-textinput, temba-completion, input[type="text"], textarea'
) as any;
if (input) {
input = input.textInputElement || input.inputElement || input;
if (!input.readOnly) {
input.focus();
input.click();
}
}
}, 100);
}
public handleClick(evt: MouseEvent) {
const button = evt.currentTarget as Button;
if (!button.disabled) {
let detail: DialogButton = {};
if (button.index >= 0 && button.index < this.buttons.length) {
detail = this.buttons[button.index];
}
this.fireCustomEvent(CustomEventType.ButtonClicked, { button, detail });
if (button.name === this.cancelButtonName || (detail && detail.closes)) {
this.open = false;
}
}
}
private getDocumentHeight(): number {
const body = document.body;
const html = document.documentElement;
return Math.max(
body.scrollHeight,
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight
);
}
private clickCancel() {
const cancel = this.getCancelButton();
if (cancel) {
cancel.click();
}
}
public getCancelButton(): Button {
return this.shadowRoot.querySelector(
`temba-button[name='${this.cancelButtonName}']`
);
}
public getPrimaryButton(): Button {
return this.shadowRoot.querySelector(`temba-button[primary]`);
}
public hasUnsavedChanges(): boolean {
return this.checkForChanges ? this.checkForChanges() : this.contentEdited;
}
private handleContentInteraction(event: Event) {
// the path covers the interacted element and its ancestors, so a change
// fired later by a containing component (e.g. a select whose inner input
// was clicked) still matches
event.composedPath().forEach((el) => this.interactedElements.add(el));
}
private handleContentEdited(event: Event) {
// only the originating element is checked (not its ancestors, which the
// paths of unrelated interactions also pass through)
if (this.interactedElements.has(event.composedPath()[0])) {
this.contentEdited = true;
}
}
private handleKeyUp(event: KeyboardEvent) {
if (event.key === 'Escape') {
if (
this.hasUnsavedChanges() &&
!window.confirm(Dialog.UNSAVED_CHANGES_MESSAGE)
) {
return;
}
this.clickCancel();
}
}
private handleClickMask(event: MouseEvent) {
if (this.hideOnClick) {
const id = (event.target as HTMLElement).id;
if (id === 'dialog-mask' || id === 'dialog-bg') {
this.fireCustomEvent(CustomEventType.DialogHidden);
this.clickCancel();
}
}
}
public show(): void {
this.open = true;
}
public hide(): void {
this.open = false;
}
public render(): TemplateResult {
const dialogStyle = {
width: this.width
};
if (!this.width) {
dialogStyle['width'] = Dialog.widths[this.size];
}
if (this.isMobile()) {
dialogStyle['width'] = '100%';
dialogStyle['height'] = '100%';
delete dialogStyle['maxWidth'];
}
const header = this.header
? html`
<div class="dialog-header">
<div class="header-text">
<div class="title">${this.header}</div>
</div>
</div>
`
: null;
return html`
<div
id="dialog-mask"
@click=${this.handleClickMask}
class="dialog-mask ${getClasses({
'dialog-open': this.open,
'dialog-loading': this.loading,
'dialog-animation-end': this.animationEnd,
'dialog-ready': this.ready,
mobile: this.isMobile()
})}"
>
<div style="position: absolute; width: 100%;">
<temba-loading
id="page-loader"
units="6"
size="12"
color="#ccc"
></temba-loading>
</div>
<div class="flex">
<div
class="grow-top"
style="${this.isMobile() ? 'flex-grow:0' : ''}"
></div>
<div
@keyup=${this.handleKeyUp}
@keydown=${this.handleContentInteraction}
@pointerdown=${this.handleContentInteraction}
@input=${this.handleContentEdited}
@change=${this.handleContentEdited}
style=${styleMap(dialogStyle)}
class="dialog-container"
>
${header}
<div class="dialog-body" @keypress=${this.handleKeyUp}>
${this.body ? this.body : html`<slot></slot>`}
<temba-loading units="6" size="8"></temba-loading>
</div>
${
// the gutter check is render-time only (not reactive) - fine
// while consumers slot gutter content declaratively up front
this.buttons.length > 0 || this.querySelector('[slot="gutter"]')
? html`<div class="dialog-footer">
<div class="flex-grow">
<slot name="gutter"></slot>
</div>
${this.buttons.map(
(button: DialogButton, index) => html`
<temba-button
name=${button.name}
?destructive=${button.type == 'primary' &&
this.destructive}
?primary=${button.type == 'primary' &&
!this.destructive}
?secondary=${button.type == 'secondary'}
?submitting=${this.submitting &&
button.type == 'primary'}
?disabled=${this.disabled && !button.closes}
index=${index}
@click=${this.handleClick}
></temba-button>
`
)}
</div>`
: null
}
</div>
<div class="grow-bottom"></div>
</div>
</div>
`;
}
}
|