Skip to content

Commit c9a65d2

Browse files
author
Fanny DECLERCK
authored
Merge pull request #105 from Coosos/fix-a-tag-undefined-url-and-add-feature
Fix a tag, undefined url + feature
2 parents 4b2d5a0 + 7b69675 commit c9a65d2

File tree

2 files changed

+87
-60
lines changed

2 files changed

+87
-60
lines changed

view/frontend/web/js/map-mixin.js

Lines changed: 86 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ define([
44
'ko',
55
'smile-storelocator-store-collection',
66
'Smile_StoreLocator/js/model/store/schedule',
7-
], function ($, L, ko, registry, MarkersList, Schedule) {
7+
], function ($, L, ko, MarkersList, Schedule) {
88
'use strict';
99

1010
var mixin = {
1111

12+
markerHasDistance: false,
13+
1214
/**
1315
* Init markers on the map
1416
*/
1517
initMarkers: function() {
16-
var markersList = new MarkersList({items : this.markers});
17-
this.markers = markersList.getList();
18+
this.markers = new MarkersList({items : this.markers}).getList();
1819
this.markers.forEach(function(marker) {
1920
marker.distance = ko.observable(0);
2021
marker.distanceBetween = ko.observable(0);
@@ -29,7 +30,8 @@ define([
2930
marker.shopStatus = ko.observable(0);
3031
});
3132
}
32-
this.displayedMarkers = ko.observable(this.markers);
33+
34+
this.displayedMarkers = ko.observableArray(this.markers);
3335
},
3436

3537
/**
@@ -88,9 +90,13 @@ define([
8890
* @param position
8991
*/
9092
displayPositionAndDistance: function(position) {
91-
if(position.coords.longitude != undefined) {
93+
if(position.coords.longitude !== undefined) {
9294
this.addMarkerWithMyPosition(position);
9395
this.applyDistanceBetween(position);
96+
this.changeDisplayList(
97+
this.markers(),
98+
new L.latLng(position.coords.latitude, position.coords.longitude)
99+
);
94100
}
95101
},
96102

@@ -100,21 +106,24 @@ define([
100106
* @param position
101107
*/
102108
applyDistanceBetween: function (position) {
103-
var newLat = position.coords.latitude;
104-
var newLon = position.coords.longitude;
105-
var coords = new L.latLng(newLat, newLon);
106-
this.changeDisplayList(this.markers(), coords);
109+
let coords = new L.latLng(position.coords.latitude, position.coords.longitude);
110+
107111
this.markers().forEach(function (marker) {
108-
var itemPosition = new L.LatLng(marker.latitude, marker.longitude);
109-
var distanceFromCoords = itemPosition.distanceTo(coords);
110-
var result = (distanceFromCoords / 1000).toFixed(1);
112+
let itemPosition = new L.LatLng(marker.latitude, marker.longitude),
113+
distanceFromCoords = itemPosition.distanceTo(coords),
114+
result = (distanceFromCoords / 1000).toFixed(1);
115+
111116
if(result === '0.0') {
112117
result = (distanceFromCoords / 1000).toFixed(3) + ' m';
113118
} else {
114119
result = (distanceFromCoords / 1000).toFixed(1) + ' km';
115120
}
121+
122+
marker.distance(distanceFromCoords);
116123
marker.distanceBetween(result);
117124
});
125+
126+
this.markerHasDistance = true;
118127
},
119128

120129
/**
@@ -124,14 +133,12 @@ define([
124133
* @param bounds
125134
*/
126135
changeDisplayList: function (markers, bounds) {
136+
let nearbyMarkers = markers;
137+
127138
if (this.geocoder) {
128-
var nearbyMarkers = this.geocoder.filterMarkersListByPositionRadius(this.markers(), bounds);
129-
nearbyMarkers = nearbyMarkers.sort(function(a, b) {
130-
var distanceA = ko.isObservable(a['distance']) ? a['distance']() : a['distance'],
131-
distanceB = ko.isObservable(b['distance']) ? b['distance']() : b['distance'];
132-
return ((distanceA < distanceB) ? - 1 : ((distanceA > distanceB) ? 1 : 0));
133-
});
139+
nearbyMarkers = this.geocoder.filterMarkersListByPositionRadius(markers, bounds);
134140
}
141+
135142
this.displayedMarkers(nearbyMarkers);
136143
},
137144

@@ -149,29 +156,27 @@ define([
149156
* Load the markers and centers the map on them.
150157
*/
151158
loadMarkers: function() {
152-
var markers = [],
159+
let markers = [],
153160
isMarkerCluster = this.marker_cluster === '1';
154-
var icon = L.icon({iconUrl: this.markerIcon, iconSize: this.markerIconSize});
161+
155162
this.markers().forEach(function(markerData) {
156-
var currentMarker = [markerData.latitude, markerData.longitude];
157-
var markerOptionLocator = L.divIcon({
158-
iconSize: null,
159-
html: '<div class="custum-lf-popup" data-lat="'+ markerData.latitude +'" data-lon="'+ markerData.longitude +'" data-n="'+ markerData.name +'"><div class="button-decor"></div><a href="'+ markerData.url +'" </div>'
160-
});
161-
var marker = L.marker(currentMarker, {icon: markerOptionLocator});
163+
let marker = this.generateMarker(markerData);
162164
if (!isMarkerCluster) {
163165
marker.addTo(this.map);
164166
}
165167

166168
markers.push(marker);
167169
markerData.shopStatus(this.prepareShopStatus(markerData));
168170
}.bind(this));
169-
var group = new L.featureGroup(markers);
171+
172+
let group = new L.featureGroup(markers);
173+
170174
if (isMarkerCluster) {
171175
group = new L.markerClusterGroup();
172176
group.addLayers(markers);
173177
this.map.addLayer(group);
174178
}
179+
175180
this.initialBounds = group.getBounds();
176181
},
177182

@@ -244,13 +249,9 @@ define([
244249
this.map.setZoom(zoom);
245250
}
246251

247-
// displayedMarkers = this.addDistanceToMarkers(displayedMarkers, this.map.getCenter());
248-
249-
displayedMarkers = displayedMarkers.sort(function(a, b) {
250-
var distanceA = ko.isObservable(a['distance']) ? a['distance']() : a['distance'],
251-
distanceB = ko.isObservable(b['distance']) ? b['distance']() : b['distance'];
252-
return ((distanceA < distanceB) ? - 1 : ((distanceA > distanceB) ? 1 : 0));
253-
});
252+
if (this.markerHasDistance) {
253+
displayedMarkers = displayedMarkers.sort(this.sortMarkersByDistance);
254+
}
254255

255256
var position = this.getLocationFromHash();
256257
if(position === null) {
@@ -408,10 +409,60 @@ define([
408409
},
409410
source: markerInfoBase
410411
});
412+
},
413+
414+
/**
415+
* Generate marker
416+
*
417+
* @param {Object} markerData
418+
* @return {Object}
419+
*/
420+
generateMarker: function (markerData) {
421+
let currentMarker = [markerData.latitude, markerData.longitude],
422+
markerOptionLocator = L.divIcon({
423+
iconSize: null,
424+
html: this.getMarkerIconHtmlString(markerData)
425+
});
426+
427+
return L.marker(currentMarker, {icon: markerOptionLocator});
428+
},
429+
430+
/**
431+
* Get marker icon html string
432+
*
433+
* @param {Object} markerData
434+
* @returns {string}
435+
*/
436+
getMarkerIconHtmlString: function (markerData) {
437+
let html = '<div class="custum-lf-popup" data-lat="' + markerData.latitude + '" data-lon="' +
438+
markerData.longitude + '" data-n="' + markerData.name + '"><div class="button-decor"></div>';
439+
440+
if (typeof markerData.url !== 'undefined') {
441+
html += '<a href="' + markerData.url + '"></a>';
442+
}
443+
444+
html += '</div>';
445+
446+
return html;
447+
},
448+
449+
/**
450+
* Sort markers by distance
451+
*
452+
* @param {Object} markerA
453+
* @param {Object} markerB
454+
*
455+
* @returns {Number}
456+
*/
457+
sortMarkersByDistance: function (markerA, markerB) {
458+
let distanceA = ko.isObservable(markerA.distance) ? markerA.distance() : markerA.distance,
459+
distanceB = ko.isObservable(markerB.distance) ? markerB.distance() : markerB.distance;
460+
461+
return ((distanceA < distanceB) ? - 1 : ((distanceA > distanceB) ? 1 : 0));
411462
}
412463
};
413464

414465
return function (Component) {
415466
return Component.extend(mixin);
416467
}
417-
});
468+
});

view/frontend/web/js/retailer/store-map.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,10 @@
11
define([
22
'ko',
33
'smile-map',
4-
'smile-map-markers',
5-
'smile-storelocator-store-collection'
6-
], function(ko, SmileMap, Markers, StoreCollection){
4+
], function(ko, SmileMap){
75

86
return SmileMap.extend({
97

10-
defaults: { },
11-
12-
initialize: function () {
13-
this._super();
14-
},
15-
16-
/**
17-
* Init the list of markers.
18-
* Markers are a collection of Stores.
19-
*/
20-
initMarkers: function() {
21-
var markersList = new StoreCollection({items : this.markers});
22-
this.markers = markersList.getList();
23-
this.markers.forEach(function(marker) {
24-
marker.distance = ko.observable('');
25-
marker.distanceBetween = ko.observable('');
26-
marker.shopStatus = ko.observable('');
27-
});
28-
29-
this.displayedMarkers = ko.observable(this.markers);
30-
},
31-
328
/**
339
* Check is show copyright info.
3410
*/

0 commit comments

Comments
 (0)