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 8, 2015
Aug 8, 2015
Aug 8, 2015
Aug 10, 2015
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Aug 8, 2015
Newer
100644
174 lines (149 sloc)
6.37 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'
]
19
20
var
plugin
=
this
;
21
22
plugin
.
settings
=
{
}
23
24
var
$element
=
$
(
element
)
,
25
element
=
element
;
26
27
plugin
.
init
=
function
(
)
{
28
plugin
.
settings
=
$
.
extend
(
{
}
,
defaults
,
options
)
;
29
$element
.
html
(
"
"
)
;
// create a plug for google to load data into
30
initialize_place
(
function
(
place
)
{
31
plugin
.
place_data
=
place
;
32
// render specified sections
33
if
(
plugin
.
settings
.
render
.
indexOf
(
'reviews'
)
>
-
1
)
{
34
renderReviews
(
plugin
.
place_data
.
reviews
)
;
38
}
40
if
(
plugin
.
settings
.
schema
.
displayElement
instanceof
jQuery
)
{
41
addSchemaMarkup
(
plugin
.
place_data
)
;
43
}
)
;
44
}
45
46
var
initialize_place
=
function
(
c
)
{
47
var
map
=
new
google
.
maps
.
Map
(
document
.
getElementById
(
'map-plug'
)
)
;
48
49
var
request
=
{
50
placeId
:
plugin
.
settings
.
placeId
51
}
;
52
53
var
service
=
new
google
.
maps
.
places
.
PlacesService
(
map
)
;
54
55
service
.
getDetails
(
request
,
function
(
place
,
status
)
{
56
if
(
status
==
google
.
maps
.
places
.
PlacesServiceStatus
.
OK
)
{
57
c
(
place
)
;
58
}
59
}
)
;
60
}
61
62
var
sort_by_date
=
function
(
ray
)
{
63
ray
.
sort
(
function
(
a
,
b
)
{
64
var
keyA
=
new
Date
(
a
.
time
)
,
65
keyB
=
new
Date
(
b
.
time
)
;
66
// Compare the 2 dates
67
if
(
keyA
<
keyB
)
return
-
1
;
68
if
(
keyA
>
keyB
)
return
1
;
69
return
;
70
}
)
;
71
return
ray
;
72
}
73
74
var
filter_minimum_rating
=
function
(
reviews
)
{
75
for
(
var
i
=
reviews
.
length
-
1
;
i
>=
;
i
--
)
{
76
if
(
reviews
[
i
]
.
rating
<
plugin
.
settings
.
min_rating
)
{
77
reviews
.
splice
(
i
,
1
)
;
78
}
79
}
80
return
reviews
;
81
}
82
83
var
renderReviews
=
function
(
reviews
)
{
84
reviews
=
sort_by_date
(
reviews
)
;
85
reviews
=
filter_minimum_rating
(
reviews
)
;
86
var
html
=
""
;
87
var
row_count
=
(
plugin
.
settings
.
max_rows
>
)
?
plugin
.
settings
.
max_rows
-
1
:
reviews
.
length
-
1
;
88
// make sure the row_count is not greater than available records
89
row_count
=
(
row_count
>
reviews
.
length
)
?
reviews
.
length
-
1
:
row_count
;
90
for
(
var
i
=
row_count
;
i
>=
;
i
--
)
{
91
var
stars
=
renderStars
(
reviews
[
i
]
.
rating
)
;
92
var
date
=
convertTime
(
reviews
[
i
]
.
time
)
;
94
}
;
95
$element
.
append
(
html
)
;
96
}
97
98
var
initRotation
=
function
(
)
{
99
var
$reviewEls
=
$element
.
children
(
'.review-item'
)
;
100
var
currentIdx
=
$reviewEls
.
length
>
?
:
false
;
101
$reviewEls
.
hide
(
)
;
102
if
(
currentIdx
!==
false
)
{
103
$
(
$reviewEls
[
currentIdx
]
)
.
show
(
)
;
104
setInterval
(
function
(
)
{
105
if
(
++
currentIdx
>=
$reviewEls
.
length
)
{
106
currentIdx
=
;
107
}
108
$reviewEls
.
hide
(
)
;
109
$
(
$reviewEls
[
currentIdx
]
)
.
fadeIn
(
'slow'
)
;
110
}
,
plugin
.
settings
.
rotateTime
)
;
111
}
112
}
113
114
var
renderStars
=
function
(
rating
)
{
115
var
stars
=
"
-
"
;
116
117
// fill in gold stars
118
for
(
var
i
=
;
i
<
rating
;
i
++
)
{
120
}
;
121
122
// fill in empty stars
123
if
(
rating
<
5
)
{
124
for
(
var
i
=
;
i
<
(
5
-
rating
)
;
i
++
)
{
126
}
;
127
}
128
stars
=
stars
+
"
"
;
129
return
stars
;
130
}
131
132
var
convertTime
=
function
(
UNIX_timestamp
)
{
133
var
a
=
new
Date
(
UNIX_timestamp
*
1000
)
;
134
var
months
=
[
'Jan'
,
'Feb'
,
'Mar'
,
'Apr'
,
'May'
,
'Jun'
,
'Jul'
,
'Aug'
,
'Sep'
,
'Oct'
,
'Nov'
,
'Dec'
]
;
135
var
time
=
months
[
a
.
getMonth
(
)
]
+
' '
+
a
.
getDate
(
)
+
', '
+
a
.
getFullYear
(
)
;
136
return
time
;
137
}
152
+
'
'
+
averageReview
+
'
/
5
'
153
+
plugin
.
settings
.
schema
.
middleText
+
'
'
+
reviews
.
length
+
'
'
154
+
plugin
.
settings
.
schema
.
afterText
159
plugin
.
init
(
)
;
161
}
162
163
$
.
fn
.
googlePlaces
=
function
(
options
)
{
164
165
return
this
.
each
(
function
(
)
{
166
if
(
undefined
==
$
(
this
)
.
data
(
'googlePlaces'
)
)
{
167
var
plugin
=
new
$
.
googlePlaces
(
this
,
options
)
;
168
$
(
this
)
.
data
(
'googlePlaces'
,
plugin
)
;
169
}
170
}
)
;
171
172
}
173