Skip to content
Permalink
Newer
Older
100644 283 lines (248 sloc) 9.95 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
, map_plug_id : 'map-plug'
12
, rotateTime : false
13
, schema : {
14
displayElement : '#schema'
15
, type : 'Store'
16
, beforeText : 'Google Users Have Rated'
17
, middleText : 'based on'
18
, afterText : 'ratings and reviews'
19
}
20
, address : {
21
displayElement : "#google-address"
22
}
23
, phone : {
24
displayElement : "#google-phone"
25
}
26
, staticMap : {
27
displayElement : "#google-static-map"
28
, width : 512
29
, height : 512
30
, zoom : 17
31
, type : "roadmap"
32
}
33
, hours : {
34
displayElement : "#google-hours"
35
}
37
38
var plugin = this ;
39
40
plugin . settings = { }
41
42
var $element = $ ( element ) ,
43
element = element ;
44
45
plugin . init = function ( ) {
46
plugin . settings = $ . extend ( { } , defaults , options ) ;
47
plugin . settings . schema = $ . extend ( { } , defaults . schema , options . schema ) ;
48
$element . html ( "
+ plugin . settings . map_plug_id + "'>
"
) ; // create a plug for google to load data into
49
initialize_place ( function ( place ) {
50
plugin . place_data = place ;
51
52
// Trigger event before render
53
$element . trigger ( 'beforeRender.googlePlaces' ) ;
54
55
// render specified sections
56
if ( plugin . settings . render . indexOf ( 'reviews' ) > - 1 ) {
57
renderReviews ( plugin . place_data . reviews ) ;
58
if ( ! ! plugin . settings . rotateTime ) {
59
initRotation ( ) ;
60
}
62
if ( plugin . settings . render . indexOf ( 'address' ) > - 1 ) {
63
renderAddress (
64
capture_element ( plugin . settings . address . displayElement )
65
, plugin . place_data . adr_address
66
) ;
67
}
68
if ( plugin . settings . render . indexOf ( 'phone' ) > - 1 ) {
69
renderPhone (
70
capture_element ( plugin . settings . phone . displayElement )
71
, plugin . place_data . formatted_phone_number
72
) ;
73
}
74
if ( plugin . settings . render . indexOf ( 'staticMap' ) > - 1 ) {
75
renderStaticMap (
76
capture_element ( plugin . settings . staticMap . displayElement )
77
, plugin . place_data . formatted_address
78
) ;
79
}
80
if ( plugin . settings . render . indexOf ( 'hours' ) > - 1 ) {
81
renderHours (
82
capture_element ( plugin . settings . hours . displayElement )
83
, plugin . place_data . opening_hours
84
) ;
85
}
86
87
// render schema markup
88
addSchemaMarkup (
89
capture_element ( plugin . settings . schema . displayElement )
90
, plugin . place_data
91
) ;
92
93
// Trigger event after render
94
$element . trigger ( 'afterRender.googlePlaces' ) ;
96
} ) ;
97
}
98
99
var capture_element = function ( element ) {
100
if ( element instanceof jQuery ) {
101
return element ;
102
} else if ( typeof element == 'string' ) {
103
try {
104
var ele = $ ( element ) ;
105
if ( ele . length ) {
106
return ele ;
107
} else {
108
throw 'Element [' + element + '] couldnt be found in the DOM. Skipping ' + element + ' markup generation.' ;
109
}
110
} catch ( e ) {
111
console . warn ( e ) ;
112
}
113
}
114
}
115
116
var initialize_place = function ( c ) {
117
var map = new google . maps . Map ( document . getElementById ( plugin . settings . map_plug_id ) ) ;
118
119
var request = {
120
placeId : plugin . settings . placeId
121
} ;
122
123
var service = new google . maps . places . PlacesService ( map ) ;
124
125
service . getDetails ( request , function ( place , status ) {
126
if ( status == google . maps . places . PlacesServiceStatus . OK ) {
127
c ( place ) ;
128
}
129
} ) ;
130
}
131
132
var sort_by_date = function ( ray ) {
133
ray . sort ( function ( a , b ) {
134
var keyA = new Date ( a . time ) ,
135
keyB = new Date ( b . time ) ;
136
// Compare the 2 dates
137
if ( keyA < keyB ) return - 1 ;
138
if ( keyA > keyB ) return 1 ;
139
return ;
140
} ) ;
141
return ray ;
142
}
143
144
var filter_minimum_rating = function ( reviews ) {
145
for ( var i = reviews . length - 1 ; i >= ; i -- ) {
146
if ( reviews [ i ] . rating < plugin . settings . min_rating ) {
147
reviews . splice ( i , 1 ) ;
148
}
149
}
150
return reviews ;
151
}
152
153
var renderReviews = function ( reviews ) {
154
reviews = sort_by_date ( reviews ) ;
155
reviews = filter_minimum_rating ( reviews ) ;
156
var html = "" ;
157
var row_count = ( plugin . settings . max_rows > ) ? plugin . settings . max_rows - 1 : reviews . length - 1 ;
158
// make sure the row_count is not greater than available records
159
row_count = ( row_count > reviews . length - 1 ) ? reviews . length - 1 : row_count ;
160
for ( var i = row_count ; i >= ; i -- ) {
161
var stars = renderStars ( reviews [ i ] . rating ) ;
162
var date = convertTime ( reviews [ i ] . time ) ;
163
html = html + "
" + reviews [ i ] . author_name + " , " + date + "
" + stars + "

" + reviews [ i ] . text + "

"
164
} ;
165
$element . append ( html ) ;
166
}
168
var renderHours = function ( element , data ) {
169
if ( element instanceof jQuery ) {
170
var html = "
    " ;
171
data . weekday_text . forEach ( function ( day ) {
172
html += "
  • " + day + "
  • "
    ;
    173
    } ) ;
    174
    html += " " ;
    175
    element . append ( html ) ;
    176
    }
    177
    }
    178
    179
    var renderStaticMap = function ( element , data ) {
    180
    if ( element instanceof jQuery ) {
    181
    var map = plugin . settings . staticMap ;
    182
    element . append (
    183
    " +
    184
    "?size=" + map . width + "x" + map . height +
    185
    "&zoom=" + map . zoom +
    186
    "&maptype=" + map . type +
    187
    "&markers=size:large%7Ccolor:red%7C" + data + "'>" +
    188
    " " ) ;
    189
    }
    190
    }
    191
    192
    var renderAddress = function ( element , data ) {
    193
    if ( element instanceof jQuery ) {
    194
    element . append ( data ) ;
    195
    }
    196
    }
    197
    198
    var renderPhone = function ( element , data ) {
    199
    if ( element instanceof jQuery ) {
    200
    element . append ( data ) ;
    201
    }
    202
    }
    203
    204
    var initRotation = function ( ) {
    205
    var $reviewEls = $element . children ( '.review-item' ) ;
    206
    var currentIdx = $reviewEls . length > ? : false ;
    207
    $reviewEls . hide ( ) ;
    208
    if ( currentIdx !== false ) {
    209
    $ ( $reviewEls [ currentIdx ] ) . show ( ) ;
    210
    setInterval ( function ( ) {
    211
    if ( ++ currentIdx >= $reviewEls . length ) {
    212
    currentIdx = ;
    213
    }
    214
    $reviewEls . hide ( ) ;
    215
    $ ( $reviewEls [ currentIdx ] ) . fadeIn ( 'slow' ) ;
    216
    } , plugin . settings . rotateTime ) ;
    217
    }
    218
    }
    219
    220
    var renderStars = function ( rating ) {
    221
    var stars = "
      " ;
    222
    223
    // fill in gold stars
    224
    for ( var i = ; i < rating ; i ++ ) {
    225
    stars = stars + "
  • "
    ;
    226
    } ;
    227
    228
    // fill in empty stars
    229
    if ( rating < 5 ) {
    230
    for ( var i = ; i < ( 5 - rating ) ; i ++ ) {
    231
    stars = stars + "
  • "
    ;
    232
    } ;
    233
    }
    234
    stars = stars + "
    " ;
    235
    return stars ;
    236
    }
    237
    238
    var convertTime = function ( UNIX_timestamp ) {
    239
    var a = new Date ( UNIX_timestamp * 1000 ) ;
    240
    var months = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ] ;
    241
    var time = months [ a . getMonth ( ) ] + ' ' + a . getDate ( ) + ', ' + a . getFullYear ( ) ;
    242
    return time ;
    243
    }
    245
    var addSchemaMarkup = function ( element , placeData ) {
    246
    var reviews = placeData . reviews ;
    247
    var lastIndex = reviews . length - 1 ;
    248
    var reviewPointTotal = ;
    249
    var schema = plugin . settings . schema ;
    250
    for ( var i = lastIndex ; i >= ; i -- ) {
    251
    reviewPointTotal += reviews [ i ] . rating ;
    252
    } ;
    253
    // Set totals and averages - may be used later.
    254
    var averageReview = reviewPointTotal / ( reviews . length ) ;
    255
    if ( element instanceof jQuery ) {
    256
    element . append ( ' '
    258
    + schema . beforeText + ' ' + placeData . name + ' '
    260
    + ' ' + averageReview . toFixed ( 2 ) + ' / 5 '
    261
    + schema . middleText + ' ' + reviews . length + ' '
    262
    + schema . afterText
    263
    + ' '
    264
    + ' ' ) ;
    268
    plugin . init ( ) ;
    269
    270
    }
    271
    272
    $ . fn . googlePlaces = function ( options ) {
    273
    274
    return this . each ( function ( ) {
    275
    if ( undefined == $ ( this ) . data ( 'googlePlaces' ) ) {
    276
    var plugin = new $ . googlePlaces ( this , options ) ;
    277
    $ ( this ) . data ( 'googlePlaces' , plugin ) ;
    278
    }
    279
    } ) ;
    280
    281
    }
    282
    283
    } ) ( jQuery ) ;
    英雄联盟竞猜现场投注网 星火电竞公告比赛 csgo完美世界 电竞竞猜 蜂鸟电竞积分联赛 亿电竞赛事外围