-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathslidemapper.min.js
9 lines (9 loc) · 10.2 KB
/
slidemapper.min.js
1
2
3
4
5
6
7
8
9
/* ==========================================================
* slidemapper.js v0.1.2
* A jQuery plugin to create an animated slideshow tied to a Leaflet map.
* http://github.com/cavis/slidemapper
* ==========================================================
* Copyright (c) 2012 Ryan Cavis
* Licensed under http://en.wikipedia.org/wiki/MIT_License
* ========================================================== */
(function(h){var e={slides:[],mapType:"mapquest",apiKey:null,center:[40.423,-98.7372],zoom:4,minZoom:2,maxZoom:10,keyEvents:true,closePopupOnClick:false,mapPosition:"bottom",mapHeight:400,slideHeight:220,autoHeight:false,leafPile:false,animateSpeed:200,controlType:"sides"};var f;var k;function l(p,q){var o=q?-(p.width()):p.width();p.animate({"margin-left":o},f.options.animateSpeed,"swing",function(){p.removeClass("active").removeAttr("style")})}function a(o,p){var q=p?o.width():-(o.width());o.css("margin-left",q).addClass("active");o.animate({"margin-left":0},f.options.animateSpeed,"swing",function(){o.removeAttr("style")});b(o,true)}function j(){var q=k.find(".ctrl-left"),o=k.find(".ctrl-right"),p=k.find(".ctrl-count");(f.index>0)?q.addClass("active"):q.removeClass("active");(f.index<f.items.length-1)?o.addClass("active"):o.removeClass("active");p.html((f.index===null?0:f.index+1)+" of "+f.items.length)}function b(s,p){if(f.options.autoHeight){var t=k.find(".smapp-show");if(!f.autoHeight){f.autoHeight=t.height()}var o=s.find(".slide-inner").height(),r=t.height();if(o>r){p?t.animate({height:o},f.options.animateSpeed):t.height(o)}else{if(o<r){var q=Math.max(o,f.autoHeight);p?t.animate({height:q},f.options.animateSpeed):t.height(q)}}}}function c(o){if(f.index!==null){if(o.keyCode==37){k.slideMapper("prev")}if(o.keyCode==39){k.slideMapper("next")}}}function g(o){if(f.tileLayer){f.map.removeLayer(f.tileLayer);f.tileLayer=false}if(o=="cloudmade"){if(!f.options.apiKey){h.error("apiKey required for cloudmade tiles")}var p={attribution:'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>'};f.tileLayer=new L.TileLayer("http://{s}.tile.cloudmade.com/"+f.options.apiKey+"/997/256/{z}/{x}/{y}.png",p)}else{if(o=="stamen-toner"){if(!L.StamenTileLayer){h.error("did you forget to include tile.stamen.js?")}f.tileLayer=new L.StamenTileLayer("toner")}else{if(o=="stamen-terrain"){if(!L.StamenTileLayer){h.error("did you forget to include tile.stamen.js?")}f.tileLayer=new L.StamenTileLayer("terrain")}else{if(o=="stamen-watercolor"){if(!L.StamenTileLayer){h.error("did you forget to include tile.stamen.js?")}f.tileLayer=new L.StamenTileLayer("watercolor")}else{if(o=="mapquest"){var p={subdomains:["otile1","otile2","otile3","otile4"],attribution:'Tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png">'};f.tileLayer=new L.TileLayer("http://{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png",p)}else{if(o=="mapquest-aerial"){var p={subdomains:["oatile1","oatile2","oatile3","oatile4"],attribution:'Tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png">'};f.tileLayer=new L.TileLayer("http://{s}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.png",p)}else{h.error("invalid tile type: "+o)}}}}}}f.map.addLayer(f.tileLayer)}function m(r){var t,p='style="height:'+r.mapHeight+'px"';if(r.mapPosition==="top"){t='<div class="smapp-map top" '+p+"></div>"}else{if(r.mapPosition==="bottom"){t='<div class="smapp-map bottom" '+p+"></div>"}else{h.error("Invalid mapPosition: "+r.mapPosition)}}var o=r.autoHeight?'<div class="smapp-show" style="min-height:'+r.slideHeight+'px">':'<div class="smapp-show" style="height:'+r.slideHeight+'px">';o+='<div class="smapp-slides-ct"></div>';if(r.controlType==="sides"){o+='<span class="ctrl-left ctrl-side">‹</span>';o+='<span class="ctrl-right ctrl-side">›</span>'}else{if(r.controlType==="top"){o+='<div class="ctrl-top">';o+='<span class="ctrl-left">‹</span>';o+='<span class="ctrl-count">0 of 0</span>';o+='<span class="ctrl-right">›</span>';o+="</div>"}else{h.error("Invalid controlType: "+r.controlType)}}o+="</div>";var q='<div class="smapp">';q+=(r.mapPosition==="top")?t:"";q+=o;q+=(r.mapPosition==="bottom")?t:"";return q}function n(p,q){f.map.closePopup();if(p.marker&&p.config.popup){if(p.marker._leafpile){p.marker.openPopup()}else{var o=p.marker._popup.setLatLng(p.marker.getLatLng());f.map.openPopup(o)}}f.map.setView(p.center,p.zoom||f.map.getZoom(),q?false:true)}function i(){var o;if(f.options.leafPile){if(!L.LeafpileGroup){h.error("Did you forget to include leafpile.js?")}var p=f.options.leafPile===true?{}:f.options.leafPile;o=new L.LeafpileGroup(p);o.on("redraw",function(q){if(f.index!==null&&f.items[f.index].config.popup){f.items[f.index].marker.openPopup()}});o.on("click",function(r){if(r.leafpile){var q=r.markers[0].index;if(q!=f.index){if(f.index!==null){l(f.items[f.index].$slide,(q>f.index))}a(f.items[q].$slide,(q>f.index));f.index=q;j()}}if(r.zooming&&f.frozen){r.cancelZoom()}})}else{o=new L.LayerGroup()}return o}var d={init:function(o){if(!f){f={};f.options=h.extend({},e,o);d.options(o||{})}},options:function(o){if(o===undefined){return f.options}else{f.options=h.extend({},f.options,o);k.empty();k.append(m(f.options));showEl=h(".smapp-show",k)[0];mapEl=h(".smapp-map",k)[0];h(showEl).on("click",".ctrl-left",function(){k.slideMapper("prev")}).on("click",".ctrl-right",function(){k.slideMapper("next")});d.keyEvents(f.options.keyEvents);f.options.center=new L.LatLng(f.options.center[0],f.options.center[1]);f.map=new L.Map(mapEl,f.options);g(f.options.mapType);f.markergroup=i();f.map.addLayer(f.markergroup);f.items=[];f.index=null;d.add(f.options.slides)}},keyEvents:function(o){h(document).unbind("keydown",c);if(o){h(document).keydown(c)}},mapEvents:function(s){var r=["dragging","touchZoom","doubleClickZoom","scrollWheelZoom","boxZoom"];var q=s?"enable":"disable";for(var o=0;o<r.length;o++){var t=r[o];if(f.map[t]&&typeof(f.map[t][q])==="function"){f.map[t][q]()}}},freeze:function(o){f.frozen=o;o?d.keyEvents(false):d.keyEvents(true)},count:function(){return f.items.length},get:function(o){o=(o===undefined||o===null)?f.index:o;return(f.items[o]===undefined)?false:f.items[o]},add:function(p){var o=(f.items&&f.items.length)?f.items.length:0;return d.insert(o,p)},insert:function(t,v){if(h.isArray(v)){var u=[];h.each(v,function(x,y){u.push(d.insert.call(this,t+x,y))});return u}if(!h.isNumeric(t)||t<0||t>f.items.length){h.error('Invalid index "'+t+'" provided to insert')}var w={index:t,config:v};if(v.marker){var o=L.Icon.extend({options:{shadowUrl:"",iconSize:[40,40],shadowSize:[0,0],iconAnchor:[22,94],shadowAnchor:[0,0],popupAnchor:[-3,-76]}});var p=new L.LatLng(v.marker[0],v.marker[1]);if(v.icon){var q=new o({iconUrl:v.icon});w.marker=new L.marker(p,{icon:q})}else{w.marker=new L.Marker(p)}if(v.popup){w.marker.bindPopup(v.popup)}w.marker.index=t;w.marker.on("click",function(y){var x=y.target.index;if(!f.frozen&&f.index!=x&&k.triggerHandler("move",[d.get(x),x])!==false){if(f.index!==null){l(f.items[f.index].$slide,(x>f.index))}a(f.items[x].$slide,(x>f.index));f.index=x;j()}});f.markergroup.addLayer(w.marker)}var s='<div class="slide"><div class="slide-inner">'+(v.html||"")+"</div></div>";if(t==f.items.length){w.$slide=h(s).appendTo(k.find(".smapp-slides-ct"))}else{w.$slide=h(s).insertBefore(f.items[t].$slide)}w.zoom=w.marker?v.zoom:f.options.zoom;w.center=w.marker?w.marker.getLatLng():f.options.center;if(v.center){w.center=new L.LatLng(v.center[0],v.center[1])}f.items.splice(t,0,w);for(var r=t+1;r<f.items.length;r++){f.items[r].index++;if(f.items[r].marker){f.items[r].marker.index++}}if(f.items.length==1){d.move(0,false)}else{if(f.index===t){f.index++}}j();return w},shuffle:function(t,s){var o=(s===undefined||s===null)?f.index:t;var r=d.get(o);if(!r){h.error('Invalid from index "'+o+'" provided to remove')}var q=(s===undefined||s===null)?t:s;if(q=="first"){q=0}if(q=="last"){q=f.items.length-1}if(!h.isNumeric(q)||q<0||q>f.items.length-1){h.error('Invalid to index "'+q+'" provided to insert')}if(o==q){return}f.items.splice(o,1);for(var p=o;p<f.items.length;p++){f.items[p].index--;if(f.items[p].marker){f.items[p].marker.index--}}if(q==f.items.length){r.$slide.appendTo(k.find(".smapp-slides-ct"))}else{r.$slide.insertBefore(f.items[q].$slide)}r.index=q;if(r.marker){r.marker.index=q}f.items.splice(q,0,r);for(var p=q+1;p<f.items.length;p++){f.items[p].index++;if(f.items[p].marker){f.items[p].marker.index++}}if(f.index==o){f.index=q}j();return r},remove:function(o){var r=d.get(o);if(!r){h.error('Invalid index "'+o+'" provided to remove')}if(f.items.length==1){return d.removeAll()}if(r.marker){f.markergroup.removeLayer(r.marker)}r.$slide.remove();f.items.splice(r.index,1);for(var p=r.index;p<f.items.length;p++){f.items[p].index--;if(f.items[p].marker){f.items[p].marker.index--}}if(f.index===r.index){var q=Math.min(r.index,f.items.length-1);a(f.items[q].$slide,(q>=f.index));n(f.items[q],true);f.index=q}j()},removeAll:function(){f.map.removeLayer(f.markergroup);k.find(".smapp-slides-ct").empty();f.markergroup=i();f.map.addLayer(f.markergroup);f.items=[];f.index=null;j();f.map.setView(f.options.center,f.options.zoom)},move:function(p,o){if(f.frozen){return}if(!d.get(p)){return}if(k.triggerHandler("move",[d.get(p),p])===false){return}if(o){if(f.index!==null){l(f.items[f.index].$slide,(p>f.index))}a(f.items[p].$slide,(p>f.index))}else{if(f.index!==null){f.items[f.index].$slide.removeClass("active")}f.items[p].$slide.addClass("active");b(f.items[0].$slide,false)}n(f.items[p],o);f.index=p;j()},next:function(){d.move(f.index===null?0:f.index+1,true)},prev:function(){d.move(f.index===null?0:f.index-1,true)}};h.fn.slideMapper=function(q){if(this.length>1){h.error("SlideMapper currently only supports 1 map per page")}else{if(q&&typeof q!=="object"&&!d[q]){h.error("Method "+q+" does not exist on jQuery.slideMapper")}else{var o=null;for(var p=0;p<this.length;p++){k=h(this[p]);f=k.data("slideMapper");if(d[q]){if(!f){h.error("Method "+q+" called on uninitialized element")}else{o=d[q].apply(this[p],Array.prototype.slice.call(arguments,1))}}else{d.init.apply(this[p],arguments)}k.data("slideMapper",f)}return(o===null?this:o)}}}})(jQuery);