react-map4d-map 1.0.7 → 1.0.9

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,4 +1,4 @@
1
- import React from 'react';
1
+ /// <reference types="react" />
2
2
  export declare function addLibrary(url: string, id: string): HTMLScriptElement | null;
3
3
  interface MapOptions {
4
4
  center?: any;
@@ -22,26 +22,12 @@ interface MapProps {
22
22
  version: string;
23
23
  accessKey: string;
24
24
  environment?: "dev" | "test" | undefined | null;
25
- onMapReady: (map: any, key?: string) => void;
25
+ onMapReady?: (map: any, key?: string) => void;
26
26
  onClickLocation?: (args: any) => void;
27
27
  onRightClickLocation?: (args: any) => void;
28
28
  onCameraChanging?: (args: any) => void;
29
29
  onTilesLoaded?: (args: any) => void;
30
+ children: any;
30
31
  }
31
- declare class MFMap extends React.Component<MapProps, any> {
32
- mapKey: string;
33
- callback: string;
34
- mapDomRef: any;
35
- mapRef: map4d.Map;
36
- scriptElement: any;
37
- url: string | null;
38
- events: any[];
39
- constructor(props: MapProps);
40
- setMapDomRef(e: any): void;
41
- componentWillUnmount(): void;
42
- destroy(): void;
43
- registerEvent(): void;
44
- createCallback(): void;
45
- render(): JSX.Element;
46
- }
32
+ declare const MFMap: (props: MapProps) => JSX.Element;
47
33
  export default MFMap;
@@ -1,7 +1,7 @@
1
1
  /// <reference types="react" />
2
2
  export declare const defaultCt: {
3
- map: null;
3
+ map: any;
4
4
  };
5
5
  export declare const Map4dContext: import("react").Context<{
6
- map: null;
6
+ map: any;
7
7
  }>;
package/dist/index.js CHANGED
@@ -709,29 +709,6 @@ var MFImageOverlay = function MFImageOverlay(props) {
709
709
  return null;
710
710
  };
711
711
 
712
- function _inheritsLoose(subClass, superClass) {
713
- subClass.prototype = Object.create(superClass.prototype);
714
- subClass.prototype.constructor = subClass;
715
-
716
- _setPrototypeOf(subClass, superClass);
717
- }
718
-
719
- function _setPrototypeOf(o, p) {
720
- _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
721
- o.__proto__ = p;
722
- return o;
723
- };
724
- return _setPrototypeOf(o, p);
725
- }
726
-
727
- function _assertThisInitialized(self) {
728
- if (self === void 0) {
729
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
730
- }
731
-
732
- return self;
733
- }
734
-
735
712
  function addLibrary(url, id) {
736
713
  var scriptId = "script_" + id;
737
714
  var exist = document.getElementById(scriptId);
@@ -758,184 +735,159 @@ var createId = function createId() {
758
735
  return uuid;
759
736
  };
760
737
 
761
- var MFMap = /*#__PURE__*/function (_React$Component) {
762
- _inheritsLoose(MFMap, _React$Component);
738
+ var MFMap = function MFMap(props) {
739
+ var mapKey = React.useRef(createId());
740
+ var callback = React.useRef("callback_" + mapKey.current);
741
+ var mapDomRef = React.useRef();
742
+ var mapRef = React.useRef();
743
+ var scriptElement = React.useRef();
744
+ var url = React.useRef();
745
+ var events = React.useRef([]);
746
+
747
+ var _useState = React.useState(),
748
+ map = _useState[0],
749
+ setMap = _useState[1];
750
+
751
+ React.useEffect(function () {
752
+ var createCallback = function createCallback() {
753
+ window[callback.current] = function () {
754
+ if (mapDomRef.current) {
755
+ var options = props.options;
756
+ mapRef.current = new window.map4d.Map(mapDomRef.current, options);
757
+
758
+ if (props.onMapReady) {
759
+ props.onMapReady(mapRef.current, mapKey.current);
760
+ }
761
+
762
+ registerEvent();
763
+ setMap(mapRef.current);
764
+ } else {
765
+ console.error("MFMap: map element is NOT found");
766
+ }
767
+ };
768
+ };
763
769
 
764
- function MFMap(props) {
765
- var _this;
770
+ var urlNew = "https://api" + (props.environment ? "-" + props.environment : "") + ".map4d.vn/sdk/map/js?version=" + props.version + "&key=" + props.accessKey + "&callback=" + callback.current;
766
771
 
767
- _this = _React$Component.call(this, props) || this;
768
- _this.events = [];
769
- _this.state = {
770
- map: null
771
- };
772
- _this.mapKey = createId();
773
- _this.callback = "callback_" + _this.mapKey;
774
- _this.setMapDomRef = _this.setMapDomRef.bind(_assertThisInitialized(_this));
775
- return _this;
776
- }
772
+ if (props.mapid) {
773
+ urlNew += "&mapId=" + props.mapid;
774
+ }
777
775
 
778
- var _proto = MFMap.prototype;
776
+ createCallback();
777
+ var script = addLibrary(urlNew, mapKey.current);
779
778
 
780
- _proto.setMapDomRef = function setMapDomRef(e) {
781
- this.mapDomRef = e;
782
- };
779
+ if (script) {
780
+ scriptElement.current = script;
781
+ }
783
782
 
784
- _proto.componentWillUnmount = function componentWillUnmount() {
785
- this.destroy();
786
- };
783
+ url.current = urlNew;
784
+ return function () {
785
+ var destroy = function destroy() {
786
+ var _events$current, _mapRef$current;
787
787
 
788
- _proto.destroy = function destroy() {
789
- var _this$events, _this$mapRef;
788
+ delete window[callback.current];
790
789
 
791
- delete window[this.callback];
790
+ if (scriptElement.current != null) {
791
+ scriptElement.current.remove();
792
+ }
792
793
 
793
- if (this.scriptElement != null) {
794
- this.scriptElement.remove();
795
- }
794
+ (_events$current = events.current) === null || _events$current === void 0 ? void 0 : _events$current.forEach(function (t) {
795
+ t === null || t === void 0 ? void 0 : t.remove();
796
+ });
797
+ (_mapRef$current = mapRef.current) === null || _mapRef$current === void 0 ? void 0 : _mapRef$current.destroy();
798
+ url.current = undefined;
799
+ };
796
800
 
797
- (_this$events = this.events) === null || _this$events === void 0 ? void 0 : _this$events.forEach(function (t) {
798
- t === null || t === void 0 ? void 0 : t.remove();
799
- });
800
- ((_this$mapRef = this.mapRef) === null || _this$mapRef === void 0 ? void 0 : _this$mapRef.destroy) && this.mapRef.destroy();
801
- this.url = null;
802
- };
801
+ destroy();
802
+ };
803
+ }, [props.environment, props.version, props.accessKey]);
803
804
 
804
- _proto.registerEvent = function registerEvent() {
805
- var _this$mapRef2,
806
- _this$mapRef3,
807
- _this$mapRef4,
808
- _this2 = this;
805
+ var registerEvent = function registerEvent() {
806
+ var _mapRef$current2, _mapRef$current3, _mapRef$current4;
809
807
 
810
- var eventClickMarker = (_this$mapRef2 = this.mapRef) === null || _this$mapRef2 === void 0 ? void 0 : _this$mapRef2.addListener("click", function (args) {
808
+ var eventClickMarker = (_mapRef$current2 = mapRef.current) === null || _mapRef$current2 === void 0 ? void 0 : _mapRef$current2.addListener("click", function (args) {
811
809
  var _args$marker, _args$marker2;
812
810
 
813
811
  ((_args$marker = args.marker) === null || _args$marker === void 0 ? void 0 : _args$marker.onClick) && ((_args$marker2 = args.marker) === null || _args$marker2 === void 0 ? void 0 : _args$marker2.onClick());
814
812
  }, {
815
813
  marker: true
816
814
  });
817
- var eventRightClickMarker = (_this$mapRef3 = this.mapRef) === null || _this$mapRef3 === void 0 ? void 0 : _this$mapRef3.addListener("rightClick", function (args) {
815
+ var eventRightClickMarker = (_mapRef$current3 = mapRef.current) === null || _mapRef$current3 === void 0 ? void 0 : _mapRef$current3.addListener("rightClick", function (args) {
818
816
  var _args$marker3, _args$marker4;
819
817
 
820
818
  ((_args$marker3 = args.marker) === null || _args$marker3 === void 0 ? void 0 : _args$marker3.onRightClick) && ((_args$marker4 = args.marker) === null || _args$marker4 === void 0 ? void 0 : _args$marker4.onRightClick());
821
819
  }, {
822
820
  marker: true
823
821
  });
824
- var eventDragEndMarker = (_this$mapRef4 = this.mapRef) === null || _this$mapRef4 === void 0 ? void 0 : _this$mapRef4.addListener("dragEnd", function (args) {
822
+ var eventDragEndMarker = (_mapRef$current4 = mapRef.current) === null || _mapRef$current4 === void 0 ? void 0 : _mapRef$current4.addListener("dragEnd", function (args) {
825
823
  var _args$marker5, _args$marker6;
826
824
 
827
825
  ((_args$marker5 = args.marker) === null || _args$marker5 === void 0 ? void 0 : _args$marker5.onDragEnd) && ((_args$marker6 = args.marker) === null || _args$marker6 === void 0 ? void 0 : _args$marker6.onDragEnd());
828
826
  }, {
829
827
  marker: true
830
828
  });
831
- this.events = this.events.concat([eventClickMarker, eventRightClickMarker, eventDragEndMarker]);
829
+ events.current = events.current.concat([eventClickMarker, eventRightClickMarker, eventDragEndMarker]);
832
830
 
833
- if (this.props.onClickLocation) {
834
- var _this$mapRef5;
831
+ if (props.onClickLocation) {
832
+ var _mapRef$current5;
835
833
 
836
- var onClickLocation = (_this$mapRef5 = this.mapRef) === null || _this$mapRef5 === void 0 ? void 0 : _this$mapRef5.addListener("click", function (args) {
837
- _this2.props.onClickLocation && _this2.props.onClickLocation(args);
834
+ var onClickLocation = (_mapRef$current5 = mapRef.current) === null || _mapRef$current5 === void 0 ? void 0 : _mapRef$current5.addListener("click", function (args) {
835
+ props.onClickLocation && props.onClickLocation(args);
838
836
  }, {
839
837
  location: true
840
838
  });
841
- this.events.push(onClickLocation);
839
+ events.current.push(onClickLocation);
842
840
  }
843
841
 
844
- if (this.props.onRightClickLocation) {
845
- var _this$mapRef6;
842
+ if (props.onRightClickLocation) {
843
+ var _mapRef$current6;
846
844
 
847
- var onRightClickLocation = (_this$mapRef6 = this.mapRef) === null || _this$mapRef6 === void 0 ? void 0 : _this$mapRef6.addListener("rightClick", function (args) {
848
- _this2.props.onRightClickLocation && _this2.props.onRightClickLocation(args);
845
+ var onRightClickLocation = (_mapRef$current6 = mapRef.current) === null || _mapRef$current6 === void 0 ? void 0 : _mapRef$current6.addListener("rightClick", function (args) {
846
+ props.onRightClickLocation && props.onRightClickLocation(args);
849
847
  }, {
850
848
  location: true
851
849
  });
852
- this.events.push(onRightClickLocation);
850
+ events.current.push(onRightClickLocation);
853
851
  }
854
852
 
855
- if (this.props.onTilesLoaded) {
856
- var _this$mapRef7;
853
+ if (props.onTilesLoaded) {
854
+ var _mapRef$current7;
857
855
 
858
- var onTilesLoaded = (_this$mapRef7 = this.mapRef) === null || _this$mapRef7 === void 0 ? void 0 : _this$mapRef7.addListener("tilesLoaded", function (args) {
859
- _this2.props.onTilesLoaded && _this2.props.onTilesLoaded(args);
856
+ var onTilesLoaded = (_mapRef$current7 = mapRef.current) === null || _mapRef$current7 === void 0 ? void 0 : _mapRef$current7.addListener("tilesLoaded", function (args) {
857
+ props.onTilesLoaded && props.onTilesLoaded(args);
860
858
  }, {
861
859
  location: true
862
860
  });
863
- this.events.push(onTilesLoaded);
861
+ events.current.push(onTilesLoaded);
864
862
  }
865
863
 
866
- if (this.props.onCameraChanging) {
867
- var _this$mapRef8;
864
+ if (props.onCameraChanging) {
865
+ var _mapRef$current8;
868
866
 
869
- var onCameraChanging = (_this$mapRef8 = this.mapRef) === null || _this$mapRef8 === void 0 ? void 0 : _this$mapRef8.addListener("cameraChanging", function (args) {
870
- _this2.props.onCameraChanging && _this2.props.onCameraChanging(args);
867
+ var onCameraChanging = (_mapRef$current8 = mapRef.current) === null || _mapRef$current8 === void 0 ? void 0 : _mapRef$current8.addListener("cameraChanging", function (args) {
868
+ props.onCameraChanging && props.onCameraChanging(args);
871
869
  }, {
872
870
  location: true
873
871
  });
874
- this.events.push(onCameraChanging);
872
+ events.current.push(onCameraChanging);
875
873
  }
876
874
  };
877
875
 
878
- _proto.createCallback = function createCallback() {
879
- var _this3 = this;
880
-
881
- window[this.callback] = function () {
882
- if (_this3.mapDomRef) {
883
- var options = _this3.props.options;
884
- _this3.mapRef = new window.map4d.Map(_this3.mapDomRef, options);
885
-
886
- if (_this3.props.onMapReady) {
887
- _this3.props.onMapReady(_this3.mapRef, _this3.mapKey);
888
- }
889
-
890
- _this3.registerEvent();
891
-
892
- _this3.setState({
893
- map: _this3.mapRef
894
- });
895
- } else {
896
- console.error("MFMap: map element is NOT found");
897
- }
898
- };
876
+ var newContext = {
877
+ map: map
899
878
  };
900
-
901
- _proto.render = function render() {
902
- var url = "https://api" + (this.props.environment ? "-" + this.props.environment : "") + ".map4d.vn/sdk/map/js?version=" + this.props.version + "&key=" + this.props.accessKey + "&callback=" + this.callback;
903
-
904
- if (this.props.mapid) {
905
- url += "&mapId=" + this.props.mapid;
906
- }
907
-
908
- if (url != this.url) {
909
- this.destroy();
910
- this.createCallback();
911
- var script = addLibrary(url, this.mapKey);
912
-
913
- if (script) {
914
- this.scriptElement = script;
915
- }
916
- }
917
-
918
- this.url = url;
919
- var newContext = {
920
- map: this.state.map
921
- };
922
- return React__default.createElement(Map4dContext.Provider, {
923
- value: newContext
924
- }, React__default.createElement("div", {
925
- style: {
926
- width: '100%',
927
- height: '100%',
928
- display: 'block'
929
- },
930
- id: "" + this.mapKey,
931
- ref: this.setMapDomRef
932
- }), this.props.children);
933
- };
934
-
935
- return MFMap;
936
- }(React__default.Component);
937
-
938
- MFMap.contextType = Map4dContext;
879
+ return React__default.createElement(Map4dContext.Provider, {
880
+ value: newContext
881
+ }, React__default.createElement("div", {
882
+ style: {
883
+ width: '100%',
884
+ height: '100%',
885
+ display: 'block'
886
+ },
887
+ id: mapKey.current,
888
+ ref: mapDomRef.current
889
+ }), props.children);
890
+ };
939
891
 
940
892
  var MFMarker = function MFMarker(props) {
941
893
  var position = props.position,