소스 검색

Viewport: trigger a resize event when changing the style

customisations
alemart 1 년 전
부모
커밋
94643bbfd5
1개의 변경된 파일27개의 추가작업 그리고 16개의 파일을 삭제
  1. 27
    16
      src/core/viewport.ts

+ 27
- 16
src/core/viewport.ts 파일 보기

@@ -333,7 +333,13 @@ export class BaseViewport extends ViewportEventTarget implements Viewport
333 333
         if(value != 'best-fit' && value != 'stretch' && value != 'inline')
334 334
             throw new IllegalArgumentError('Invalid viewport style: ' + value);
335 335
 
336
+        const changed = (value != this._style);
336 337
         this._style = value;
338
+
339
+        if(changed) {
340
+            const event = new ViewportEvent('resize');
341
+            this.dispatchEvent(event);
342
+        }
337 343
     }
338 344
 
339 345
     /**
@@ -711,9 +717,6 @@ abstract class ResizableViewport extends ViewportDecorator
711 717
     /** is this viewport subject to being resized? */
712 718
     private _active: boolean;
713 719
 
714
-    /** a bound _onResize */
715
-    private _resize: () => void;
716
-
717 720
 
718 721
 
719 722
     /**
@@ -725,7 +728,7 @@ abstract class ResizableViewport extends ViewportDecorator
725 728
     {
726 729
         super(base, getSize);
727 730
         this._active = false;
728
-        this._resize = this._onResize.bind(this);
731
+        this.addEventListener('resize', this._onResize.bind(this));
729 732
     }
730 733
 
731 734
     /**
@@ -737,13 +740,16 @@ abstract class ResizableViewport extends ViewportDecorator
737 740
         super._init();
738 741
         this._active = true;
739 742
 
743
+        // bound resize
744
+        const resize = this._resize.bind(this);
745
+
740 746
         // Configure the resize listener. We want the viewport
741 747
         // to adjust itself if the phone/screen is resized or
742 748
         // changes orientation
743 749
         let timeout: Nullable<ReturnType<typeof setTimeout>> = null;
744
-        const onresize = () => {
750
+        const onWindowResize = () => {
745 751
             if(!this._active) {
746
-                window.removeEventListener('resize', onresize);
752
+                window.removeEventListener('resize', onWindowResize);
747 753
                 return;
748 754
             }
749 755
 
@@ -752,20 +758,20 @@ abstract class ResizableViewport extends ViewportDecorator
752 758
 
753 759
             timeout = setTimeout(() => {
754 760
                 timeout = null;
755
-                this._resize();
756
-            }, 100);
761
+                resize();
762
+            }, 50);
757 763
         };
758
-        window.addEventListener('resize', onresize);
764
+        window.addEventListener('resize', onWindowResize);
759 765
 
760 766
         // handle changes of orientation
761 767
         // (is this needed? we already listen to resize events)
762 768
         if(screen.orientation !== undefined)
763
-            screen.orientation.addEventListener('change', this._resize);
769
+            screen.orientation.addEventListener('change', resize);
764 770
         else
765
-            window.addEventListener('orientationchange', this._resize); // deprecated
771
+            window.addEventListener('orientationchange', resize); // deprecated
766 772
 
767 773
         // trigger a resize to setup the sizes / the CSS
768
-        setTimeout(this._resize, 0);
774
+        resize();
769 775
     }
770 776
 
771 777
     /**
@@ -784,6 +790,15 @@ abstract class ResizableViewport extends ViewportDecorator
784 790
     }
785 791
 
786 792
     /**
793
+     * Trigger a resize event
794
+     */
795
+    private _resize(): void
796
+    {
797
+        const event = new ViewportEvent('resize');
798
+        this.dispatchEvent(event);
799
+    }
800
+
801
+    /**
787 802
      * Function to be called when the viewport is resized
788 803
      */
789 804
     protected _onResize(): void
@@ -801,10 +816,6 @@ abstract class ResizableViewport extends ViewportDecorator
801 816
         const realSize = this._realSize;
802 817
         backgroundCanvas.width = realSize.width;
803 818
         backgroundCanvas.height = realSize.height;
804
-
805
-        // dispatch event
806
-        const event = new ViewportEvent('resize');
807
-        this.dispatchEvent(event);
808 819
     }
809 820
 }
810 821
 

Loading…
취소
저장