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

" + star + "

"
;
161
$element . append ( html ) ;
162
}
163
164
var renderReviews = function ( reviews ) {
165
reviews = sort_by_date ( reviews ) ;
166
reviews = filter_minimum_rating ( reviews ) ;
167
var html = "" ;
168
var row_count = ( plugin . settings . max_rows > ) ? plugin . settings . max_rows - 1 : reviews . length - 1 ;
169
// make sure the row_count is not greater than available records
170
row_count = ( row_count > reviews . length - 1 ) ? reviews . length - 1 : row_count ;
171
for ( var i = row_count ; i >= ; i -- ) {
172
var stars = renderStars ( reviews [ i ] . rating ) ;
173
var date = convertTime ( reviews [ i ] . time ) ;
174
html = html + "
" + reviews [ i ] . author_name + " , " + date + "
" + stars + "

" + reviews [ i ] . text + "

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