Skip to content

Commit ca9d9c3

Browse files
committed
Externalize map-implementation specific logic into separate methods, allows overriding to add in alternative mapping providers.
git-svn-id: https://share-extras.googlecode.com/svn/trunk/Site Geotagged Content Dashlet@1336 a3f5c567-fd0f-3a89-9b71-a290c5a5f590
1 parent 5a5371c commit ca9d9c3

File tree

1 file changed

+127
-57
lines changed

1 file changed

+127
-57
lines changed

source/web/extras/components/documentlibrary/documentlist-geo.js

Lines changed: 127 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ if (typeof Extras == "undefined" || !Extras)
212212

213213
Extras.DocumentListGeoViewRenderer.prototype.setupRenderer = function DL_GVR_setupRenderer(scope)
214214
{
215-
//Extras.DocumentListGeoViewRenderer.superclass.superclass.setupRenderer.call(this, scope);
216215
Alfresco.DocumentListGalleryViewRenderer.superclass.setupRenderer.call(this, scope);
217216

218217
this.documentList = scope;
@@ -271,57 +270,16 @@ if (typeof Extras == "undefined" || !Extras)
271270
{
272271
center = [51, 0];
273272
}
274-
275-
// set up the map
276-
var map = new L.Map(mapId).setView([center[0], center[1]], this.zoomLevel);
277-
278-
// create the tile layer with correct attribution
279-
L.tileLayer(this.options.leafletTileUrl, {
280-
attribution: scope.msg("label.copyright.osm")
281-
}).addTo(map);
282-
283-
// save map position and zoom levels
284-
// when zooming leaflet fires both events, which leads to an exception being thrown from the repo
285-
// therefore we must first check that the other event is not 'in progress' before attempting the save
286273

287-
function savePrefs() {
288-
if (this.savingPrefs == false)
289-
{
290-
this.savingPrefs = true;
291-
var latlng = map.getCenter();
292-
Alfresco.logger.debug("Set " + PREFERENCES_DOCLIST + " to " + map.getZoom());
293-
scope.services.preferences.set(PREFERENCES_DOCLIST, { zoomLevel: map.getZoom(), center: latlng.lat + "," + latlng.lng }, {
294-
successCallback: {
295-
fn: function() {
296-
this.savingPrefs = false;
297-
},
298-
scope: this
299-
},
300-
failureCallback: {
301-
fn: function() {
302-
this.savingPrefs = false;
303-
},
304-
scope: this
305-
}
306-
});
274+
this._renderMap(scope, mapId, {
275+
center: {
276+
lat: center[0],
277+
lng: center[1]
307278
}
308-
};
309-
310-
map.on('zoomend', function(e) {
311-
savePrefs.call(this);
312-
}, this);
313-
314-
map.on('moveend', function(e) {
315-
savePrefs.call(this);
316-
}, this);
317-
318-
this.map = map;
279+
});
319280
}
320281

321-
for (var i = 0; i < this.markers.length; i++)
322-
{
323-
this.map.removeLayer(this.markers[i]);
324-
}
282+
this._removeAllMarkers();
325283

326284
var galleryItemTemplate = Dom.get(scope.id + '-geo-item-template'),
327285
galleryItem = null;
@@ -342,9 +300,6 @@ if (typeof Extras == "undefined" || !Extras)
342300
var galleryItemDetailDiv = this.getRowItemDetailElement(galleryItem);
343301
var galleryItemActionsDiv = this.getRowItemActionsElement(galleryItem);
344302

345-
// Set the item header id
346-
//galleryItemHeaderDiv.setAttribute('id', scope.id + '-item-header-' + oRecord.getId());
347-
348303
// Suffix of the content actions div id must match the onEventHighlightRow target id
349304
galleryItemActionsDiv.setAttribute('id', scope.id + '-actions-' + galleryItemId);
350305

@@ -356,14 +311,129 @@ if (typeof Extras == "undefined" || !Extras)
356311
// create a marker in the given location and add it to the map
357312
if (properties["cm:latitude"] && properties["cm:longitude"])
358313
{
359-
var marker = L.marker([properties["cm:latitude"], properties["cm:longitude"]], {
360-
title: record.displayName
361-
}).addTo(this.map);
362-
marker.bindPopup(Dom.get(scope.id + '-details-' + galleryItemId), { width: 400, maxWidth: 400 });
363-
Alfresco.logger.debug("Has geo data");
364-
this.markers.push(marker);
314+
this._addMarker({
315+
lat: properties["cm:latitude"],
316+
lng: properties["cm:longitude"],
317+
title: record.displayName,
318+
galleryItemDetailDivId: scope.id + '-details-' + galleryItemId
319+
});
365320
}
366321
};
367322
};
323+
324+
/**
325+
* Render the map instance into the map Dom element. Override this if you wish to use a different map implementation.
326+
*
327+
* @method _renderMap
328+
* @param scope {object} The DocumentList object
329+
* @param {string} mapId Dom ID of the element into which the map should be rendered
330+
* @param {object} pObj Map parameters. Must define a property `center` with `lat` and `lng` values.
331+
* @private
332+
*/
333+
Extras.DocumentListGeoViewRenderer.prototype._renderMap = function DL_GVR__renderMap(scope, mapId, pObj)
334+
{
335+
// set up the map
336+
var center = pObj.center,
337+
map = new L.Map(mapId).setView([center.lat, center.lng], this.zoomLevel);
338+
339+
// create the tile layer with correct attribution
340+
L.tileLayer(this.options.leafletTileUrl, {
341+
attribution: scope.msg("label.copyright.osm")
342+
}).addTo(map);
343+
344+
map.on('zoomend', function(e) {
345+
this._saveMapPreferences.call(this, scope);
346+
}, this);
347+
348+
map.on('moveend', function(e) {
349+
this._saveMapPreferences.call(this, scope);
350+
}, this);
351+
352+
this.map = map;
353+
}
354+
355+
/**
356+
* Add a marker to the map. Override this if you wish to use a different map implementation.
357+
*
358+
* @method _addMarker
359+
* @param {string} mapId Dom ID of the element into which the map should be rendered
360+
* @param {object} mObj Marker parameters. Must define properties `lat`, `lng`, `title` and `galleryItemDetailDivId`.
361+
* @private
362+
*/
363+
Extras.DocumentListGeoViewRenderer.prototype._addMarker = function DL_GVR__addMarker(mObj)
364+
{
365+
var marker = L.marker([mObj.lat, mObj.lng], {
366+
title: mObj.title
367+
}).addTo(this.map);
368+
marker.bindPopup(Dom.get(mObj.galleryItemDetailDivId), { width: 400, maxWidth: 400 });
369+
Alfresco.logger.debug("Binding popup to item ID " + mObj.galleryItemDetailDivId);
370+
this.markers.push(marker);
371+
}
372+
373+
/**
374+
* Remove all markers from the map. Override this if you wish to use a different map implementation.
375+
*
376+
* @method _removeAllMarkers
377+
* @private
378+
*/
379+
Extras.DocumentListGeoViewRenderer.prototype._removeAllMarkers = function DL_GVR__removeAllMarkers()
380+
{
381+
for (var i = 0; i < this.markers.length; i++)
382+
{
383+
this.map.removeLayer(this.markers[i]);
384+
}
385+
}
386+
387+
/**
388+
* Save map settings using the preferences service. Override this if you wish to use a different map implementation.
389+
*
390+
* @method _saveMapPreferences
391+
* @param scope {object} The DocumentList object
392+
* @private
393+
*/
394+
Extras.DocumentListGeoViewRenderer.prototype._saveMapPreferences = function DL_GVR__saveMapPreferences(scope)
395+
{
396+
this._savePreferenceValues(scope, {
397+
zoom: this.map.getZoom(),
398+
center: {
399+
lat: this.map.getCenter().lat,
400+
lng: this.map.getCenter().lng
401+
}
402+
});
403+
}
404+
405+
/**
406+
* Save preference values
407+
*
408+
* @method _savePreferenceValues
409+
* @param scope {object} The DocumentList object
410+
* @param {object} pObj Setting values. Must define properties `center` and `zoom`.
411+
* @private
412+
*/
413+
Extras.DocumentListGeoViewRenderer.prototype._savePreferenceValues = function DL_GVR__savePreferenceValues(scope, pObj)
414+
{
415+
// save map position and zoom levels
416+
// when zooming leaflet fires both events, which leads to an exception being thrown from the repo
417+
// therefore we must first check that the another event is not 'in progress' before attempting the save
418+
if (this.savingPrefs == false)
419+
{
420+
this.savingPrefs = true;
421+
Alfresco.logger.debug("Set " + PREFERENCES_DOCLIST + " to " + pObj.zoom);
422+
scope.services.preferences.set(PREFERENCES_DOCLIST, { zoomLevel: pObj.zoom, center: pObj.center.lat + "," + pObj.center.lng }, {
423+
successCallback: {
424+
fn: function() {
425+
this.savingPrefs = false;
426+
},
427+
scope: this
428+
},
429+
failureCallback: {
430+
fn: function() {
431+
this.savingPrefs = false;
432+
},
433+
scope: this
434+
}
435+
});
436+
}
437+
}
368438

369439
})();

0 commit comments

Comments
 (0)