(function( $ ) { 'use strict'; /** * Render a Google Map onto the selected jQuery element. * * @since 1.0.0 */ function acadp_render_map( $el ) { // var var $marker = $el.find('.marker'); // vars var args = { zoom : parseInt( acadp.zoom_level ), center : new google.maps.LatLng(0, 0), mapTypeId : google.maps.MapTypeId.ROADMAP, scrollwheel : false }; // create map var map = new google.maps.Map( $el[0], args ); // add a markers reference map.marker = null; // add marker acadp_add_marker( $marker, map ); // center map acadp_center_map( map ); // Update map when contact details fields are updated in the custom post type "acadp_listings" $( '.acadp-map-field', '#acadp-contact-details' ).on('blur', function() { var address = []; address.push( $( '#acadp-address' ).val() ); address.push( $( '#acadp_location' ).find('option:selected').text() ); address.push( $( '#acadp-zipcode' ).val() ); address = address.filter( function( v ) { return v !== '' } ); address = address.join(); acadp_get_latlng_from_address( address, map ); }); }; /** * Add a marker to the selected Google Map. * * @since 1.0.0 */ function acadp_add_marker( $marker, map ) { var lat = $( '#acadp-latitude' ).val(); var lng = $( '#acadp-longitude' ).val(); if( ! lat && ! lng ) { acadp_get_latlng_from_address( $( '#acadp_location option:selected' ).text(), map ); lat = $( '#acadp-latitude' ).val(); lng = $( '#acadp-longitude' ).val(); }; var latlng = new google.maps.LatLng( lat, lng ); // create marker var marker = new google.maps.Marker({ position : latlng, map : map, draggable : true }); // add to array map.marker = marker; // if marker contains HTML, add it to an infoWindow if( $marker.html() ) { // create info window var infowindow = new google.maps.InfoWindow({ content : $marker.html() }); // show info window when marker is clicked google.maps.event.addListener(marker, 'click', function() { infowindow.open( map, marker ); }); }; // update latitude and longitude values in the form when marker is moved google.maps.event.addListener(marker, "dragend", function() { var point = marker.getPosition(); map.panTo(point); acadp_update_latlng(point); }); }; /** * Center the map, showing all markers attached to this map. * * @since 1.0.0 */ function acadp_center_map( map ) { // vars var bounds = new google.maps.LatLngBounds(); // create bounds if( map.marker != null ) { var latlng = new google.maps.LatLng( map.marker.position.lat(), map.marker.position.lng() ); bounds.extend( latlng ); } map.setCenter( bounds.getCenter() ); map.setZoom( parseInt( acadp.zoom_level ) ); }; /** * Set the latitude and longitude values from the address. * * @since 1.0.0 */ function acadp_get_latlng_from_address( address, map ) { var geoCoder = new google.maps.Geocoder(); geoCoder.geocode({'address': address}, function(results, status) { if( status == google.maps.GeocoderStatus.OK ) { var point = results[0].geometry.location; map.marker.setPosition( point ); acadp_center_map( map ); acadp_update_latlng( point ); }; }); }; /** * Update the latitude and logitude values in the custom post type "acadp_listings". * * @since 1.0.0 */ function acadp_update_latlng( point ) { $( '#acadp-latitude' ).val( point.lat() ); $( '#acadp-longitude' ).val( point.lng() ); }; /** * Make images inside the listing form sortable. * * @since 1.0.0 */ function acadp_sort_images() { var $sortable_element = $('#acadp-images tbody'); if( $sortable_element.hasClass('ui-sortable') ) { $sortable_element.sortable('destroy'); }; $sortable_element.sortable({ handle: '.acadp-handle' }); }; /** * Display the media uploader for selecting an image. * * @since 1.0.0 */ function acadp_render_media_uploader( page ) { var file_frame, image_data, json; // If an instance of file_frame already exists, then we can open it rather than creating a new instance if( undefined !== file_frame ) { file_frame.open(); return; }; // Here, use the wp.media library to define the settings of the media uploader file_frame = wp.media.frames.file_frame = wp.media({ frame : 'post', state : 'insert', multiple : false }); // Setup an event handler for what to do when an image has been selected file_frame.on( 'insert', function() { // Read the JSON data returned from the media uploader json = file_frame.state().get( 'selection' ).first().toJSON(); // First, make sure that we have the URL of an image to display if( 0 > $.trim( json.url.length ) ) { return; }; // After that, set the properties of the image and display it if( 'listings' == page ) { var html = '