| 1 |
/** |
| 2 |
* CodePeople Post Map |
| 3 |
* Version: 1.0.1 |
| 4 |
* Author: CodePeople |
| 5 |
* Plugin URI: http://wordpress.dwbooster.com |
| 6 |
*/ |
| 7 |
|
| 8 |
(function ($) { |
| 9 |
var _latlng_btn, _api_key = ''; |
| 10 |
|
| 11 |
if( |
| 12 |
typeof cpm_global != 'undefined' && |
| 13 |
typeof cpm_global['api_key'] != 'undefined' && |
| 14 |
cpm_global['api_key'] != '' |
| 15 |
) _api_key = 'key='+cpm_global['api_key']+'&'; |
| 16 |
|
| 17 |
if(!('gm_authFailure' in window)) |
| 18 |
{ |
| 19 |
window['gm_authFailure'] = function() |
| 20 |
{ |
| 21 |
$('.cpm_error_mssg').css({'font-weight': 'bold', 'font-size': '1.2em', 'color':'#FF0000', 'padding': '10px 0'}).html('Google maps failed to load! Be sure has been entered an API Key through the settings page of the plugin, and it can be used on this website.'); |
| 22 |
}; |
| 23 |
} |
| 24 |
|
| 25 |
window["cpm_thumbnail_selection"] = function(e){ |
| 26 |
var thumbnail_field = $(e).parent().find('input[type="text"]'); |
| 27 |
var media = wp.media({ |
| 28 |
title: 'Select Point Thumbnail', |
| 29 |
button: { |
| 30 |
text: 'Select Image' |
| 31 |
}, |
| 32 |
multiple: false |
| 33 |
}).on('select', |
| 34 |
(function( field ){ |
| 35 |
return function() { |
| 36 |
var attachment = media.state().get('selection').first().toJSON(); |
| 37 |
if( !/image/g.test( attachment.mime ) ) return; |
| 38 |
var fullSize = attachment.url; |
| 39 |
var imgUrl = (typeof attachment.sizes.thumbnail === "undefined") ? fullSize : attachment.sizes.thumbnail.url; |
| 40 |
field.val( imgUrl ); |
| 41 |
}; |
| 42 |
})( thumbnail_field ) |
| 43 |
).open(); |
| 44 |
return false; |
| 45 |
}; |
| 46 |
|
| 47 |
//--------------------------------------------------------- |
| 48 |
|
| 49 |
function _get_latlng(request, callback){ |
| 50 |
var g = new google.maps.Geocoder(); |
| 51 |
g.geocode(request, callback); |
| 52 |
}; |
| 53 |
|
| 54 |
window['cpm_get_latlng'] = function (){ |
| 55 |
function transform( v ) |
| 56 |
{ |
| 57 |
if( !isNaN( parseFloat( v ) ) && isFinite( v ) ) return v; |
| 58 |
v = v.replace(/[\W_]/g, " " ).replace(/\s+/g, ' ' ).replace( /^\s+/, '' ).replace( /\s+$/, '' ).toLowerCase(); |
| 59 |
var ref = ( /[ne]/.test( v ) ) ? 1 : -1, |
| 60 |
parts = v.split( ' ' ), |
| 61 |
l = parts.length; |
| 62 |
|
| 63 |
if( l >= 3 ) return ref * ( parts[ l - 3 ]*1 + parts[ l - 2 ]*1 / 60 + parts[ l - 1 ]*1 / 3600 ); |
| 64 |
return v; |
| 65 |
}; |
| 66 |
|
| 67 |
var f = _latlng_btn.parents('.point_form'), |
| 68 |
a = $('#cpm_point_address').val(), |
| 69 |
longitude = $('#cpm_point_longitude').val(), |
| 70 |
latitude = $('#cpm_point_latitude').val(), |
| 71 |
language = $('#cpm_map_language').val(), |
| 72 |
request = {}; |
| 73 |
|
| 74 |
// Remove unnecessary spaces characters |
| 75 |
longitude = longitude.replace(/^\s+/, '').replace(/\s+$/, ''); |
| 76 |
latitude = latitude.replace(/^\s+/, '').replace(/\s+$/, ''); |
| 77 |
a = a.replace(/^\s+/, '').replace(/\s+$/, ''); |
| 78 |
|
| 79 |
if(longitude.length && latitude.length){ |
| 80 |
request['location'] = new google.maps.LatLng( transform( latitude ), transform( longitude ) ); |
| 81 |
}else if(a.length){ |
| 82 |
request['address'] = a.replace(/[\n\r]/g, ''); |
| 83 |
}else{ |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
_get_latlng( |
| 88 |
request, |
| 89 |
(function( a, r ) |
| 90 |
{ |
| 91 |
return function(result, status) |
| 92 |
{ |
| 93 |
if(status && status == "OK"){ |
| 94 |
// Update fields |
| 95 |
var address = ( String( a ).trim().length && typeof r[ 'location' ] != 'undefined' ) ? a : result[0]['formatted_address'], |
| 96 |
latitude = result[0]['geometry']['location'].lat(), |
| 97 |
longitude = result[0]['geometry']['location'].lng(); |
| 98 |
|
| 99 |
if(address && latitude && longitude){ |
| 100 |
$('#cpm_point_address').val(address); |
| 101 |
$('#cpm_point_longitude').val(longitude); |
| 102 |
$('#cpm_point_latitude').val(latitude); |
| 103 |
|
| 104 |
// Load Map |
| 105 |
cpm_load_map(f.find('.cpm_map_container'),latitude, longitude); |
| 106 |
} |
| 107 |
}else{ |
| 108 |
alert('The point is not located: Be sure the address is valid, has been entered an API Key through the settings page of the plugin, and the Geocoding API is enabled in the Google Project.'); |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
} )( a, request ) |
| 113 |
); |
| 114 |
}; |
| 115 |
|
| 116 |
// Check the point or address existence |
| 117 |
window['cpm_checking_point'] = function (e){ |
| 118 |
var language = 'en'; |
| 119 |
_latlng_btn = $(e); |
| 120 |
|
| 121 |
if(typeof google != 'undefined' && google.maps){ |
| 122 |
cpm_get_latlng(); |
| 123 |
}else{ |
| 124 |
$('<script type="text/javascript" src="'+(( typeof window.location.protocol != 'undefined' ) ? window.location.protocol : 'http:' )+'//maps.google.com/maps/api/js?loading=async&'+_api_key+'callback=cpm_get_latlng'+((language) ? '&language='+language: '')+'&libraries=marker"></script>').appendTo('body'); |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
window['cpm_load_map'] = function(container, latitude, longitude){ |
| 129 |
var icon_img = document.createElement("img"); |
| 130 |
icon_img.src = cpm_default_marker.replace(/^http:/i, ''); |
| 131 |
|
| 132 |
var c = container, |
| 133 |
f = c.parents('.point_form'), |
| 134 |
p = new google.maps.LatLng(latitude, longitude), |
| 135 |
m = new google.maps.Map(c[0], { |
| 136 |
zoom: 5, |
| 137 |
center: p, |
| 138 |
mapTypeId: google.maps.MapTypeId['ROADMAP'], |
| 139 |
|
| 140 |
// Show / Hide controls |
| 141 |
panControl: true, |
| 142 |
scaleControl: true, |
| 143 |
zoomControl: true, |
| 144 |
mapTypeControl: true, |
| 145 |
scrollWheel: true, |
| 146 |
|
| 147 |
mapId: 'cpm_main_map' |
| 148 |
}), |
| 149 |
mk = new google.maps.marker.AdvancedMarkerElement({ |
| 150 |
position: p, |
| 151 |
map: m, |
| 152 |
content: icon_img, |
| 153 |
draggable: true |
| 154 |
}); |
| 155 |
|
| 156 |
google.maps.event.addListener(m, "click", function(e){ |
| 157 |
var latLng = e.latLng; |
| 158 |
mk.setPosition(latLng); |
| 159 |
}); |
| 160 |
|
| 161 |
google.maps.event.addListener(mk, 'position_changed', function(){ |
| 162 |
f.find('#cpm_point_latitude').val(mk.getPosition().lat()); |
| 163 |
f.find('#cpm_point_longitude').val(mk.getPosition().lng()); |
| 164 |
}); |
| 165 |
}; |
| 166 |
|
| 167 |
window['cpm_set_map_flag'] = function(){ |
| 168 |
var request = {}; |
| 169 |
if(cpm_point['longitude'] && cpm_point['latitude']){ |
| 170 |
request['location'] = new google.maps.LatLng(cpm_point['latitude'], cpm_point['longitude']); |
| 171 |
}else if(cpm_point['address']){ |
| 172 |
request['address'] = cpm_point['address'].replace(/[\n\r]/g, ''); |
| 173 |
} |
| 174 |
|
| 175 |
_get_latlng(request, function(result, status){ |
| 176 |
if(status && status == "OK"){ |
| 177 |
// Update fields |
| 178 |
var address = result[0]['formatted_address'], |
| 179 |
latitude = result[0]['geometry']['location'].lat(), |
| 180 |
longitude = result[0]['geometry']['location'].lng(); |
| 181 |
|
| 182 |
if(address && latitude && longitude){ |
| 183 |
// Load Map |
| 184 |
cpm_load_map($('.cpm_map_container'),latitude, longitude); |
| 185 |
} |
| 186 |
} |
| 187 |
}); |
| 188 |
}; |
| 189 |
|
| 190 |
window[ 'cpm_display_more_info' ] = function( e ){ |
| 191 |
e = $( e ); |
| 192 |
e.parent().hide().siblings( '.cpm_more_info' ).show(); |
| 193 |
}; |
| 194 |
|
| 195 |
window[ 'cpm_hide_more_info' ] = function( e ){ |
| 196 |
e = $( e ); |
| 197 |
e.parent().hide().siblings( '.cpm_more_info_hndl' ).show(); |
| 198 |
}; |
| 199 |
|
| 200 |
function enable_disable_fields(f, v){ |
| 201 |
var p = f.parents('#map_data'); |
| 202 |
p.find('input[type="text"]').attr({'DISABLED':v,'READONLY':v}); |
| 203 |
p.find('select').attr({'DISABLED':v,'READONLY':v}); |
| 204 |
p.find('input[type="checkbox"]').filter('[id!="cpm_map_single"]').attr({'DISABLED':v,'READONLY':v}); |
| 205 |
}; |
| 206 |
|
| 207 |
$(function(){ |
| 208 |
// Actions for icons |
| 209 |
$(".cpm_icon").on('click',function(){ |
| 210 |
var i = $(this); |
| 211 |
$('.cpm_icon.cpm_selected').removeClass('cpm_selected'); |
| 212 |
i.addClass('cpm_selected'); |
| 213 |
$('#default_icon').val($('img', i).attr('src')); |
| 214 |
}).on('mouseover',function(){ |
| 215 |
$(this).css({"border":"solid #BBBBBB 1px"}) |
| 216 |
}).on('mouseout',function(){ |
| 217 |
$(this).css({"border":"solid #F9F9F9 1px"}) |
| 218 |
}); |
| 219 |
|
| 220 |
window[ 'cpm_generate_shortcode' ] = function() |
| 221 |
{ |
| 222 |
return '[codepeople-post-map mapid=""]'; |
| 223 |
}; |
| 224 |
|
| 225 |
// Action for insert shortcode |
| 226 |
$('#cpm_map_shortcode').on('click',function(){ |
| 227 |
if(window.cpm_send_to_editor_default) |
| 228 |
window.send_to_editor = window.cpm_send_to_editor_default; |
| 229 |
if(send_to_editor){ |
| 230 |
send_to_editor(cpm_generate_shortcode()); |
| 231 |
} |
| 232 |
var t = $('#content'); |
| 233 |
if(t.length){ |
| 234 |
var v= t.val() |
| 235 |
if(v.indexOf('codepeople-post-map') == -1) |
| 236 |
t.val(v+'[codepeople-post-map]'); |
| 237 |
} |
| 238 |
}); |
| 239 |
|
| 240 |
// Create the script tag and load the maps api |
| 241 |
if($('.cpm_map_container').length){ |
| 242 |
if(typeof google != 'undefined' && google.maps) |
| 243 |
{ |
| 244 |
cpm_set_map_flag(); |
| 245 |
} |
| 246 |
else |
| 247 |
{ |
| 248 |
$('<script type="text/javascript" src="'+(( typeof window.location.protocol != 'undefined' ) ? window.location.protocol : 'http:' )+'//maps.google.com/maps/api/js?loading=async&'+_api_key+'callback=cpm_set_map_flag&libraries=marker"></script>').appendTo('body'); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
$('#cpm_map_single').each(function(){ |
| 253 |
var f = $(this); |
| 254 |
enable_disable_fields(f, !f[0].checked); |
| 255 |
f.on('click',function(){ |
| 256 |
enable_disable_fields(f,!f[0].checked); |
| 257 |
}); |
| 258 |
}); |
| 259 |
|
| 260 |
// Show/hide map settings on single pages. |
| 261 |
$('#cpm_map_single').on('change',function(){ |
| 262 |
$('.cpm-map-settings')[this.checked ? 'show' : 'hide'](); |
| 263 |
}); |
| 264 |
|
| 265 |
}); |
| 266 |
})(jQuery); |