소스 검색

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
         if(value != 'best-fit' && value != 'stretch' && value != 'inline')
333
         if(value != 'best-fit' && value != 'stretch' && value != 'inline')
334
             throw new IllegalArgumentError('Invalid viewport style: ' + value);
334
             throw new IllegalArgumentError('Invalid viewport style: ' + value);
335
 
335
 
336
+        const changed = (value != this._style);
336
         this._style = value;
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
     /** is this viewport subject to being resized? */
717
     /** is this viewport subject to being resized? */
712
     private _active: boolean;
718
     private _active: boolean;
713
 
719
 
714
-    /** a bound _onResize */
715
-    private _resize: () => void;
716
-
717
 
720
 
718
 
721
 
719
     /**
722
     /**
725
     {
728
     {
726
         super(base, getSize);
729
         super(base, getSize);
727
         this._active = false;
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
         super._init();
740
         super._init();
738
         this._active = true;
741
         this._active = true;
739
 
742
 
743
+        // bound resize
744
+        const resize = this._resize.bind(this);
745
+
740
         // Configure the resize listener. We want the viewport
746
         // Configure the resize listener. We want the viewport
741
         // to adjust itself if the phone/screen is resized or
747
         // to adjust itself if the phone/screen is resized or
742
         // changes orientation
748
         // changes orientation
743
         let timeout: Nullable<ReturnType<typeof setTimeout>> = null;
749
         let timeout: Nullable<ReturnType<typeof setTimeout>> = null;
744
-        const onresize = () => {
750
+        const onWindowResize = () => {
745
             if(!this._active) {
751
             if(!this._active) {
746
-                window.removeEventListener('resize', onresize);
752
+                window.removeEventListener('resize', onWindowResize);
747
                 return;
753
                 return;
748
             }
754
             }
749
 
755
 
752
 
758
 
753
             timeout = setTimeout(() => {
759
             timeout = setTimeout(() => {
754
                 timeout = null;
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
         // handle changes of orientation
766
         // handle changes of orientation
761
         // (is this needed? we already listen to resize events)
767
         // (is this needed? we already listen to resize events)
762
         if(screen.orientation !== undefined)
768
         if(screen.orientation !== undefined)
763
-            screen.orientation.addEventListener('change', this._resize);
769
+            screen.orientation.addEventListener('change', resize);
764
         else
770
         else
765
-            window.addEventListener('orientationchange', this._resize); // deprecated
771
+            window.addEventListener('orientationchange', resize); // deprecated
766
 
772
 
767
         // trigger a resize to setup the sizes / the CSS
773
         // trigger a resize to setup the sizes / the CSS
768
-        setTimeout(this._resize, 0);
774
+        resize();
769
     }
775
     }
770
 
776
 
771
     /**
777
     /**
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
      * Function to be called when the viewport is resized
802
      * Function to be called when the viewport is resized
788
      */
803
      */
789
     protected _onResize(): void
804
     protected _onResize(): void
801
         const realSize = this._realSize;
816
         const realSize = this._realSize;
802
         backgroundCanvas.width = realSize.width;
817
         backgroundCanvas.width = realSize.width;
803
         backgroundCanvas.height = realSize.height;
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…
취소
저장