-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.min.js
1 lines (1 loc) · 15.8 KB
/
main.min.js
1
var GrowlNotification,profile;function welcome(){console.log("Welcome notification loading...");GrowlNotification.notify({title:"Welcome "+profile.getName().split(" ")[0],description:"",image:{visible:true,customImage:""},closeTimeout:3000,type:"default",closeWith:["click","button"],animationDuration:0.5,position:"top-center"});console.log("Welcome complete!!")}function updateUserData(){if(profile){$.ajax({type:"POST",url:"save-user",data:{id:profile.getId(),name:profile.getName(),email:profile.getEmail()}}).done(function(a){console.log(a);window.location.href="#"})}}function onFailure(a){console.log(a)}function onSuccess(a){profile=a.getBasicProfile();window.location.href="#";setTimeout(function(){welcome()},1000);updateUserData()}function renderButton(){try{gapi.signin2.render("g-signin2",{scope:"profile email",width:240,height:50,longtitle:true,theme:"dark",onsuccess:onSuccess,onfailure:onFailure})}catch(a){console.log(a)}}function signOut(){var a=gapi.auth2.getAuthInstance();if(confirm("Clicking this button will sign you out from Google!\n Click OK to proceed.")){a.signOut().then(function(){console.log("User signed out.")})}}var locations=[],GrowlNotification,Mousetrap,updateUserData;function cleanPhotos(a){var b=[];if(a.photos!==undefined){if((a.photos[0] instanceof Array)||typeof a.photos[0]==="string"){return a.photos}console.log(a.photos);a.photos.forEach(function(c){if(c!==undefined){b.push(c.getUrl())}})}else{b.push("https://www.mygoyangi.com/wp-content/uploads/2016/11/download.jpeg")}return b}function cleanOperatingHours(a){if(a.opening_hours!==undefined){var b=[];a.opening_hours.weekday_text.forEach(function(c){b.push(c)});console.log("Opening hours present");return b}console.log("Undefined opening hours");return[null]}function cleanLatLng(a){if(a.geometry!==undefined){return[a.geometry.location.lat(),a.geometry.location.lng()]}return[a.lat,a.lng]}function cleanPath(a){locations=[];a.forEach(function(b){locations.push({name:b.name,lat:cleanLatLng(b)[0],lng:cleanLatLng(b)[1],place_id:b.place_id,photos:cleanPhotos(b),rating:(b.rating===undefined?0:b.rating),opening_hours:{weekday_text:cleanOperatingHours(b)}})});return a}function notify(c){var a="success",d="Successful "+c,b=JSON.stringify(locations,["name"]);console.log("Saving in progress...");if(locations===undefined||locations.length<=0){d=c+" failed, no locations entered";b="Please try again";a="error"}GrowlNotification.notify({title:d,description:b,image:{visible:true,customImage:""},closeTimeout:3000,type:a,closeWith:["click","button"],animationDuration:0.5,position:"top-center"})}function savePath(){if(gapi.auth2.getAuthInstance().isSignedIn.get()){updateUserData();$.ajax({type:"POST",data:{locations:locations},url:"save-path"}).done(function(){window.location.href="#"});notify("Save")}else{document.getElementById("save").setAttribute("href","#popup1")}}function updatePath(b,a){$.ajax({type:"POST",data:{_id:b,locations:a},url:"update-path"}).done(function(){window.location.href="#"});notify("Update")}function changeSaveToUpdate(a){Mousetrap.unbind("s");var b=document.querySelector("#save");b.innerHTML="<i class='fas fa-check'></i> Update";b.onclick=function(){updatePath(a.id,locations)};Mousetrap.bind("s",function(){b.click()})}function downloadPath(){var a=JSON.stringify(locations);console.log(a);document.getElementById("idHidden").value=a;document.getElementById("selectForm").submit();notify("Download")}function deletePath(a){$.ajax({type:"POST",data:{_id:a.id},url:"delete-path"})}function createDeleteButton(b){var a=document.createElement("button");a.appendChild(document.createTextNode("delete"));b.appendChild(a);a.onclick=function(c){deletePath(b);c.stopPropagation();b.removeChild(a);b.remove()}}var updateButtons,Mousetrap,displayImagePanel,hideImagePanel,CalculateButton;function AddLocation(b,f,e,a){var d=document.createElement("div"),c=document.createElement("div");d.style.backgroundColor="#fff";d.style.border="2px solid #fff";d.style.borderRadius="0px 15px 15px 0px";d.style.boxShadow="0 2px 6px rgba(0,0,0,.3)";d.style.cursor="pointer";d.style.marginBottom="40px";d.style.textAlign="center";d.title="Click to add selected place";b.appendChild(d);c.style.color="rgb(25,25,25)";c.style.fontFamily="Roboto,Arial,sans-serif";c.style.fontSize="16px";c.style.lineHeight="36px";c.style.paddingLeft="5px";c.style.paddingRight="5px";c.innerHTML="Add";d.appendChild(c);d.addEventListener("click",function(){var g;if(e[0]===null){return}for(g=0;g<a.length;g+=1){if(e[0].place_id===a[g].place_id){return}}a.push(e[0]);updateButtons(a,f);document.getElementById("directions-panel").style.visibility="hidden"});Mousetrap.bindGlobal("a",function(){d.click()})}function PlaceButton(a,g,b,f){var c=document.createElement("a"),e=document.createElement("div"),d=document.createElement("div");c.style.color="#800000";c.style.fontFamily="Arial, sans-serif";c.style.fontSize="24px";c.style.marginTop="15px";c.style.paddingRight="35px";c.innerHTML="×";a.appendChild(c);e.style.backgroundColor="#fff";e.style.border="2px solid #fff";e.style.borderRadius="3px";e.style.boxShadow="0 2px 6px rgba(0,0,0,.3)";e.style.cursor="pointer";e.style.marginTop="-3px";e.style.marginBottom="1px";e.style.textAlign="center";e.title="Click to view place";e.draggable="true";a.appendChild(e);d.style.color="rgb(25,25,25)";d.style.fontFamily="Roboto,Arial,sans-serif";d.style.fontSize="16px";d.style.lineHeight="38px";d.style.paddingLeft="10px";d.style.paddingRight="8px";d.innerHTML=b.name;d.draggable="true";e.appendChild(d);e.addEventListener("click",function(){try{g.setCenter(b.geometry.location)}catch(h){g.setCenter(new google.maps.LatLng(b.lat,b.lng))}finally{displayImagePanel(b)}});e.addEventListener("mouseover",function(){try{displayImagePanel(b)}catch(h){console.log(h)}});e.addEventListener("mouseout",function(){hideImagePanel()});c.addEventListener("click",function(){var h=f.indexOf(b);f.splice(h,1);a.removeChild(c);e.removeChild(d);a.removeChild(e);updateButtons(f,g)})}function updateButtons(j,b){if(j.length<=0){return}if(document.getElementById("NavButtons")){document.getElementById("NavButtons").childNodes.forEach(function(i){document.getElementById("NavButtons").removeChild(i)})}else{document.createElement("NavButtons")}if(document.getElementById("CalculateButton")){document.getElementById("CalculateButton").childNodes.forEach(function(i){document.getElementById("CalculateButton").removeChild(i)})}else{document.createElement("CalculateButton")}b.controls[google.maps.ControlPosition.TOP_CENTER].clear();b.controls[google.maps.ControlPosition.RIGHT_BOTTOM].clear();var c,h,e,g=document.createElement("NavButtons"),d=document.createElement("CalculateButton"),f=document.createElement("div"),a=new CalculateButton(f,j);for(c=0;c<j.length;c+=1){h=document.createElement("div");g.appendChild(h);e=new PlaceButton(h,b,j[c],j);b.controls[google.maps.ControlPosition.TOP_CENTER].push(h)}d.appendChild(f);b.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(f)}var AddLocation,CalculateButton,calculateAndDisplayRoute,updateButtons,verticesPanel;function initAutocomplete(){var p=new google.maps.Map(document.getElementById("map"),{backgroundColor:"#0099dd",center:{lat:1.352083,lng:103.819839},zoom:3,maxZoom:15,minZoom:3,mapTypeId:"roadmap",disableDefaultUI:true,gestureHandling:"greedy",mapTypeControlOptions:{mapTypeIds:[]},styles:[{stylers:[{saturation:-100}]},{featureType:"water",elementType:"geometry.fill",stylers:[{color:"#0099dd"}]},{elementType:"labels",stylers:[{visibility:"off"}]},{featureType:"poi.park",elementType:"geometry.fill",stylers:[{color:"#aadd55"}]},{featureType:"road.highway",elementType:"labels",stylers:[{visibility:"on"}]},{featureType:"road.arterial",elementType:"labels.text",stylers:[{visibility:"on"}]},{featureType:"road.local",elementType:"labels.text",stylers:[{visibility:"on"}]},{}]}),i=new google.maps.DirectionsService(),c=new google.maps.DirectionsRenderer(),k=document.getElementById("pac-input"),g=new google.maps.places.SearchBox(k),h={},m=[],n=[null],o=[],a=document.createElement("div"),e=new AddLocation(a,p,n,o),d=new google.maps.LatLngBounds(new google.maps.LatLng(85,-180),new google.maps.LatLng(-85,180)),l=p.getCenter();c.setMap(p);p.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(k);p.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(a);p.addListener("bounds_changed",function(){g.setBounds(p.getBounds())});g.addListener("places_changed",function(){var s=g.getPlaces(),t=new google.maps.LatLngBounds();if(s.length===0){return}m.forEach(function(u){u.setMap(null)});m=[];s.forEach(function(u){if(!u.geometry){console.log("Returned place contains no geometry");return}var v={url:u.icon,size:new google.maps.Size(71,71),origin:new google.maps.Point(0,0),anchor:new google.maps.Point(17,34),scaledSize:new google.maps.Size(25,25)};m.push(new google.maps.Marker({map:p,icon:v,title:u.name,position:u.geometry.location}));if(u.geometry.viewport){t.union(u.geometry.viewport)}else{t.extend(u.geometry.location)}});p.fitBounds(t);if(s.length===1){n[0]=s[0]}});google.maps.event.addListener(p,"center_changed",function(){if(d.contains(p.getCenter())){l=p.getCenter()}var v=p.getCenter(),u=v.lat(),t=d.getNorthEast().lat(),s=d.getSouthWest().lat();if(u<t||u>s){p.panTo(l)}else{l=p.getCenter()}});function q(s,t){return calculateAndDisplayRoute(i,c,s,t)}function j(s){p.controls[google.maps.ControlPosition.BOTTOM_CENTER].removeAt(1);var t=document.createElement("div");n=[null];e=new AddLocation(t,p,n,s);p.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(t);updateButtons(s,p)}function f(s){verticesPanel(p,h,s)}function r(u,t,s){return new CalculateButton(u,t,s)}function b(s){h=s;console.log(h)}initAutocomplete.callCalculateAndDisplay=q;initAutocomplete.callUpdateButtons=j;initAutocomplete.callVerticesPanel=f;initAutocomplete.initCalculateButton=r;initAutocomplete.setResult=b}var createDeleteButton,initAutocomplete,changeSaveToUpdate,cleanPath;function createList(a){var b=document.getElementById("myList");if(!b.hasChildNodes()){a.forEach(function(e){var d=document.createElement("li"),f=document.createElement("text"),c=document.createElement("date");d.setAttribute("id",e._id);if(e.route!==null){e.route.forEach(function(g,h){if(h>=1){f.appendChild(document.createTextNode(", "+g.name))}else{f.appendChild(document.createTextNode(g.name))}})}c.appendChild(document.createTextNode(moment(e.created_at).fromNow()));d.append(f);d.append(c);createDeleteButton(d);d.onclick=function(){document.getElementById("directions-panel").style.visibility="hidden";initAutocomplete.callUpdateButtons(e.route);cleanPath(e.route);window.location.href="#";changeSaveToUpdate(d)};b.appendChild(d)})}else{console.log("Already contain")}}function onLoad(){var a=this.responseText,b=JSON.parse(a);console.log(b);createList(b)}function onError(){console.log("error receiving async AJAX call")}function getPaths(){var b=new XMLHttpRequest(),a="/paths";b.open("GET",a,true);b.addEventListener("load",onLoad);b.addEventListener("error",onError);b.send()}function checkIfLoggedIn(){$(document).click(function(a){if(!$(a.target).closest("#popup").length){window.location.href="#"}});if(gapi.auth2.getAuthInstance().isSignedIn.get()){alert("Signed in");document.getElementById("view").setAttribute("href","#popup2");getPaths()}else{alert("Signed out");document.getElementById("view").setAttribute("href","#popup1")}return false}var initAutocomplete,cleanPath,Mousetrap;function CalculateButton(a,d){var c=document.createElement("div"),b=document.createElement("div");c.style.backgroundColor="#fff";c.style.border="2px solid #fff";c.style.borderRadius="3px";c.style.boxShadow="0 2px 6px rgba(0,0,0,.3)";c.style.cursor="pointer";c.style.marginTop="-3px";c.style.marginBottom="1px";c.style.textAlign="center";c.title="Click to view place";a.appendChild(c);b.style.color="rgb(25,25,25)";b.style.fontFamily="Roboto,Arial,sans-serif";b.style.fontSize="16px";b.style.lineHeight="38px";b.style.paddingLeft="10px";b.style.paddingRight="8px";b.innerHTML="Calculate!";c.appendChild(b);c.addEventListener("click",function(){cleanPath(d);initAutocomplete.callCalculateAndDisplay(cleanPath(d),true);document.getElementById("image-panel").style.visibility="hidden"});Mousetrap.bindGlobal("mod+c",function(){c.click();document.getElementById("pac-input").blur()})}function calculateAndDisplayRoute(f,c,d,g){var b,a=[],e=document.getElementById("directions-panel");for(b=1;b<d.length;b+=1){a.push({location:{placeId:d[b].place_id},stopover:true})}while(e.firstChild){e.removeChild(e.firstChild)}f.route({origin:{placeId:d[0].place_id},destination:{placeId:d[0].place_id},waypoints:a,optimizeWaypoints:g,travelMode:"DRIVING"},function(l,i){if(i==="OK"){initAutocomplete.setResult(JSON.parse(JSON.stringify(l)));c.setDirections(l);initAutocomplete.callVerticesPanel(d);var m,k=l.routes[0],h=document.createElement("div"),n;e.style.visibility="visible";for(m=0;m<k.legs.length;m+=1){h.className="slide";e.appendChild(h);n=m+1;h.innerHTML+="<b>Route Segment: "+n+"</b><br>";h.innerHTML+=k.legs[m].start_address+" to ";h.innerHTML+=k.legs[m].end_address+"<br>";h.innerHTML+=k.legs[m].distance.text+"<br><br>"}}else{window.alert("Directions request failed due to "+i)}})}var Sortable,cleanPath,initAutocomplete,getImageUrl;function verticesPanel(c,d,b){console.log("vertices Panel");if(document.getElementById("directions-panel")){document.getElementById("directions-panel").childNodes.forEach(function(g){document.getElementById("directions-panel").removeChild(g)})}else{document.createElement("directions-panel")}c.controls[google.maps.ControlPosition.TOP_CENTER].clear();var f=[],a=document.createElement("ul"),e=new Sortable(a,{animation:150,filter:".filtered",onUpdate:function(){var h,j=[],g=$("#sortable").children();console.log(g.length);for(h=0;h<g.length;h+=1){console.log(g[h].innerText+" "+g[h].getAttribute("place_id"));j.push({name:g[h].innerText,place_id:g[h].getAttribute("place_id"),rating:g[h].getAttribute("rating"),lat:g[h].getAttribute("lat"),lng:g[h].getAttribute("lng"),photos:JSON.parse(g[h].getAttribute("photos"))})}cleanPath(j);initAutocomplete.callCalculateAndDisplay(j,false)}});a.setAttribute("id","sortable");d.geocoded_waypoints.forEach(function(g){b.forEach(function(h){if(h.place_id===g.place_id){f.push(h);console.log(h.name)}})});f.forEach(function(h,j){if(j!==f.length-1){var g=document.createElement("li"),k=document.createElement("span"),l=[];l.push(getImageUrl(h));g.setAttribute("class","ui-state-default");g.innerHTML=h.name;g.setAttribute("place_id",h.place_id);g.setAttribute("rating",h.rating);g.setAttribute("lat",h.lat);g.setAttribute("lng",h.lng);g.setAttribute("photos",JSON.stringify(l));k.setAttribute("class","ui-icon ui-icon-arrowthick-2-n-s");g.appendChild(k);a.appendChild(g)}});c.controls[google.maps.ControlPosition.TOP_CENTER].push(a)}var imagePanel=document.getElementById("image-panel");var image=document.getElementById("location-image");var info=document.getElementById("info");var rating=document.getElementById("rating");function getImageUrl(a){var b="";if(a.photos!==undefined&&image!==null){if(typeof a.photos[0]==="string"){b=a.photos[0]}else{b=a.photos[0].getUrl()}}else{b="https://www.mygoyangi.com/wp-content/uploads/2016/11/download.jpeg"}return b}function getRating(b){var a="<br> Rating: ";if(b.rating!==undefined){a+=b.rating+"</br>";rating.setAttribute("class","rating-static rating-"+Math.ceil(b.rating*10/5)*5)}else{a+="none </br>"}return a}function getOpeningHours(a){var b="<br><br> <b> Operating hours </b> </br> ";if(a.opening_hours!==undefined){a.opening_hours.weekday_text.forEach(function(c){b+="<br>"+c+"</br>"})}else{b+="<br> No opening hours available </br>"}return b}function displayImagePanel(a){try{image.setAttribute("src",getImageUrl(a));if(info.innerHTML===""){info.innerHTML+="<br><b>"+a.name+"</b></br>";info.innerHTML+=getRating(a);info.innerHTML+=getOpeningHours(a)}imagePanel.style.visibility="visible"}catch(b){console.log(b)}}function hideImagePanel(){try{info.innerHTML="";rating.setAttribute("class","rating-static rating-0");imagePanel.style.visibility="hidden"}catch(a){console.log(a)}};