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

" + reviews [ i ] . text + "

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