'use strict'; /* Controllers */ var mod = angular.module('bananaraceApp.controllers', ['bananaraceApp.services']); mod.controller('ChampionshipCtrl', ['$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.setSelectedChampionship = function(championship) { $scope.selectedChampionship = championship; console.log('selected: '+championship.name); }; $scope.isSelectedChampionship = function(championship) { return championship == $scope.selectedChampionship; }; $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('PouleCtrl', ['$scope', '$routeParams', 'PouleService', function($scope, $routeParams, PouleService) { //fetch all poules for the given championship var poules = []; $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) { var idchampionship = $routeParams.idchampionship; PouleService.addPoule(newpoule, idchampionship). 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...'); } );; }; $scope.refreshPoules(); }]); mod.controller('TeamCtrl', ['$scope', '$routeParams', 'TeamService', function($scope, $routeParams, TeamService) { //fetch all poules for the given championship var teams = []; $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; TeamService.addTeam(newteam, idchampionship). success(function(data, status, headers, config) { console.log('addTeam successful...'); console.log(data); if(data['ok']==true) { $scope.refreshTeams(); newteam.name = ""; } }). error(function(data, status, headers, config) { console.log('addTeam failed...'); } );; }; $scope.refreshTeams(); }]); // STOPWATCH CONTROLLER mod.controller('StopwatchCtrl', ['$scope', 'StopwatchService', 'TemplateBackendService', 'hotkeys', function($scope, StopwatchService, TemplateBackendService, hotkeys) { //set the interval in ms to 0ms $scope.intervalValue = StopwatchService.getValue(); //set the current templates: this can be refreshed when necessary. initialise with first call. $scope.templates = refreshTemplates(); //keep image of the current lefas here... update when necessary! $scope.currentTemplateItems = StopwatchService.getCurrentTemplateItems(); $scope.templateItemIndex = StopwatchService.currentlyRunningTemplateItemIndex(); //get the current time var stopwatchUpdateTimer; hotkeys.add({ combo: 'space', description: 'Tap to function... press space!', callback: function(event, hotkey) { console.log('pressed space'); $scope.tap(); event.preventDefault(); } }); //keep track of leafs //leaf1 - leaf2 - leaf3 - ... //overall timer //leaf timer //events (taps) //start sequence //end leaf1 (add timing) //end leaf2 (add timing) //final leaf done? end sequence -> save timings $scope.getCurrentStopwatchValue = function() { return StopwatchService.getValue(); }; $scope.readyForTapping = function() { var currentTemplate = StopwatchService.getActiveTemplate(); var ready = currentTemplate != null; return ready; }; $scope.getActiveTemplate = function() { return StopwatchService.getActiveTemplate(); } $scope.tap = function() { if(StopwatchService.isRunning()===false) { //start refreshing timer! stopwatchUpdateTimer = setInterval( function () { fireStopwatchTimer(); }, 10); } //now tap the thing StopwatchService.tap(); $scope.templateItemIndex = StopwatchService.currentlyRunningTemplateItemIndex(); //if, after the tap, the timer isn't running: we stopped! stop listening if(StopwatchService.isRunning()===false) { console.log('clearing timeout!'); clearTimeout(stopwatchUpdateTimer); //save the timings!? //make assoc array for the timings var currentTemplateItems = StopwatchService.getCurrentTemplateItems(); var currentTemplateid = StopwatchService.getActiveTemplate().idtemplate; //just push them all to the database: everything is filled in //save it to db TemplateBackendService.saveTiming(currentTemplateid, currentTemplateItems). success(function(data, status, headers, config) { console.log('saveTiming call successfull...'); if(data['ok']==true) { $scope.refreshTimings(); } else { console.log('timing wasnt saved'); } console.log(data); }). error(function(data, status, headers, config) { console.log('saveTiming call failed...'); console.log(data); }); }; }; $scope.cancel = function() { StopwatchService.cancel(); clearTimeout(stopwatchUpdateTimer); }; $scope.isTimerRunning = function() { return StopwatchService.isRunning(); }; $scope.refreshTimings = function() { console.log('HI refreshing timings'); TemplateBackendService.getTimings(StopwatchService.getActiveTemplate().idtemplate) .success(function(data, status, headers, config) { console.log('getTimings call successful...'); console.log(data); if(data['ok']==true) { //create header array for the table var currentTemplateItems = StopwatchService.getCurrentTemplateItems(); $scope.timingheaders = []; for(var i = 0; i0) { $scope.setActiveTemplate($scope.templates[0]); } } else { //not ok... do we explicitly call logout? //TODO call logout console.log('refresh templates went bad: '+data.error); } }). error(function(data, status, headers, config) { // called asynchronously if an error occurs // or server returns response with an error status. console.log('refresh templates ERROR'); console.log(data); }); } }]); mod.controller('TimerTemplateCtrl', ['$scope', 'TemplateBackendService', function($scope, TemplateBackendService) { //init the list $scope.timerTemplateList = []; $scope.newpauselength = 1000; //some vars var maxTreeDepth = 3; //init the template with default name $scope.templateitems = []; $scope.templatename = "defaultname"; $scope.removeItem = function(item) { $scope.templateitems.splice($scope.templateitems.indexOf(item),1); }; $scope.addItem = function() { $scope.templateitems.push(null); }; $scope.toggle = function(scope) { scope.toggle(); }; $scope.saveTemplate = function() { console.log('saving template'); console.log($scope.templatename); console.log($scope.templateitems); //save a new one! TemplateBackendService.saveTemplate($scope.templatename, $scope.templateitems). success(function(data, status, headers, config) { console.log("got this data"); console.log(data); console.log(data); if(data.ok) { //save succeeded console.log('save ok'); } else { //not ok... do we explicitly call logout? //TODO call logout console.log('save bad'); } }). error(function(data, status, headers, config) { // called asynchronously if an error occurs // or server returns response with an error status. console.log('save template ERROR'); console.log(data); }) ; }; }]); mod.controller('NavCtrl', ['$scope', 'LoginService', '$location', function ($scope, LoginService, $location) { console.log('navctrller knows: '+LoginService.isLoggedIn()); $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); }; }]); mod.controller('LoginCtrl', ['$scope','LoginService', '$window', 'md5', '$location', function($scope, LoginService, $window, md5, $location) { $scope.loginErrorred = false; $scope.loginError = "blablabla"; $scope.loggedIn = function() { return LoginService.isLoggedIn(); } $scope.loggedInUsername = function() { var u = LoginService.getLoggedInUsername(); // console.log('loggedinusername requested: '+u); return u; } $scope.doLogin = function () { console.log($scope.auth); LoginService.login($scope.auth.username, md5.createHash($scope.auth.password)). success(function(data, status, headers, config) { // this callback will be called asynchronously // when the response is available console.log('login call came back OK'); console.log(data); if(data.ok) { //login succeeded $scope.loginErrorred = false; LoginService.setLoggedIn(true); LoginService.setLoggedInUsername(data.user.username); $location.path('/') } else { //didn't login, show error $scope.loginErrorred = true; $scope.loginError = "Couldn't login..."; //logout $scope.logout(); } //redirect to main }). error(function(data, status, headers, config) { // called asynchronously if an error occurs // or server returns response with an error status. console.log('login ERROR'); console.log(data); }) ; }; $scope.logout = function() { LoginService.logout(). success(function(data, status, headers, config) { // this callback will be called asynchronously // when the response is available console.log('logout call came back OK'); console.log(data); if(data.ok) { //login succeeded LoginService.setLoggedIn(false); LoginService.setLoggedInUsername(null); } else { //not ok... do we explicitly call logout? //TODO call logout } //redirect to main $location.path('/') }). error(function(data, status, headers, config) { // called asynchronously if an error occurs // or server returns response with an error status. console.log('logout ERROR'); console.log(data); }) ; }; }]); mod.controller('RegisterCtrl', ['$scope', 'LoginService', 'RegisterService', 'md5', '$location', function($scope, LoginService, RegisterService, md5, $location) { $scope.registerErrorred = false; $scope.registerError = "blablabla"; $scope.doRegister = function () { console.log('auth obj'); console.log($scope.auth); //reset the error... $scope.registerErrorred = false; RegisterService.register($scope.auth.username, $scope.auth.email, md5.createHash($scope.auth.password)) // .then( // function (data, status) { // console.log('register came back'); // console.log(data); // console.log(status); // if (data.data.status != 200) { // //$window.alert("Fout: "+data.data.errors[0]); // console.log('got a loginservice error, register failed!'); // } // else { // console.log('register came backl with this data...'); // console.log(data.data); // //$window.alert("GELUKT: "+data.data.user.username); // LoginService.setLoggedIn(data.data.user != null); // } // } // ) .success(function(data, status, headers, config) { // this callback will be called asynchronously // when the response is available console.log('register returned successfully'); console.log(data); console.log(status); if(data['ok']) { console.log('logged in true :)'); //set us to be logged in LoginService.setLoggedIn(true); LoginService.setLoggedInUsername(data.newuser.username); $location.path('/') } else { console.log('logged in false :('); } }). error(function(data, status, headers, config) { // called asynchronously if an error occurs // or server returns response with an error status. console.log('register failed'); console.log(data); console.log(status); if(status==400) { $scope.registerError = data.error.message; $scope.registerErrorred = true; } // $scope.registerError = status. }); }; }]); mod.controller('MembersCtrl', ['$scope','LoginService', '$window', function($scope, $window) { }]); mod.controller('MemberCtrl', ['$scope', '$routeParams', function($scope, $routeParams) { console.log('member ctrl knows: '+$routeParams.id); $scope.memberid = $routeParams.id; }]); mod.controller('NewsCtrl', ['$scope', 'NewsService', 'LoginService', function($scope, NewsService, LoginService) { NewsService.fetchNews().then( function (data, status) { $scope.newsItems = data.data.news; } ); $scope.isLoggedIn = function() { return LoginService.isLoggedIn(); } $scope.submitNews = function(newNewsTitle, newNewsBody) { NewsService.submitNews(newNewsTitle, newNewsBody).then( function(data, status) { console.log("submitnews has returned!"); console.log(data); console.log(data.data.news); console.log(data.data.errors); //check if we are still logged in (login error might indicate we have to login again) if(data.data.error!=null && data.data.error.nr == 2) { console.log('we got a login error! setting the login service to loggedin = false'); LoginService.setLoggedIn(false); return; } //add the result to the news items (so we don't have to reload the page) $scope.newsItems.push(data.data.news); //empty the input fields $scope.newNewsBody = ""; $scope.newNewsTitle = ""; } ); } $scope.saveNewsItemText = function(newsItemImage) { NewsService.saveNewsItemText(newsItemImage).then( function(data, status) { console.log("save newsitemtext has returned!"); console.log(data); console.log(data.data.news); console.log(data.data.errors); //check if we are still logged in (login error might indicate we have to login again) if(data.data.error!=null && data.data.error.nr == 2) { console.log('we got a login error! setting the login service to loggedin = false'); LoginService.setLoggedIn(false); return; } //add the result to the news items (so we don't have to reload the page) $scope.newsItems.push(data.data.news); //empty the input fields $scope.newNewsBody = ""; $scope.newNewsTitle = ""; } ); } $scope.removeNewsItem = function(item) { //look for the item to remove var toremove = -1; for(var i = 0; i<$scope.newsItems.length; i++) { if($scope.newsItems[i].idnews == item.idnews) { toremove = i; break; } } console.log('got index: '+toremove); if(toremove>=0) { $scope.newsItems.splice(toremove,1); } console.log('got to remove: '); console.log(item); //let the backend know to remove it as well NewsService.removeNewsItem(item); } $scope.deleteNewsItemImage = function(newsItem, newsItemImage) { //delete in the backend NewsService.deleteNewsItemImage(newsItemImage); //remove it in the frontend var toremove = -1; for(var i = 0; i=0) { newsItem.images.splice(toremove,1); } } $scope.onFileSelect = function($files,item) { //$files: an array of files selected, each file has name, size, and type. for (var i = 0; i < $files.length; i++) { var file = $files[i]; //upload the file to the news service NewsService.addImageToNewsItem(item, file).progress(function(evt) { var percent = parseInt(100.0 * evt.loaded / evt.total); console.log('progress percent addImageToNewsItem (newsitem '+item.idnews+'): ' + percent); }).success(function(data, status, headers, config) { // file is uploaded successfully console.log('addimagetonewsitemservicelog: '+data); console.log(data.newsimage); if(data.error == null) { //file successfully uploaded! fetch the filename and add it to the list of items console.log('added news image item...'); console.log(data.data); console.log(data); item.images.push(data.newsimage); } else { console.log('addimagetonewsitem service errorred: '+data.error); } }); } }; }]);