-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathya-map-2.0.min.js
2 lines (2 loc) · 10.3 KB
/
ya-map-2.0.min.js
1
2
/*! yaMap 16-09-2015 */
"use strict";angular.module("yaMap",[]).constant("GEOMETRY_TYPES",{POINT:"Point",LINESTRING:"LineString",RECTANGLE:"Rectangle",POLYGON:"Polygon",CIRCLE:"Circle"}).value("yaMapSettings",{lang:"ru-RU",order:"longlat"}).factory("mapApiLoad",["yaMapSettings",function(a){var b=!1,c=[],d=function(){for(var a;c.length;)a=c.splice(0,1),a[0]()},e="//api-maps.yandex.ru/2.0/?load=package.full&lang="+(a.lang||"ru-RU")+"&coordorder="+(a.order||"longlat"),f=!1,g=function(a,b){if(!f){f=!0;var c=document.createElement("script");c.type="text/javascript",c.readyState?c.onreadystatechange=function(){("loaded"==c.readyState||"complete"==c.readyState)&&(c.onreadystatechange=null,b())}:c.onload=function(){b()},c.src=a,document.getElementsByTagName("head")[0].appendChild(c)}};return function(a){c.push(a),b?d():f||g(e,function(){ymaps.ready(function(){b=!0,d()})})}}]).service("yaLayer",[function(){this.create=function(a,b){return new ymaps.Layer(a,b)}}]).service("yaMapType",[function(){this.create=function(a,b){return new ymaps.MapType(a,b)}}]).service("layerStorage",["mapApiLoad",function(a){this.get=function(b){if(this._storage)b(this._storage);else{var c=this;a(function(){c._storage=ymaps.layer.storage,b(c._storage)})}}}]).service("mapTypeStorage",["mapApiLoad",function(a){this.get=function(b){if(this._storage)b(this._storage);else{var c=this;a(function(){c._storage=ymaps.mapType.storage,b(c._storage)})}}}]).service("yaSubscriber",function(){var a=/^yaEvent(\w*)?([A-Z]{1}[a-z]+)$/;this.subscribe=function(b,c,d,e){var f=a.exec(d),g=f[2].toLowerCase(),h=f[1]?f[1][1].toLowerCase()+f[1].substring(1):void 0;e[d]=function(a){return c(e.$parent||e,a)};var i=h?b[h].events:b.events;i.add(g,function(a){setTimeout(function(){e.$apply(function(){e[d]({$event:a})})})})}}).service("templateLayoutFactory",["mapApiLoad",function(a){this._cache={},this.get=function(a){return this._cache[a]},this.create=function(b,c,d){if(!this._cache[b]){var e=this;a(function(){e._cache[b]=ymaps.templateLayoutFactory.createClass(c,d)})}}}]).directive("yaTemplateLayout",["templateLayoutFactory",function(a){return{restrict:"E",priority:1001,scope:{overrides:"=yaOverrides"},compile:function(b){var c=b.html();return b.children().remove(),function(b,d,e){if(!e.yaKey)throw new Error('not require attribute "key"');var f=e.yaKey;a.create(f,c,b.overrides)}}}}]).controller("YaMapCtrl",["$scope","mapApiLoad",function(a,b){var c=this;b(function(){c.addGeoObjects=function(b){a.map.geoObjects.add(b)},c.removeGeoObjects=function(b){a.map.geoObjects.remove(b)},c.addControl=function(b,c){a.map.controls.add(b,c)},c.getMap=function(){return a.map},c.addImageLayer=function(b,c){var d=new ymaps.Layer(b,c);a.map.layers.add(d)},c.addHotspotLayer=function(b,c,d){var e=new ymaps.hotspot.ObjectSource(b,c),f=new ymaps.hotspot.Layer(e,d);a.map.layers.add(f)},c.addToolbar=function(b){a.map.controls.add(b)}})}]).directive("yaMap",["$compile","mapApiLoad","yaMapSettings","$window","yaSubscriber","$parse","$q","$timeout",function(a,b,c,d,e,f,g,h){return{restrict:"E",scope:{yaCenter:"@",yaType:"@",yaBeforeInit:"&",yaAfterInit:"&"},compile:function(d){var i=d.children(),j=null;return d.children().remove(),function(d,k,l){for(var m,n=function(a){try{return d.$eval(a)}catch(b){return a}},o=function(a){j&&j.reject(),j=g.defer();var e;return a?angular.isArray(a)?h(function(){j.resolve(a)}):angular.isString(a)&&b(function(){ymaps.geocode(a,{results:1}).then(function(a){var b=a.geoObjects.get(0);e=b.geometry.getCoordinates(),d.$apply(function(){j.resolve(e)})},function(a){d.$apply(function(){j.reject(a)})})}):b(function(){e="longlat"===c.order?[ymaps.geolocation.longitude,ymaps.geolocation.latitude]:[ymaps.geolocation.latitude,ymaps.geolocation.longitude],h(function(){j.resolve(e)})}),j.promise},p=Number(l.yaZoom),q=l.yaBehaviors?l.yaBehaviors.split(" "):["default"],r=[],s=[],t=0,u=q.length;u>t;t++)m=q[t],"-"===m[0]?r.push(m.substring(1)):s.push(m);0>p?p=0:p>23&&(p=23);var v,w=function(c){var h=g.defer();return b(function(){d.yaBeforeInit();var b=l.yaOptions?d.$eval(l.yaOptions):void 0;b&&b.projection&&(b.projection=new ymaps.projection[b.projection.type](b.projection.bounds)),d.map=new ymaps.Map(k[0],{center:c,zoom:p,type:l.yaType||"yandex#map",behaviors:s},b),d.map.behaviors.disable(r);for(var g in l)if(0===g.indexOf("yaEvent")){var j=f(l[g]);e.subscribe(d.map,j,g,d)}h.resolve(d.map),d.yaAfterInit({$target:d.map}),k.append(i),setTimeout(function(){d.$apply(function(){a(k.children())(d.$parent)})})}),h.promise};d.$watch("yaCenter",function(a){var b=n(a);o(b).then(function(a){if(!v){v=w(a);var b=!0}v.then(function(c){b||c.setCenter(a)})})}),d.$watch("yaType",function(a){a&&v&&v.then(function(b){b.setType(a)})}),d.$on("$destroy",function(){d.map&&d.map.destroy()})}},controller:"YaMapCtrl"}}]).controller("MapToolbarCtrl",["$scope",function(a){this.add=function(b){a.toolbar.add(b)}}]).directive("yaToolbar",["$compile","$parse","yaSubscriber",function(a,b,c){return{require:"^yaMap",restrict:"E",scope:{yaAfterInit:"&"},compile:function(d){var e=d.contents();return d.children().remove(),function(d,f,g,h){if(!g.yaName)throw new Error('not pass attribute "name"');var i=g.yaOptions?d.$eval(g.yaOptions):void 0,j=g.yaParams?d.$eval(g.yaParams):void 0,k=g.yaName[0].toUpperCase()+g.yaName.substring(1);d.toolbar=new ymaps.control[k](j);for(var l in g)if(0===l.indexOf("yaEvent")){var m=b(g[l]);c.subscribe(d.toolbar,m,l,d)}h.addControl(d.toolbar,i),d.yaAfterInit({$target:d.toolbar}),f.append(e),a(f.children())(d.$parent)}},controller:"MapToolbarCtrl"}}]).directive("yaControl",["yaSubscriber","templateLayoutFactory","$parse",function(a,b,c){return{restrict:"E",require:"^yaToolbar",scope:{yaAfterInit:"&"},link:function(d,e,f,g){var h=f.yaType[0].toUpperCase()+f.yaType.substring(1),i=function(a){try{return d.$eval(a)}catch(b){return a}},j=i(f.yaParams),k=f.yaOptions?d.$eval(f.yaOptions):void 0;k&&k.layout&&(k.layout=b.get(k.layout)),k&&k.itemLayout&&(k.itemLayout=b.get(k.itemLayout));var l,m=["SearchControl","SmallZoomControl","ScaleLine","ZoomControl"];if(m.indexOf(h)>-1)l=new ymaps.control[h](k);else{if(j&&j.items){for(var n,o=[],p=0,q=j.items.length;q>p;p++)n=j.items[p],o.push(new ymaps.control.ListBoxItem(n));j.items=o}l=new ymaps.control[h](j,k)}for(var r in f)if(0===r.indexOf("yaEvent")){var s=c(f[r]);a.subscribe(l,s,r,d)}g.add(l),d.yaAfterInit({$target:l})}}}]).controller("CollectionCtrl",["$scope",function(a){this.addGeoObjects=function(b){a.collection.add(b)},this.removeGeoObjects=function(b){a.collection.remove(b)}}]).directive("yaCollection",["$compile","yaMapSettings","$timeout","yaSubscriber","$parse",function(a,b,c,d,e){return{require:"^yaMap",restrict:"E",scope:{yaAfterInit:"&"},compile:function(b){var f=b.contents();return b.children().remove(),function(b,g,h,i){var j=h.yaOptions?b.$eval(h.yaOptions):{},k=angular.isDefined(h.showAll)&&"false"!=h.showAll;if(k){var l,m=i.getMap(),n=function(){l&&c.cancel(l),l=c(function(){m.geoObjects.events.remove("add",n);var a=m.geoObjects.getBounds();a&&m.setBounds(a)},300)};m.geoObjects.events.add("add",n)}b.collection=new ymaps.GeoObjectCollection({},j);for(var o in h)if(0===o.indexOf("yaEvent")){var p=e(h[o]);d.subscribe(b.collection,p,o,b)}i.addGeoObjects(b.collection),b.yaAfterInit({$target:b.collection}),b.$on("$destroy",function(){b.collection&&i.removeGeoObjects(b.collection)}),g.append(f),a(g.children())(b.$parent)}},controller:"CollectionCtrl"}}]).directive("yaCluster",["yaMapSettings","yaSubscriber","$compile","templateLayoutFactory","$parse",function(a,b,c,d,e){return{require:"^yaMap",restrict:"E",scope:{yaAfterInit:"&"},compile:function(a){var f=a.contents();return a.children().remove(),function(a,g,h,i){var j=h.yaOptions?a.$eval(h.yaOptions):{};j&&j.clusterBalloonMainContentLayout&&(j.clusterBalloonMainContentLayout=d.get(j.clusterBalloonMainContentLayout)),j&&j.clusterBalloonSidebarItemLayout&&(j.clusterBalloonSidebarItemLayout=d.get(j.clusterBalloonSidebarItemLayout)),j&&j.clusterBalloonContentItemLayout&&(j.clusterBalloonContentItemLayout=d.get(j.clusterBalloonContentItemLayout)),j&&j.clusterBalloonAccordionItemContentLayout&&(j.clusterBalloonAccordionItemContentLayout=d.get(j.clusterBalloonAccordionItemContentLayout)),a.collection=new ymaps.Clusterer(j);for(var k in h)if(0===k.indexOf("yaEvent")){var l=e(h[k]);b.subscribe(a.collection,l,k,a)}i.addGeoObjects(a.collection),a.yaAfterInit({$target:a.collection}),a.$on("$destroy",function(){a.collection&&i.removeGeoObjects(a.collection)}),g.append(f),c(f)(a.$parent)}},controller:"CollectionCtrl"}}]).directive("yaGeoObject",["GEOMETRY_TYPES","yaSubscriber","templateLayoutFactory","$parse",function(a,b,c,d){return{restrict:"E",require:["^yaMap","?^yaCollection","?^yaCluster"],scope:{yaSource:"=",yaShowBalloon:"=",yaAfterInit:"&"},link:function(e,f,g,h){var i,j=h[2]||h[1]||h[0],k=g.yaOptions?e.$eval(g.yaOptions):void 0;k&&k.balloonContentLayout&&(k.balloonContentLayout=c.get(k.balloonContentLayout));var l=function(a,c){i=new ymaps.GeoObject(a,c);for(var f in g)if(0===f.indexOf("yaEvent")){var h=d(g[f]);b.subscribe(i,h,f,e)}j.addGeoObjects(i),e.yaAfterInit({$target:i}),m(g.yaEdit),n(g.yaDraw),o(e.yaShowBalloon)};e.$watch("yaSource",function(b){if(b)if(i){i.geometry.setCoordinates(b.geometry.coordinates),i.geometry.getType()===a.CIRCLE&&i.geometry.setRadius(b.geometry.radius);var c=b.properties;for(var d in c)c.hasOwnProperty(d)&&i.properties.set(d,c[d])}else l(b,k);else i&&j.removeGeoObjects(i)},angular.equals);var m=function(a){angular.isDefined(a)&&"false"!==a?i&&i.editor.startEditing():angular.isDefined(a)&&i&&i.editor.stopEditing()},n=function(a){angular.isDefined(a)&&"false"!==a?i&&i.editor.startDrawing():angular.isDefined(a)&&i&&i.editor.stopDrawing()},o=function(a){a?i&&i.balloon.open():i&&i.balloon.close()};g.$observe("yaEdit",m),g.$observe("yaDraw",n),e.$watch("yaShowBalloon",o),e.$on("$destroy",function(){i&&j.removeGeoObjects(i)})}}}]).directive("yaHotspotLayer",[function(){return{restrict:"E",require:"^yaMap",link:function(a,b,c,d){if(!c.yaUrlTemplate)throw new Error('not exists required attribute "url-template"');if(!c.yaKeyTemplate)throw new Error('not exists required attribute "key-template"');var e=c.yaOptions?a.$eval(c.yaOptions):void 0;d.addHotspotLayer(c.yaUrlTemplate,c.yaKeyTemplate,e)}}}]).directive("yaImageLayer",[function(){return{restrict:"E",require:"^yaMap",link:function(a,b,c,d){if(!c.yaUrlTemplate)throw new Error('not exists required attribute "url-template"');var e=c.yaOptions?a.$eval(c.yaOptions):void 0;d.addImageLayer(c.yaUrlTemplate,e)}}}]);