Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
171 lines (147 sloc)
6.26 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* https://github.com/peledies/google-places */ | |
( function ( $ ) { | |
$ . googlePlaces = function ( element , options ) { | |
var defaults = { | |
placeId : 'ChIJN1t_tDeuEmsRUsoyG83frY4' // placeId provided by google api documentation | |
, render : [ 'reviews' ] | |
, min_rating : | |
, max_rows : | |
, rotateTime : false | |
} ; | |
var plugin = this ; | |
plugin . settings = { } | |
var $element = $ ( element ) , | |
element = element ; | |
plugin . init = function ( ) { | |
plugin . settings = $ . extend ( { } , defaults , options ) ; | |
$element . html ( " " ) ; // create a plug for google to load data into | |
initialize_place ( function ( place ) { | |
plugin . place_data = place ; | |
// render specified sections | |
if ( plugin . settings . render . indexOf ( 'reviews' ) > - 1 ) { | |
renderReviews ( plugin . place_data . reviews ) ; | |
if ( ! ! plugin . settings . rotateTime ) { | |
initRotation ( ) ; | |
} | |
} | |
// render schema markup | |
if ( plugin . settings . render . indexOf ( 'schema' ) > - 1 ) { | |
getSchemaMarkup ( plugin . place_data ) ; | |
} | |
} ) ; | |
} | |
var initialize_place = function ( c ) { | |
var map = new google . maps . Map ( document . getElementById ( 'map-plug' ) ) ; | |
var request = { | |
placeId : plugin . settings . placeId | |
} ; | |
var service = new google . maps . places . PlacesService ( map ) ; | |
service . getDetails ( request , function ( place , status ) { | |
if ( status == google . maps . places . PlacesServiceStatus . OK ) { | |
c ( place ) ; | |
} | |
} ) ; | |
} | |
var sort_by_date = function ( ray ) { | |
ray . sort ( function ( a , b ) { | |
var keyA = new Date ( a . time ) , | |
keyB = new Date ( b . time ) ; | |
// Compare the 2 dates | |
if ( keyA < keyB ) return - 1 ; | |
if ( keyA > keyB ) return 1 ; | |
return ; | |
} ) ; | |
return ray ; | |
} | |
var filter_minimum_rating = function ( reviews ) { | |
for ( var i = reviews . length - 1 ; i >= ; i -- ) { | |
if ( reviews [ i ] . rating < plugin . settings . min_rating ) { | |
reviews . splice ( i , 1 ) ; | |
} | |
} | |
return reviews ; | |
} | |
var renderReviews = function ( reviews ) { | |
reviews = sort_by_date ( reviews ) ; | |
reviews = filter_minimum_rating ( reviews ) ; | |
var html = "" ; | |
var row_count = ( plugin . settings . max_rows > ) ? plugin . settings . max_rows - 1 : reviews . length - 1 ; | |
var reviewPointTotal = ; | |
// make sure the row_count is not greater than available records | |
row_count = ( row_count > reviews . length ) ? reviews . length - 1 : row_count ; | |
for ( var i = row_count ; i >= ; i -- ) { | |
var stars = renderStars ( reviews [ i ] . rating ) ; | |
reviewPointTotal += reviews [ i ] . rating ; | |
var date = convertTime ( reviews [ i ] . time ) ; | |
html
=
html
+
"
+
stars
+
"
"
"
" + reviews [ i ] . text + " |
|
} ; | |
// Set totals and averages - may be used later. | |
numReviews = row_count ; | |
averageReview = reviewPointTotal / numReviews ; | |
$element . append ( html ) ; | |
} | |
var initRotation = function ( ) { | |
var $reviewEls = $element . children ( '.review-item' ) ; | |
var currentIdx = $reviewEls . length > ? : false ; | |
$reviewEls . hide ( ) ; | |
if ( currentIdx !== false ) { | |
$ ( $reviewEls [ currentIdx ] ) . show ( ) ; | |
setInterval ( function ( ) { | |
if ( ++ currentIdx >= $reviewEls . length ) { | |
currentIdx = ; | |
} | |
$reviewEls . hide ( ) ; | |
$ ( $reviewEls [ currentIdx ] ) . fadeIn ( 'slow' ) ; | |
} , plugin . settings . rotateTime ) ; | |
} | |
} | |
var renderStars = function ( rating ) { | |
var
stars
=
"
|
|
// fill in gold stars | |
for ( var i = ; i < rating ; i ++ ) { | |
stars
=
stars
+
"
|
|
} ; | |
// fill in empty stars | |
if ( rating < 5 ) { | |
for ( var i = ; i < ( 5 - rating ) ; i ++ ) { | |
stars
=
stars
+
"
|
|
} ; | |
} | |
stars = stars + " " ; | |
return stars ; | |
} | |
var convertTime = function ( UNIX_timestamp ) { | |
var a = new Date ( UNIX_timestamp * 1000 ) ; | |
var months = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ] ; | |
var time = months [ a . getMonth ( ) ] + ' ' + a . getDate ( ) + ', ' + a . getFullYear ( ) ; | |
return time ; | |
} | |
var getSchemaMarkup = function ( placeData ) { | |
var reviews = placeData . reviews ; | |
var row_count = reviews . length - 1 ; | |
var reviewPointTotal = ; | |
for ( var i = row_count ; i >= ; i -- ) { | |
reviewPointTotal += reviews [ i ] . rating ; | |
} ; | |
// Set totals and averages. | |
var averageReview = reviewPointTotal / ( reviews . length ) ; | |
$element . append ( ' ' | |
+ ' ' | |
+ 'Google users have rated ' + placeData . name + ' ' | |
+ ' ' | |
+ ' ' + averageReview + ' / 5 ' | |
+ ' based on ' + reviews . length + ' ratings and reviews' | |
+ ' ' | |
+ ' ' ) ; | |
} | |
plugin . init ( ) ; | |
} | |
$ . fn . googlePlaces = function ( options ) { | |
return this . each ( function ( ) { | |
if ( undefined == $ ( this ) . data ( 'googlePlaces' ) ) { | |
var plugin = new $ . googlePlaces ( this , options ) ; | |
$ ( this ) . data ( 'googlePlaces' , plugin ) ; | |
} | |
} ) ; | |
} | |
} ) ( jQuery ) ; |