PluginProbe ʕ •ᴥ•ʔ
Meta Box / 5.3.9
Meta Box v5.3.9
trunk 4.1.10 4.1.11 4.10 4.10.1 4.10.2 4.10.3 4.10.4 4.11 4.11.1 4.11.2 4.12.1 4.12.4 4.12.5 4.12.6 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.10 4.14.11 4.14.2 4.14.4 4.14.5 4.14.6 4.14.7 4.14.8 4.14.9 4.15.0 4.15.1 4.15.2 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.2 4.16.3 4.17.0 4.17.1 4.17.2 4.17.3 4.18.0 4.18.1 4.18.2 4.18.3 4.18.4 4.2 4.2.1 4.2.2 4.2.3 4.2.4 4.3 4.3.1 4.3.10 4.3.11 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.8 4.3.9 4.4.0 4.4.1 4.4.3 4.5 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.5.7 4.6 4.7 4.7.1 4.7.2 4.7.3 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.9 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.10.0 5.10.1 5.10.10 5.10.11 5.10.12 5.10.13 5.10.14 5.10.15 5.10.16 5.10.17 5.10.18 5.10.19 5.10.2 5.10.3 5.10.4 5.10.5 5.10.6 5.10.7 5.10.8 5.10.9 5.11.0 5.11.1 5.11.2 5.11.3 5.11.4 5.12.0 5.2.0 5.2.1 5.2.10 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.10 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.3.7 5.3.8 5.3.9 5.4.0 5.4.1 5.4.2 5.4.3 5.4.4 5.4.5 5.4.6 5.4.7 5.4.8 5.5.0 5.5.1 5.6.0 5.6.1 5.6.10 5.6.11 5.6.12 5.6.13 5.6.14 5.6.15 5.6.16 5.6.17 5.6.18 5.6.2 5.6.3 5.6.4 5.6.5 5.6.6 5.6.7 5.6.8 5.6.9 5.7.0 5.7.1 5.7.2 5.7.3 5.7.4 5.7.5 5.8.0 5.8.1 5.8.2 5.9.0 5.9.1 5.9.10 5.9.11 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9
meta-box / js / map.js
meta-box / js Last commit date
jqueryui 9 years ago select2 6 years ago wp-color-picker-alpha 5 years ago autocomplete.js 6 years ago autosave.js 7 years ago button-group.js 6 years ago clone.js 5 years ago color.js 6 years ago date.js 6 years ago datetime.js 6 years ago file-input.js 6 years ago file-upload.js 6 years ago file.js 5 years ago image-advanced.js 6 years ago image-select.js 6 years ago image-upload.js 6 years ago input-list.js 6 years ago map-frontend.js 6 years ago map.js 5 years ago media.js 5 years ago notification.js 6 years ago oembed.js 6 years ago osm-frontend.js 7 years ago osm.js 5 years ago range.js 6 years ago script.js 6 years ago select-advanced.js 6 years ago select-tree.js 6 years ago select.js 6 years ago slider.js 6 years ago taxonomy.js 6 years ago time.js 6 years ago validation.min.js 5 years ago video.js 6 years ago wysiwyg.js 5 years ago
map.js
272 lines
1 ( function ( $, document, window, google, rwmb, i18n ) {
2 'use strict';
3
4 // Use function construction to store map & DOM elements separately for each instance
5 var MapField = function ( $container ) {
6 this.$container = $container;
7 };
8
9 // Geocoder service.
10 var geocoder = new google.maps.Geocoder();
11
12 // Use prototype for better performance
13 MapField.prototype = {
14 // Initialize everything
15 init: function () {
16 this.initDomElements();
17 this.initMapElements();
18
19 this.initMarkerPosition();
20 this.addListeners();
21 this.autocomplete();
22 },
23
24 // Initialize DOM elements
25 initDomElements: function () {
26 this.$canvas = this.$container.find( '.rwmb-map-canvas' );
27 this.canvas = this.$canvas[0];
28 this.$coordinate = this.$container.find( '.rwmb-map' );
29 this.addressField = this.$container.data( 'address-field' );
30 },
31
32 // Initialize map elements
33 initMapElements: function () {
34 var defaultLoc = this.$canvas.data( 'default-loc' ),
35 latLng;
36
37 defaultLoc = defaultLoc ? defaultLoc.split( ',' ) : [53.346881, - 6.258860];
38 latLng = new google.maps.LatLng( defaultLoc[0], defaultLoc[1] ); // Initial position for map
39
40 this.map = new google.maps.Map( this.canvas, {
41 center: latLng,
42 zoom: 14,
43 streetViewControl: 0,
44 mapTypeId: google.maps.MapTypeId.ROADMAP
45 } );
46 this.marker = new google.maps.Marker( {position: latLng, map: this.map, draggable: true} );
47 },
48
49 // Initialize marker position
50 initMarkerPosition: function () {
51 var coordinate = this.$coordinate.val(),
52 location,
53 zoom;
54
55 if ( coordinate ) {
56 location = coordinate.split( ',' );
57 this.marker.setPosition( new google.maps.LatLng( location[0], location[1] ) );
58
59 zoom = location.length > 2 ? parseInt( location[2], 10 ) : 14;
60
61 this.map.setCenter( this.marker.position );
62 this.map.setZoom( zoom );
63 } else if ( this.addressField ) {
64 this.geocodeAddress( false );
65 }
66 },
67
68 // Add event listeners for 'click' & 'drag'
69 addListeners: function () {
70 var that = this;
71
72 /*
73 * Auto change the map when there's change in address fields.
74 * Works only for multiple address fields as single address field has autocomplete functionality.
75 */
76 if ( this.addressField.split( ',' ).length > 1 ) {
77 var geocodeAddress = that.geocodeAddress.bind( that );
78 var addressFields = this.addressField.split( ',' ).forEach( function( part ) {
79 var $field = that.findAddressField( part );
80 if ( null !== $field ) {
81 $field.on( 'change', geocodeAddress );
82 }
83 } );
84 }
85
86 google.maps.event.addListener( this.map, 'click', function ( event ) {
87 that.marker.setPosition( event.latLng );
88 that.updateCoordinate( event.latLng );
89 } );
90
91 google.maps.event.addListener( this.map, 'zoom_changed', function ( event ) {
92 that.updateCoordinate( that.marker.getPosition() );
93 } );
94
95 google.maps.event.addListener( this.marker, 'drag', function ( event ) {
96 that.updateCoordinate( event.latLng );
97 } );
98
99 /**
100 * Custom event to refresh maps when in hidden divs.
101 * @see https://developers.google.com/maps/documentation/javascript/reference ('resize' Event)
102 */
103 var refresh = that.refresh.bind( this );
104 $( window ).on( 'rwmb_map_refresh', refresh );
105
106 // Refresh on meta box hide and show
107 rwmb.$document.on( 'postbox-toggled', refresh );
108 // Refresh on sorting meta boxes
109 $( '.meta-box-sortables' ).on( 'sortstop', refresh );
110 },
111
112 refresh: function () {
113 if ( ! this.map ) {
114 return;
115 }
116 var zoom = this.map.getZoom(),
117 center = this.map.getCenter();
118
119 google.maps.event.trigger( this.map, 'resize' );
120 this.map.setZoom( zoom );
121 this.map.panTo( center );
122 },
123
124 // Autocomplete address
125 autocomplete: function () {
126 var that = this,
127 $address = this.getAddressField();
128
129 if ( null === $address ) {
130 return;
131 }
132
133 // If Meta Box Geo Location installed. Do not run autocomplete.
134 if ( $( '.rwmb-geo-binding' ).length ) {
135 var geocodeAddress = that.geocodeAddress.bind( that );
136 $address.on( 'selected_address', geocodeAddress );
137 return false;
138 }
139
140 $address.autocomplete( {
141 source: function ( request, response ) {
142 var options = {
143 'address': request.term,
144 'region': that.$canvas.data( 'region' )
145 };
146 geocoder.geocode( options, function ( results ) {
147 if ( ! results.length ) {
148 response( [ {
149 value: '',
150 label: i18n.no_results_string
151 } ] );
152 return;
153 }
154 response( results.map( function ( item ) {
155 return {
156 label: item.formatted_address,
157 value: item.formatted_address,
158 latitude: item.geometry.location.lat(),
159 longitude: item.geometry.location.lng()
160 };
161 } ) );
162 } );
163 },
164 select: function ( event, ui ) {
165 var latLng = new google.maps.LatLng( ui.item.latitude, ui.item.longitude );
166 that.map.setCenter( latLng );
167 that.marker.setPosition( latLng );
168 that.updateCoordinate( latLng );
169 }
170 } );
171 },
172
173 // Update coordinate to input field
174 updateCoordinate: function ( latLng ) {
175 var zoom = this.map.getZoom();
176 this.$coordinate.val( latLng.lat() + ',' + latLng.lng() + ',' + zoom ).trigger( 'change' );
177 },
178
179 // Find coordinates by address
180 geocodeAddress: function ( notify ) {
181 var address = this.getAddress(),
182 that = this;
183 if ( ! address ) {
184 return;
185 }
186
187 if ( false !== notify ) {
188 notify = true;
189 }
190 geocoder.geocode( {'address': address}, function ( results, status ) {
191 if ( status !== google.maps.GeocoderStatus.OK ) {
192 if ( notify ) {
193 alert( i18n.no_results_string );
194 }
195 return;
196 }
197 that.map.setCenter( results[0].geometry.location );
198 that.marker.setPosition( results[0].geometry.location );
199 that.updateCoordinate( results[0].geometry.location );
200 } );
201 },
202
203 // Get the address field.
204 getAddressField: function() {
205 // No address field or more than 1 address fields, ignore
206 if ( ! this.addressField || this.addressField.split( ',' ).length > 1 ) {
207 return null;
208 }
209 return this.findAddressField( this.addressField );
210 },
211
212 // Get the address value for geocoding.
213 getAddress: function() {
214 var that = this;
215
216 return this.addressField.split( ',' )
217 .map( function( part ) {
218 part = that.findAddressField( part );
219 return null === part ? '' : part.val();
220 } )
221 .join( ',' ).replace( /\n/g, ',' ).replace( /,,/g, ',' );
222 },
223
224 // Find address field based on its name attribute. Auto search inside groups when needed.
225 findAddressField: function( fieldName ) {
226 // Not in a group.
227 var $address = $( 'input[name="' + fieldName + '"]');
228 if ( $address.length ) {
229 return $address;
230 }
231
232 // If map and address is inside a cloneable group.
233 $address = this.$container.closest( '.rwmb-group-clone' ).find( 'input[name*="[' + fieldName + ']"]' );
234 if ( $address.length ) {
235 return $address;
236 }
237
238 // If map and address is inside a non-cloneable group.
239 $address = this.$container.closest( '.rwmb-group-wrapper' ).find( 'input[name*="[' + fieldName + ']"]' );
240 if ( $address.length ) {
241 return $address;
242 }
243
244 return null;
245 }
246 };
247
248 function createController() {
249 var $this = $( this ),
250 controller = $this.data( 'mapController' );
251 if ( controller ) {
252 return;
253 }
254
255 controller = new MapField( $this );
256 controller.init();
257 $this.data( 'mapController', controller );
258 }
259
260 function init( e ) {
261 $( e.target ).find( '.rwmb-map-field' ).each( createController );
262 }
263
264 function restart() {
265 $( '.rwmb-map-field' ).each( createController );
266 }
267
268 rwmb.$document
269 .on( 'mb_ready', init )
270 .on( 'clone', '.rwmb-input', restart );
271 } )( jQuery, document, window, google, rwmb, RWMB_Map );
272