react-map4d-map 1.0.6 → 1.0.8

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