/
application.js
173 lines (140 loc) · 4.71 KB
/
application.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var lastOpened = null;
var WEBSOCKET_HOST = "0.0.0.0";
var infoTemplate = "<div class='tweet-bubble'><img src='<%= user.profile_image_url %>' alt='<%= user.username %>' align='left'/><div><strong><%= user.name %></strong><div><%= text %></div></div></div>";
var tweetTemplate = "<div class='img-col'><img src='<%= user.profile_image_url %>'/></div><div class='tweet-col'><strong><%= user.screen_name %></strong> <%= text %></div><div class='clear'></div>"
// Define a custom marker, if you choose.
// var markerSize = new google.maps.Size(562, 352, "px", "px");
// var scaledSize = new google.maps.Size(24, 24, "px", "px");
// var anchor = new google.maps.Point(281, 352);
// var origin = new google.maps.Point(0, 0);
// var markerImage = new google.maps.MarkerImage("http://cdn1.iconfinder.com/data/icons/aquaticus/60%20X%2060/twitter.png", null, null, null, scaledSize);
var Tweet = Backbone.Model.extend({
position: function(){
return new google.maps.LatLng(this.get("latlng").coordinates[1], this.get("latlng").coordinates[0]);
}
});
var Tweets = Backbone.Collection.extend({
model: Tweet
});
var TweetView = Backbone.View.extend({
tagName: "li",
className: "tweet",
initialize: function(){
_.bindAll(this, "render", "remove", "placeMarker", "showInfoWindow");
this.model.bind("change", this.render);
this.model.bind("remove", this.remove);
this.template = _.template(tweetTemplate);
this.infoTemplate = _.template(infoTemplate);
},
render: function(){
$(this.el).html(this.template(this.model.toJSON())).fadeIn(200);
var $this = this;
$(this.el).click(function(){$this.showInfoWindow()});
return this;
},
remove: function(){
window.lastView = this;
$(this.el).remove();
google.maps.event.clearListeners(this.marker);
this.marker.setMap(null);
this.infoWindow.close();
},
placeMarker: function(map) {
this.marker = new google.maps.Marker({
position: this.model.position(),
map: map
});
this.infoWindow = new google.maps.InfoWindow({
content: this.infoTemplate(this.model.toJSON())
});
google.maps.event.addListener(this.marker, "click", this.showInfoWindow);
return this.marker
},
showInfoWindow: function(){
if (lastOpened != null) {
lastOpened.close();
};
this.infoWindow.open(window.map, this.marker);
window.map.panTo(this.marker.getPosition());
lastOpened = this.infoWindow;
}
});
var TweetsView = Backbone.View.extend({
el: "#tweetbar",
initialize: function() {
_.bindAll(this, "render", "renderTweet");
this.collection.bind("add", this.renderTweet);
},
renderTweet: function(tweet){
var tweetView = new TweetView({
model: tweet
});
$("#tweetbar").prepend(tweetView.render().el);
tweetView.placeMarker(map);
}
});
tweets = new Tweets();
tweets.bind("add", function(tweet){
if (tweets.length > 25) {
tweets.remove(tweets.first());
};
});
function addTweet(json) {
var tweet = new Tweet({
text: json.text,
latlng: json.coordinates,
user: json.user,
id: json.id
});
var included = tweets.detect(function(tweet) { return tweet.id === json.id });
if (!included) {
tweets.add(tweet);
}
};
function cleanUpTweets() {
var remove_tweets = tweets.reject(function(tweet) {
return withinMapView(tweet.get("latlng").coordinates);
});
tweets.remove(remove_tweets);
}
function handleMapMove(){
setWindowBounds();
cleanUpTweets();
window.ws.send(window.map.getBounds());
}
function setWindowBounds () {
var bounds = map.getBounds()
window.slat = bounds.getSouthWest().lat();
window.wlng = bounds.getSouthWest().lng();
window.nlat = bounds.getNorthEast().lat();
window.elng = bounds.getNorthEast().lng();
}
function withinMapView(coordinates) {
if (coordinates[1] > window.slat && coordinates[1] < window.nlat && coordinates[0] > window.wlng && coordinates[0] < window.elng) {
return true
} else {
return false
};
};
$(function(){
window.map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(30.284540, -97.7933959),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
window.ws = new WebSocket("ws://"+WEBSOCKET_HOST+":8080")
ws.onopen = function(){
// Send the map bounds to start getting Tweets
ws.send(map.getBounds());
//google.maps.event.addListener(window.map, "dragend", handleMapMove);
//google.maps.event.addListener(window.map, "zoom_changed", handleMapMove);
google.maps.event.addListener(window.map, "idle", handleMapMove);
}
ws.onmessage = function(msg) {
var json = JSON.parse(msg.data);
addTweet(json);
};
window.tweetsView = new TweetsView({
collection: tweets
});
});