Skip to content
Permalink
Newer
Older
100644 174 lines (149 sloc) 6.37 KB
1
/* https://github.com/peledies/google-places */
2
( function ( $ ) {
3
4
$ . googlePlaces = function ( element , options ) {
5
6
var defaults = {
7
placeId : 'ChIJN1t_tDeuEmsRUsoyG83frY4' // placeId provided by google api documentation
8
, render : [ 'reviews' ]
9
, min_rating :
10
, max_rows :
11
, rotateTime : false
12
, schema : {
13
displayElement : { } ,
14
beforeText : 'Google Users Have Rated' ,
Apr 2, 2016
15
middleText : 'based on' ,
16
afterText : 'ratings and reviews'
17
}
19
20
var plugin = this ;
21
22
plugin . settings = { }
23
24
var $element = $ ( element ) ,
25
element = element ;
26
27
plugin . init = function ( ) {
28
plugin . settings = $ . extend ( { } , defaults , options ) ;
29
$element . html ( "
"
) ; // create a plug for google to load data into
30
initialize_place ( function ( place ) {
31
plugin . place_data = place ;
32
// render specified sections
33
if ( plugin . settings . render . indexOf ( 'reviews' ) > - 1 ) {
34
renderReviews ( plugin . place_data . reviews ) ;
35
if ( ! ! plugin . settings . rotateTime ) {
36
initRotation ( ) ;
37
}
39
// render schema markup
40
if ( plugin . settings . schema . displayElement instanceof jQuery ) {
41
addSchemaMarkup ( plugin . place_data ) ;
43
} ) ;
44
}
45
46
var initialize_place = function ( c ) {
47
var map = new google . maps . Map ( document . getElementById ( 'map-plug' ) ) ;
48
49
var request = {
50
placeId : plugin . settings . placeId
51
} ;
52
53
var service = new google . maps . places . PlacesService ( map ) ;
54
55
service . getDetails ( request , function ( place , status ) {
56
if ( status == google . maps . places . PlacesServiceStatus . OK ) {
57
c ( place ) ;
58
}
59
} ) ;
60
}
61
62
var sort_by_date = function ( ray ) {
63
ray . sort ( function ( a , b ) {
64
var keyA = new Date ( a . time ) ,
65
keyB = new Date ( b . time ) ;
66
// Compare the 2 dates
67
if ( keyA < keyB ) return - 1 ;
68
if ( keyA > keyB ) return 1 ;
69
return ;
70
} ) ;
71
return ray ;
72
}
73
74
var filter_minimum_rating = function ( reviews ) {
75
for ( var i = reviews . length - 1 ; i >= ; i -- ) {
76
if ( reviews [ i ] . rating < plugin . settings . min_rating ) {
77
reviews . splice ( i , 1 ) ;
78
}
79
}
80
return reviews ;
81
}
82
83
var renderReviews = function ( reviews ) {
84
reviews = sort_by_date ( reviews ) ;
85
reviews = filter_minimum_rating ( reviews ) ;
86
var html = "" ;
87
var row_count = ( plugin . settings . max_rows > ) ? plugin . settings . max_rows - 1 : reviews . length - 1 ;
88
// make sure the row_count is not greater than available records
89
row_count = ( row_count > reviews . length ) ? reviews . length - 1 : row_count ;
90
for ( var i = row_count ; i >= ; i -- ) {
91
var stars = renderStars ( reviews [ i ] . rating ) ;
92
var date = convertTime ( reviews [ i ] . time ) ;
93
html = html + "
" + reviews [ i ] . author_name + " , " + date + "
" + stars + "

" + reviews [ i ] . text + "

"
94
} ;
95
$element . append ( html ) ;
96
}
97
98
var initRotation = function ( ) {
99
var $reviewEls = $element . children ( '.review-item' ) ;
100
var currentIdx = $reviewEls . length > ? : false ;
101
$reviewEls . hide ( ) ;
102
if ( currentIdx !== false ) {
103
$ ( $reviewEls [ currentIdx ] ) . show ( ) ;
104
setInterval ( function ( ) {
105
if ( ++ currentIdx >= $reviewEls . length ) {
106
currentIdx = ;
107
}
108
$reviewEls . hide ( ) ;
109
$ ( $reviewEls [ currentIdx ] ) . fadeIn ( 'slow' ) ;
110
} , plugin . settings . rotateTime ) ;
111
}
112
}
113
114
var renderStars = function ( rating ) {
115
var stars = "
    " ;
116
117
// fill in gold stars
118
for ( var i = ; i < rating ; i ++ ) {
119
stars = stars + "
  • "
    ;
    120
    } ;
    121
    122
    // fill in empty stars
    123
    if ( rating < 5 ) {
    124
    for ( var i = ; i < ( 5 - rating ) ; i ++ ) {
    125
    stars = stars + "
  • "
    ;
    126
    } ;
    127
    }
    128
    stars = stars + "
    " ;
    129
    return stars ;
    130
    }
    131
    132
    var convertTime = function ( UNIX_timestamp ) {
    133
    var a = new Date ( UNIX_timestamp * 1000 ) ;
    134
    var months = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ] ;
    135
    var time = months [ a . getMonth ( ) ] + ' ' + a . getDate ( ) + ', ' + a . getFullYear ( ) ;
    136
    return time ;
    137
    }
    139
    var addSchemaMarkup = function ( placeData ) {
    140
    var reviews = placeData . reviews ;
    141
    var lastIndex = reviews . length - 1 ;
    142
    var reviewPointTotal = ;
    143
    for ( var i = lastIndex ; i >= ; i -- ) {
    144
    reviewPointTotal += reviews [ i ] . rating ;
    145
    } ;
    146
    // Set totals and averages - may be used later.
    147
    var averageReview = reviewPointTotal / ( reviews . length ) ;
    148
    plugin . settings . schema . displayElement . append ( ' '
    150
    + plugin . settings . schema . beforeText + ' ' + placeData . name + ' '
    152
    + ' ' + averageReview + ' / 5 '
    153
    + plugin . settings . schema . middleText + ' ' + reviews . length + ' '
    154
    + plugin . settings . schema . afterText
    155
    + ' '
    156
    + ' ' ) ;
    157
    }
    158
    159
    plugin . init ( ) ;
    160
    161
    }
    162
    163
    $ . fn . googlePlaces = function ( options ) {
    164
    165
    return this . each ( function ( ) {
    166
    if ( undefined == $ ( this ) . data ( 'googlePlaces' ) ) {
    167
    var plugin = new $ . googlePlaces ( this , options ) ;
    168
    $ ( this ) . data ( 'googlePlaces' , plugin ) ;
    169
    }
    170
    } ) ;
    171
    172
    }
    173
    174
    } ) ( jQuery ) ;
    英雄联盟竞猜现场投注网 星火电竞公告比赛 csgo完美世界 电竞竞猜 蜂鸟电竞积分联赛 亿电竞赛事外围