Permalink
Aug 8, 2015
Aug 8, 2015
Aug 10, 2015
Aug 8, 2015
Aug 10, 2015
Aug 8, 2015
Aug 10, 2015
Aug 8, 2015
Newer
100644
123 lines (100 sloc)
3.93 KB
1
(
function
(
$
)
{
2
3
$
.
googlePlaces
=
function
(
element
,
options
)
{
4
5
var
defaults
=
{
6
placeId
:
'ChIJN1t_tDeuEmsRUsoyG83frY4'
// placeId provided by google api documentation
7
,
render
:
[
'reviews'
]
10
}
11
12
var
plugin
=
this
;
13
14
plugin
.
settings
=
{
}
15
16
var
$element
=
$
(
element
)
,
17
element
=
element
;
18
19
plugin
.
init
=
function
(
)
{
20
plugin
.
settings
=
$
.
extend
(
{
}
,
defaults
,
options
)
;
21
$element
.
html
(
"
"
)
;
// create a plug for google to load data into
22
initialize_place
(
function
(
place
)
{
23
plugin
.
place_data
=
place
;
24
// render specified sections
25
if
(
plugin
.
settings
.
render
.
indexOf
(
'reviews'
)
>
-
1
)
{
26
renderReviews
(
plugin
.
place_data
.
reviews
)
;
27
}
28
}
)
;
29
}
30
31
var
initialize_place
=
function
(
c
)
{
32
var
map
=
new
google
.
maps
.
Map
(
document
.
getElementById
(
'map-plug'
)
)
;
33
34
var
request
=
{
35
placeId
:
plugin
.
settings
.
placeId
36
}
;
37
38
var
service
=
new
google
.
maps
.
places
.
PlacesService
(
map
)
;
39
40
service
.
getDetails
(
request
,
function
(
place
,
status
)
{
41
if
(
status
==
google
.
maps
.
places
.
PlacesServiceStatus
.
OK
)
{
42
c
(
place
)
;
43
}
44
}
)
;
45
}
46
47
var
sort_by_date
=
function
(
ray
)
{
48
ray
.
sort
(
function
(
a
,
b
)
{
49
var
keyA
=
new
Date
(
a
.
time
)
,
50
keyB
=
new
Date
(
b
.
time
)
;
51
// Compare the 2 dates
52
if
(
keyA
<
keyB
)
return
-
1
;
53
if
(
keyA
>
keyB
)
return
1
;
54
return
;
55
}
)
;
56
return
ray
;
57
}
58
59
var
filter_minimum_rating
=
function
(
reviews
)
{
60
for
(
var
i
=
reviews
.
length
-
1
;
i
>=
;
i
--
)
{
61
if
(
reviews
[
i
]
.
rating
<
plugin
.
settings
.
min_rating
)
{
62
reviews
.
splice
(
i
,
1
)
;
63
}
64
}
65
return
reviews
;
66
}
67
68
var
renderReviews
=
function
(
reviews
)
{
69
reviews
=
sort_by_date
(
reviews
)
;
70
reviews
=
filter_minimum_rating
(
reviews
)
;
71
var
html
=
""
;
72
var
row_count
=
(
plugin
.
settings
.
max_rows
>
)
?
plugin
.
settings
.
max_rows
-
1
:
reviews
.
length
-
1
;
73
// make sure the row_count is not greater than available records
74
row_count
=
(
row_count
>
reviews
.
length
)
?
reviews
.
length
-
1
:
row_count
;
75
for
(
var
i
=
row_count
;
i
>=
;
i
--
)
{
76
var
stars
=
renderStars
(
reviews
[
i
]
.
rating
)
;
77
var
date
=
convertTime
(
reviews
[
i
]
.
time
)
;
78
html
=
html
+
"
"
+
reviews
[
i
]
.
author_name
+
"
"
+
date
+
"
"
+
stars
+
"
"
+ reviews [ i ] . text + " "
79
}
;
80
$element
.
append
(
html
)
;
81
}
82
83
var
renderStars
=
function
(
rating
)
{
84
var
stars
=
"
85
86
// fill in gold stars
87
for
(
var
i
=
;
i
<
rating
;
i
++
)
{
88
stars
=
stars
+
"
"
;
89
}
;
90
91
// fill in empty stars
92
if
(
rating
<
5
)
{
93
for
(
var
i
=
;
i
<
(
5
-
rating
)
;
i
++
)
{
94
stars
=
stars
+
"
"
;
95
}
;
96
}
97
stars
=
stars
+
"
"
;
98
return
stars
;
99
}
100
101
var
convertTime
=
function
(
UNIX_timestamp
)
{
102
var
a
=
new
Date
(
UNIX_timestamp
*
1000
)
;
103
var
months
=
[
'Jan'
,
'Feb'
,
'Mar'
,
'Apr'
,
'May'
,
'Jun'
,
'Jul'
,
'Aug'
,
'Sep'
,
'Oct'
,
'Nov'
,
'Dec'
]
;
104
var
time
=
months
[
a
.
getMonth
(
)
]
+
' '
+
a
.
getDate
(
)
+
', '
+
a
.
getFullYear
(
)
;
105
return
time
;
106
}
107
108
plugin
.
init
(
)
;
109
110
}
111
112
$
.
fn
.
googlePlaces
=
function
(
options
)
{
113
114
return
this
.
each
(
function
(
)
{
115
if
(
undefined
==
$
(
this
)
.
data
(
'googlePlaces'
)
)
{
116
var
plugin
=
new
$
.
googlePlaces
(
this
,
options
)
;
117
$
(
this
)
.
data
(
'googlePlaces'
,
plugin
)
;
118
}
119
}
)
;
120
121
}
122
123
}
)
(
jQuery
)
;