/* ============================================ * onclick ajax background loading * ============================================ */ $(function() { $('html.touch .navbar-default .navbar-link').addClass('dropdown-toggle'); $('html.touch .navbar-default .navbar-link').attr('data-toggle','dropdown'); $('#user-nav > li > a.log-out-cookie, .navbar-nav > li > a.log-out-cookie').on('click', function(event) { var connectUrl = $('#header').attr('data-domain'); console.log('firing //'+connectUrl+'/api-logout'); $.ajax({ url: '//'+connectUrl+'/api-logout', type: "POST", xhrFields: {withCredentials: true}, }).done(function( msg ){ location.reload(); }); }); $('.checkcanedit').each(function(){ var connectUrl = $('#header').attr('data-domain'); console.log(connectUrl); var el = $(this); var id = el.data('id'); var model = el.data('model'); $.ajax({ url: '//'+connectUrl+'/api-checkcanedit', type: "POST", data: {model:model,id:id}, xhrFields: {withCredentials: true}, }).done(function( msg ) { if(msg == '1') { console.log('User can edit '+model+':'+id); el.removeClass('hidden'); } }); }); formsApiUpdateLoginStatus(); }); function formsApiClosePopupCallback() { //formsApiUpdateLoginStatus(); location.reload(); } function formsApiUpdateLoginStatus() { var connectUrl = $('#header').attr('data-domain'); console.log('loading: //'+connectUrl+'/api-checkloginstatus'); $.ajax({ type: "GET", url: '//'+connectUrl+'/api-checkloginstatus', dataType: 'json', // use json only, not jsonp crossDomain: true, // tell browser to allow cross domain xhrFields: {withCredentials: true} }).done(function( msg ) { if(msg == '1') $('body').addClass('logged-in'); else $('body').removeClass('logged-in'); }); } /* ============================================ * Forms API V2 * ============================================ */ $('.forms-btn').each(function () { $(this).click(function(){ var page = $(this).data('page'); var currentpage = $('#forms-modal').data('currentpage'); $('#forms-modal').modal('show'); if(page && page != currentpage) { $('#forms-modal').data('currentpage', page); var town = currentTownUrl ? '?town='+currentTownUrl : ''; $('#forms-modal').find('iframe').attr('src','/forms/'+page+'/'+town); } }); }); function resizeIframe(iframe) { iframe.height = iframe.contentWindow.document.body.scrollHeight + "px"; } /* ============================================ * use current/browser location button * ============================================ */ $(function() { $('.go-to-browser-location').on('click', function(event) { var el = $(this); var dataurl = el.data("url"); console.log(dataurl); if (navigator.geolocation) { var latitude = longitude = ''; navigator.geolocation.getCurrentPosition( function(position) { latitude = position.coords.latitude; longitude = position.coords.longitude; $.ajax({ data:{latitude: latitude, longitude: longitude, settown:true}, url: '/ajax/towns/locate/' }).done(function( msg ) { console.log("Location found."); msg = msg ? jQuery.parseJSON(msg) : ''; if(msg.sef_url) { var texturl = '/'; if(typeof(dataurl) != "undefined" && dataurl !== null) { texturl = dataurl; } var path = '/'+ msg.sef_url + texturl; console.log("redirecting to "+path+" ..."); window.location = path; } }); }, function() { el.addClass('btn disabled'); el.html('This browser does not support geolocation services.'); console.log("navigator.geolocation.getCurrentPosition is not supported by this browser."); } ); } else { console.log("navigator.geolocation is not supported by this browser."); } }); }); /* ============================================ * simplified map-module widget for atn4 * ============================================ */ jQuery('.map-module').each(function(){ var parent = jQuery(this); var options = parent.data('options'); var bounds, pages = [], markers = [], current_page = 1; var places = parent.data('places'); var last_page = Math.ceil(Number(options.count)/Number(options.limit)); var am_list = parent.find("#map-places"); var am_next = parent.find(".map-next"); var am_prev = parent.find(".map-prev"); var showmore = parent.find("a#showmore_indicator"); var legend = parent.find(".highlight-legend"); var mongo_id = options.mongo_id; var lat = options.lat, lng = options.lng; var navigation = options.navigation ? options.navigation : ''; var loading = jQuery('
').addClass('loading').append(jQuery('
').html('Searching...')) .append(jQuery('').attr('src','https://cdn-p300.americantowns/img/progress-bar.gif')); var map = new google.maps.Map(parent.find(".map-canvas")[0], {scrollwheel: false, zoom: 12, center: new google.maps.LatLng(lat, lng)}); if (options.source == 'gp') { if(places.length) pages[1] = places.slice(0,10); if(places.length > 10) pages[2] = places.slice(10,20); var last_page = options.morepages ? 99 : (places.length > 10 && places.length < 20) ? 2 : 1; } else pages[1] = places; check_am_nav(); if(places[0]) { places = pages[1]; load_places(); } /* drag and drop map search */ if(options.drag_search) { map.addListener('dragend', performSearch); function performSearch() { window.setTimeout(function() { var center = map.getCenter(); lat = center.lat(); lng = center.lng(); fetch_gp(center.lat()+','+center.lng()); }, 500); } } am_next.click(function(){navigate('next');}); am_prev.click(function(){navigate('back');}); function navigate(direction){ direction = direction ? direction : 'next'; if(direction == 'next') { if(current_page == last_page) return; am_next.html('Loading...'); pg = current_page+1; } else { if(current_page <= 1) return; am_prev.html('Loading...'); pg = current_page-1; } if(pages[pg]) { am_list.html(populate_am_list(pages[pg])); if(direction == 'next') am_next.html('NEXT'); else am_prev.html('BACK'); current_page = pg; check_am_nav(); } else { //console.log({'mongo_id':mongo_id,'pg':pg,'source':options.source}); jQuery.ajax({ type: "GET", dataType: 'json', data: (options.source == 'gp') ? {"id":mongo_id,"page":pg} : {"page":pg,"options":options}, url: navigation }).done(function( msg ) { data_places = (msg.places) ? msg.places : msg; if(direction == 'next') am_next.html('NEXT'); else am_prev.html('BACK'); if(data_places.length) { pages[pg] = (options.source == 'gp') ? data_places.slice(0,10) : data_places; am_list.html(populate_am_list(pages[pg])); current_page = pg; if(options.source == 'gp') { if (data_places.length > 10) { pg++; pages[pg] = msg.places.slice(10,20); } if (!msg.morepages) last_page = pg; } } check_am_nav(); }); } } function check_am_nav() { if (parent.find("div.right").height() > 475) jQuery("#map-canvas").css({"height":parent.find("div.right").height()}); if (current_page <= 1) am_prev.addClass("disabled"); else am_prev.removeClass("disabled"); if (current_page == last_page) am_next.addClass("disabled"); else am_next.removeClass("disabled"); } function populate_am_list(data) { var list = jQuery(''); var new_places = {}; var has_highlight = false; am_list.html(''); jQuery.each(data, function(i,area) { if (area.name.length != 0) { if(area.highlight) has_highlight = true var row = ''+ '

'+area.name+'

'+ '

'+area.address+'

'; } new_places[i] = {'key':area.key,'id':area.id,'name':area.name,'highlight':area.highlight,'address':area.address,'latitude':area.latitude,'longitude':area.longitude,'sef_tail':area.sef_tail}; am_list.append(row); }); if(has_highlight) legend.removeClass("hidden"); else legend.addClass("hidden"); places = new_places; load_places(); } function load_places() { setTimeout( function(){ //wait a bit for the listing to fully load before we get the height if(parent.find("div.right").height() > 475) jQuery("#map-canvas").css({"height":parent.find("div.right").height()}); google.maps.event.trigger(map, 'resize'); bounds = new google.maps.LatLngBounds(); while (markers.length > 0) { var marker = markers.pop(); marker.setMap(null); } var first,last; for (var i in places) { var r = places[i]; var index = (parseInt(r['key'])).toString(); if(first==null) first = index; last = index; if(r.latitude && r.longitude) { places[i].latlng = new google.maps.LatLng(places[i].latitude, places[i].longitude); set_marker (i); } } bounds.extend(new google.maps.LatLng(lat, lng)); map.fitBounds(bounds); }, 500); } function set_marker(i) { var r = places[i]; var index = (parseInt(r['key'])).toString(); var index_str = (index.length < 2) ? '0' + index : index; var marker = new google.maps.Marker({ position: new google.maps.LatLng(r.latitude, r.longitude), map: map, title: r['name'], icon: {url: 'https://imgcdn.americantowns.com/img/marker-pin-' + (r['highlight'] ? 'green.png' : 'light-blue.png')}, index: index, zIndex: parseInt(99-index)}); am_list.on('mouseenter', '#list_'+index, function(){/*map.panTo(marker.getPosition());*/marker.setAnimation(google.maps.Animation.BOUNCE);}); am_list.on('mouseleave', '#list_'+index, function(){marker.setAnimation(null);}); google.maps.event.addListener(marker, "mouseover", function() {el = parent.find('#map-places #list_'+this.index);el.css("background-color","#f5f5f5");}); google.maps.event.addListener(marker, "mouseout", function() {el = parent.find('#map-places #list_'+this.index);el.css("background-color","");}); markers.push(marker); bounds.extend(new google.maps.LatLng(r.latitude, r.longitude)); //map.fitBounds(bounds); } jQuery('.gp-update-results').click(function() { lat = options.lat; lng = options.lng; am_list.html(loading); if(jQuery('input[name=category]:checked', '#GpSearchCategoryForm').val() == 'shooting-and-hunting') { navigation = '/ajax/retailers/navigate/'; pages = []; pg = 1; last_page = 99; options.source = 'retailers'; options.radius = jQuery('input[name=radius]:checked', '#GpSearchCategoryForm').val(); options.limit = 10; jQuery.ajax({ type: "GET", dataType: 'json', data: {"page":pg,"options":options}, url: navigation }).done(function(data) { places = data.places; last_page = Math.ceil(Number(data.count)/Number(options.limit)); am_next.html('NEXT'); if(places.length) { pages[pg] = places; am_list.html(populate_am_list(pages[pg])); current_page = pg; check_am_nav(); } else fetch_gp(); }); } else fetch_gp(); return false; }); function fetch_gp(latlng) { navigation = '/ajax/gp/update_result/' + (latlng ? '?latlng='+latlng : ''); options.source = 'gp'; am_list.html(loading); jQuery.ajax({ type: 'GET', dataType: 'json', url: navigation, data: jQuery('#GpSearchCategoryForm').serialize(), always: function() {navigation = '/ajax/gp/navigate/';}, success: function(data) { jQuery('.map-module-gp-result').html(data); if(data.places.length > 0) { places = data.places; mongo_id = data.mongo_id; morepages = data.morepages; pages = []; if(places.length) pages[1] = places.slice(0,10); if(places.length > 10) pages[2] = places.slice(10,20); current_page = 1; last_page = morepages ? 99 : (places.length > 10 && places.length < 20) ? 2 : 1; am_list.html(populate_am_list(pages[1])); check_am_nav(); } else { pages = []; current_page = 1; last_page = 1; am_list.html('
There are no results. Please try changing your refine search.
'); check_am_nav(); } } }); } }); /* end map module */ //Check Mobile Devices var checkMobile = function(){ var isTouch = ('ontouchstart' in document.documentElement); if ( isTouch ) { $('html').addClass('touch'); } else { $('html').addClass('no-touch'); }; }; checkMobile();