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

" + reviews [ i ] . text + "

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