/** * angular-dg-maps.js * Simple to use Angular.js component to work with 2GIS Maps API * @version v0.0.3 - 2013-08-17 * @link http://burivuhster.github.io/angular-dg-maps * @author Eugene Molodkin <burivuh@gmail.com> * @license MIT License, http://www.opensource.org/licenses/MIT */ !function(){"use strict";var a=angular.module("dg-maps",[]);a.directive("dgMap",["$log","$timeout","$rootScope",function(a,b,c){var d=angular.element('<div class="inner-dg-map" style="height: 100%; width: 100%;"></div>');return{restrict:"E",priority:100,transclude:!0,template:"<div class='angular-dg-map'> <dg-markers ng-transclude></dg-markers></div>",replace:!0,scope:{latitude:"=",longitude:"=",zoom:"=",zoomControls:"=",fullscreenControls:"=",fitToMarkers:"=",draggable:"=",geoclicker:"="},controller:["$scope",function(b){this._markers=[],this.addMarker=function(a){this._markers.push(a),b.map&&b.map.markers.add(a)},this.removeMarker=function(c){b.map&&b.map.markers.remove(c);for(var d=0;d<this._markers.length;d++)if(this._markers[d]===c)return this._markers.splice(d,1),void 0;a.error("angular-dg-maps: error while removing marker - marker not found")}}],compile:function(a){return a.append(d.attr("id","map"+Math.round(1e6*Math.random()))),this.link},link:function(b){if(!angular.isDefined(b.zoom))return a.error("angular-dg-maps: map zoom property not set"),void 0;var e=new DG.Map(d.attr("id"));angular.isDefined(b.latitude)&&angular.isDefined(b.longitude)&&e.setCenter(new DG.GeoPoint(b.longitude,b.latitude),b.zoom);var f=new DG.Controls.Zoom;e.controls.add(f);var g=!1;!angular.isDefined(b.zoomControls)||b.zoomControls?f.show():f.hide(),!angular.isDefined(b.fullscreenControls)||b.fullscreenControls?e.fullscreen.enable():e.fullscreen.disable(),angular.isDefined(b.draggable)&&!b.draggable&&e.disableDragging(),angular.isDefined(b.geoclicker)&&!b.geoclicker&&e.geoclicker.disable(),b.map=e,b.$watch("latitude",function(a,c){a===c||g||e.setCenter(new DG.GeoPoint(b.longitude,a),b.zoom)},!0),b.$watch("longitude",function(a,c){a===c||g||e.setCenter(new DG.GeoPoint(a,b.latitude),b.zoom)},!0),b.$watch("zoom",function(a,b){a===b||g||e.setZoom(a)},!0),b.$watch("zoomControls",function(a,b){a!=b&&(a?f.show():f.hide())}),b.$watch("fullscreenControls",function(a,b){a!=b&&(a?e.fullscreen.enable():e.fullscreen.disable())}),e.addEventListener(e.getContainerId(),"DgZoomChange",function(a){c.$root.$$phase?b.zoom=a.getZoom():b.$apply(function(){b.zoom=a.getZoom()})}),e.addEventListener(e.getContainerId(),"DgMapMove",function(a){var d=a.getCenter();d&&(c.$root.$$phase?(b.latitude=d.lat,b.longitude=d.lon):b.$apply(function(){b.latitude=d.lat,b.longitude=d.lon}))}),e.addEventListener(e.getContainerId(),"DgDragStart",function(){g=!0}),e.addEventListener(e.getContainerId(),"DgDragStop",function(){g=!1})}}}]),a.directive("dgMarker",["$log",function(a){return{restrict:"E",require:"^dgMap",scope:{lon:"=longitude",lat:"=latitude",dgClick:"&ngClick",show:"=",hint:"=",dragStop:"&",dragStart:"&"},link:function(b,c,d,e){var f={geoPoint:new DG.GeoPoint(b.lon,b.lat),draggable:!!d.draggable,hint:d.hint,dragStartCallback:function(){b.dragStart()},dragStopCallback:function(){var a=h.getPosition();a&&b.$apply(function(){b.lon=a.lon,b.lat=a.lat}),b.dragStop()}};if(angular.isDefined(d.ngClick)&&angular.extend(f,{clickCallback:function(){b.dgClick()}}),d.iconSrc)if(angular.isDefined(d.iconWidth)&&angular.isDefined(d.iconHeight)){var g=new DG.Icon(d.iconSrc,new DG.Size(parseInt(d.iconWidth,10),parseInt(d.iconHeight,10)));angular.extend(f,{icon:g})}else a.error("angular-dg-marker: icon width and height should be specified");var h=new DG.Markers.Common(f);e.addMarker(h),b.$watch("lon",function(a){if(angular.isDefined(a)){var b=h.getPosition();b.lon=a,h.setPosition(b)}}),b.$watch("lat",function(a){if(angular.isDefined(a)){var b=h.getPosition();b.lat=a,h.setPosition(b)}}),b.$watch("show",function(a){angular.isDefined(a)&&(a?h.show():h.hide())}),b.$watch("hint",function(a){angular.isDefined(a)&&h.setHintContent(a)}),b.$watch("draggable",function(a){angular.isDefined(a)&&(a?h.enableDraggable():h.disableDraggable())}),c.bind("$destroy",function(){e.removeMarker(h)})}}}]),a.directive("dgStaticMap",["$log",function(a){return{restrict:"E",transclude:!0,template:"<img class='angular-dg-static-map' ng-src='{{ mapSrc }}'><dg-static-markers ng-transclude></dg-static-markers>",scope:{latitude:"=",longitude:"=",zoom:"=",width:"=",height:"="},controller:["$scope",function(a){a.markers=[],this.addMarker=function(b){a.markers.push(b)},this.removeMarker=function(b){angular.forEach(a.markers,function(c,d){c===b&&a.markers.splice(d,1)})}}],link:function(b,c){if(!angular.isDefined(b.latitude))return a.error("angular-dg-static-maps: map latitude property not set"),void 0;if(!angular.isDefined(b.longitude))return a.error("angular-dg-static-maps: map longitude property not set"),void 0;if(!angular.isDefined(b.markers)&&!angular.isDefined(b.zoom))return a.error("angular-dg-static-maps: map zoom property not set"),void 0;if(!angular.isDefined(b.width)||!angular.isDefined(b.height))return a.error("angular-dg-static-maps: width and height properties should be set"),void 0;angular.element(c).addClass("angular-dg-static-map");var d="http://static.maps.api.2gis.ru/1.0?";if(d+="center="+b.longitude+","+b.latitude,d+="&zoom="+b.zoom,d+="&size="+b.width+","+b.height,angular.isDefined(b.markers)&&angular.isArray(b.markers)&&b.markers.length){var e=[];angular.forEach(b.markers,function(a){e.push(a.lon+","+a.lat+(a.hint?","+a.hint:""))}),d+="&markers="+e.join("~")}b.mapSrc=d}}}]),a.directive("dgStaticMarker",["$log",function(a){return{restrict:"E",priority:100,require:"^dgStaticMap",link:function(b,c,d,e){return"hint"in d&&(!angular.isNumber(parseInt(d.hint,10))||parseInt(d.hint,10)!=d.hint)?(a.error("angular-dg-static-marker: hint should be a number"),void 0):(e.addMarker({lon:d.longitude,lat:d.latitude,hint:parseInt(d.hint,10)}),void 0)}}}]),a.service("geocoder",function(){return{get:function(a,b){return DG.Geocoder.get(a,b)}}})}();