@egjs/flicking-plugins 4.2.2 → 4.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,11 @@
1
- import Flicking, { Plugin } from "@egjs/flicking";
2
- interface SyncOptions {
3
- type: "camera" | "index";
1
+ import Flicking from "@egjs/flicking";
2
+ import type { Plugin } from "@egjs/flicking";
3
+ import { SYNC } from "./const";
4
+ export interface SyncOptions {
5
+ type: typeof SYNC.TYPE.CAMERA | typeof SYNC.TYPE.INDEX;
4
6
  synchronizedFlickingOptions: SychronizableFlickingOptions[];
5
7
  }
6
- interface SychronizableFlickingOptions {
8
+ export interface SychronizableFlickingOptions {
7
9
  flicking: Flicking;
8
10
  isClickable?: boolean;
9
11
  isSlidable?: boolean;
@@ -28,8 +30,8 @@ declare class Sync implements Plugin {
28
30
  private _onMove;
29
31
  private _onMoveStart;
30
32
  private _onMoveEnd;
33
+ private _onSelect;
31
34
  private _synchronizeByIndex;
32
35
  private _updateClass;
33
- private _findNearsetPanel;
34
36
  }
35
37
  export default Sync;
@@ -2,7 +2,8 @@ import Parallax from "./Parallax";
2
2
  import Fade from "./Fade";
3
3
  import AutoPlay from "./AutoPlay";
4
4
  import Arrow from "./Arrow";
5
- import Sync from "./Sync";
5
+ import Sync, { SyncOptions, SychronizableFlickingOptions } from "./Sync";
6
6
  export * from "./pagination";
7
7
  export { Parallax, Fade, AutoPlay, Arrow, Sync };
8
+ export type { SyncOptions, SychronizableFlickingOptions };
8
9
  export * from "./const";
@@ -4,9 +4,9 @@ name: @egjs/flicking-plugins
4
4
  license: MIT
5
5
  author: NAVER Corp.
6
6
  repository: https://github.com/naver/egjs-flicking-plugins
7
- version: 4.2.2
7
+ version: 4.2.3
8
8
  */
9
- import { EVENTS, DIRECTION, FlickingError } from '@egjs/flicking';
9
+ import { EVENTS, DIRECTION, FlickingError, clamp } from '@egjs/flicking';
10
10
 
11
11
  /**
12
12
  * You can apply parallax effect while panel is moving.
@@ -719,46 +719,52 @@ function () {
719
719
 
720
720
  this._flicking = null;
721
721
 
722
- this._addEvents = function (synchronizedFlickingOptions) {
723
- synchronizedFlickingOptions.forEach(function (_a) {
722
+ this._addEvents = function () {
723
+ var type = _this._type;
724
+ var synced = _this._synchronizedFlickingOptions;
725
+ synced.forEach(function (_a) {
724
726
  var flicking = _a.flicking,
725
727
  isSlidable = _a.isSlidable,
726
728
  isClickable = _a.isClickable;
727
729
 
728
- if (_this._type === "camera") {
730
+ if (type === SYNC.TYPE.CAMERA) {
729
731
  flicking.on(EVENTS.MOVE, _this._onMove);
730
732
  flicking.on(EVENTS.MOVE_START, _this._onMoveStart);
731
733
  flicking.on(EVENTS.MOVE_END, _this._onMoveEnd);
732
734
  }
733
735
 
734
- if (_this._type === "index" && isSlidable) {
736
+ if (type === SYNC.TYPE.INDEX && isSlidable) {
735
737
  flicking.on(EVENTS.WILL_CHANGE, _this._onIndexChange);
738
+ flicking.on(EVENTS.WILL_RESTORE, _this._onIndexChange);
736
739
  }
737
740
 
738
741
  if (isClickable) {
739
- flicking.on(EVENTS.SELECT, _this._onIndexChange);
742
+ flicking.on(EVENTS.SELECT, _this._onSelect);
740
743
  }
741
744
  });
742
745
  };
743
746
 
744
- this._removeEvents = function (synchronizedFlickingOptions) {
745
- synchronizedFlickingOptions.forEach(function (_a) {
747
+ this._removeEvents = function () {
748
+ var type = _this._type;
749
+ var synced = _this._synchronizedFlickingOptions;
750
+ synced.forEach(function (_a) {
746
751
  var flicking = _a.flicking,
747
752
  isSlidable = _a.isSlidable,
748
753
  isClickable = _a.isClickable;
749
754
 
750
- if (_this._type === "camera") {
755
+ if (type === SYNC.TYPE.CAMERA) {
751
756
  flicking.off(EVENTS.MOVE, _this._onMove);
752
757
  flicking.off(EVENTS.MOVE_START, _this._onMoveStart);
753
758
  flicking.off(EVENTS.MOVE_END, _this._onMoveEnd);
754
759
  }
755
760
 
756
- if (_this._type === "index" && isSlidable) {
761
+ if (type === SYNC.TYPE.INDEX && isSlidable) {
757
762
  flicking.off(EVENTS.WILL_CHANGE, _this._onIndexChange);
763
+ flicking.off(EVENTS.WILL_RESTORE, _this._onIndexChange);
758
764
  }
759
765
 
760
766
  if (isClickable) {
761
- flicking.off(EVENTS.SELECT, _this._onIndexChange);
767
+ flicking.off(EVENTS.SELECT, _this._onSelect);
762
768
  }
763
769
  });
764
770
  };
@@ -779,16 +785,18 @@ function () {
779
785
 
780
786
  _this._synchronizedFlickingOptions.forEach(function (_a) {
781
787
  var flicking = _a.flicking;
788
+ if (flicking === e.currentTarget) return;
789
+ var targetPosition = 0;
782
790
 
783
- if (flicking !== e.currentTarget) {
784
- if (camera.position < camera.range.min) {
785
- void flicking.camera.lookAt(camera.position);
786
- } else if (camera.position > camera.range.max) {
787
- void flicking.camera.lookAt(flicking.camera.range.max + camera.position - camera.range.max);
788
- } else {
789
- void flicking.camera.lookAt(flicking.camera.range.min + flicking.camera.rangeDiff * progress);
790
- }
791
+ if (camera.position < camera.range.min) {
792
+ targetPosition = camera.position;
793
+ } else if (camera.position > camera.range.max) {
794
+ targetPosition = flicking.camera.range.max + camera.position - camera.range.max;
795
+ } else {
796
+ targetPosition = flicking.camera.range.min + flicking.camera.rangeDiff * progress;
791
797
  }
798
+
799
+ void flicking.camera.lookAt(targetPosition);
792
800
  });
793
801
  };
794
802
 
@@ -813,59 +821,53 @@ function () {
813
821
  });
814
822
  };
815
823
 
816
- this._synchronizeByIndex = function (activeFlicking, index) {
817
- var synchronizedFlickingOptions = _this._synchronizedFlickingOptions;
818
- var activePanel = activeFlicking.panels.find(function (panel) {
819
- return panel.index === index;
824
+ this._onSelect = function (e) {
825
+ void e.currentTarget.moveTo(e.index).catch(function () {
826
+ return void 0;
820
827
  });
821
- var lastPanel = activeFlicking.panels[activeFlicking.panels.length - 1];
822
-
823
- if (!activePanel) {
824
- return;
825
- }
826
828
 
827
- _this._preventEvent(function () {
828
- synchronizedFlickingOptions.forEach(function (_a) {
829
- var flicking = _a.flicking,
830
- activeClass = _a.activeClass; // calculate new target flicking position with active flicking size and target flicking size
829
+ _this._synchronizeByIndex(e.currentTarget, e.index);
830
+ };
831
831
 
832
- var targetLastPanel = flicking.panels[flicking.panels.length - 1];
833
- var targetPos = activePanel.position / (lastPanel.position + lastPanel.size / 2) * (targetLastPanel.position + targetLastPanel.size / 2);
834
- flicking.control.moveToPosition(targetPos, 500).catch(function () {
835
- return void 0;
836
- });
832
+ this._synchronizeByIndex = function (activeFlicking, index) {
833
+ var synchronizedFlickingOptions = _this._synchronizedFlickingOptions;
837
834
 
838
- if (activeClass) {
839
- _this._updateClass({
840
- flicking: flicking,
841
- activeClass: activeClass
842
- }, targetPos);
835
+ _this._preventEvent(function () {
836
+ synchronizedFlickingOptions.forEach(function (options) {
837
+ // Active class should be applied same to the Flicking which triggered event
838
+ _this._updateClass(options, index);
839
+
840
+ var flicking = options.flicking;
841
+ if (flicking === activeFlicking) return;
842
+ var targetIndex = clamp(index, 0, flicking.panels.length);
843
+
844
+ if (flicking.animating) {
845
+ // Reserve moveTo once previous animation is finished
846
+ flicking.once(EVENTS.MOVE_END, function () {
847
+ void flicking.moveTo(targetIndex).catch(function () {
848
+ return void 0;
849
+ });
850
+ });
851
+ } else {
852
+ void flicking.moveTo(targetIndex);
843
853
  }
844
854
  });
845
855
  });
846
856
  };
847
857
 
848
- this._updateClass = function (synchronizedFlickingOption, pos) {
849
- var target = _this._findNearsetPanel(synchronizedFlickingOption.flicking, pos);
850
-
851
- synchronizedFlickingOption.flicking.panels.forEach(function (panel) {
852
- return panel.index === target.index ? addClass(panel.element, synchronizedFlickingOption.activeClass) : removeClass(panel.element, synchronizedFlickingOption.activeClass);
858
+ this._updateClass = function (_a, index) {
859
+ var flicking = _a.flicking,
860
+ activeClass = _a.activeClass;
861
+ if (!activeClass) return;
862
+ flicking.panels.forEach(function (panel) {
863
+ if (panel.index === index) {
864
+ addClass(panel.element, activeClass);
865
+ } else {
866
+ removeClass(panel.element, activeClass);
867
+ }
853
868
  });
854
869
  };
855
870
 
856
- this._findNearsetPanel = function (flicking, pos) {
857
- var nearsetIndex = flicking.panels.reduce(function (nearest, panel, index) {
858
- return Math.abs(panel.position - pos) <= nearest.range ? {
859
- index: index,
860
- range: Math.abs(panel.position - pos)
861
- } : nearest;
862
- }, {
863
- index: 0,
864
- range: Infinity
865
- }).index;
866
- return flicking.panels[nearsetIndex];
867
- };
868
-
869
871
  this._type = type;
870
872
  this._synchronizedFlickingOptions = synchronizedFlickingOptions;
871
873
  }
@@ -876,11 +878,7 @@ function () {
876
878
  return this._type;
877
879
  },
878
880
  set: function (val) {
879
- var _this = this;
880
-
881
- this._preventEvent(function () {
882
- _this._type = val;
883
- });
881
+ this._type = val;
884
882
  },
885
883
  enumerable: false,
886
884
  configurable: true
@@ -890,11 +888,7 @@ function () {
890
888
  return this._synchronizedFlickingOptions;
891
889
  },
892
890
  set: function (val) {
893
- var _this = this;
894
-
895
- this._preventEvent(function () {
896
- _this._synchronizedFlickingOptions = val;
897
- });
891
+ this._synchronizedFlickingOptions = val;
898
892
  },
899
893
  enumerable: false,
900
894
  configurable: true
@@ -903,16 +897,20 @@ function () {
903
897
  __proto.init = function (flicking) {
904
898
  var _this = this;
905
899
 
900
+ var synced = this._synchronizedFlickingOptions;
901
+
906
902
  if (this._flicking) {
907
903
  this.destroy();
908
904
  }
909
905
 
910
906
  this._flicking = flicking;
911
907
 
912
- this._addEvents(this._synchronizedFlickingOptions);
908
+ this._addEvents();
913
909
 
914
- this._synchronizedFlickingOptions.forEach(function (synchronizedFlickingOption) {
915
- _this._updateClass(synchronizedFlickingOption, 0);
910
+ synced.forEach(function (options) {
911
+ var syncedFlicking = options.flicking;
912
+
913
+ _this._updateClass(options, syncedFlicking.defaultIndex);
916
914
  });
917
915
  };
918
916
 
@@ -923,7 +921,7 @@ function () {
923
921
  return;
924
922
  }
925
923
 
926
- this._removeEvents(this._synchronizedFlickingOptions);
924
+ this._removeEvents();
927
925
 
928
926
  this._flicking = null;
929
927
  };
@@ -931,23 +929,17 @@ function () {
931
929
  __proto.update = function () {
932
930
  var _this = this;
933
931
 
934
- this._synchronizedFlickingOptions.forEach(function (_a) {
935
- var flicking = _a.flicking,
936
- activeClass = _a.activeClass;
937
-
938
- _this._updateClass({
939
- flicking: flicking,
940
- activeClass: activeClass
941
- }, flicking.camera.position);
932
+ this._synchronizedFlickingOptions.forEach(function (options) {
933
+ _this._updateClass(options, options.flicking.index);
942
934
  });
943
935
  };
944
936
 
945
937
  __proto._preventEvent = function (fn) {
946
- this._removeEvents(this._synchronizedFlickingOptions);
938
+ this._removeEvents();
947
939
 
948
940
  fn();
949
941
 
950
- this._addEvents(this._synchronizedFlickingOptions);
942
+ this._addEvents();
951
943
  };
952
944
 
953
945
  return Sync;