'use strict'; /* Services */ var appServices = angular.module('bananaraceApp.services', []); appServices.service('StopwatchService', ['TemplateBackendService', function(TemplateBackendService) { //init the stopwatch value in ms var intervalMs = 0; var startMs = null; var running = false; var timings = null; //this is the currently tracked leaf var templateItemIndex = -1; //this is the starttime of the currently tracked leaf var leafStart = null; //the currently active template var activeTemplate = null; this.updateValue = function() { var currentMs = +new Date(); intervalMs = currentMs - startMs; } this.isRunning = function() { return running; } this.getValue = function() { if(running) { this.updateValue(); } return intervalMs; }; this.currentlyRunningTemplateItemIndex = function() { return templateItemIndex; } this.getCurrentTemplateItems = function() { if(activeTemplate != null && activeTemplate.templateitems != null) { return activeTemplate.templateitems; } else { return []; } } this.getActiveTemplate = function() { return activeTemplate; } this.setActiveTemplate = function(template) { console.log('setActiveTemplate called'); activeTemplate = template; console.log('active template: '+template.templatename); console.log(template); //now, set up the timer so we can process the active template //1) active template needs to be read from left to right, leaf to leaf //2) time each leaf: start , tap , tap , end > save timings to db //-- stop will break off the attempt //get the leafs of the active template console.log('got these active items: '); console.log(template.templateitems); } this.cancel = function() { running = false; templateItemIndex = -1; leafStart = 0; } this.tap = function() { //okay, if we are not running right now, initialise everything var stopRunningTimer = false; if(!running) { //start up the first leaf templateItemIndex = 0; leafStart = 0; running = true; startMs = +new Date(); } else { //already running! var temptime = this.getValue(); var leafTime = temptime-leafStart; activeTemplate.templateitems[templateItemIndex]['timing'] = leafTime; //check if we're at the end if(templateItemIndex==activeTemplate.templateitems.length-1) { //reached the end, stop everything console.log('got to the end...'); console.log('leaf '+activeTemplate.templateitems[templateItemIndex].templateitemname+' got: '+leafTime); var currentMs = +new Date(); intervalMs = currentMs - startMs; running = false; templateItemIndex = -1; stopRunningTimer = true; } else { //end the current leaf console.log('leaf '+activeTemplate.templateitems[templateItemIndex].templateitemname+' got: '+leafTime); //go to the next leaf templateItemIndex++; leafStart = temptime; } } return stopRunningTimer; } }]); appServices.service('TemplateBackendService', function($http) { this.saveTemplate = function(templateName, templateItems) { console.log('gonna save this'); console.log(templateName+' '+templateItems); return $http.post('server/savetemplate.php', {templateName:templateName, templateItems:templateItems}); }; this.getTemplates = function() { console.log('TemplateBackendService.getTemplates called'); return $http.post('server/gettemplates.php', {}); }; this.removeTemplate = function(template) { var idtemplate = template.idtemplate; // var templateid = template->{'$id'}; // console.log('going to remove: '+templateid); return $http.post('server/removetemplate.php', {idtemplate:idtemplate}); }; /** Templateid: dbid of the template. timings: assoc array with nodeid=>timing(ms) */ this.saveTiming = function(idtemplate, templateitems) { console.log('saving timing...'); console.log(idtemplate); console.log(templateitems); return $http.post('server/savetiming.php', {idtemplate:idtemplate,templateitems:templateitems}); }; /** Fetch an array with all the timings for the given templateid, sorted on insert date. */ this.getTimings = function(idtemplate) { return $http.post('server/gettimings.php', {idtemplate:idtemplate}); } this.removeTiming = function(idtimingrun) { return $http.post('server/removetiming.php', {idtimingrun:idtimingrun}); } }); appServices.service('TemplateCreationService', function() { }); appServices.service('RegisterService', function($http) { var url = "server/register.php"; //?email=emailtest&md5pass=passtest&username=usertest this.register = function (username, email, md5pass) { return $http.post(url, { username:username, md5pass:md5pass, email:email }); }; }); appServices.service('LoginService', function($http) { var url = "server/login.php"; // var loggedIn = $cookieStore.get('loggedIn'); var email = null; // console.log('cookiestore says loggedin is: '+loggedIn); this.login = function (username, md5password) { console.log('doing the login... '+username+' '+md5password); return $http.post(url, { md5password:md5password, username:username }); }; this.setLoggedInUsername = function(newusername) { console.log('setting loggedin username: '+newusername); $cookieStore.put('loggedInUsername', newusername); }; this.getLoggedInUsername = function() { return $cookieStore.get('loggedInUsername'); }; this.setLoggedIn = function(newValue) { console.log('set logged in : '+newValue); // loggedIn = newValue; $cookieStore.put('loggedIn', newValue); }; this.isLoggedIn = function() { return false; }; this.logout = function() { return $http.post("server/logout.php"); }; }); appServices.service('NewsService', function($http,$upload) { this.fetchNews = function() { return $http.post("phpservices/news.php"); }; this.submitNews = function(newNewsTitle, newNewsBody) { var newNews = { title:newNewsTitle, body:newNewsBody }; console.log("submitnewsing service: "+newNews); return $http.post("phpservices/addnews.php",newNews); }; this.removeNewsItem = function(newsItem) { var postObj = { idnews:newsItem.idnews }; $http.post("phpservices/removenews.php", postObj); }; this.saveNewsItemText = function(newsItemImage) { return $http.post("phpservices/savenewsimagetext.php", newsItemImage); }; this.deleteNewsItemImage = function(newsItemImage) { return $http.post("phpservices/removenewsitemimage.php", newsItemImage); }; });