Skip to content
Permalink
Newer
Older
100644 189 lines (164 sloc) 7.06 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 : '#schema'
14
, type : 'Store'
15
, beforeText : 'Google Users Have Rated'
16
, middleText : 'based on'
17
, afterText : 'ratings and reviews'
18
}
20
21
var plugin = this ;
22
23
plugin . settings = { }
24
25
var $element = $ ( element ) ,
26
element = element ;
27
28
plugin . init = function ( ) {
29
plugin . settings = $ . extend ( { } , defaults , options ) ;
30
plugin . settings . schema = $ . extend ( { } , defaults . schema , options . schema ) ;
31
$element . html ( "
"
) ; // create a plug for google to load data into
32
initialize_place ( function ( place ) {
33
plugin . place_data = place ;
34
// render specified sections
35
if ( plugin . settings . render . indexOf ( 'reviews' ) > - 1 ) {
36
renderReviews ( plugin . place_data . reviews ) ;
37
if ( ! ! plugin . settings . rotateTime ) {
38
initRotation ( ) ;
39
}
41
// render schema markup
42
if ( plugin . settings . schema . displayElement instanceof jQuery ) {
43
addSchemaMarkup ( plugin . place_data ) ;
44
} else if ( typeof plugin . settings . schema . displayElement == 'string' ) {
45
try {
46
var ele = $ ( plugin . settings . schema . displayElement ) ;
47
if ( ele . length ) {
48
plugin . settings . schema . displayElement = ele ;
49
addSchemaMarkup ( plugin . place_data ) ;
50
} else {
51
throw 'Element [' + plugin . settings . schema . displayElement + '] couldnt be found in the DOM. Skipping schema markup generation.' ;
52
}
53
} catch ( e ) {
54
console . warn ( e ) ;
55
}
57
} ) ;
58
}
59
60
var initialize_place = function ( c ) {
61
var map = new google . maps . Map ( document . getElementById ( 'map-plug' ) ) ;
62
63
var request = {
64
placeId : plugin . settings . placeId
65
} ;
66
67
var service = new google . maps . places . PlacesService ( map ) ;
68
69
service . getDetails ( request , function ( place , status ) {
70
if ( status == google . maps . places . PlacesServiceStatus . OK ) {
71
c ( place ) ;
72
}
73
} ) ;
74
}
75
76
var sort_by_date = function ( ray ) {
77
ray . sort ( function ( a , b ) {
78
var keyA = new Date ( a . time ) ,
79
keyB = new Date ( b . time ) ;
80
// Compare the 2 dates
81
if ( keyA < keyB ) return - 1 ;
82
if ( keyA > keyB ) return 1 ;
83
return ;
84
} ) ;
85
return ray ;
86
}
87
88
var filter_minimum_rating = function ( reviews ) {
89
for ( var i = reviews . length - 1 ; i >= ; i -- ) {
90
if ( reviews [ i ] . rating < plugin . settings . min_rating ) {
91
reviews . splice ( i , 1 ) ;
92
}
93
}
94
return reviews ;
95
}
96
97
var renderReviews = function ( reviews ) {
98
reviews = sort_by_date ( reviews ) ;
99
reviews = filter_minimum_rating ( reviews ) ;
100
var html = "" ;
101
var row_count = ( plugin . settings . max_rows > ) ? plugin . settings . max_rows - 1 : reviews . length - 1 ;
102
// make sure the row_count is not greater than available records
103
row_count = ( row_count > reviews . length - 1 ) ? reviews . length - 1 : row_count ;
104
for ( var i = row_count ; i >= ; i -- ) {
105
var stars = renderStars ( reviews [ i ] . rating ) ;
106
var date = convertTime ( reviews [ i ] . time ) ;
107
html = html + "
" + reviews [ i ] . author_name + " , " + date + "
" + stars + "

" + reviews [ i ] . text + "

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