@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 4.01 kB
JavaScript
import{GlassPane}from"./GlassPane.js";import popoverStyles from"./popover.css.legacy.js";export class PopoverHelper{disableOnClick;hasPadding;getRequest;scheduledRequest;hidePopoverCallback;container;showTimeout;hideTimeout;hidePopoverTimer;showPopoverTimer;boundMouseDown;boundMouseMove;boundMouseOut;constructor(e,o){this.disableOnClick=!1,this.hasPadding=!1,this.getRequest=o,this.scheduledRequest=null,this.hidePopoverCallback=null,this.container=e,this.showTimeout=0,this.hideTimeout=0,this.hidePopoverTimer=null,this.showPopoverTimer=null,this.boundMouseDown=this.mouseDown.bind(this),this.boundMouseMove=this.mouseMove.bind(this),this.boundMouseOut=this.mouseOut.bind(this),this.container.addEventListener("mousedown",this.boundMouseDown,!1),this.container.addEventListener("mousemove",this.boundMouseMove,!1),this.container.addEventListener("mouseout",this.boundMouseOut,!1),this.setTimeout(1e3)}setTimeout(e,o){this.showTimeout=e,this.hideTimeout="number"==typeof o?o:e/2}setHasPadding(e){this.hasPadding=e}setDisableOnClick(e){this.disableOnClick=e}eventInScheduledContent(e){const o=e;return!!this.scheduledRequest&&this.scheduledRequest.box.contains(o.clientX,o.clientY)}mouseDown(e){this.disableOnClick?this.hidePopover():this.eventInScheduledContent(e)||(this.startHidePopoverTimer(0),this.stopShowPopoverTimer(),this.startShowPopoverTimer(e,0))}mouseMove(e){const o=e;if(this.eventInScheduledContent(o))return this.stopShowPopoverTimer(),void this.startShowPopoverTimer(o,this.isPopoverVisible()?.6*this.showTimeout:this.showTimeout);this.startHidePopoverTimer(this.hideTimeout),this.stopShowPopoverTimer(),o.buttons&&this.disableOnClick||this.startShowPopoverTimer(o,this.isPopoverVisible()?.6*this.showTimeout:this.showTimeout)}popoverMouseMove(e){this.stopHidePopoverTimer()}popoverMouseOut(e,o){const t=o;if(!e.isShowing())return;const i=t.relatedTarget;i&&!i.isSelfOrDescendant(e.contentElement)&&this.startHidePopoverTimer(this.hideTimeout)}mouseOut(e){this.isPopoverVisible()&&(this.eventInScheduledContent(e)||this.startHidePopoverTimer(this.hideTimeout))}startHidePopoverTimer(e){this.hidePopoverCallback&&!this.hidePopoverTimer&&(this.hidePopoverTimer=window.setTimeout((()=>{this.hidePopoverInternal(),this.hidePopoverTimer=null}),e))}startShowPopoverTimer(e,o){this.scheduledRequest=this.getRequest.call(null,e),this.scheduledRequest&&(this.showPopoverTimer=window.setTimeout((()=>{this.showPopoverTimer=null,this.stopHidePopoverTimer(),this.hidePopoverInternal();const o=e.target.ownerDocument;this.showPopover(o)}),o))}stopShowPopoverTimer(){this.showPopoverTimer&&(clearTimeout(this.showPopoverTimer),this.showPopoverTimer=null)}isPopoverVisible(){return Boolean(this.hidePopoverCallback)}hidePopover(){this.stopShowPopoverTimer(),this.hidePopoverInternal()}hidePopoverInternal(){this.hidePopoverCallback&&(this.hidePopoverCallback.call(null),this.hidePopoverCallback=null)}showPopover(e){const o=new GlassPane;o.registerRequiredCSS(popoverStyles),o.setSizeBehavior("MeasureContent"),o.setMarginBehavior("Arrow");const t=this.scheduledRequest;t&&t.show.call(null,o).then((i=>{i&&(this.scheduledRequest===t?(popoverHelperInstance&&popoverHelperInstance.hidePopover(),popoverHelperInstance=this,o.contentElement.classList.toggle("has-padding",this.hasPadding),o.contentElement.addEventListener("mousemove",this.popoverMouseMove.bind(this),!0),o.contentElement.addEventListener("mouseout",this.popoverMouseOut.bind(this,o),!0),o.setContentAnchorBox(t.box),o.show(e),this.hidePopoverCallback=()=>{t.hide&&t.hide.call(null),o.hide(),popoverHelperInstance=null}):t.hide&&t.hide.call(null))}))}stopHidePopoverTimer(){this.hidePopoverTimer&&(clearTimeout(this.hidePopoverTimer),this.hidePopoverTimer=null,this.stopShowPopoverTimer())}dispose(){this.container.removeEventListener("mousedown",this.boundMouseDown,!1),this.container.removeEventListener("mousemove",this.boundMouseMove,!1),this.container.removeEventListener("mouseout",this.boundMouseOut,!1)}}let popoverHelperInstance=null;