-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
257 lines (201 loc) · 6.81 KB
/
main.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
var Foursquare = (function () {
var user = {};
var accessToken;
var getAllCheckins = function (options) {
var deferred = $.Deferred();
accessToken = options.accessToken;
fetchUser().then( function () {
user.checkins.items = [];
var requests = [];
for (i=0; i<user.checkins.count; i+=250) {
var options = {
limit : 250,
offset: i
};
requests.push( fetchCheckins( options ).then( function (response) { user.checkins.items.push( response.response.checkins.items ) } ) );
}
$.when.apply( $, requests ).done( function () {
user.checkins.items = _.flatten( user.checkins.items );
deferred.resolve( user.checkins.items );
});
});
return deferred;
};
var fetchCheckins = function (options) {
var deferred = $.Deferred();
$.get( "https://api.foursquare.com/v2/users/self/checkins", {
oauth_token : accessToken,
v : "20141111",
limit : options.limit || 250,
offset : options.offset || 0
}, function (response) {
deferred.resolve( response );
});
return deferred;
};
var fetchUser = function () {
var deferred = $.Deferred();
$.get( "https://api.foursquare.com/v2/users/self?oauth_token=" + accessToken + "&v=20141111", function (response) {
user = response.response.user;
deferred.resolve( response );
});
return deferred;
};
return {
getAllCheckins : getAllCheckins
}
}());
var SwarmaCarta = (function () {
var user = {};
var position;
var map;
var init = function () {
$( "#about" ).click( function () {
$( "#about-modal" ).modal();
});
$( "#share" ).click( function () {
$( "#share" ).button( "loading" );
share().done( function (shareObjectId) {
$( "#share" ).button( "reset" );
var shareUrl = "http://swarmacarta.s3-website-us-east-1.amazonaws.com/#share=" + shareObjectId;
$( "#modals").html( _.template( $( "#share-modal" ).html() )( {shareUrl : shareUrl} ) );
$( "#modals > div" ).modal();
});
});
getUserLocation().then( function () {
initializeMap();
reactToHash();
});
};
var getUserLocation = function () {
var deferred = $.Deferred();
navigator.geolocation.getCurrentPosition( function (response) {
position = response;
deferred.resolve();
}, deferred.reject, {
enableHighAccuracy : false,
timeout : 10000,
maximumAge : 0
});
return deferred;
};
var reactToHash = function () {
var match;
match = window.location.hash.match(/#access_token=(\w*)/);
window.M = match;
if (match) {
user.accessToken = match[1];
$( "#share" ).show();
fetchCheckins().then( plotCheckins ).then( showContent );
return;
}
match = window.location.hash.match(/#share=(\w*)/);
if (match) {
loadShared( match[1] ).then( showContent );
$( "#new" ).show();
return;
}
showContent();
$( "#intro-modal" ).modal({
keyboard : false,
backdrop : "static"
});
};
var initializeMap = function () {
map = new google.maps.Map( document.getElementById( "map" ), {
center : {
lat : position.coords.latitude,
lng : position.coords.longitude
},
zoom: 11,
styles : [{"featureType":"landscape","stylers":[{"hue":"#F1FF00"},{"saturation":-27.4},{"lightness":9.4},{"gamma":1}]},{"featureType":"road.highway","stylers":[{"hue":"#0099FF"},{"saturation":-20},{"lightness":36.4},{"gamma":1}]},{"featureType":"road.arterial","stylers":[{"hue":"#00FF4F"},{"saturation":0},{"lightness":0},{"gamma":1}]},{"featureType":"road.local","stylers":[{"hue":"#FFB300"},{"saturation":-38},{"lightness":11.2},{"gamma":1}]},{"featureType":"water","stylers":[{"hue":"#00B6FF"},{"saturation":4.2},{"lightness":-63.4},{"gamma":1}]},{"featureType":"poi","stylers":[{"hue":"#9FFF00"},{"saturation":0},{"lightness":0},{"gamma":1}]}]
});
};
var fetchCheckins = function () {
var deferred = $.Deferred();
Foursquare.getAllCheckins( {accessToken : user.accessToken} ).then( function (response) {
user.checkins = response;
deferred.resolve();
});
return deferred;
};
var plotCheckins = function () {
var markers = [];
_.each( user.checkins, function (checkin) {
if (checkin.venue) {
var position = new google.maps.LatLng(
checkin.venue.location.lat,
checkin.venue.location.lng
);
var marker = new google.maps.Marker({
position : position,
map : map,
title : checkin.venue.name
});
markers.push( marker );
google.maps.event.addListener( marker, "click", function () {
$( "#modals").html( _.template( $( "#venue-modal" ).html() )( {checkin : checkin} ) );
$( "#modals > div" ).modal();
});
}
});
var bounds = new google.maps.LatLngBounds();
_.each( markers, function (marker) {
bounds.extend( marker.getPosition() );
});
map.fitBounds( bounds );
};
var share = function () {
var deferred = $.Deferred();
Parse.initialize("TppV3dU3Hbs7gg7027cq13HWDFOuXr8kWQsbBSs4", "ls4QubII694fiyiYCUfUutXVKd7YOH3jYWgOQii0");
var fileData = {
base64 : checkinsToParseFile()
};
var checkinsFile = new Parse.File( "checkins", fileData )
checkinsFile.save().then( function (response) {
console.log( "success", response );
var shareObject = new Parse.Object( "ShareObject" );
shareObject.set( "checkinsFile", checkinsFile );
shareObject.save().then( function (response) {
deferred.resolve( response.id );
});
}, function (error) {
deferred.reject();
});
return deferred;
};
var loadShared = function (shareObjectID) {
var deferred = $.Deferred();
Parse.initialize("TppV3dU3Hbs7gg7027cq13HWDFOuXr8kWQsbBSs4", "ls4QubII694fiyiYCUfUutXVKd7YOH3jYWgOQii0");
var ShareObject = Parse.Object.extend( "ShareObject" );
new Parse.Query( ShareObject ).get( shareObjectID, {
success : function (shareObject) {
$.get( shareObject.get( "checkinsFile" ).url(), function (response) {
user.checkins = parseFileToCheckins( response );
plotCheckins();
deferred.resolve();
});
},
error : function () {
console.log( "Retrieval error." );
}
});
return deferred;
};
var checkinsToParseFile = function () {
return base64.encode( encodeURIComponent( JSON.stringify( user.checkins ) ) );
};
var parseFileToCheckins = function (parseFileContent) {
return JSON.parse( decodeURIComponent( parseFileContent ) );
};
var showContent = function () {
$( "#loading" ).fadeOut();
};
return {
init : init,
share : share
}
}());
$( function () {
SwarmaCarta.init();
});