|
| 1 | +import { css, customElement, html, LitElement, property } from 'lit-element'; |
| 2 | +import { PersonCardInteraction } from '../../PersonCardInteraction'; |
| 3 | + |
| 4 | +/** |
| 5 | + * |
| 6 | + * |
| 7 | + * @export |
| 8 | + * @class mgt-flyout |
| 9 | + * @extends {MgtTemplatedComponent} |
| 10 | + */ |
| 11 | +@customElement('mgt-flyout') |
| 12 | +export class MgtFlyout extends LitElement { |
| 13 | + /** |
| 14 | + * Array of styles to apply to the element. The styles should be defined |
| 15 | + * using the `css` tag function. |
| 16 | + */ |
| 17 | + static get styles() { |
| 18 | + return css` |
| 19 | + .title { |
| 20 | + color: red; |
| 21 | + } |
| 22 | + `; |
| 23 | + } |
| 24 | + |
| 25 | + // TODO: create new type |
| 26 | + @property({ |
| 27 | + attribute: 'person-card', |
| 28 | + converter: (value, type) => { |
| 29 | + value = value.toLowerCase(); |
| 30 | + if (typeof PersonCardInteraction[value] === 'undefined') { |
| 31 | + return PersonCardInteraction.none; |
| 32 | + } else { |
| 33 | + return PersonCardInteraction[value]; |
| 34 | + } |
| 35 | + } |
| 36 | + }) |
| 37 | + public personCardInteraction: PersonCardInteraction = PersonCardInteraction.hover; |
| 38 | + |
| 39 | + /** |
| 40 | + * |
| 41 | + * |
| 42 | + * @type {string} |
| 43 | + * @memberof MgtComponent |
| 44 | + */ |
| 45 | + @property({ |
| 46 | + attribute: 'my-title', |
| 47 | + type: String |
| 48 | + }) |
| 49 | + public myTitle: string = 'My First Component'; |
| 50 | + private _mouseLeaveTimeout; |
| 51 | + private _mouseEnterTimeout; |
| 52 | + private _openLeft: boolean = false; |
| 53 | + private _openUp: boolean = false; |
| 54 | + @property({ attribute: false }) private _isPersonCardVisible: boolean = false; |
| 55 | + @property({ attribute: false }) private _personCardShouldRender: boolean = false; |
| 56 | + |
| 57 | + /** |
| 58 | + * Synchronizes property values when attributes change. |
| 59 | + * |
| 60 | + * @param {*} name |
| 61 | + * @param {*} oldValue |
| 62 | + * @param {*} newValue |
| 63 | + * @memberof MgtPersonCard |
| 64 | + */ |
| 65 | + public attributeChangedCallback(name, oldval, newval) { |
| 66 | + super.attributeChangedCallback(name, oldval, newval); |
| 67 | + |
| 68 | + // TODO: handle when an attribute changes. |
| 69 | + // |
| 70 | + // Ex: load data when the name attribute changes |
| 71 | + // if (name === 'person-id' && oldval !== newval){ |
| 72 | + // this.loadData(); |
| 73 | + // } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Invoked when the element is first updated. Implement to perform one time |
| 78 | + * work on the element after update. |
| 79 | + * |
| 80 | + * Setting properties inside this method will trigger the element to update |
| 81 | + * again after this update cycle completes. |
| 82 | + * |
| 83 | + * * @param _changedProperties Map of changed properties with old values |
| 84 | + */ |
| 85 | + public firstUpdated() {} |
| 86 | + |
| 87 | + /** |
| 88 | + * Invoked on each update to perform rendering tasks. This method must return |
| 89 | + * a lit-html TemplateResult. Setting properties inside this method will *not* |
| 90 | + * trigger the element to update. |
| 91 | + */ |
| 92 | + protected render() { |
| 93 | + return html` |
| 94 | + <div |
| 95 | + class="root" |
| 96 | + @mouseenter=${this._handleMouseEnter} |
| 97 | + @mouseleave=${this._handleMouseLeave} |
| 98 | + @click=${this._handleMouseClick} |
| 99 | + > |
| 100 | + <slot></slot> |
| 101 | + <slot name="flyout"></slot> |
| 102 | + </div> |
| 103 | + `; |
| 104 | + } |
| 105 | + |
| 106 | + private _handleMouseClick() { |
| 107 | + if (this.personCardInteraction === PersonCardInteraction.click && !this._isPersonCardVisible) { |
| 108 | + this._showPersonCard(); |
| 109 | + } else { |
| 110 | + this._hidePersonCard(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private _handleMouseEnter(e: MouseEvent) { |
| 115 | + if (this.personCardInteraction !== PersonCardInteraction.hover) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + clearTimeout(this._mouseEnterTimeout); |
| 120 | + clearTimeout(this._mouseLeaveTimeout); |
| 121 | + this._mouseEnterTimeout = setTimeout(this._showPersonCard.bind(this), 500); |
| 122 | + } |
| 123 | + |
| 124 | + private _handleMouseLeave(e: MouseEvent) { |
| 125 | + clearTimeout(this._mouseEnterTimeout); |
| 126 | + clearTimeout(this._mouseLeaveTimeout); |
| 127 | + this._mouseLeaveTimeout = setTimeout(this._hidePersonCard.bind(this), 500); |
| 128 | + } |
| 129 | + |
| 130 | + private _showPersonCard() { |
| 131 | + if (!this._personCardShouldRender) { |
| 132 | + this._personCardShouldRender = true; |
| 133 | + } |
| 134 | + |
| 135 | + this._isPersonCardVisible = true; |
| 136 | + } |
| 137 | + |
| 138 | + private _hidePersonCard() { |
| 139 | + this._isPersonCardVisible = false; |
| 140 | + |
| 141 | + // TODO expose an event to do this outside of the component |
| 142 | + // const personCard = this.querySelector('mgt-person-card') as MgtPersonCard; |
| 143 | + // if (personCard) { |
| 144 | + // personCard.isExpanded = false; |
| 145 | + // } |
| 146 | + } |
| 147 | +} |
0 commit comments