UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

196 lines (195 loc) 14.6 kB
import { html as uHtml } from 'uhtml'; // SPDX-License-Identifier: Apache-2.0 import GirafeHTMLElement from '../../base/GirafeHTMLElement.js'; import tippy from 'tippy.js'; import { FeedbackResult } from '../../tools/feedback/feedbackmanager.js'; class FeedbackButtonComponent extends GirafeHTMLElement { static observedAttributes = ['question', 'component-name', 'placement', 'margins']; templateUrl = null; styleUrls = null; template = () => { return uHtml `<style> #feedback-container{background-color:var(--bkg-color);position:absolute;& #feedback-button img{content:url(icons/feedback-comment.svg)}& #feedback-button:hover img{content:url(icons/feedback-comment-add.svg)}}.feedback-popup,.thankyou-popup{background-color:var(--bkg-color);border:var(--app-standard-border);align-items:center;width:max-content;min-width:8rem;min-height:1.5rem;padding:.25rem .5rem;display:flex} </style><style> .hidden{display:none!important}.gg-rotate90{transform:rotate(90deg)}.gg-rotate180{transform:rotate(180deg)}.gg-rotate270{transform:rotate(270deg)}img{filter:var(--svg-filter)}img.legend-image{filter:var(--svg-map-filter);background:var(--svg-legend-bkg)}div{scrollbar-width:thin}a,a:visited{color:var(--link-color)}@keyframes spin{0%{transform:rotate(0)}to{transform:rotate(360deg)}}@keyframes spin-wait{0%{transform:rotate(0)}7%{transform:rotate(360deg)}to{transform:rotate(360deg)}}.gg-spin{animation-name:spin;animation-duration:2s;animation-timing-function:linear;animation-iteration-count:infinite}.gg-spin-wait{animation-name:spin-wait;animation-duration:10s;animation-timing-function:linear;animation-iteration-count:infinite}::-webkit-scrollbar{width:5px}::-webkit-scrollbar-thumb{background:#999}button,input,select,textarea{font:inherit}.gg-button,.gg-select,.gg-input,.gg-textarea{background-color:var(--bkg-color);color:var(--text-color);border:var(--app-standard-border);box-sizing:border-box;cursor:pointer;border-radius:3px;outline:0;margin:0;padding:0 0 0 .5rem;display:inline-block}.gg-label{background-color:var(--bkg-color);color:var(--text-color);border:none;align-items:center;margin:0;padding:0;display:flex}.gg-button,.gg-select,.gg-input,.gg-label{min-height:calc(var(--app-standard-height) / 1.5)}.gg-textarea{max-height:initial;resize:vertical;height:6rem;padding:.5rem;line-height:1.3rem}.gg-input{cursor:text}.gg-checkbox{accent-color:var(--text-color);width:1.2rem}.gg-range{accent-color:var(--text-color)}.gg-button{padding:0 .5rem}.gg-button.active{border:solid 1px var(--text-color-grad1);background-color:var(--bkg-color-grad2)}.gg-button:disabled{background-color:var(--bkg-color-grad1);color:var(--text-color-grad2);cursor:not-allowed;border:none}.gg-button:disabled img{filter:opacity(.6)}.gg-input:disabled,.gg-select:disabled,.gg-textarea:disabled{background-color:var(--bkg-color-grad1);color:var(--text-color-grad2);cursor:not-allowed}.gg-button>img{vertical-align:middle}.gg-icon-button{color:var(--text-color);cursor:pointer;background-color:#0000;border:none;flex-direction:column;justify-content:center;align-items:center;padding:0;display:flex}.gg-icon{justify-content:center;align-items:center;display:flex}.gg-big,.gg-big-withtext{min-width:var(--app-standard-height);min-height:var(--app-standard-height);max-height:var(--app-standard-height)}.gg-big img,.gg-big-withtext img{width:calc(var(--app-standard-height) - 1.5rem);margin:0}.gg-big-withtext span{font-variant:small-caps;padding:0 1rem;font-size:.9rem}.gg-medium,.gg-medium-withtext{min-width:calc(var(--app-standard-height) / 1.2);min-height:calc(var(--app-standard-height) / 1.2);max-height:calc(var(--app-standard-height) / 1.2);flex-direction:row}.gg-medium img{width:calc(var(--app-standard-height) / 2.4);margin:0}.gg-medium-withtext img{width:calc(var(--app-standard-height) / 2.4);margin-left:.5rem}.gg-medium-withtext span{padding:0 1rem 0 .5rem;font-size:.9rem}.gg-small,.gg-small-withtext{min-width:calc(var(--app-standard-height) / 2);min-height:calc(var(--app-standard-height) / 2);max-height:calc(var(--app-standard-height) / 2);flex-direction:row}.gg-small img{width:calc(var(--app-standard-height) / 3);margin:0}.gg-small-withtext img{width:calc(var(--app-standard-height) / 3);margin-left:.5rem}.gg-small-withtext span{padding:0 .5rem 0 .3rem;font-size:.9rem}.gg-button:hover:not(:disabled),.gg-select:hover:not(:disabled),.gg-input:hover:not(:disabled),.gg-textarea:hover:not(:disabled),.gg-icon-button:hover:not(:disabled){background-color:var(--bkg-color-grad1)}.gg-opacity{opacity:.5}.gg-opacity:hover{opacity:1;background-color:#0000}.gg-tabs{cursor:pointer;grid-auto-flow:column;padding-bottom:1rem;font-size:1rem;display:grid}.gg-tab{border:none;border-bottom:var(--app-standard-border);cursor:pointer;color:var(--text-color);background:0 0;padding:.5rem}.gg-tab.active{border-bottom:solid 1px var(--text-color)}.girafe-button-big,.girafe-button-large,.girafe-button-small,.girafe-button-tiny{color:var(--text-color);background-color:#0000;border:none;flex-direction:column;display:flex}.girafe-button-big:hover,.girafe-button-large:hover,.girafe-button-small:hover,.girafe-button-tiny:hover{background-color:var(--bkg-color-grad1);cursor:pointer}.girafe-button-big.dark,.girafe-button-large.dark,.girafe-button-small.dark,.girafe-button-tiny.dark{background-color:var(--bkg-color);filter:invert()}.girafe-button-big{width:var(--app-standard-height);height:var(--app-standard-height);align-items:center;padding:1rem}.girafe-button-big img{overflow:hidden}.girafe-button-large{flex-direction:row}.girafe-button-large img{height:2rem;margin:.3rem}.girafe-button-large span{height:2rem;margin:.3rem;line-height:2rem}.girafe-button-small{min-width:calc(var(--app-standard-height) / 2);height:calc(var(--app-standard-height) / 2);align-items:center;padding:.5rem}.girafe-button-small img{overflow:hidden}.girafe-button-small span{text-align:left;text-overflow:ellipsis;width:100%;overflow:hidden}.girafe-button-tiny{align-items:center;width:1rem;height:1rem;padding:0}.girafe-button-tiny img{overflow:hidden}.girafe-onboarding-theme{background-color:var(--bkg-color)!important;color:var(--text-color)!important}.girafe-onboarding-theme button{background-color:var(--bkg-color)!important;color:var(--text-color)!important;text-shadow:none!important}.girafe-onboarding-theme button.driver-popover-close-btn{z-index:10000}.tippy-box{background-color:var(--bkg-color)!important;border:var(--app-standard-border)!important}.tippy-content{color:var(--text-color)!important}.tippy-arrow{color:var(--bkg-color)!important}.tippy-box[data-theme~=error]{background-color:var(--error-color);white-space:pre-line}div.tippy-box[data-theme=image-popup]{border:var(--app-standard-border)!important;& div.tippy-content{background-color:var(--bkg-color)!important;& img{max-width:20rem;max-height:20rem}}& div.tippy-arrow{color:var(--bkg-color)!important}} </style> <style>${this.customStyle}</style> <div id="feedback-container"><button id="feedback-button" tip="feedback-button-tip" class="gg-icon-button gg-medium"><img alt="thumbs icon" src="icons/feedback-comment.svg"></button><div id="feedback-popup" class="hidden"><span i18n="${this.question}">${this.question}</span> <button tip="feedback-negative" class="gg-icon-button gg-small" onclick="${() => this.negativeFeedback()}"><img alt="thumbs down icon" src="icons/feedback-thumb-down.svg"></button> <button tip="feedback-positive" class="gg-icon-button gg-small" onclick="${() => this.positiveFeedback()}"><img alt="thumbs up icon" src="icons/feedback-thumb-up.svg"></button></div><div id="thankyou-popup" class="hidden"><span i18n="feedback-thankyou-default">Thank you for your feedback!</span></div></div> ${this.htmlUnsafe(this.feedbackTemplateHtml ?? '')}`; }; question; componentName; placement; verticalMargin = '0px'; horizontalMargin = '0px'; tippyInstance; constructor() { super('feedbackbutton'); this.displayStyle = 'block'; } async positiveFeedback() { await this.context.feedbackManager.giveFeedback(this.componentName, FeedbackResult.POSITIVE); this.showThankYouPopup(); } async negativeFeedback() { await this.context.feedbackManager.giveFeedback(this.componentName, FeedbackResult.NEGATIVE); this.showThankYouPopup(); } showThankYouPopup() { super.girafeTranslate(); const popup = this.shadow.querySelector('#thankyou-popup'); popup.classList.remove('hidden'); popup.classList.add('thankyou-popup'); this.tippyInstance?.setProps({ animation: 'scale' }); this.tippyInstance?.setContent(popup); globalThis.setTimeout(() => { this.tippyInstance?.hide(); }, 2000); } connectedCallback() { super.connectedCallback(); this.render(); } disconnectedCallback() { super.disconnectedCallback(); } render() { super.render(); this.question = this.getAttribute('question') ?? 'feedback-question-default'; this.componentName = this.getAttribute('component-name') ?? undefined; this.placement = this.getAttribute('placement') ?? undefined; this.calculateMargins(this.getAttribute('margins')); this.calculatePlacement(); this.createPopupHandler(); super.girafeTranslate(); this.refreshRender(); } calculateMargins(componentMargin) { if (!componentMargin || componentMargin.trim().length === 0) { this.verticalMargin = '0px'; this.horizontalMargin = '0px'; return; } const margins = componentMargin.split(' '); if (margins.length != 2) { throw new Error('Invalid value for margins'); } this.verticalMargin = margins[0]; this.horizontalMargin = margins[1]; } calculatePlacement() { console.log('calculatePlacement()', this.shadow.parentElement); if (this.componentName) { const componentsByName = this.context.componentManager.getComponentsByName(this.componentName); if (componentsByName.length != 1) { console.error(`Multiple components with name ${this.componentName} found.`); return; } const girafeHTMLElement = componentsByName[0]; if (girafeHTMLElement) { const feedbackContainer = this.shadow.querySelector('#feedback-container'); if (feedbackContainer) { this.setContainerPosition(feedbackContainer); } } } } setContainerPosition(feedbackContainer) { switch (this.placement) { case 'top-left': feedbackContainer.style.top = `${this.verticalMargin}`; feedbackContainer.style.left = `${this.horizontalMargin}`; break; case 'top-center': feedbackContainer.style.top = `${this.verticalMargin}`; feedbackContainer.style.left = `calc(50% - ${feedbackContainer.clientWidth / 2}px)`; break; case 'top-right': feedbackContainer.style.top = `${this.verticalMargin}`; feedbackContainer.style.right = `${this.horizontalMargin}`; break; case 'center-left': feedbackContainer.style.top = `calc(50% - ${feedbackContainer.clientHeight / 2}px)`; feedbackContainer.style.left = `${this.horizontalMargin}`; break; case 'center-center': feedbackContainer.style.top = `calc(50% - ${feedbackContainer.clientHeight / 2}px)`; feedbackContainer.style.left = `calc(50% - ${feedbackContainer.clientWidth / 2}px)`; break; case 'center-right': feedbackContainer.style.top = `calc(50% - ${feedbackContainer.clientHeight / 2}px)`; feedbackContainer.style.right = `${this.horizontalMargin}`; break; case 'bottom-left': feedbackContainer.style.bottom = `${this.verticalMargin}`; feedbackContainer.style.left = `${this.horizontalMargin}`; break; case 'bottom-center': feedbackContainer.style.bottom = `${this.verticalMargin}`; feedbackContainer.style.left = `calc(50% - ${feedbackContainer.clientWidth / 2}px)`; break; case 'bottom-right': default: feedbackContainer.style.bottom = `${this.verticalMargin}`; feedbackContainer.style.right = `${this.horizontalMargin}`; break; } } createPopupHandler() { const button = this.shadow.querySelector('#feedback-button'); this.tippyInstance = tippy(button, { trigger: 'click', arrow: true, interactive: true, theme: 'light', placement: this.getPopupPlacement(), content: (_ref) => { const popup = this.shadow.querySelector('#feedback-popup'); this.context.i18nManager.translate(popup); popup.classList.remove('hidden'); popup.classList.add('feedback-popup'); return popup; } }); } getPopupPlacement() { switch (this.placement) { case 'top-left': return 'bottom-start'; case 'top-center': return 'bottom'; case 'top-right': return 'bottom-end'; case 'center-left': return 'left-start'; case 'center-center': return 'bottom'; case 'center-right': return 'left-end'; case 'bottom-left': return 'top-start'; case 'bottom-center': return 'top'; case 'bottom-right': return 'top-end'; } return 'top-end'; } attributeChangedCallback(name, _oldValue, newValue) { if (name === 'question') { this.question = newValue; } else if (name === 'component-name') { this.componentName = newValue; } else if (name === 'placement') { this.placement = newValue; } else if (name === 'margin') { this.calculateMargins(newValue); } } } export default FeedbackButtonComponent;