koerseadmin/js/controllers.js

692 lines
19 KiB
JavaScript

'use strict';
/* Controllers */
var mod = angular.module('bananaraceApp.controllers', ['bananaraceApp.services']);
mod.controller('ChampionshipsCtrl', ['$scope', 'ChampionshipService', function($scope, ChampionshipService) {
var championships = [];
$scope.refreshChampionships = function() {
console.log('doing refreshChampionships');
ChampionshipService.getChampionships().
success(function(data, status, headers, config) {
console.log('refreshChampionships successful...');
console.log(data);
if(data['ok']==true) {
$scope.championships = data.championships;
} else {
}
}).
error(function(data, status, headers, config) {
console.log('refreshChampionships failed...');
});
};
$scope.deleteChampionship = function(championship) {
ChampionshipService.deleteChampionship(championship).
success(function(data, status, headers, config) {
console.log('refreshChampionships successful...');
if(data['ok']==true) {
$scope.refreshChampionships();
} else {
}
}).
error(function(data, status, headers, config) {
console.log('refreshChampionships failed...');
});
};
$scope.addChampionship = function(newchampionship) {
console.log("adding: "+newchampionship);
ChampionshipService.addChampionship(newchampionship).
success(function(data, status, headers, config) {
console.log('addChampionships successful...');
console.log(data);
if(data['ok']==true) {
$scope.refreshChampionships();
newchampionship.name = "";
}
}).
error(function(data, status, headers, config) {
console.log('addChampionships failed...');
}
);
};
//initial refresh championships when building the controller
$scope.refreshChampionships();
}]);
mod.controller('RaceCtrl', ['$scope', '$routeParams', 'RaceService', function($scope, $routeParams, RaceService) {
//fetch all drives for the current race
var drives = [];
//set route params in scope variables
$scope.currentChampionship = $routeParams.idchampionship;
$scope.currentTier = $routeParams.tier;
$scope.currentPoule = $routeParams.poule;
$scope.currentRace = $routeParams.idrace;
//possible lapamounts
$scope.lapamounts = [1,2,3,4,5];
//initial selection
$scope.selectedLapamount = 2;
//start of the lap, if necessary
$scope.newMeasurementStarttime = new Date();
//set the starting values for the relative start applet
$scope.relativeRaceStartAddMinutes = 0;
$scope.relativeRaceStartAddSeconds = 0;
$scope.relativeRaceTime = new Date();
$scope.rightnow = new Date();
$scope.measurementValidityChanged = function(measurement) {
console.log('measurement changed: '+measurement.valid);
RaceService.updateMeasurement(measurement).
success(function(data, status, headers, config) {
console.log('updateMeasurement successful...');
if(data['ok']==true) {
} else {
}
}).
error(function(data, status, headers, config) {
console.log('updateMeasurement failed...');
});;
};
$scope.relativeTimestampChanged = function() {
var start = $scope.relativeRaceStart;
var plusMinutes = $scope.relativeRaceStartAddMinutes;
var plusSeconds = $scope.relativeRaceStartAddSeconds;
//using moments to calculate offsets and relative shit
var newMoment = moment(start);
newMoment.add(plusMinutes, 'minutes').add(plusSeconds, 'seconds');
//set it!
$scope.relativeRaceTime = newMoment;
};
$scope.setSelectedLapamount = function(amount) {
$scope.selectedLapamount = amount;
};
$scope.refreshDrives = function() {
console.log('doing refreshRaces');
RaceService.getDrives($scope.currentRace).
success(function(data, status, headers, config) {
console.log('refreshDrives successful...');
console.log(data);
if(data['ok']==true) {
$scope.drives = data.drives;
} else {
}
}).
error(function(data, status, headers, config) {
console.log('refreshDrives failed...');
});
};
$scope.refreshMeasurements = function() {
console.log('doing refreshMeasurements');
RaceService.getMeasurements($scope.currentRace).
success(function(data, status, headers, config) {
console.log('refreshMeasurements successful...');
console.log(data);
if(data['ok']==true) {
$scope.measurements = data.measurements;
} else {
}
}).
error(function(data, status, headers, config) {
console.log('refreshMeasurements failed...');
});
};
$scope.refreshTeam = function() {
RaceService.getTeam($scope.currentRace).
success(function(data, status, headers, config) {
console.log('refreshTeam successful...');
console.log(data);
if(data['ok']==true) {
$scope.team = data.team;
if(data.team.drivers.length > 0) {
$scope.selectedDriver = data.team.drivers[0];
}
} else {
}
}).
error(function(data, status, headers, config) {
console.log('refreshDrivers failed...');
});
};
$scope.setSelectedDriver = function(driver) {
$scope.selectedDriver = driver;
};
$scope.addDrive = function() {
RaceService.addDrive($scope.currentRace, $scope.selectedDriver, $scope.selectedLapamount).
success(function(data, status, headers, config) {
console.log('addDrive successful...');
console.log(data);
if(data['ok']==true) {
$scope.refreshDrives();
} else {
}
}).
error(function(data, status, headers, config) {
console.log('addDrive failed...');
});
};
$scope.deleteDrive = function(drivenr, idrace) {
RaceService.deleteDrive(drivenr, idrace).
success(function(data, status, headers, config) {
console.log('deleteDrive successful...');
console.log(data);
if(data['ok']==true) {
$scope.refreshDrives();
} else {
}
}).
error(function(data, status, headers, config) {
console.log('deleteDrive failed...');
});
};
$scope.addMeasurementNow = function() {
RaceService.addMeasurementNow($scope.currentRace).
success(function(data, status, headers, config) {
console.log('addMeasurementNow successful...');
console.log(data);
if(data['ok']==true) {
$scope.refreshMeasurements();
} else {
}
}).
error(function(data, status, headers, config) {
console.log('addMeasurementNow failed...');
});
};
$scope.addMeasurementRelative = function() {
RaceService.addMeasurementRelative($scope.currentRace, $scope.relativeRaceTime).
success(function(data, status, headers, config) {
console.log('addMeasurementRelative successful...');
console.log(data);
if(data['ok']==true) {
$scope.refreshMeasurements();
} else {
}
}).
error(function(data, status, headers, config) {
console.log('addMeasurementRelative failed...');
});
};
$scope.deleteMeasurement = function(idmeasurement) {
RaceService.deleteMeasurement(idmeasurement).
success(function(data, status, headers, config) {
console.log('deleteMeasurement successful...');
console.log(data);
if(data['ok']==true) {
$scope.refreshMeasurements();
} else {
}
}).
error(function(data, status, headers, config) {
console.log('deleteMeasurement failed...');
});
};
$scope.refreshMeasurements();
$scope.refreshDrives();
$scope.refreshTeam();
}]);
mod.controller('ChampionshipCtrl', ['$scope', '$routeParams', 'PouleService', 'TeamService', function($scope, $routeParams, PouleService, TeamService) {
//fetch all poules for the given championship
var poules = [];
//set scope vars
$scope.currentChampionship = $routeParams.idchampionship;
$scope.refreshPoules = function() {
var idchampionship = $routeParams.idchampionship;
console.log('doing refreshPoules');
PouleService.getPoules(idchampionship).
success(function(data, status, headers, config) {
console.log('refreshPoules successful...');
console.log(data);
if(data['ok']==true) {
$scope.poules = data.poules;
} else {
}
}).
error(function(data, status, headers, config) {
console.log('refreshChampionships failed...');
});
};
$scope.addPoule = function(newpoule) {
PouleService.addPoule(newpoule, $scope.currentChampionship).
success(function(data, status, headers, config) {
console.log('addPoule successful...');
console.log(data);
if(data['ok']==true) {
$scope.refreshPoules();
newpoule.tier = "";
newpoule.poule = "";
}
}).
error(function(data, status, headers, config) {
console.log('addPoule failed...');
}
);;
};
//do initial refresh of poules
$scope.refreshPoules();
}]);
mod.controller('PouleCtrl', ['$scope', '$routeParams', 'PouleService', 'TeamService', 'MainService', function($scope, $routeParams, PouleService, TeamService, MainService) {
$scope.currentChampionship = $routeParams.idchampionship;
$scope.currentTier = $routeParams.tier;
$scope.currentPoule = $routeParams.poule;
$scope.activeIdRace = null;
$scope.addTeamToPoule = function(team) {
var idchampionship = $routeParams.idchampionship;
var tier = $routeParams.tier;
var poule = $routeParams.poule;
PouleService.addTeam(team, idchampionship, tier, poule).
success(function(data, status, headers, config) {
console.log('added team to poule...');
if(data['ok']==true) {
$scope.refreshTeamsForPoule();
$scope.refreshTeamsAvailableForPoule();
}
}).
error(function(data, status, headers, config) {
console.log('addPoule failed...');
}
);
};
$scope.removeTeamFromPoule = function(team) {
var idchampionship = $routeParams.idchampionship;
var tier = $routeParams.tier;
var poule = $routeParams.poule;
PouleService.removeTeam(team, idchampionship, tier, poule).
success(function(data, status, headers, config) {
console.log('added team to poule...');
if(data['ok']==true) {
$scope.refreshTeamsForPoule();
$scope.refreshTeamsAvailableForPoule();
}
}).
error(function(data, status, headers, config) {
console.log('addPoule failed...');
}
);
};
$scope.refreshTeamsForPoule = function() {
var idchampionship = $routeParams.idchampionship;
var tier = $routeParams.tier;
var poule = $routeParams.poule;
console.log('TeamCtrl refreshing team in poule');
TeamService.getTeamsInPoule(idchampionship, tier, poule).
success(function(data, status, headers, config) {
console.log('getTeamsInPoule successful...');
console.log(data);
if(data['ok']==true) {
$scope.teamsInPoule = data.teams;
//set first to be selected
$scope.selectedTeam = data.teams[0];
} else {
console.log('call returned but wasnt ok: '+data['error']);
}
}).
error(function(data, status, headers, config) {
console.log('getTeamsInPoule failed...');
});
};
$scope.refreshTeamsAvailableForPoule = function() {
var idchampionship = $routeParams.idchampionship;
var tier = $routeParams.tier;
var poule = $routeParams.poule;
console.log('TeamCtrl refreshing team in poule');
TeamService.getTeamsAvailableForPoule(idchampionship, tier, poule).
success(function(data, status, headers, config) {
console.log('refreshTeamsAvailableForPoule successful...');
console.log(data);
if(data['ok']==true) {
$scope.teamsAvailableForPoule = data.teams;
} else {
console.log('call returned but wasnt ok: '+data['error']);
}
}).
error(function(data, status, headers, config) {
console.log('getTeamsInPoule failed...');
});
};
$scope.setSelectedTeam = function(team) {
$scope.selectedTeam = team;
console.log('team is selected: '+team);
};
$scope.addRace = function() {
var idchampionship = $routeParams.idchampionship;
var tier = $routeParams.tier;
var poule = $routeParams.poule;
var selectedTeam = $scope.selectedTeam;
PouleService.addRace(idchampionship, tier, poule, selectedTeam.idteam).
success(function(data, status, headers, config) {
console.log('addRace successful...');
console.log(data);
if(data['ok']==true) {
$scope.refreshRaces();
}
}).
error(function(data, status, headers, config) {
console.log('addRace failed...');
}
);;
};
$scope.deleteRace = function(race) {
var idchampionship = $routeParams.idchampionship;
var tier = $routeParams.tier;
var poule = $routeParams.poule;
console.log('PouleCtrl deleting race');
PouleService.deleteRace(idchampionship, tier, poule, race.idrace).
success(function(data, status, headers, config) {
console.log('deleteRace call successful...');
console.log(data);
if(data['ok']==true) {
$scope.refreshRaces();
} else {
console.log('deleteRace call returned but wasnt ok: '+data['error']);
}
}).
error(function(data, status, headers, config) {
console.log('deleteRace failed...');
});
};
$scope.refreshRaces = function() {
var idchampionship = $routeParams.idchampionship;
var tier = $routeParams.tier;
var poule = $routeParams.poule;
console.log('PouleCtrl refreshing races');
PouleService.getRacesInPoule(idchampionship, tier, poule).
success(function(data, status, headers, config) {
console.log('getRacesInPoule successful...');
console.log(data);
if(data['ok']==true) {
$scope.racesInPoule = data.races;
} else {
console.log('getRacesInPoule call returned but wasnt ok: '+data['error']);
}
}).
error(function(data, status, headers, config) {
console.log('getTeamsInPoule failed...');
});
};
$scope.toggleActiveRace = function(race) {
var idchampionship = $routeParams.idchampionship;
var tier = $routeParams.tier;
var poule = $routeParams.poule;
MainService.toggleActiveRace(race.idrace, tier, poule, idchampionship);
};
$scope.getActiveRace = function() {
return MainService.getActiveRace();
};
$scope.addRaceForAllteams = function() {
var idchampionship = $routeParams.idchampionship;
var tier = $routeParams.tier;
var poule = $routeParams.poule;
for(var i = 0; i<$scope.teamsInPoule.length; i++) {
PouleService.addRace(idchampionship, tier, poule, $scope.teamsInPoule[i].idteam).
success(function(data, status, headers, config) {
$scope.refreshRaces();;
});
// this.addRace = function(idchampionship, tier, poule, idteam) {
}
};
$scope.$on('activeIdRaceChanged', function(event, message) {
//the raceid changed!
$scope.activeIdRace = message;
});
$scope.refreshTeamsForPoule();
$scope.refreshTeamsAvailableForPoule();
$scope.refreshRaces();
}]);
mod.controller('TeamCtrl', ['$scope', '$routeParams', 'TeamService', function($scope, $routeParams, TeamService) {
//fetch all poules for the given championship
var teams = [];
$scope.currentChampionship = $routeParams.idchampionship;
$scope.refreshTeams = function() {
console.log('doing refreshTeams');
var idchampionship = $routeParams.idchampionship;
TeamService.getTeams(idchampionship).
success(function(data, status, headers, config) {
console.log('refreshTeams successful...');
console.log(data);
if(data['ok']==true) {
$scope.teams = data.teams;
} else {
}
}).
error(function(data, status, headers, config) {
console.log('refreshTeams failed...');
});
};
$scope.addTeam = function(newteam) {
var idchampionship = $routeParams.idchampionship;
console.log('controller adding team... '+idchampionship);
TeamService.addTeam(newteam, idchampionship).
success(function(data, status, headers, config) {
console.log(data);
if(data['ok']==true) {
$scope.refreshTeams();
newteam.name = "";
}
}).
error(function(data, status, headers, config) {
console.log('addTeam failed...');
}
);;
};
$scope.deleteTeam = function(team) {
var idchampionship = $routeParams.idchampionship;
console.log('controller deleting team...');
TeamService.deleteTeam(team, idchampionship).
success(function(data, status, headers, config) {
console.log(data);
if(data['ok']==true) {
$scope.refreshTeams();
}
}).
error(function(data, status, headers, config) {
console.log('deleteTeam failed...');
}
);
};
$scope.refreshTeams();
}]);
mod.controller('DriverCtrl', ['$scope', '$routeParams', 'DriverService', function($scope, $routeParams, DriverService) {
//fetch all drivers for the given team
var drivers = [];
$scope.currentTeam = $routeParams.idteam;
$scope.currentChampionship = $routeParams.idchampionship;
$scope.refreshDrivers = function() {
console.log('doing refreshTeams');
var idchampionship = $routeParams.idchampionship;
var idteam = $routeParams.idteam;
DriverService.getDrivers(idchampionship, idteam).
success(function(data, status, headers, config) {
console.log('refreshDrivers successful...');
console.log(data);
if(data['ok']==true) {
$scope.drivers = data.drivers;
} else {
}
}).
error(function(data, status, headers, config) {
console.log('refreshTeams failed...');
});
};
$scope.addDriver = function(newdriver) {
var idchampionship = $routeParams.idchampionship;
var idteam = $routeParams.idteam;
console.log('controller adding driver... '+idchampionship+' '+idteam);
DriverService.addDriver(newdriver, idchampionship, idteam).
success(function(data, status, headers, config) {
console.log(data);
if(data['ok']==true) {
$scope.refreshDrivers();
newdriver.name = "";
}
}).
error(function(data, status, headers, config) {
console.log('addDriver failed...');
}
);;
};
$scope.deleteDriver = function(driver) {
var idchampionship = $routeParams.idchampionship;
var idteam = $routeParams.idteam;
console.log('controller deleting driver... ');
console.log(driver);
DriverService.deleteDriver(driver).
success(function(data, status, headers, config) {
console.log(data);
if(data['ok']==true) {
$scope.refreshDrivers();
}
}).
error(function(data, status, headers, config) {
console.log('deleteDriver failed...');
}
);
};
$scope.refreshDrivers();
}]);
mod.controller('NavCtrl', ['$scope', '$location', 'MainService', function ($scope, $location, MainService) {
$scope.loggedIn = function() {
return LoginService.isLoggedIn();
};
$scope.getActiveCss = function (path) {
// console.log('get activecss: '+path+' '+$location.path());
var ok = $location.path().indexOf(path) != -1;
return {
active: ok
};
};
$scope.logout = function() {
LoginService.setLoggedIn(false);
};
$scope.getActiveRace = function() {
return MainService.getActiveRace();
};
}]);
mod.controller('TimepickerDemoCtrl', ['$scope', function ($scope, $log) {
console.log('TIMERPICKERDEMOCTRL DOES SOMETHING');
$scope.mytime = new Date();
$scope.hstep = 1;
$scope.mstep = 15;
$scope.options = {
hstep: [1, 2, 3],
mstep: [1, 5, 10, 15, 25, 30]
};
$scope.ismeridian = true;
$scope.toggleMode = function() {
$scope.ismeridian = ! $scope.ismeridian;
};
$scope.update = function() {
var d = new Date();
d.setHours( 14 );
d.setMinutes( 0 );
$scope.mytime = d;
};
$scope.changed = function () {
$log.log('Time changed to: ' + $scope.mytime);
};
$scope.clear = function() {
$scope.mytime = null;
};
}]);