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

" + star + "

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

" + reviews [ i ] . text + "

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