Skip to content
Permalink
Newer
Older
100644 167 lines (142 sloc) 6.05 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
} ;
13
14
var plugin = this ;
15
16
plugin . settings = { }
17
18
var $element = $ ( element ) ,
19
element = element ;
20
21
plugin . init = function ( ) {
22
plugin . settings = $ . extend ( { } , defaults , options ) ;
23
$element . html ( "
"
) ; // create a plug for google to load data into
24
initialize_place ( function ( place ) {
25
plugin . place_data = place ;
26
// render specified sections
27
if ( plugin . settings . render . indexOf ( 'reviews' ) > - 1 ) {
28
renderReviews ( plugin . place_data . reviews ) ;
29
if ( ! ! plugin . settings . rotateTime ) {
30
initRotation ( ) ;
31
}
33
// render schema markup
34
if ( plugin . settings . render . indexOf ( 'schema' ) > - 1 ) {
35
getSchemaMarkup ( plugin . place_data ) ;
36
}
37
} ) ;
38
}
39
40
var initialize_place = function ( c ) {
41
var map = new google . maps . Map ( document . getElementById ( 'map-plug' ) ) ;
42
43
var request = {
44
placeId : plugin . settings . placeId
45
} ;
46
47
var service = new google . maps . places . PlacesService ( map ) ;
48
49
service . getDetails ( request , function ( place , status ) {
50
if ( status == google . maps . places . PlacesServiceStatus . OK ) {
51
c ( place ) ;
52
}
53
} ) ;
54
}
55
56
var sort_by_date = function ( ray ) {
57
ray . sort ( function ( a , b ) {
58
var keyA = new Date ( a . time ) ,
59
keyB = new Date ( b . time ) ;
60
// Compare the 2 dates
61
if ( keyA < keyB ) return - 1 ;
62
if ( keyA > keyB ) return 1 ;
63
return ;
64
} ) ;
65
return ray ;
66
}
67
68
var filter_minimum_rating = function ( reviews ) {
69
for ( var i = reviews . length - 1 ; i >= ; i -- ) {
70
if ( reviews [ i ] . rating < plugin . settings . min_rating ) {
71
reviews . splice ( i , 1 ) ;
72
}
73
}
74
return reviews ;
75
}
76
77
var renderReviews = function ( reviews ) {
78
reviews = sort_by_date ( reviews ) ;
79
reviews = filter_minimum_rating ( reviews ) ;
80
var html = "" ;
81
var row_count = ( plugin . settings . max_rows > ) ? plugin . settings . max_rows - 1 : reviews . length - 1 ;
82
// make sure the row_count is not greater than available records
83
row_count = ( row_count > reviews . length ) ? reviews . length - 1 : row_count ;
84
for ( var i = row_count ; i >= ; i -- ) {
85
var stars = renderStars ( reviews [ i ] . rating ) ;
86
var date = convertTime ( reviews [ i ] . time ) ;
87
html = html + "
" + reviews [ i ] . author_name + " , " + date + "
" + stars + "

" + reviews [ i ] . text + "

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