| 1 |
(function ($) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
$(document).ready(function ($) { |
| 5 |
|
| 6 |
//Initiate Color Picker |
| 7 |
$('.cbxgooglemapmeta_colorpicker').wpColorPicker(); |
| 8 |
|
| 9 |
$('.selecttwo-select').select2({ |
| 10 |
placeholder: cbxgooglemap_meta.please_select, |
| 11 |
allowClear: false |
| 12 |
}); |
| 13 |
|
| 14 |
|
| 15 |
$('.cbxgooglemapmeta_filepicker_btn').on('click', function (event) { |
| 16 |
event.preventDefault(); |
| 17 |
|
| 18 |
var self = $(this); |
| 19 |
|
| 20 |
// Create the media frame. |
| 21 |
var file_frame = wp.media.frames.file_frame = wp.media({ |
| 22 |
title : self.data('uploader_title'), |
| 23 |
button : { |
| 24 |
text: self.data('uploader_button_text') |
| 25 |
}, |
| 26 |
multiple: false |
| 27 |
}); |
| 28 |
|
| 29 |
file_frame.on('select', function () { |
| 30 |
var attachment = file_frame.state().get('selection').first().toJSON(); |
| 31 |
|
| 32 |
self.prev('.cbxgooglemapmeta_filepicker').val(attachment.url); |
| 33 |
}); |
| 34 |
|
| 35 |
// Finally, open the modal |
| 36 |
file_frame.open(); |
| 37 |
}); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
// Switches option sections |
| 43 |
$('.metabox-content-cbxgooglemap').hide(); |
| 44 |
var activetab = ''; |
| 45 |
if (typeof(localStorage) !== 'undefined') { |
| 46 |
activetab = localStorage.getItem('cbxgooglemapmetaactivetab'); |
| 47 |
|
| 48 |
} |
| 49 |
if (activetab !== '' && $(activetab).length) { |
| 50 |
$(activetab).fadeIn(); |
| 51 |
} else { |
| 52 |
$('.metabox-content-cbxgooglemap:first').fadeIn(); |
| 53 |
} |
| 54 |
$('.metabox-content-cbxgooglemap .collapsed').each(function () { |
| 55 |
$(this).find('input:checked').parent().parent().parent().nextAll().each( |
| 56 |
function () { |
| 57 |
if ($(this).hasClass('last')) { |
| 58 |
$(this).removeClass('hidden'); |
| 59 |
return false; |
| 60 |
} |
| 61 |
$(this).filter('.hidden').removeClass('hidden'); |
| 62 |
}); |
| 63 |
}); |
| 64 |
|
| 65 |
if (activetab !== '' && $(activetab + '-tab').length) { |
| 66 |
$(activetab + '-tab').addClass('nav-tab-active'); |
| 67 |
} |
| 68 |
else { |
| 69 |
$('.nav-tab-wrapper a:first').addClass('nav-tab-active'); |
| 70 |
} |
| 71 |
|
| 72 |
$('.nav-tab-wrapper a').on('click', function (evt) { |
| 73 |
evt.preventDefault(); |
| 74 |
|
| 75 |
$('.nav-tab-wrapper a').removeClass('nav-tab-active'); |
| 76 |
$(this).addClass('nav-tab-active').blur(); |
| 77 |
var clicked_group = $(this).attr('href'); |
| 78 |
if (typeof(localStorage) !== 'undefined') { |
| 79 |
localStorage.setItem('cbxgooglemapmetaactivetab', $(this).attr('href')); |
| 80 |
} |
| 81 |
$('.metabox-content-cbxgooglemap').hide(); |
| 82 |
$(clicked_group).fadeIn(); |
| 83 |
|
| 84 |
}); |
| 85 |
|
| 86 |
var cbxmap = ''; |
| 87 |
|
| 88 |
|
| 89 |
//backend map integration |
| 90 |
$('.cbxgooglemapmeta_input_location').each(function (index, element) { |
| 91 |
|
| 92 |
var $element = $(element); |
| 93 |
var $parent = $element.parents('.form-table'); |
| 94 |
var $heading = $('#title').val(); |
| 95 |
var $address = $parent.find('.cbxgooglemapmeta_input_address').val(); |
| 96 |
var $website = $parent.find('.cbxgooglemapmeta_input_website').val(); |
| 97 |
|
| 98 |
var $lat = $parent.find('.cbxgooglemapmeta_input_lat') |
| 99 |
var $lng = $parent.find('.cbxgooglemapmeta_input_lng'); |
| 100 |
var $mapicon = $parent.find('.cbxgooglemapmeta_filepicker').val(); |
| 101 |
|
| 102 |
var $current_lat = $lat.val(); |
| 103 |
var $current_lng = $lng.val(); |
| 104 |
var $zoom = $parent.find('.cbxgooglemapmeta_input_zoom'); |
| 105 |
var $zoom_val = Number($zoom.val()); |
| 106 |
//var radius = $parent.find(".cbxgooglemapmeta_input_radius").val(); |
| 107 |
|
| 108 |
var $showinfo = Number($("input[name='_cbxgooglemapshowinfo']:checked").val()); |
| 109 |
var $scrollwheel = Number($("input[name='_cbxgooglemapscrollwheel']:checked").val()); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
var $map = $parent.find('.map_canvas'); |
| 114 |
var $mapsource = Number($map.data('mapsource')); |
| 115 |
|
| 116 |
//$showinfo = ($showinfo == 1)? true: false; |
| 117 |
//$scrollwheel = ($scrollwheel == 1)? true: false; |
| 118 |
|
| 119 |
var $maptype = $("select[name='_cbxgooglemapmaptype']").val(); |
| 120 |
|
| 121 |
if ($mapsource === 1) { |
| 122 |
var $apikey = $map.data('apikey'); |
| 123 |
if ($apikey !== '') { |
| 124 |
//console.log(google.maps.MapTypeId.ROADMAP); |
| 125 |
$map.locationpicker({ |
| 126 |
mapTypeId: $maptype, |
| 127 |
location : { |
| 128 |
latitude : [$current_lat], |
| 129 |
longitude: [$current_lng] |
| 130 |
}, |
| 131 |
radius : 0, |
| 132 |
zoom : $zoom_val, |
| 133 |
scrollwheel : ($scrollwheel === 1)? true: false, |
| 134 |
inputBinding : { |
| 135 |
latitudeInput : $parent.find('.cbxgooglemapmeta_input_lat'), |
| 136 |
longitudeInput : $parent.find('.cbxgooglemapmeta_input_lng'), |
| 137 |
//radiusInput: $parent.find(".cbxgooglemapmeta_input_radius"), |
| 138 |
locationNameInput: $parent.find('.cbxgooglemapmeta_input_location'), |
| 139 |
zoomInput: $parent.find('.cbxgooglemapmeta_input_zoom') |
| 140 |
}, |
| 141 |
enableAutocomplete: true, |
| 142 |
markerInCenter : true, |
| 143 |
markerIcon: $mapicon, |
| 144 |
onchanged : function (currentLocation, radius, isMarkerDropped) { |
| 145 |
//console.log("Location changed. New location (" + currentLocation.latitude + ", " + currentLocation.longitude + ")"); |
| 146 |
} |
| 147 |
}); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
else { |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
var $map = L.map($map.get(0)).setView([$current_lat, $current_lng], $zoom_val); |
| 156 |
|
| 157 |
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { |
| 158 |
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' |
| 159 |
}).addTo($map); |
| 160 |
|
| 161 |
/* L.Control.geocoder({ |
| 162 |
placeholder: cbxgooglemap_meta.search_address, |
| 163 |
}).addTo($map);*/ |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
if($mapicon !== ''){ |
| 169 |
|
| 170 |
var $l_icon = L.icon({ |
| 171 |
iconUrl: $mapicon, |
| 172 |
popupAnchor: [15, 0] |
| 173 |
}); |
| 174 |
|
| 175 |
var $marker = L.marker([$current_lat, $current_lng],{ |
| 176 |
draggable: true, |
| 177 |
icon:$l_icon |
| 178 |
}).addTo($map); |
| 179 |
} |
| 180 |
else{ |
| 181 |
var $marker = L.marker([$current_lat, $current_lng],{ |
| 182 |
draggable: true |
| 183 |
}).addTo($map); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
if($showinfo === 1){ |
| 188 |
|
| 189 |
if($website !== ''){ |
| 190 |
$heading = '<a href="'+$website+'" target="_blank">'+$heading+'</a>'; |
| 191 |
} |
| 192 |
|
| 193 |
$marker.bindPopup('<div class="jqcbxgoglemap_info"><h3 class="jqcbxgoglemap_info_heading">'+$heading+'</h3><div class="jqcbxgoglemap_info_body">'+$address+'</div></div>') |
| 194 |
.openPopup(); |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
if($scrollwheel === 1){ |
| 199 |
$map.scrollWheelZoom.disable(); |
| 200 |
} |
| 201 |
|
| 202 |
$marker.on('dragend', function (e) { |
| 203 |
$lat.val($marker.getLatLng().lat); |
| 204 |
$lng.val($marker.getLatLng().lng); |
| 205 |
|
| 206 |
}); |
| 207 |
|
| 208 |
$map.on('zoomend', function (e) { |
| 209 |
$zoom.val( $map.getZoom()); |
| 210 |
}); |
| 211 |
|
| 212 |
var geocoder = L.Control.geocoder({ |
| 213 |
placeholder: cbxgooglemap_meta.search_address, |
| 214 |
defaultMarkGeocode: false |
| 215 |
}) |
| 216 |
.on('markgeocode', function(e) { |
| 217 |
var bbox = e.geocode.bbox; |
| 218 |
//console.log(e.geocode); |
| 219 |
//console.log(bbox.getCenter().lat); |
| 220 |
//console.log(bbox.getCenter().lng); |
| 221 |
|
| 222 |
$lat.val(bbox.getCenter().lat); |
| 223 |
$lng.val(bbox.getCenter().lng); |
| 224 |
|
| 225 |
$map.setView(bbox.getCenter(), $zoom_val); |
| 226 |
$marker.setLatLng(bbox.getCenter()); |
| 227 |
|
| 228 |
/*var poly = L.polygon([ |
| 229 |
bbox.getSouthEast(), |
| 230 |
bbox.getNorthEast(), |
| 231 |
bbox.getNorthWest(), |
| 232 |
bbox.getSouthWest() |
| 233 |
]).addTo(map);*/ |
| 234 |
|
| 235 |
//map.fitBounds(poly.getBounds()); |
| 236 |
}).addTo($map); |
| 237 |
}//end if openstreetmap |
| 238 |
|
| 239 |
});//end google map |
| 240 |
|
| 241 |
|
| 242 |
//select shortcode text and copy to clipboard |
| 243 |
$('.cbxgooglemapshortcode').on('click', function (e) { |
| 244 |
|
| 245 |
var text = $(this).text(); |
| 246 |
var $this = $(this); |
| 247 |
var $input = $('<input class="cbxgooglemapshortcode-text" type="text">'); |
| 248 |
$input.prop('value', text); |
| 249 |
$input.insertAfter($(this)); |
| 250 |
$input.focus(); |
| 251 |
$input.select(); |
| 252 |
|
| 253 |
try { |
| 254 |
document.execCommand('copy'); |
| 255 |
} catch (err) { |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
$this.hide(); |
| 260 |
$input.on('focusout', function (e) { |
| 261 |
$this.show(); |
| 262 |
$input.remove(); |
| 263 |
}); |
| 264 |
}); |
| 265 |
|
| 266 |
}); |
| 267 |
})(jQuery); |
| 268 |
|