ng-map.min.js 19 KB

1
  1. var ngMap=angular.module("ngMap",[]);ngMap.service("Attr2Options",["$parse","NavigatorGeolocation","GeoCoder",function($parse,NavigatorGeolocation,GeoCoder){var SPECIAL_CHARS_REGEXP=/([\:\-\_]+(.))/g,MOZ_HACK_REGEXP=/^moz([A-Z])/,orgAttributes=function(e){e.length>0&&(e=e[0]);for(var t={},n=0;n<e.attributes.length;n++){var o=e.attributes[n];t[o.name]=o.value}return t},camelCase=function(e){return e.replace(SPECIAL_CHARS_REGEXP,function(e,t,n,o){return o?n.toUpperCase():n}).replace(MOZ_HACK_REGEXP,"Moz$1")},JSONize=function(e){try{return JSON.parse(e),e}catch(t){return e.replace(/([\$\w]+)\s*:/g,function(e,t){return'"'+t+'":'}).replace(/'([^']+)'/g,function(e,t){return'"'+t+'"'})}},toOptionValue=function(input,options){var output,key=options.key,scope=options.scope;try{var num=Number(input);if(isNaN(num))throw"Not a number";output=num}catch(err){try{if(input.match(/^[\+\-]?[0-9\.]+,[ ]*\ ?[\+\-]?[0-9\.]+$/)&&(input="["+input+"]"),output=JSON.parse(JSONize(input)),output instanceof Array){var t1stEl=output[0];if(t1stEl.constructor==Object);else if(t1stEl.constructor==Array)output=output.map(function(e){return new google.maps.LatLng(e[0],e[1])});else if(!isNaN(parseFloat(t1stEl))&&isFinite(t1stEl))return new google.maps.LatLng(output[0],output[1])}}catch(err2){if(input.match(/^[A-Z][a-zA-Z0-9]+\(.*\)$/))try{var exp="new google.maps."+input;output=eval(exp)}catch(e){output=input}else if(input.match(/^([A-Z][a-zA-Z0-9]+)\.([A-Z]+)$/))try{var matches=input.match(/^([A-Z][a-zA-Z0-9]+)\.([A-Z]+)$/);output=google.maps[matches[1]][matches[2]]}catch(e){output=input}else if(input.match(/^[A-Z]+$/))try{var capitalizedKey=key.charAt(0).toUpperCase()+key.slice(1);key.match(/temperatureUnit|windSpeedUnit|labelColor/)?(capitalizedKey=capitalizedKey.replace(/s$/,""),output=google.maps.weather[capitalizedKey][input]):output=google.maps[capitalizedKey][input]}catch(e){output=input}else output=input}}return output},setDelayedGeoLocation=function(e,t,n,o){o=o||{};var a=e.centered||o.centered,r=function(){var n=o.fallbackLocation||new google.maps.LatLng(0,0);e[t](n)};!n||n.match(/^current/i)?NavigatorGeolocation.getCurrentPosition().then(function(n){var r=n.coords.latitude,i=n.coords.longitude,s=new google.maps.LatLng(r,i);e[t](s),a&&e.map.setCenter(s),o.callback&&o.callback.apply(e)},r):GeoCoder.geocode({address:n}).then(function(n){e[t](n[0].geometry.location),a&&e.map.setCenter(n[0].geometry.location)},r)},getAttrsToObserve=function(e){var t=[];if(e["ng-repeat"]||e.ngRepeat);else for(var n in e){var o=e[n];o&&o.match(/\{\{.*\}\}/)&&t.push(camelCase(n))}return t},observeAttrSetObj=function(e,t,n){var o=getAttrsToObserve(e);Object.keys(o).length;for(var a=0;a<o.length;a++)observeAndSet(t,o[a],n)},observeAndSet=function(e,t,n){e.$observe(t,function(e){if(e){var o=camelCase("set-"+t),a=toOptionValue(e,{key:t});n[o]&&(t.match(/center|position/)&&"string"==typeof a?setDelayedGeoLocation(n,o,a):n[o](a))}})};return{filter:function(e){var t={};for(var n in e)n.match(/^\$/)||n.match(/^ng[A-Z]/)||(t[n]=e[n]);return t},getOptions:function(e,t){var n={};for(var o in e)if(e[o]){if(o.match(/^on[A-Z]/))continue;if(o.match(/ControlOptions$/))continue;n[o]=toOptionValue(e[o],{scope:t,key:o})}return n},getEvents:function(e,t){var n={},o=function(e){return"_"+e.toLowerCase()},a=function(t){var n=t.match(/([^\(]+)\(([^\)]*)\)/),o=n[1],a=n[2].replace(/event[ ,]*/,""),r=e.$eval("["+a+"]");return function(t){function n(e,t){return e[t]}f=o.split(".").reduce(n,e),f.apply(this,[t].concat(r)),e.$apply()}};for(var r in t)if(t[r]){if(!r.match(/^on[A-Z]/))continue;var i=r.replace(/^on/,"");i=i.charAt(0).toLowerCase()+i.slice(1),i=i.replace(/([A-Z])/g,o);var s=t[r];n[i]=new a(s)}return n},getControlOptions:function(e){var t={};if("object"!=typeof e)return!1;for(var n in e)if(e[n]){if(!n.match(/(.*)ControlOptions$/))continue;var o=e[n],a=o.replace(/'/g,'"');a=a.replace(/([^"]+)|("[^"]+")/g,function(e,t,n){return t?t.replace(/([a-zA-Z0-9]+?):/g,'"$1":'):n});try{var r=JSON.parse(a);for(var i in r)if(r[i]){var s=r[i];if("string"==typeof s?s=s.toUpperCase():"mapTypeIds"===i&&(s=s.map(function(e){return e.match(/^[A-Z]+$/)?google.maps.MapTypeId[e.toUpperCase()]:e})),"style"===i){var p=n.charAt(0).toUpperCase()+n.slice(1),c=p.replace(/Options$/,"")+"Style";r[i]=google.maps[c][s]}else r[i]="position"===i?google.maps.ControlPosition[s]:s}t[n]=r}catch(l){}}return t},toOptionValue:toOptionValue,camelCase:camelCase,setDelayedGeoLocation:setDelayedGeoLocation,getAttrsToObserve:getAttrsToObserve,observeAndSet:observeAndSet,observeAttrSetObj:observeAttrSetObj,orgAttributes:orgAttributes}}]),ngMap.service("GeoCoder",["$q",function(e){return{geocode:function(t){var n=e.defer(),o=new google.maps.Geocoder;return o.geocode(t,function(e,t){t==google.maps.GeocoderStatus.OK?n.resolve(e):n.reject("Geocoder failed due to: "+t)}),n.promise}}}]),ngMap.service("NavigatorGeolocation",["$q",function(e){return{getCurrentPosition:function(){var t=e.defer();return navigator.geolocation?navigator.geolocation.getCurrentPosition(function(e){t.resolve(e)},function(e){t.reject(e)}):t.reject("Browser Geolocation service failed."),t.promise},watchPosition:function(){return"TODO"},clearWatch:function(){return"TODO"}}}]),ngMap.service("StreetView",["$q",function(e){return{getPanorama:function(t,n){n=n||t.getCenter();var o=e.defer(),a=new google.maps.StreetViewService;return a.getPanoramaByLocation(n||t.getCenter,100,function(e,t){t===google.maps.StreetViewStatus.OK?o.resolve(e.location.pano):o.resolve(!1)}),o.promise},setPanorama:function(e,t){var n=new google.maps.StreetViewPanorama(e.getDiv(),{enableCloseButton:!0});n.setPano(t)}}}]),ngMap.directive("bicyclingLayer",["Attr2Options",function(e){var t=e,n=function(e,t){var n=new google.maps.BicyclingLayer(e);for(var o in t)google.maps.event.addListener(n,o,t[o]);return n};return{restrict:"E",require:"^map",link:function(e,o,a,r){var i=t.orgAttributes(o),s=t.filter(a),p=t.getOptions(s),c=t.getEvents(e,s),l=n(p,c);r.addObject("bicyclingLayers",l),t.observeAttrSetObj(i,a,l)}}}]),ngMap.directive("cloudLayer",["Attr2Options",function(e){var t=e,n=function(e,t){var n=new google.maps.weather.CloudLayer(e);for(var o in t)google.maps.event.addListener(n,o,t[o]);return n};return{restrict:"E",require:"^map",link:function(e,o,a,r){var i=t.orgAttributes(o),s=t.filter(a),p=t.getOptions(s),c=t.getEvents(e,s),l=n(p,c);r.addObject("cloudLayers",l),t.observeAttrSetObj(i,a,l)}}}]),ngMap.directive("customControl",["Attr2Options","$compile",function(e,t){var n=e;return{restrict:"E",require:"^map",link:function(e,o,a,r){o.css("display","none");var i=(n.orgAttributes(o),n.filter(a)),s=n.getOptions(i,e),p=n.getEvents(e,i),c=t(o.html().trim())(e),l=c[0];for(var u in p)google.maps.event.addDomListener(l,u,p[u]);r.addObject("customControls",l),e.$on("mapInitialized",function(e,t){var n=s.position;t.controls[google.maps.ControlPosition[n]].push(l)})}}}]),ngMap.directive("drawingManager",["Attr2Options",function(e){var t=e;return{restrict:"E",require:"^map",link:function(e,n,o,a){var r=(t.orgAttributes(n),t.filter(o)),i=t.getOptions(r),s=t.getControlOptions(r),p=t.getEvents(e,r),c=new google.maps.drawing.DrawingManager({drawingMode:i.drawingmode,drawingControl:i.drawingcontrol,drawingControlOptions:s.drawingControlOptions,circleOptions:i.circleoptions,markerOptions:i.markeroptions,polygonOptions:i.polygonoptions,polylineOptions:i.polylineoptions,rectangleOptions:i.rectangleoptions}),p=t.getEvents(e,r);for(var l in p)google.maps.event.addListener(c,l,p[l]);a.addObject("mapDrawingManager",c)}}}]),ngMap.directive("dynamicMapsEngineLayer",["Attr2Options",function(e){var t=e,n=function(e,t){var n=new google.maps.visualization.DynamicMapsEngineLayer(e);for(var o in t)google.maps.event.addListener(n,o,t[o]);return n};return{restrict:"E",require:"^map",link:function(e,o,a,r){var i=t.filter(a),s=t.getOptions(i),p=t.getEvents(e,i,p),c=n(s,p);r.addObject("mapsEngineLayers",c)}}}]),ngMap.directive("fusionTablesLayer",["Attr2Options",function(e){var t=e,n=function(e,t){var n=new google.maps.FusionTablesLayer(e);for(var o in t)google.maps.event.addListener(n,o,t[o]);return n};return{restrict:"E",require:"^map",link:function(e,o,a,r){var i=t.filter(a),s=t.getOptions(i),p=t.getEvents(e,i,p),c=n(s,p);r.addObject("fusionTablesLayers",c)}}}]),ngMap.directive("heatmapLayer",["Attr2Options","$window",function(e,t){var n=e;return{restrict:"E",require:"^map",link:function(e,o,a,r){var i=n.filter(a),s=n.getOptions(i);if(s.data=t[a.data]||e[a.data],!(s.data instanceof Array))throw"invalid heatmap data";s.data=new google.maps.MVCArray(s.data);{var p=new google.maps.visualization.HeatmapLayer(s);n.getEvents(e,i)}r.addObject("heatmapLayers",p)}}}]),ngMap.directive("infoWindow",["Attr2Options","$compile","$timeout",function(Attr2Options,$compile,$timeout){var parser=Attr2Options,getInfoWindow=function(options,events,element){var infoWindow;if(!options.position||options.position instanceof google.maps.LatLng)infoWindow=new google.maps.InfoWindow(options);else{var address=options.position;delete options.position,infoWindow=new google.maps.InfoWindow(options);var callback=function(){infoWindow.open(infoWindow.map)};parser.setDelayedGeoLocation(infoWindow,"setPosition",address,{callback:callback})}Object.keys(events).length>0;for(var eventName in events)eventName&&google.maps.event.addListener(infoWindow,eventName,events[eventName]);var template=element.html().trim();if(1!=angular.element(template).length)throw"info-window working as a template must have a container";return infoWindow.__template=template.replace(/\s?ng-non-bindable[='"]+/,""),infoWindow.__compile=function(e){var t=$compile(infoWindow.__template)(e);e.$apply(),infoWindow.setContent(t[0])},infoWindow.__eval=function(event){var template=infoWindow.__template,_this=this;return template=template.replace(/{{(event|this)[^;\}]+}}/g,function(match){var expression=match.replace(/[{}]/g,"").replace("this.","_this.");return eval(expression)})},infoWindow};return{restrict:"E",require:"^map",link:function(e,t,n,o){t.css("display","none");var a=parser.orgAttributes(t),r=parser.filter(n),i=parser.getOptions(r,e),s=parser.getEvents(e,r),p=getInfoWindow(i,s,t);o.addObject("infoWindows",p),parser.observeAttrSetObj(a,n,p),p.visible&&e.$on("mapInitialized",function(t,n){$timeout(function(){p.__template=p.__eval.apply(this,[t]),p.__compile(e),p.map=n,p.position&&p.open(n)})}),p.visibleOnMarker&&e.$on("mapInitialized",function(t,n){$timeout(function(){var o=p.visibleOnMarker,a=n.markers[o];if(!a)throw"Invalid marker id";p.__template=p.__eval.apply(this,[t]),p.__compile(e),p.open(n,a)})}),e.showInfoWindow=e.showInfoWindow||function(t,n,a){var r=o.map.infoWindows[n],i=r.__template;r.__template=r.__eval.apply(this,[t]),r.__compile(e),a?r.open(o.map,a):this.getPosition?r.open(o.map,this):r.open(o.map),r.__template=i},e.hideInfoWindow=e.hideInfoWindow||function(t,n){var a=o.map.infoWindows[n];a.__template=a.__eval.apply(this,[t]),a.__compile(e),a.close()}}}}]),ngMap.directive("kmlLayer",["Attr2Options",function(e){var t=e,n=function(e,t){var n=new google.maps.KmlLayer(e);for(var o in t)google.maps.event.addListener(n,o,t[o]);return n};return{restrict:"E",require:"^map",link:function(e,o,a,r){var i=t.orgAttributes(o),s=t.filter(a),p=t.getOptions(s),c=t.getEvents(e,s),l=n(p,c);r.addObject("kmlLayers",l),t.observeAttrSetObj(i,a,l)}}}]),ngMap.directive("mapData",["Attr2Options",function(e){var t=e;return{restrict:"E",require:"^map",link:function(e,n,o){var a=t.filter(o),r=t.getOptions(a),i=t.getEvents(e,a,i);e.$on("mapInitialized",function(t,n){for(var o in r)if(o){var a=r[o];"function"==typeof e[a]?n.data[o](e[a]):n.data[o](a)}for(var s in i)i[s]&&n.data.addListener(s,i[s])})}}}]),ngMap.directive("mapLazyLoad",["$compile","$timeout",function(e,t){"use strict";var n={compile:function(n,o){!o.mapLazyLoad&&void 0;var a=n.html(),r=o.mapLazyLoad;return document.querySelector('script[src="'+r+'"]')?!1:(n.html(""),{pre:function(n,o){window.lazyLoadCallback=function(){t(function(){o.html(a),e(o.contents())(n)},100)};var i=document.createElement("script");i.src=r+"?callback=lazyLoadCallback",document.body.appendChild(i)}})}};return n}]),ngMap.directive("mapType",["Attr2Options","$window",function(e,t){return{restrict:"E",require:"^map",link:function(e,n,o,a){var r,i=o.name;if(!i)throw"invalid map-type name";if(o.object){var s=e[o.object]?e:t;r=s[o.object],"function"==typeof r&&(r=new r)}if(!r)throw"invalid map-type object";e.$on("mapInitialized",function(e,t){t.mapTypes.set(i,r)}),a.addObject("mapTypes",r)}}}]),ngMap.directive("map",["Attr2Options","$timeout",function(e,t){function n(e,t){if(e.currentStyle)var n=e.currentStyle[t];else if(window.getComputedStyle)var n=document.defaultView.getComputedStyle(e,null).getPropertyValue(t);return n}var o=e;return{restrict:"AE",controller:ngMap.MapController,link:function(a,r,i,s){var p=o.orgAttributes(r);a.google=google;var c=document.createElement("div");c.style.width="100%",c.style.height="100%",r.prepend(c),"block"!=n(r[0],"display")&&r.css("display","block"),n(r[0],"height").match(/^(0|auto)/)&&r.css("height","300px");var l=function(n,r){var l=new google.maps.Map(c,{});l.markers={},l.shapes={},t(function(){google.maps.event.trigger(l,"resize")}),n.zoom=n.zoom||15;var u=n.center;u?u instanceof google.maps.LatLng||(delete n.center,e.setDelayedGeoLocation(l,"setCenter",u,{fallbackLocation:g.geoFallbackCenter})):n.center=new google.maps.LatLng(0,0),l.setOptions(n);for(var m in r)m&&google.maps.event.addListener(l,m,r[m]);o.observeAttrSetObj(p,i,l),s.map=l,s.addObjects(s._objects),a.map=l,a.map.scope=a,a.$emit("mapInitialized",l),a.maps=a.maps||{},a.maps[g.id||Object.keys(a.maps).length]=l,a.$emit("mapsInitialized",a.maps)},u=o.filter(i),g=o.getOptions(u,a),m=o.getControlOptions(u),v=angular.extend(g,m),d=o.getEvents(a,u);i.initEvent?a.$on(i.initEvent,function(){!s.map&&l(v,d)}):l(v,d)}}}]),ngMap.MapController=function(){this.map=null,this._objects=[],this.addMarker=function(e){if(this.map){this.map.markers=this.map.markers||{},e.setMap(this.map),e.centered&&this.map.setCenter(e.position);var t=Object.keys(this.map.markers).length;this.map.markers[e.id||t]=e}else this._objects.push(e)},this.addShape=function(e){if(this.map){this.map.shapes=this.map.shapes||{},e.setMap(this.map);var t=Object.keys(this.map.shapes).length;this.map.shapes[e.id||t]=e}else this._objects.push(e)},this.addObject=function(e,t){if(this.map){this.map[e]=this.map[e]||{};var n=Object.keys(this.map[e]).length;this.map[e][t.id||n]=t,"infoWindows"!=e&&t.setMap&&t.setMap(this.map)}else t.groupName=e,this._objects.push(t)},this.addObjects=function(e){for(var t=0;t<e.length;t++){var n=e[t];n instanceof google.maps.Marker?this.addMarker(n):n instanceof google.maps.Circle||n instanceof google.maps.Polygon||n instanceof google.maps.Polyline||n instanceof google.maps.Rectangle||n instanceof google.maps.GroundOverlay?this.addShape(n):this.addObject(n.groupName,n)}}},ngMap.directive("mapsEngineLayer",["Attr2Options",function(e){var t=e,n=function(e,t){var n=new google.maps.visualization.MapsEngineLayer(e);for(var o in t)google.maps.event.addListener(n,o,t[o]);return n};return{restrict:"E",require:"^map",link:function(e,o,a,r){var i=t.filter(a),s=t.getOptions(i),p=t.getEvents(e,i,p),c=n(s,p);r.addObject("mapsEngineLayers",c)}}}]),ngMap.directive("marker",["Attr2Options",function(e){var t=e,n=function(e,n){var o;if(e.icon instanceof Object){(""+e.icon.path).match(/^[A-Z_]+$/)&&(e.icon.path=google.maps.SymbolPath[e.icon.path]);for(var a in e.icon){var r=e.icon[a];"anchor"==a||"origin"==a?e.icon[a]=new google.maps.Point(r[0],r[1]):("size"==a||"scaledSize"==a)&&(e.icon[a]=new google.maps.Size(r[0],r[1]))}}if(e.position instanceof google.maps.LatLng)o=new google.maps.Marker(e);else{var i=e.position;e.position=new google.maps.LatLng(0,0),o=new google.maps.Marker(e),t.setDelayedGeoLocation(o,"setPosition",i)}Object.keys(n).length>0;for(var s in n)s&&google.maps.event.addListener(o,s,n[s]);return o};return{restrict:"E",require:"^map",link:function(e,o,a,r){var i=t.orgAttributes(o),s=t.filter(a),p=t.getOptions(s,e),c=t.getEvents(e,s);o.bind("$destroy",function(){var e=l.map.markers;for(var t in e)e[t]==l&&delete e[t];l.setMap(null)});var l=n(p,c);r.addMarker(l),t.observeAttrSetObj(i,a,l)}}}]),ngMap.directive("overlayMapType",["Attr2Options","$window",function(e,t){return{restrict:"E",require:"^map",link:function(e,n,o,a){var r,i=o.initMethod||"insertAt";if(o.object){var s=e[o.object]?e:t;r=s[o.object],"function"==typeof r&&(r=new r)}if(!r)throw"invalid map-type object";e.$on("mapInitialized",function(e,t){if("insertAt"==i){var n=parseInt(o.index,10);t.overlayMapTypes.insertAt(n,r)}else"push"==i&&t.overlayMapTypes.push(r)}),a.addObject("overlayMapTypes",r)}}}]),ngMap.directive("shape",["Attr2Options",function(e){var t=e,n=function(e){return new google.maps.LatLngBounds(e[0],e[1])},o=function(e,o){var a,r=e.name;if(delete e.name,e.icons)for(var i=0;i<e.icons.length;i++){var s=e.icons[i];s.icon.path.match(/^[A-Z_]+$/)&&(s.icon.path=google.maps.SymbolPath[s.icon.path])}switch(r){case"circle":if(e.center instanceof google.maps.LatLng)a=new google.maps.Circle(e);else{var p=e.center;e.center=new google.maps.LatLng(0,0),a=new google.maps.Circle(e),t.setDelayedGeoLocation(a,"setCenter",p)}break;case"polygon":a=new google.maps.Polygon(e);break;case"polyline":a=new google.maps.Polyline(e);break;case"rectangle":e.bounds&&(e.bounds=n(e.bounds)),a=new google.maps.Rectangle(e);break;case"groundOverlay":case"image":var c=e.url,l=n(e.bounds),u={opacity:e.opacity,clickable:e.clickable,id:e.id};a=new google.maps.GroundOverlay(c,l,u)}for(var g in o)o[g]&&google.maps.event.addListener(a,g,o[g]);return a};return{restrict:"E",require:"^map",link:function(e,n,a,r){var i=t.orgAttributes(n),s=t.filter(a),p=t.getOptions(s),c=t.getEvents(e,s),l=o(p,c);r.addShape(l),t.observeAttrSetObj(i,a,l)}}}]),ngMap.directive("trafficLayer",["Attr2Options",function(e){var t=e,n=function(e,t){var n=new google.maps.TrafficLayer(e);for(var o in t)google.maps.event.addListener(n,o,t[o]);return n};return{restrict:"E",require:"^map",link:function(e,o,a,r){var i=t.orgAttributes(o),s=t.filter(a),p=t.getOptions(s),c=t.getEvents(e,s),l=n(p,c);r.addObject("trafficLayers",l),t.observeAttrSetObj(i,a,l)}}}]),ngMap.directive("transitLayer",["Attr2Options",function(e){var t=e,n=function(e,t){var n=new google.maps.TransitLayer(e);for(var o in t)google.maps.event.addListener(n,o,t[o]);return n};return{restrict:"E",require:"^map",link:function(e,o,a,r){var i=t.orgAttributes(o),s=t.filter(a),p=t.getOptions(s),c=t.getEvents(e,s),l=n(p,c);r.addObject("transitLayers",l),t.observeAttrSetObj(i,a,l)}}}]),ngMap.directive("weatherLayer",["Attr2Options",function(e){var t=e,n=function(e,t){var n=new google.maps.weather.WeatherLayer(e);for(var o in t)google.maps.event.addListener(n,o,t[o]);return n};return{restrict:"E",require:"^map",link:function(e,o,a,r){var i=t.orgAttributes(o),s=t.filter(a),p=t.getOptions(s),c=t.getEvents(e,s),l=n(p,c);r.addObject("weatherLayers",l),t.observeAttrSetObj(i,a,l)}}}]);