Skip to content
Permalink
Newer
Older
100644 284 lines (249 sloc) 9.99 KB
1
/* https://github.com/peledies/google-places */
2
( function ( $ ) {
3
4
$ . googlePlaces = function ( element , options ) {
5
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 ) ;
56
// render specified sections
57
if ( plugin . settings . render . indexOf ( 'reviews' ) > - 1 ) {
58
renderReviews ( plugin . place_data . reviews ) ;
59
if ( ! ! plugin . settings . rotateTime ) {
60
initRotation ( ) ;
61
}
63
if ( plugin . settings . render . indexOf ( 'address' ) > - 1 ) {
64
renderAddress (
65
capture_element ( plugin . settings . address . displayElement )
66
, plugin . place_data . adr_address
67
) ;
68
}
69
if ( plugin . settings . render . indexOf ( 'phone' ) > - 1 ) {
70
renderPhone (
71
capture_element ( plugin . settings . phone . displayElement )
72
, plugin . place_data . formatted_phone_number
73
) ;
74
}
75
if ( plugin . settings . render . indexOf ( 'staticMap' ) > - 1 ) {
76
renderStaticMap (
77
capture_element ( plugin . settings . staticMap . displayElement )
78
, plugin . place_data . formatted_address
79
) ;
80
}
81
if ( plugin . settings . render . indexOf ( 'hours' ) > - 1 ) {
82
renderHours (
83
capture_element ( plugin . settings . hours . displayElement )
84
, plugin . place_data . opening_hours
85
) ;
86
}
87
88
// render schema markup
89
addSchemaMarkup (
90
capture_element ( plugin . settings . schema . displayElement )
91
, plugin . place_data
92
) ;
93
94
// Trigger event after render
95
$element . trigger ( 'afterRender.' + namespace ) ;
97
} ) ;
98
}
99
100
var capture_element = function ( element ) {
101
if ( element instanceof jQuery ) {
102
return element ;
103
} else if ( typeof element == 'string' ) {
104
try {
105
var ele = $ ( element ) ;
106
if ( ele . length ) {
107
return ele ;
108
} else {
109
throw 'Element [' + element + '] couldnt be found in the DOM. Skipping ' + element + ' markup generation.' ;
110
}
111
} catch ( e ) {
112
console . warn ( e ) ;
113
}
114
}
115
}
116
117
var initialize_place = function ( c ) {
118
var map = new google . maps . Map ( document . getElementById ( plugin . settings . map_plug_id ) ) ;
119
120
var request = {
121
placeId : plugin . settings . placeId
122
} ;
123
124
var service = new google . maps . places . PlacesService ( map ) ;
125
126
service . getDetails ( request , function ( place , status ) {
127
if ( status == google . maps . places . PlacesServiceStatus . OK ) {
128
c ( place ) ;
129
}
130
} ) ;
131
}
132
133
var sort_by_date = function ( ray ) {
134
ray . sort ( function ( a , b ) {
135
var keyA = new Date ( a . time ) ,
136
keyB = new Date ( b . time ) ;
137
// Compare the 2 dates
138
if ( keyA < keyB ) return - 1 ;
139
if ( keyA > keyB ) return 1 ;
140
return ;
141
} ) ;
142
return ray ;
143
}
144
145
var filter_minimum_rating = function ( reviews ) {
146
for ( var i = reviews . length - 1 ; i >= ; i -- ) {
147
if ( reviews [ i ] . rating < plugin . settings . min_rating ) {
148
reviews . splice ( i , 1 ) ;
149
}
150
}
151
return reviews ;
152
}
153
154
var renderReviews = function ( reviews ) {
155
reviews = sort_by_date ( reviews ) ;
156
reviews = filter_minimum_rating ( reviews ) ;
157
var html = "" ;
158
var row_count = ( plugin . settings . max_rows > ) ? plugin . settings . max_rows - 1 : reviews . length - 1 ;
159
// make sure the row_count is not greater than available records
160
row_count = ( row_count > reviews . length - 1 ) ? reviews . length - 1 : row_count ;
161
for ( var i = row_count ; i >= ; i -- ) {
162
var stars = renderStars ( reviews [ i ] . rating ) ;
163
var date = convertTime ( reviews [ i ] . time ) ;
164
html = html + "
" + reviews [ i ] . author_name + " , " + date + "
" + stars + "

" + reviews [ i ] . text + "

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