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

" + reviews [ i ] . text + "

"
148
} ;
149
$element . append ( html ) ;
150
}
152
153
var renderAddress = function ( element , data ) {
154
if ( element instanceof jQuery ) {
155
element . append ( data ) ;
156
}
157
}
158
159
var renderPhone = function ( element , data ) {
160
if ( element instanceof jQuery ) {
161
element . append ( data ) ;
162
}
163
}
164
165
var initRotation = function ( ) {
166
var $reviewEls = $element . children ( '.review-item' ) ;
167
var currentIdx = $reviewEls . length > ? : false ;
168
$reviewEls . hide ( ) ;
169
if ( currentIdx !== false ) {
170
$ ( $reviewEls [ currentIdx ] ) . show ( ) ;
171
setInterval ( function ( ) {
172
if ( ++ currentIdx >= $reviewEls . length ) {
173
currentIdx = ;
174
}
175
$reviewEls . hide ( ) ;
176
$ ( $reviewEls [ currentIdx ] ) . fadeIn ( 'slow' ) ;
177
} , plugin . settings . rotateTime ) ;
178
}
179
}
180
181
var renderStars = function ( rating ) {
182
var stars = "
    " ;
183
184
// fill in gold stars
185
for ( var i = ; i < rating ; i ++ ) {
186
stars = stars + "
  • "
    ;
    187
    } ;
    188
    189
    // fill in empty stars
    190
    if ( rating < 5 ) {
    191
    for ( var i = ; i < ( 5 - rating ) ; i ++ ) {
    192
    stars = stars + "
  • "
    ;
    193
    } ;
    194
    }
    195
    stars = stars + "
    " ;
    196
    return stars ;
    197
    }
    198
    199
    var convertTime = function ( UNIX_timestamp ) {
    200
    var a = new Date ( UNIX_timestamp * 1000 ) ;
    201
    var months = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ] ;
    202
    var time = months [ a . getMonth ( ) ] + ' ' + a . getDate ( ) + ', ' + a . getFullYear ( ) ;
    203
    return time ;
    204
    }
    206
    var addSchemaMarkup = function ( element , placeData ) {
    207
    var reviews = placeData . reviews ;
    208
    var lastIndex = reviews . length - 1 ;
    209
    var reviewPointTotal = ;
    210
    var schema = plugin . settings . schema ;
    211
    for ( var i = lastIndex ; i >= ; i -- ) {
    212
    reviewPointTotal += reviews [ i ] . rating ;
    213
    } ;
    214
    // Set totals and averages - may be used later.
    215
    var averageReview = reviewPointTotal / ( reviews . length ) ;
    216
    if ( element instanceof jQuery ) {
    217
    element . append ( ' '
    219
    + schema . beforeText + ' ' + placeData . name + ' '
    221
    + ' ' + averageReview . toFixed ( 2 ) + ' / 5 '
    222
    + schema . middleText + ' ' + reviews . length + ' '
    223
    + schema . afterText
    224
    + ' '
    225
    + ' ' ) ;
    229
    plugin . init ( ) ;
    230
    231
    }
    232
    233
    $ . fn . googlePlaces = function ( options ) {
    234
    235
    return this . each ( function ( ) {
    236
    if ( undefined == $ ( this ) . data ( 'googlePlaces' ) ) {
    237
    var plugin = new $ . googlePlaces ( this , options ) ;
    238
    $ ( this ) . data ( 'googlePlaces' , plugin ) ;
    239
    }
    240
    } ) ;
    241
    242
    }
    243
    244
    } ) ( jQuery ) ;
    英雄联盟竞猜现场投注网 星火电竞公告比赛 csgo完美世界 电竞竞猜 蜂鸟电竞积分联赛 亿电竞赛事外围