Permalink
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Aug 10, 2015
Aug 8, 2015
Aug 10, 2015
Aug 8, 2015
Aug 10, 2015
Aug 10, 2015
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Aug 10, 2015
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Newer
100644
171 lines (147 sloc)
6.26 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
14
var
plugin
=
this
;
15
16
plugin
.
settings
=
{
}
17
18
var
$element
=
$
(
element
)
,
19
element
=
element
;
20
21
plugin
.
init
=
function
(
)
{
22
plugin
.
settings
=
$
.
extend
(
{
}
,
defaults
,
options
)
;
23
$element
.
html
(
"
"
)
;
// create a plug for google to load data into
24
initialize_place
(
function
(
place
)
{
25
plugin
.
place_data
=
place
;
26
// render specified sections
27
if
(
plugin
.
settings
.
render
.
indexOf
(
'reviews'
)
>
-
1
)
{
28
renderReviews
(
plugin
.
place_data
.
reviews
)
;
32
}
33
// render schema markup
34
if
(
plugin
.
settings
.
render
.
indexOf
(
'schema'
)
>
-
1
)
{
35
getSchemaMarkup
(
plugin
.
place_data
)
;
36
}
37
}
)
;
38
}
39
40
var
initialize_place
=
function
(
c
)
{
41
var
map
=
new
google
.
maps
.
Map
(
document
.
getElementById
(
'map-plug'
)
)
;
42
43
var
request
=
{
44
placeId
:
plugin
.
settings
.
placeId
45
}
;
46
47
var
service
=
new
google
.
maps
.
places
.
PlacesService
(
map
)
;
48
49
service
.
getDetails
(
request
,
function
(
place
,
status
)
{
50
if
(
status
==
google
.
maps
.
places
.
PlacesServiceStatus
.
OK
)
{
51
c
(
place
)
;
52
}
53
}
)
;
54
}
55
56
var
sort_by_date
=
function
(
ray
)
{
57
ray
.
sort
(
function
(
a
,
b
)
{
58
var
keyA
=
new
Date
(
a
.
time
)
,
59
keyB
=
new
Date
(
b
.
time
)
;
60
// Compare the 2 dates
61
if
(
keyA
<
keyB
)
return
-
1
;
62
if
(
keyA
>
keyB
)
return
1
;
63
return
;
64
}
)
;
65
return
ray
;
66
}
67
68
var
filter_minimum_rating
=
function
(
reviews
)
{
69
for
(
var
i
=
reviews
.
length
-
1
;
i
>=
;
i
--
)
{
70
if
(
reviews
[
i
]
.
rating
<
plugin
.
settings
.
min_rating
)
{
71
reviews
.
splice
(
i
,
1
)
;
72
}
73
}
74
return
reviews
;
75
}
76
77
var
renderReviews
=
function
(
reviews
)
{
78
reviews
=
sort_by_date
(
reviews
)
;
79
reviews
=
filter_minimum_rating
(
reviews
)
;
80
var
html
=
""
;
81
var
row_count
=
(
plugin
.
settings
.
max_rows
>
)
?
plugin
.
settings
.
max_rows
-
1
:
reviews
.
length
-
1
;
83
// make sure the row_count is not greater than available records
84
row_count
=
(
row_count
>
reviews
.
length
)
?
reviews
.
length
-
1
:
row_count
;
85
for
(
var
i
=
row_count
;
i
>=
;
i
--
)
{
86
var
stars
=
renderStars
(
reviews
[
i
]
.
rating
)
;
88
var
date
=
convertTime
(
reviews
[
i
]
.
time
)
;
90
}
;
91
// Set totals and averages - may be used later.
92
numReviews
=
row_count
;
93
averageReview
=
reviewPointTotal
/
numReviews
;
94
$element
.
append
(
html
)
;
95
}
96
97
var
initRotation
=
function
(
)
{
98
var
$reviewEls
=
$element
.
children
(
'.review-item'
)
;
99
var
currentIdx
=
$reviewEls
.
length
>
?
:
false
;
100
$reviewEls
.
hide
(
)
;
101
if
(
currentIdx
!==
false
)
{
102
$
(
$reviewEls
[
currentIdx
]
)
.
show
(
)
;
103
setInterval
(
function
(
)
{
104
if
(
++
currentIdx
>=
$reviewEls
.
length
)
{
105
currentIdx
=
;
106
}
107
$reviewEls
.
hide
(
)
;
108
$
(
$reviewEls
[
currentIdx
]
)
.
fadeIn
(
'slow'
)
;
109
}
,
plugin
.
settings
.
rotateTime
)
;
110
}
111
}
112
113
var
renderStars
=
function
(
rating
)
{
114
var
stars
=
"
-
"
;
115
116
// fill in gold stars
117
for
(
var
i
=
;
i
<
rating
;
i
++
)
{
119
}
;
120
121
// fill in empty stars
122
if
(
rating
<
5
)
{
123
for
(
var
i
=
;
i
<
(
5
-
rating
)
;
i
++
)
{
125
}
;
126
}
127
stars
=
stars
+
"
"
;
128
return
stars
;
129
}
130
131
var
convertTime
=
function
(
UNIX_timestamp
)
{
132
var
a
=
new
Date
(
UNIX_timestamp
*
1000
)
;
133
var
months
=
[
'Jan'
,
'Feb'
,
'Mar'
,
'Apr'
,
'May'
,
'Jun'
,
'Jul'
,
'Aug'
,
'Sep'
,
'Oct'
,
'Nov'
,
'Dec'
]
;
134
var
time
=
months
[
a
.
getMonth
(
)
]
+
' '
+
a
.
getDate
(
)
+
', '
+
a
.
getFullYear
(
)
;
135
return
time
;
136
}
137
138
var
getSchemaMarkup
=
function
(
placeData
)
{
139
var
reviews
=
placeData
.
reviews
;
140
var
row_count
=
reviews
.
length
-
1
;
141
var
reviewPointTotal
=
;
142
for
(
var
i
=
row_count
;
i
>=
;
i
--
)
{
143
reviewPointTotal
+=
reviews
[
i
]
.
rating
;
144
}
;
145
// Set totals and averages.
146
var
averageReview
=
reviewPointTotal
/
(
reviews
.
length
)
;
147
$element
.
append
(
'
'
148
+
'
'
149
+
'Google users have rated
'
+
placeData
.
name
+
'
'
150
+
'
'
151
+
'
'
+
averageReview
+
'
/
5
'
152
+
' based on
'
+
reviews
.
length
+
'
ratings and reviews'
153
+
'
'
154
+
'
'
)
;
155
}
156
157
plugin
.
init
(
)
;
158
}
159
160
$
.
fn
.
googlePlaces
=
function
(
options
)
{
161
162
return
this
.
each
(
function
(
)
{
163
if
(
undefined
==
$
(
this
)
.
data
(
'googlePlaces'
)
)
{
164
var
plugin
=
new
$
.
googlePlaces
(
this
,
options
)
;
165
$
(
this
)
.
data
(
'googlePlaces'
,
plugin
)
;
166
}
167
}
)
;
168
169
}
170