(function( $ ) {
'use strict';
/**
* Render a Google Map onto the selected jQuery element.
*
* @since 1.0.0
*/
function acadp_render_map( $el ) {
$el.addClass( 'acadp-map-loaded' );
// var
var $markers = $el.find('.marker');
// vars
var args = {
zoom : parseInt( acadp.zoom_level ),
center : new google.maps.LatLng( 0, 0 ),
mapTypeId : google.maps.MapTypeId.ROADMAP,
zoomControl : true,
scrollwheel : false
};
// create map
var map = new google.maps.Map( $el[0], args );
// add a markers reference
map.markers = [];
// set map type
map.type = $el.data('type');
// add markers
$markers.each(function() {
acadp_add_marker( $( this ), map );
});
// center map
acadp_center_map( map );
// update map when contact details fields are updated in the custom post type 'acadp_listings'
if( map.type == 'form' ) {
var geoCoder = new google.maps.Geocoder();
$( '.acadp-map-field', '#acadp-contact-details' ).on('blur', function() {
var address = [];
address.push( $( '#acadp-address' ).val() );
var location = '';
$( 'select', '#acadp-contact-details' ).each(function() {
var _default = $( this ).find( 'option:first' ).text();
var _selected = $( this ).find( 'option:selected' ).text();
if( _selected != _default ) location = _selected;
});
if( '' == location ) location = $( '#acadp-default-location' ).val();
address.push( location );
address.push( $( '#acadp-zipcode' ).val() );
address = address.filter( function( v ) { return v !== '' } );
address = address.join();
geoCoder.geocode({'address': address}, function(results, status) {
if( status == google.maps.GeocoderStatus.OK ) {
var point = results[0].geometry.location;
map.markers[0].setPosition( point );
acadp_center_map( map );
acadp_update_latlng( point );
};
});
});
$( '#acadp-address' ).trigger( 'blur' );
} else if( map.type == 'markerclusterer' ) {
var markerCluster = new MarkerClusterer( map, map.markers, { imagePath: acadp.plugin_url+'public/images/m' } );
};
};
/**
* Add a marker to the selected Google Map.
*
* @since 1.0.0
*/
function acadp_add_marker( $marker, map ) {
// var
var latlng = new google.maps.LatLng( $marker.data( 'latitude' ), $marker.data( 'longitude' ) );
// check to see if any of the existing markers match the latlng of the new marker
if( map.markers.length ) {
for( var i = 0; i < map.markers.length; i++ ) {
var existing_marker = map.markers[i];
var pos = existing_marker.getPosition();
// if a marker already exists in the same position as this marker
if( latlng.equals( pos ) ) {
// update the position of the coincident marker by applying a small multipler to its coordinates
var latitude = latlng.lat() + ( Math.random() - .5 ) / 1500; // * (Math.random() * (max - min) + min);
var longitude = latlng.lng() + ( Math.random() - .5 ) / 1500; // * (Math.random() * (max - min) + min);
latlng = new google.maps.LatLng( latitude, longitude );
}
}
}
// create marker
var marker = new google.maps.Marker({
position : latlng,
map : map,
draggable : ( map.type == 'form' ) ? true : false
});
// add to array
map.markers.push( 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
if( map.type == 'form' ) {
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();
// loop through all markers and create bounds
$.each( map.markers, function( i, marker ){
var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
bounds.extend( latlng );
});
// only 1 marker?
if( map.markers.length == 1 ) {
// set center of map
map.setCenter( bounds.getCenter() );
map.setZoom( parseInt( acadp.zoom_level ) );
} else {
// fit to bounds
map.fitBounds( bounds );
};
};
/**
* Set the latitude and longitude values from the address.
*
* @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() {
if( $.fn.sortable ) {
var $sortable_element = $('#acadp-images tbody');
if( $sortable_element.hasClass('ui-sortable') ) {
$sortable_element.sortable('destroy');
};
$sortable_element.sortable({
handle: '.acadp-handle'
});
};
}
/**
* Check if the user have permission to upload image.
*
* @since 1.0.0
*
* @return bool True if can upload image, false if not.
*/
function acadp_can_upload_image() {
var limit = acadp_images_limit();
var uploaded = acadp_images_uploaded_count();
if( ( limit > 0 && uploaded >= limit ) || $( '#acadp-progress-image-upload' ).hasClass( 'uploading' ) ) {
return false;
}
return true;
}
/**
* Get the maximum number of images the user can upload in the current listing.
*
* @since 1.5.8
*
* @return int Number of images.
*/
function acadp_images_limit() {
var limit = $( '#acadp-upload-image' ).attr( 'data-limit' );
if( typeof limit !== typeof undefined && limit !== false ) {
limit = parseInt( limit );
} else {
limit = parseInt( acadp.maximum_images_per_listing );
}
return limit;
}
/**
* Get the number of images user had uploaded for the current listing.
*
* @since 1.5.8
*
* @return int Number of images.
*/
function acadp_images_uploaded_count() {
return $( '.acadp-image-field' ).length;
}
/**
* Enable or disable image upload
*
* @since 1.0.0
*/
function acadp_enable_disable_image_upload() {
if( acadp_can_upload_image() ) {
$( '#acadp-upload-image' ).removeAttr( 'disabled' );
} else {
$( '#acadp-upload-image' ).attr( 'disabled', 'disabled' );
};
}
/**
* Called when the page has loaded.
*
* @since 1.0.0
*/
$(function() {
// load custom fields of the selected category in the search form
$( 'body' ).on( 'change', '.acadp-category-search', function() {
var $search_elem = $( this ).closest ( 'form' ).find( ".acadp-custom-fields-search" );
if( $search_elem.length ) {
$search_elem.html( '
' );
var data = {
'action': 'acadp_custom_fields_search',
'term_id': $( this ).val(),
'style': $search_elem.data( 'style' ),
'security': acadp.ajax_nonce
};
$.post( acadp.ajax_url, data, function(response) {
$search_elem.html( response );
});
};
});
// add "required" attribute to the category field in the listing form [fallback for versions prior to 1.5.5]
$( '#acadp_category' ).attr( 'required', 'required' );
// load custom fields of the selected category in the custom post type "acadp_listings"
$( 'body' ).on( 'change', '.acadp-category-listing', function() {
$( '.acadp-listing-form-submit-btn' ).prop( 'disabled', true );
$( '#acadp-custom-fields-listings' ).html( '' );
var data = {
'action': 'acadp_public_custom_fields_listings',
'post_id': $( '#acadp-custom-fields-listings' ).data( 'post_id' ),
'terms': $( this ).val(),
'security': acadp.ajax_nonce
};
$.post( acadp.ajax_url, data, function( response ) {
$( '#acadp-custom-fields-listings' ).html( response );
$( '.acadp-listing-form-submit-btn' ).prop( 'disabled', false );
});
});
// slick slider
if( $.fn.slick ) {
$( '.acadp-slider-for' ).on( 'init', function( slick ) {
$( this ).fadeIn( 1000 );
}).slick({
rtl : ( parseInt( acadp.is_rtl ) ? true : false ),
asNavFor : '.acadp-slider-nav',
arrows : false,
fade : true,
slidesToShow : 1,
slidesToScroll : 1,
adaptiveHeight : true
});
$( '.acadp-slider-nav' ).on( 'init', function( slick ) {
$( this ).fadeIn( 1000 );
}).slick({
rtl : ( parseInt( acadp.is_rtl ) ? true : false ),
asNavFor : '.acadp-slider-for',
nextArrow : '
',
prevArrow : '
',
focusOnSelect : true,
slidesToShow : 5,
slidesToScroll : 1,
infinite : false,
responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 1,
}
},
{
breakpoint: 600,
settings: {
slidesToShow: 2,
slidesToScroll: 1
}
}
]
});
};
// render map in the custom post type "acadp_listings"
$( '.acadp-map' ).each(function() {
acadp_render_map( $( this ) );
});
// display the media uploader when "Upload Image" button clicked in the custom post type "acadp_listings"
$( '#acadp-upload-image' ).on( 'click', function( e ) {
e.preventDefault();
if( acadp_can_upload_image() ) {
$( '#acadp-upload-image-hidden' ).trigger('click');
};
});
// upload image
$( "#acadp-upload-image-hidden" ).change( function() {
var selected = $( this )[0].files.length;
if( ! selected ) return false;
var limit = acadp_images_limit();
var uploaded = acadp_images_uploaded_count();
var remaining = limit - uploaded;
if( limit > 0 && selected > remaining ) {
alert( acadp.upload_limit_alert_message.replace( /%d/gi, remaining ) );
return false;
};
$( '#acadp-progress-image-upload' ).addClass( 'uploading' ).html( '' );
acadp_enable_disable_image_upload();
var options = {
dataType : 'json',
url : acadp.ajax_url,
success : function( json, statusText, xhr, $form ) {
// do extra stuff after submit
$( '#acadp-progress-image-upload' ).removeClass( 'uploading' ).html( '' );
$.each( json, function( key, value ) {
if( ! value['error'] ) {
var html = '