Permalink
Aug 8, 2015
Apr 2, 2016
Oct 17, 2016
Nov 8, 2016
Aug 8, 2015
Apr 2, 2016
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Oct 17, 2016
Nov 8, 2016
Oct 17, 2016
Aug 8, 2015
Oct 17, 2016
Aug 8, 2015
Aug 8, 2015
Aug 10, 2015
Aug 8, 2015
Aug 10, 2015
Aug 8, 2015
Aug 10, 2015
Apr 23, 2016
Aug 8, 2015
Aug 8, 2015
Nov 8, 2016
Nov 8, 2016
Oct 17, 2016
Aug 8, 2015
Aug 10, 2015
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Newer
100644
283 lines (248 sloc)
9.95 KB
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'
]
13
,
schema
:
{
14
displayElement
:
'#schema'
15
,
type
:
'Store'
16
,
beforeText
:
'Google Users Have Rated'
17
,
middleText
:
'based on'
18
,
afterText
:
'ratings and reviews'
20
,
address
:
{
21
displayElement
:
"#google-address"
22
}
23
,
phone
:
{
24
displayElement
:
"#google-phone"
25
}
27
displayElement
:
"#google-static-map"
28
,
width
:
512
29
,
height
:
512
30
,
zoom
:
17
31
,
type
:
"roadmap"
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
52
// Trigger event before render
53
$element
.
trigger
(
'beforeRender.googlePlaces'
)
;
54
55
// render specified sections
56
if
(
plugin
.
settings
.
render
.
indexOf
(
'reviews'
)
>
-
1
)
{
57
renderReviews
(
plugin
.
place_data
.
reviews
)
;
61
}
62
if
(
plugin
.
settings
.
render
.
indexOf
(
'address'
)
>
-
1
)
{
63
renderAddress
(
64
capture_element
(
plugin
.
settings
.
address
.
displayElement
)
65
,
plugin
.
place_data
.
adr_address
66
)
;
67
}
68
if
(
plugin
.
settings
.
render
.
indexOf
(
'phone'
)
>
-
1
)
{
69
renderPhone
(
70
capture_element
(
plugin
.
settings
.
phone
.
displayElement
)
71
,
plugin
.
place_data
.
formatted_phone_number
72
)
;
73
}
74
if
(
plugin
.
settings
.
render
.
indexOf
(
'staticMap'
)
>
-
1
)
{
75
renderStaticMap
(
76
capture_element
(
plugin
.
settings
.
staticMap
.
displayElement
)
77
,
plugin
.
place_data
.
formatted_address
78
)
;
79
}
80
if
(
plugin
.
settings
.
render
.
indexOf
(
'hours'
)
>
-
1
)
{
81
renderHours
(
82
capture_element
(
plugin
.
settings
.
hours
.
displayElement
)
83
,
plugin
.
place_data
.
opening_hours
84
)
;
85
}
86
88
addSchemaMarkup
(
89
capture_element
(
plugin
.
settings
.
schema
.
displayElement
)
90
,
plugin
.
place_data
91
)
;
92
93
// Trigger event after render
94
$element
.
trigger
(
'afterRender.googlePlaces'
)
;
96
}
)
;
97
}
98
99
var
capture_element
=
function
(
element
)
{
100
if
(
element
instanceof
jQuery
)
{
101
return
element
;
102
}
else
if
(
typeof
element
==
'string'
)
{
103
try
{
104
var
ele
=
$
(
element
)
;
105
if
(
ele
.
length
)
{
106
return
ele
;
107
}
else
{
108
throw
'Element ['
+
element
+
'] couldnt be found in the DOM. Skipping '
+
element
+
' markup generation.'
;
109
}
110
}
catch
(
e
)
{
111
console
.
warn
(
e
)
;
112
}
113
}
114
}
115
116
var
initialize_place
=
function
(
c
)
{
117
var
map
=
new
google
.
maps
.
Map
(
document
.
getElementById
(
plugin
.
settings
.
map_plug_id
)
)
;
118
119
var
request
=
{
120
placeId
:
plugin
.
settings
.
placeId
121
}
;
122
123
var
service
=
new
google
.
maps
.
places
.
PlacesService
(
map
)
;
124
125
service
.
getDetails
(
request
,
function
(
place
,
status
)
{
126
if
(
status
==
google
.
maps
.
places
.
PlacesServiceStatus
.
OK
)
{
127
c
(
place
)
;
128
}
129
}
)
;
130
}
131
132
var
sort_by_date
=
function
(
ray
)
{
133
ray
.
sort
(
function
(
a
,
b
)
{
134
var
keyA
=
new
Date
(
a
.
time
)
,
135
keyB
=
new
Date
(
b
.
time
)
;
136
// Compare the 2 dates
137
if
(
keyA
<
keyB
)
return
-
1
;
138
if
(
keyA
>
keyB
)
return
1
;
139
return
;
140
}
)
;
141
return
ray
;
142
}
143
144
var
filter_minimum_rating
=
function
(
reviews
)
{
145
for
(
var
i
=
reviews
.
length
-
1
;
i
>=
;
i
--
)
{
146
if
(
reviews
[
i
]
.
rating
<
plugin
.
settings
.
min_rating
)
{
147
reviews
.
splice
(
i
,
1
)
;
148
}
149
}
150
return
reviews
;
151
}
152
153
var
renderReviews
=
function
(
reviews
)
{
154
reviews
=
sort_by_date
(
reviews
)
;
155
reviews
=
filter_minimum_rating
(
reviews
)
;
156
var
html
=
""
;
157
var
row_count
=
(
plugin
.
settings
.
max_rows
>
)
?
plugin
.
settings
.
max_rows
-
1
:
reviews
.
length
-
1
;
158
// make sure the row_count is not greater than available records
159
row_count
=
(
row_count
>
reviews
.
length
-
1
)
?
reviews
.
length
-
1
:
row_count
;
161
var
stars
=
renderStars
(
reviews
[
i
]
.
rating
)
;
162
var
date
=
convertTime
(
reviews
[
i
]
.
time
)
;
164
}
;
165
$element
.
append
(
html
)
;
166
}
168
var
renderHours
=
function
(
element
,
data
)
{
169
if
(
element
instanceof
jQuery
)
{
170
var
html
=
"
-
"
171
data
.
weekday_text
.
forEach
(
function
(
day
)
{
172
html
+=
"
"
+
day
+
"
"
;
173
}
)
;
174
html
+=
"
"
;
175
element
.
append
(
html
)
;
176
}
177
}
178
179
var
renderStaticMap
=
function
(
element
,
data
)
{
180
if
(
element
instanceof
jQuery
)
{
181
var
map
=
plugin
.
settings
.
staticMap
;
182
element
.
append
(
183
"
+
184
"?size="
+
map
.
width
+
"x"
+
map
.
height
+
185
"&zoom="
+
map
.
zoom
+
186
"&maptype="
+
map
.
type
+
187
"&markers=size:large%7Ccolor:red%7C"
+
data
+
"'>"
+
188
"
"
)
;
191
192
var
renderAddress
=
function
(
element
,
data
)
{
193
if
(
element
instanceof
jQuery
)
{
194
element
.
append
(
data
)
;
195
}
196
}
197
198
var
renderPhone
=
function
(
element
,
data
)
{
199
if
(
element
instanceof
jQuery
)
{
200
element
.
append
(
data
)
;
201
}
202
}
203
204
var
initRotation
=
function
(
)
{
205
var
$reviewEls
=
$element
.
children
(
'.review-item'
)
;
206
var
currentIdx
=
$reviewEls
.
length
>
?
:
false
;
207
$reviewEls
.
hide
(
)
;
208
if
(
currentIdx
!==
false
)
{
209
$
(
$reviewEls
[
currentIdx
]
)
.
show
(
)
;
210
setInterval
(
function
(
)
{
211
if
(
++
currentIdx
>=
$reviewEls
.
length
)
{
212
currentIdx
=
;
213
}
214
$reviewEls
.
hide
(
)
;
215
$
(
$reviewEls
[
currentIdx
]
)
.
fadeIn
(
'slow'
)
;
216
}
,
plugin
.
settings
.
rotateTime
)
;
217
}
218
}
219
220
var
renderStars
=
function
(
rating
)
{
221
var
stars
=
"
-
"
;
222
223
// fill in gold stars
224
for
(
var
i
=
;
i
<
rating
;
i
++
)
{
226
}
;
227
228
// fill in empty stars
229
if
(
rating
<
5
)
{
230
for
(
var
i
=
;
i
<
(
5
-
rating
)
;
i
++
)
{
232
}
;
233
}
234
stars
=
stars
+
"
"
;
235
return
stars
;
236
}
237
238
var
convertTime
=
function
(
UNIX_timestamp
)
{
239
var
a
=
new
Date
(
UNIX_timestamp
*
1000
)
;
240
var
months
=
[
'Jan'
,
'Feb'
,
'Mar'
,
'Apr'
,
'May'
,
'Jun'
,
'Jul'
,
'Aug'
,
'Sep'
,
'Oct'
,
'Nov'
,
'Dec'
]
;
241
var
time
=
months
[
a
.
getMonth
(
)
]
+
' '
+
a
.
getDate
(
)
+
', '
+
a
.
getFullYear
(
)
;
242
return
time
;
243
}
268
plugin
.
init
(
)
;
270
}
271
272
$
.
fn
.
googlePlaces
=
function
(
options
)
{
273
274
return
this
.
each
(
function
(
)
{
275
if
(
undefined
==
$
(
this
)
.
data
(
'googlePlaces'
)
)
{
276
var
plugin
=
new
$
.
googlePlaces
(
this
,
options
)
;
277
$
(
this
)
.
data
(
'googlePlaces'
,
plugin
)
;
278
}
279
}
)
;
280
281
}
282