Skip to content

Commit 2ef00e0

Browse files
author
Constantin Groß
committed
restructured initialization in order to prevent js erros when recolorSVG was used but the marker svg had not been loaded by the time the clusters were created
1 parent dd75189 commit 2ef00e0

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

src/datalayerclusterer.js

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,32 @@ function DataLayerClusterer(optOptions) {
9696
this.recolorSVG_ = typeof options.recolorSVG !== "undefined" && (typeof options.recolorSVG === "string" || options.recolorSVG instanceof String || options.recolorSVG === false) ? options.recolorSVG : 'g:first-child';
9797
this.baseSVG_ = null;
9898

99-
this.setupStyles_();
99+
if (this.recolorSVG_ && this.imageExtension_ == 'svg') {
100+
var self = this,
101+
xhr = new XMLHttpRequest();
102+
xhr.open("GET",/\.svg$/.test(this.imagePath_) ? this.imagePath_ : this.imagePath_ + '1.' + this.imageExtension_);
103+
// Following line is just to be on the safe side;
104+
// not needed if your server delivers SVG with correct MIME type
105+
xhr.overrideMimeType("image/svg+xml");
106+
xhr.send("");
100107

101-
if (this.setProperty_) {
102-
this._dataLayer.forEach(function(feature) {
103-
feature.setProperty('in_cluster', true);
104-
});
105-
} else {
106-
this._dataLayer.setStyle(DataLayerClusterer.HIDDEN_FEATURE);
107-
}
108-
if (this.map !== null) {
109-
this.setMap(this.map);
110-
}
108+
xhr.onreadystatechange = function() {
109+
if (this.readyState == 4) {
110+
if (this.status == 200) {
111+
self.baseSVG_ = {
112+
'document': xhr.responseXML.documentElement,
113+
'colorElement': xhr.responseXML.documentElement.querySelector(self.recolorSVG_)
114+
};
115+
if (!self.baseSVG_.document || !self.baseSVG_.colorElement) {
116+
self.recolorSVG_ = false;
117+
}
118+
} else {
119+
self.recolorSVG_ = false;
120+
}
121+
self.init_();
122+
}
123+
};
124+
} else this.init_();
111125
}
112126

113127
/* ---- Constants ---- */
@@ -1134,6 +1148,27 @@ DataLayerClusterer.MARKER_CLUSTER_IMAGE_PATH_ =
11341148
'https://cdn.rawgit.com/Connum/data-layer-clusterer/master/images/m';
11351149
DataLayerClusterer.MARKER_CLUSTER_IMAGE_EXTENSION_ = document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image", "1.1") ? 'svg' : 'png';
11361150

1151+
1152+
/**
1153+
* Initialises the clustering when everything is ready
1154+
*
1155+
* @private
1156+
*/
1157+
DataLayerClusterer.prototype.init_ = function() {
1158+
this.setupStyles_();
1159+
1160+
if (this.setProperty_) {
1161+
this._dataLayer.forEach(function(feature) {
1162+
feature.setProperty('in_cluster', true);
1163+
});
1164+
} else {
1165+
this._dataLayer.setStyle(DataLayerClusterer.HIDDEN_FEATURE);
1166+
}
1167+
if (this.map !== null) {
1168+
this.setMap(this.map);
1169+
}
1170+
}
1171+
11371172
/**
11381173
* Sets up the styles object.
11391174
*
@@ -1144,34 +1179,6 @@ DataLayerClusterer.prototype.setupStyles_ = function() {
11441179
return;
11451180
}
11461181

1147-
if (!this.baseSVG_ && this.recolorSVG_ && this.imageExtension_ == 'svg') {
1148-
var self = this,
1149-
xhr = new XMLHttpRequest();
1150-
xhr.open("GET",/\.svg$/.test(this.imagePath_) ? this.imagePath_ : this.imagePath_ + '1.' + this.imageExtension_);
1151-
// Following line is just to be on the safe side;
1152-
// not needed if your server delivers SVG with correct MIME type
1153-
xhr.overrideMimeType("image/svg+xml");
1154-
xhr.send("");
1155-
1156-
xhr.onreadystatechange = function() {
1157-
if (this.readyState == 4) {
1158-
if (this.status == 200) {
1159-
self.baseSVG_ = {
1160-
'document': xhr.responseXML.documentElement,
1161-
'colorElement': xhr.responseXML.documentElement.querySelector(self.recolorSVG_)
1162-
};
1163-
if (!self.baseSVG_.document || !self.baseSVG_.colorElement) {
1164-
self.recolorSVG_ = false;
1165-
}
1166-
} else {
1167-
self.recolorSVG_ = false;
1168-
}
1169-
self.setupStyles_();
1170-
}
1171-
};
1172-
return;
1173-
}
1174-
11751182
var ssizes = this.sizes.length;
11761183
for (var i = 0; i !== ssizes; ++i) {
11771184
var thisSize = this.sizes[i],

0 commit comments

Comments
 (0)