-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheasyLocator.min.js
12 lines (10 loc) · 11.8 KB
/
easyLocator.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*!
* jQuery easyLocator v2.0
* https://github.com/SaulBurgos/easyLocator
*
* Copyright Saul Burgos
* http://saulburgos.com/
*
* Date: 09/06/2021
*/
!function($){var that=this,deferEvents=$.Deferred();this.easyLocatorMethods={locations:[],onEvents:deferEvents.promise(),locationActive:null,selectorMapList:".locatorMap_list",htmlPlug:'<div class="locatorMap_loader">Loading...</div><div id="mapContainer_map" class="locatorMap_map"></div><div class="locatorMap_listContainer locatorMap_list--desktop js-locatorMap_listContainerDesktop"><div class="locatorMap_listContainer_filter"> <input class="locatorMap_listContainer_filter_input" type="text" placeholder="filter.."></div><ul class="locatorMap_list js-locatorMap_list"></ul></div><div class="locatorMap_listContainer locatorMap_list--mobile js-locatorMap_listContainerMobile" style="display:none"><div class="locatorMap_list_close js-locatorMap_list_Close"><i class="fa fa-chevron-down"></i></div><ul class="locatorMap_list"></ul></div><div class="locatorMap_template"></div>',options:{mapContainer:void 0,map:void 0,mapOptions:void 0,isAPIloaded:!1,myLocations:[],openInfowindowAfterClick:!1,showListOnDesktop:!0,showListOnMobile:!0,itemListActiveCustomClass:"",infoWindowCustomClass:"",contentTemplate:"",useMarkerCluster:!1,mapType:void 0,centerMapOnLocation:!0,extraFields:[],markerClustererOptions:{maxZoom:12}},loadScripts:function(t){this.showHideLoader("show");var o="https://maps.googleapis.com/maps/api/js?libraries=places&language=en&callback=window.easyLocatorMethods.loadMap",e=document.createElement("link");e.rel="stylesheet",e.href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css";var a=document.getElementsByTagName("link")[0];if(a.parentNode.insertBefore(e,a),"object"==typeof google&&"object"==typeof google.maps)that.easyLocatorMethods.options.isAPIloaded=!0,this.loadMap();else{void 0!==this.options.apiKey&&(o="https://maps.googleapis.com/maps/api/js?libraries=places&language=en&key="+this.options.apiKey+"&callback=window.easyLocatorMethods.loadMap");var i=document.createElement("script");i.type="text/javascript",i.src=o,document.body.appendChild(i)}},loadMap:function(){var t;that.easyLocatorMethods.triggerEvent({eventName:"loadingMap",data:{}}),this.options.isAPIloaded=!0,t=void 0===this.options.mapOptions?{zoom:8,center:new google.maps.LatLng(-34.397,150.644),mapTypeId:google.maps.MapTypeId.ROADMAP}:this.options.mapOptions,this.options.map=new google.maps.Map(document.getElementById("mapContainer_map"),t),this.options.map.controls[google.maps.ControlPosition.TOP_CENTER].push(this.createButtonList()),this.options.markerClusterer=new MarkerClusterer(this.options.map,null,this.options.markerClustererOptions),google.maps.event.addListenerOnce(this.options.map,"idle",function(){that.easyLocatorMethods.triggerEvent({eventName:"mapLoaded",data:{}}),void 0===that.easyLocatorMethods.options.spreadsheetId?that.easyLocatorMethods.options.myLocations.length>0&&that.easyLocatorMethods.loadMyLocations():that.easyLocatorMethods.getJsonData()}),""==this.options.contentTemplate&&(this.options.infoWindow=new google.maps.InfoWindow({maxWidth:400}),google.maps.event.addListener(this.options.infoWindow,"closeclick",function(){that.easyLocatorMethods.removeAllIconsActive(),that.easyLocatorMethods.triggerEvent({eventName:"infoWindowClosed",data:{}})}))},createEvents:function(){$(".js-locatorMap_list_Close").on("click",function(){$(".js-locatorMap_listContainerMobile").slideToggle("fast")}),$(".locatorMap_listContainer_filter_input").change(function(){that.easyLocatorMethods.filterList(this)}).keyup(function(){that.easyLocatorMethods.filterList(this)})},filterList:function(t){var o=$(t).val();""!=o?($(".js-locatorMap_list").find('.locatorMap_list_item_title:not(:contains_("'+o+'"))').parent().slideUp(),$(".js-locatorMap_list").find('.locatorMap_list_item_title:contains_("'+o+'")').parent().slideDown()):$(".js-locatorMap_list").find("li").slideDown()},createButtonList:function(t){if(that.easyLocatorMethods.options.showListOnMobile){var o=$.parseHTML('<div id="locatorMap_openList"><i class="fa fa-list"></i></div>')[0];return google.maps.event.addDomListener(o,"click",function(){$(".js-locatorMap_listContainerMobile").slideToggle("fast")}),o}},showHideLoader:function(t){"show"==t?$(".locatorMap_loader").show():$(".locatorMap_loader").hide()},getJsonData:function(){const spreadsheetId="1GsuoK3XyWJoiie1eq0qrd-2DxRVSQ0Ut7DkGI23Gq0s";fetch(`https://docs.google.com/spreadsheets/d/${spreadsheetId}/gviz/tq?tqx=out:json;responseHandler:window.easyLocatorMethods.successGetJsonData`).then(t=>t.text()).then(text=>{eval(text.replace("/*O_o*/",""))})},getContentITemList:function(t){var o='<li class="locatorMap_list_item" data-indexarray="'+t.index+'" data-isactive="false"><span class="ocatorMap_list_itemPlaceHolder"></span> <span class="locatorMap_list_item_title">'+t.title+"</span></li>";return""!=t.iconMarker&&void 0!==t.iconMarker||(o=o.replace('<span class="ocatorMap_list_itemPlaceHolder"></span>','<i class="locatorMap_list_item_icon fa fa-map-marker"></i>')),""!=t.iconMarker&&(o=o.replace('<span class="ocatorMap_list_itemPlaceHolder"></span>','<img src="'+t.iconMarker+'" class="locatorMap_list_item_iconImage" />')),o},addExtraFields:function(t,o){},createLocation:function(t){var o=this.getContentITemList(t),e=new google.maps.Marker({position:new google.maps.LatLng(t.lat,t.lng),map:this.options.map,title:t.title});""!=t.iconMarker&&void 0!==t.iconMarker&&e.setOptions({icon:{url:t.iconMarker,scaledSize:new google.maps.Size(32,32)}});var a={index:t.index,title:t.title,description:t.description,image:t.image,link:t.link,iconMarker:t.iconMarker,iconMarkerActive:t.iconMarkerActive,marker:e,active:!1};return this.options.useMarkerCluster&&this.options.markerClusterer.addMarker(e),{location:a,html:o}},successGetJsonData:function(t){var o=$(this.selectorMapList);o.empty();for(var e="",a=0;a<t.table.rows.length;a++){const o=t.table.rows[a].c,s=o[0]?o[0].v:"",n=o[1]?o[1].v:"",r=o[4]?o[4].v:"",l=o[5]?o[5].v:"",c=o[6]?o[6].v:"",p=o[7]?o[7].v:"",d=o[2]?o[2].v:"",h=o[3]?o[3].v:"";var i=this.createLocation({index:a,title:s,description:n,image:r,link:l,iconMarker:c,iconMarkerActive:p,lat:d,lng:h});e+=i.html,this.locations.push(i.location)}this.loadItemsOnList(o,e),that.easyLocatorMethods.triggerEvent({eventName:"getDataDone",data:this.locations})},loadMyLocations:function(){var t=$(this.selectorMapList);t.empty();for(var o="",e=0;e<this.options.myLocations.length;e++){var a=this.options.myLocations[e];a.index=e;var i=this.createLocation(a);$.extend(i.location,a),o+=i.html,this.locations.push(i.location)}this.loadItemsOnList(t,o),that.easyLocatorMethods.triggerEvent({eventName:"getDataDone",data:this.locations})},loadItemsOnList:function(t,o){t.html(o),this.attachEventLocations(),this.showHideLoader("hide"),this.options.centerMapOnLocation&&this.centerMapOnLocations()},centerMapOnLocations:function(){for(var t=new google.maps.LatLngBounds,o=0;o<this.locations.length;o++)t.extend(this.locations[o].marker.getPosition());this.options.map.fitBounds(t)},attachEventLocations:function(){function t(t){google.maps.event.addListener(t.marker,"click",function(){""==that.easyLocatorMethods.options.contentTemplate?that.easyLocatorMethods.openInfoWindow(t):that.easyLocatorMethods.openTemplate(t),that.easyLocatorMethods.setIconsActiveOnItem({elementClicked:$(".locatorMap_list_item")[t.index],location:t}),that.easyLocatorMethods.triggerEvent({eventName:"locationClicked",data:t})})}for(var o=0;o<this.locations.length;o++)t(this.locations[o]);$(".locatorMap_list_item").on("click",function(){that.easyLocatorMethods.removeAllIconsActive(),$(".locatorMap_list_item").removeClass(that.easyLocatorMethods.options.itemListActiveCustomClass);var t=that.easyLocatorMethods.locations[$(this).attr("data-indexarray")];that.easyLocatorMethods.options.map.setCenter(t.marker.getPosition()),that.easyLocatorMethods.options.openInfowindowAfterClick&&""==that.easyLocatorMethods.options.contentTemplate&&that.easyLocatorMethods.openInfoWindow(t),""!=that.easyLocatorMethods.options.contentTemplate&&that.easyLocatorMethods.openTemplate(t),$(window).width()<=768&&$(".js-locatorMap_listContainerMobile").slideToggle("fast"),""!=that.easyLocatorMethods.options.itemListActiveCustomClass&&$(this).addClass(that.easyLocatorMethods.options.itemListActiveCustomClass),that.easyLocatorMethods.setIconsActiveOnItem({elementClicked:this,location:t}),that.easyLocatorMethods.triggerEvent({eventName:"locationClicked",data:t})}),$(this.options.mapContainer).on("click",".locatorMap_template_close",function(){that.easyLocatorMethods.closeTemplate()})},setIconsActiveOnItem:function(t){$(t.elementClicked).addClass("locatorMap_list_item--active"),$(t.elementClicked).attr("data-isactive","true"),t.location.active=!0,""!=t.location.iconMarkerActive&&void 0!==t.location.iconMarkerActive&&($(t.elementClicked).find("img").attr("src",t.location.iconMarkerActive),t.location.marker.setOptions({icon:{url:t.location.iconMarkerActive,scaledSize:new google.maps.Size(42,42)}}))},removeAllIconsActive:function(){$(".locatorMap_list_item[data-isactive=true]").each(function(t){var o=that.easyLocatorMethods.locations[$(this).attr("data-indexarray")];$(this).removeClass("locatorMap_list_item--active"),o.active=!1,""!=o.iconMarker&&void 0!==o.iconMarker?($(this).find("img").attr("src",o.iconMarker),o.marker.setIcon({url:o.iconMarker,scaledSize:new google.maps.Size(32,32)})):o.marker.setIcon(null)})},openTemplate:function(t){var o=_.template(this.options.contentTemplate),e=$(this.options.mapContainer).find(".locatorMap_template");e.html(o(t)),e.show()},triggerEvent:function(t){deferEvents.notify(t)},closeTemplate:function(){$(this.options.mapContainer).find(".locatorMap_template").hide(),that.easyLocatorMethods.triggerEvent({eventName:"templateClosed",data:{}})},openInfoWindow:function(t){var o="",e="";this.locationActive=t,""!=t.link&&(o='<p><a href="'+t.link+'" target="_blank">View</a></p>'),""!=t.image&&(e='<img src="'+t.image+'" class="locatorMap_responsiveImg"/>');var a='<div id="locatorMap_contentInfoWindow" class="'+that.easyLocatorMethods.options.infoWindowCustomClass+' ">'+e+'<p class="locatorMap_contentInfoWindow_title"><b>'+t.title+"</b></p><p>"+t.description+"</p>"+o+"</div>";this.options.infoWindow.setContent(a),this.options.infoWindow.open(this.options.map,t.marker)},getMapInstance:function(){return this.options.map},cleanMap:function(){for(var t=0;t<this.locations.length;t++)this.locations[t].marker.setMap(null);this.options.useMarkerCluster&&this.options.markerClusterer.clearMarkers()},rebuild:function(t){this.cleanMap(),$(this.selectorMapList).empty(),this.locations=[];for(var o="",e=0;e<t.length;e++){var a,i=t[e];o+=this.getContentITemList({index:e,title:i.title,iconMarker:i.iconMarker}),a=i.marker?i.marker.getPosition():new google.maps.LatLng(i.lat,i.lng);var s=new google.maps.Marker({position:a,map:this.options.map,title:i.title});void 0!==i.iconMarker&&""!=i.iconMarker&&s.setOptions({icon:{url:i.iconMarker,scaledSize:new google.maps.Size(32,32)}});var n={index:e,active:!1};$.extend(n,i),n.marker=s,this.options.extraFields.length>0&&this.addExtraFields(i,n),this.options.useMarkerCluster&&this.options.markerClusterer.addMarker(s),this.locations.push(n)}this.loadItemsOnList($(this.selectorMapList),o),that.easyLocatorMethods.triggerEvent({eventName:"rebuildDone",data:{}})}},$.fn.easyLocator=function(t){return jQuery.expr[":"].contains_=function(t,o,e){return(t.textContent||t.innerText||"").toUpperCase().indexOf(e[3].toUpperCase())>=0},this.addClass("locatorMap"),this.html(that.easyLocatorMethods.htmlPlug),that.easyLocatorMethods.options=$.extend(that.easyLocatorMethods.options,t),that.easyLocatorMethods.options.mapContainer=this,that.easyLocatorMethods.loadScripts(),that.easyLocatorMethods.createEvents(),that.easyLocatorMethods.options.showListOnDesktop||($("#mapContainer_map").addClass("locatorMap_map--fullWidth"),$(".js-locatorMap_listContainerDesktop").hide()),that.easyLocatorMethods}}(jQuery);