Skip to content
Permalink
Newer
Older
100644 144 lines (120 sloc) 4.83 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
} ;
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 ) ;
29
if ( ! ! plugin . settings . rotateTime ) {
30
initRotation ( ) ;
31
}
32
}
33
} ) ;
34
}
35
36
var initialize_place = function ( c ) {
37
var map = new google . maps . Map ( document . getElementById ( 'map-plug' ) ) ;
38
39
var request = {
40
placeId : plugin . settings . placeId
41
} ;
42
43
var service = new google . maps . places . PlacesService ( map ) ;
44
45
service . getDetails ( request , function ( place , status ) {
46
if ( status == google . maps . places . PlacesServiceStatus . OK ) {
47
c ( place ) ;
48
}
49
} ) ;
50
}
51
52
var sort_by_date = function ( ray ) {
53
ray . sort ( function ( a , b ) {
54
var keyA = new Date ( a . time ) ,
55
keyB = new Date ( b . time ) ;
56
// Compare the 2 dates
57
if ( keyA < keyB ) return - 1 ;
58
if ( keyA > keyB ) return 1 ;
59
return ;
60
} ) ;
61
return ray ;
62
}
63
64
var filter_minimum_rating = function ( reviews ) {
65
for ( var i = reviews . length - 1 ; i >= ; i -- ) {
66
if ( reviews [ i ] . rating < plugin . settings . min_rating ) {
67
reviews . splice ( i , 1 ) ;
68
}
69
}
70
return reviews ;
71
}
72
73
var renderReviews = function ( reviews ) {
74
reviews = sort_by_date ( reviews ) ;
75
reviews = filter_minimum_rating ( reviews ) ;
76
var html = "" ;
77
var row_count = ( plugin . settings . max_rows > ) ? plugin . settings . max_rows - 1 : reviews . length - 1 ;
78
// make sure the row_count is not greater than available records
79
row_count = ( row_count > reviews . length ) ? reviews . length - 1 : row_count ;
80
for ( var i = row_count ; i >= ; i -- ) {
81
var stars = renderStars ( reviews [ i ] . rating ) ;
82
var date = convertTime ( reviews [ i ] . time ) ;
83
html = html + "
" + reviews [ i ] . author_name + " , " + date + "
" + stars + "

" + reviews [ i ] . text + "

"
84
} ;
85
$element . append ( html ) ;
86
}
87
88
var initRotation = function ( ) {
89
var $reviewEls = $element . children ( '.review-item' ) ;
90
var currentIdx = $reviewEls . length > ? : false ;
91
$reviewEls . hide ( ) ;
92
if ( currentIdx !== false ) {
93
$ ( $reviewEls [ currentIdx ] ) . show ( ) ;
94
setInterval ( function ( ) {
95
if ( ++ currentIdx >= $reviewEls . length ) {
96
currentIdx = ;
97
}
98
$reviewEls . hide ( ) ;
99
$ ( $reviewEls [ currentIdx ] ) . fadeIn ( 'slow' ) ;
100
} , plugin . settings . rotateTime ) ;
101
}
102
}
103
104
var renderStars = function ( rating ) {
105
var stars = "
    " ;
106
107
// fill in gold stars
108
for ( var i = ; i < rating ; i ++ ) {
109
stars = stars + "
  • "
    ;
    110
    } ;
    111
    112
    // fill in empty stars
    113
    if ( rating < 5 ) {
    114
    for ( var i = ; i < ( 5 - rating ) ; i ++ ) {
    115
    stars = stars + "
  • "
    ;
    116
    } ;
    117
    }
    118
    stars = stars + "
    " ;
    119
    return stars ;
    120
    }
    121
    122
    var convertTime = function ( UNIX_timestamp ) {
    123
    var a = new Date ( UNIX_timestamp * 1000 ) ;
    124
    var months = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ] ;
    125
    var time = months [ a . getMonth ( ) ] + ' ' + a . getDate ( ) + ', ' + a . getFullYear ( ) ;
    126
    return time ;
    127
    }
    128
    129
    plugin . init ( ) ;
    130
    131
    }
    132
    133
    $ . fn . googlePlaces = function ( options ) {
    134
    135
    return this . each ( function ( ) {
    136
    if ( undefined == $ ( this ) . data ( 'googlePlaces' ) ) {
    137
    var plugin = new $ . googlePlaces ( this , options ) ;
    138
    $ ( this ) . data ( 'googlePlaces' , plugin ) ;
    139
    }
    140
    } ) ;
    141
    142
    }
    143
    144
    } ) ( jQuery ) ;
    英雄联盟竞猜现场投注网 星火电竞公告比赛 csgo完美世界 电竞竞猜 蜂鸟电竞积分联赛 亿电竞赛事外围