Skip to content

Commit

Permalink
added updateTripDistance
Browse files Browse the repository at this point in the history
  • Loading branch information
abarnhard committed Aug 23, 2014
1 parent 2ec647d commit e085345
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/controllers/trips.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ exports.show = function(req, res){
});
};

exports.updateDist = function(req, res){

};
1 change: 1 addition & 0 deletions app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = function(app, express){
app.post('/trips', trips.create);
app.get('/trips', trips.index);
app.get('/trips/:tripId', trips.show);
app.put('/trips/:tripId/distance', trips.updateDist);

app.post('/trips/:tripId/stops', stops.create);
app.get('/trips/:tripId/stops/:stopId', stops.show);
Expand Down
2 changes: 1 addition & 1 deletion app/static/js/user/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function calcRoute(waypoints, cb){
origin:origin,
destination:destination,
waypoints:waypoints,
optimizeWaypoints:false,
optimizeWaypoints:true,
travelMode:google.maps.TravelMode.DRIVING
};
// console.log('request', request);
Expand Down
19 changes: 16 additions & 3 deletions app/static/js/user/trips-show.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

calcRoute(waypoints, function(response){
directionsDisplay.setDirections(response);
updateTripDistance(response);
});

});

function createStops(e){
debugger;
// debugger;
var stopGroups = $('.stop-group').toArray();
console.log(stopGroups);
async.map(stopGroups, function(stopGroup, done){
Expand Down Expand Up @@ -56,14 +57,14 @@
$formGroup.append($i);
$i = $('<input>').prop('type', 'hidden').prop('name', name + '[lng]').attr('data-id', 'lng');
$formGroup.append($i);
$i = $('<input>').prop('type', 'hidden').prop('name', name + '[tripId]').attr('data-id', 'tripId').val($('form').attr('data-trip-id'));
$i = $('<input>').prop('type', 'hidden').prop('name', name + '[tripId]').attr('data-id', 'tripId').val($('h2').attr('data-trip-id'));
$formGroup.append($i);
$last.after($formGroup);
$formGroup.children('input[type=text]').focus();
}

function makeWaypoints(){
var $stops = $('ol > li'),
var $stops = $('#stops > li'),
waypoints = $stops.toArray().map(function(s){
return new google.maps.LatLng(parseFloat($(s).attr('data-lat')), parseFloat($(s).attr('data-lng')));
}),
Expand All @@ -74,5 +75,17 @@
return waypoints;
}

function updateTripDistance(response){
// debugger;
var distance = response.routes[0].legs.reduce(function(total, leg){return total + leg.distance.value * 0.00062137;}, 0),
id = $('h2').attr('data-trip-id'),
type = 'put',
url = '/trips/' + id + '/distance',
data = 'distance=' + distance;
$.ajax({url:url, type:type, data:data, dataType:'json', success:function(data){
console.log(data);
}});
}

})();

6 changes: 3 additions & 3 deletions app/views/trips/show.jade
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ block content
.panel-body
.row
.col-xs-12
h2= trip.name
h2(data-trip-id='#{trip._id}')= trip.name
.row
.col-xs-12
#trip-map(data-orig-name='#{trip.originLoc.name}', data-orig-lat='#{trip.originLoc.lat}', data-orig-lng='#{trip.originLoc.lng}', data-dest-name='#{trip.destinationLoc.name}', data-dest-lat='#{trip.destinationLoc.lat}', data-dest-lng='#{trip.destinationLoc.lng}')
.row
.col-xs-6
h4 Stops:
ol
ul#stops
each stop in trip.stops
li(data-name='#{stop.loc.name}', data-lat='#{stop.loc.lat}', data-lng='#{stop.loc.lng}'): a(href='/trips/#{trip._id}/stops/#{stop._id}')= stop.loc.name
.col-xs-6
h4 Add Stops:
form(role='form', method='post', action='/trips/#{trip._id}/stops', data-trip-id='#{trip._id}')
form(role='form', method='post', action='/trips/#{trip._id}/stops')
.form-group
button.btn.btn-info.btn-xs(type='button') + Stops
.form-group.stop-group
Expand Down

0 comments on commit e085345

Please sign in to comment.