Select Git revision
controller.js
controller.js 4.87 KiB
/**
* Created by jeremyguiselin on 25/09/2016.
*/
(function (angular) {
"use strict";
/**
* @ngdoc controller
* @name FootballController
*
* @description
* Controller for the football page.
*
* @ngInject
*/
function FootballController(
$ionicHistory,
$scope,
$http,
$q,
$ionicLoading,
constantConfig
) {
/**
Static variables
**/
var deregistrationCallbackList = [];
var self = this;
var uuid = window.cordova ? ionic.Platform.device().uuid : '7f4a6a40e5c87157';
/**
End Controller variables
**/
/**
Scope variables
**/
$scope.pictureUrl = constantConfig.imgUrl;
$scope.locale = window.navigator.language.split('-')[0];
$scope.selected = [];
$scope.price = 0;
$scope.predictionsNumber = 0;
/**
End Scope variables
**/
/**
Scope functions
**/
$scope.isBought = function (league) {
return league.device_status === 'unlock';
}
$scope.getLink = function (league) {
if (league.device_status === 'unlock') {
return 'details({leagueId:' + league.id + '})';
}
return '-';
}
$scope.isSelected = function (league) {
return $scope.selected.indexOf(league) !== -1;
}
$scope.select = function (league) {
if (!$scope.isBought(league.id)) {
if ($scope.selected.indexOf(league) !== -1) {
$scope.selected.splice($scope.selected.indexOf(league), 1);
$scope.predictionsNumber -= league.predictions_number;
} else {
$scope.selected.push(league);
$scope.predictionsNumber += league.predictions_number;
}
}
}
$scope.getPredictionClass = function (prediction, value) {
var percentages = [prediction.prediction_win_first, prediction.prediction_win_second, prediction.prediction_draw];
percentages.sort();
if (percentages.indexOf(value) === 2) {
return "main-prediction";
} else if (percentages.indexOf(value) === 1) {
return "second-prediction";
}
return '';
}
$scope.getTick = function (prediction) {
var percentages = [
prediction.prediction_win_first,
prediction.prediction_win_second,
prediction.prediction_draw
];
var ticks = [
'red-cross',
'orange-tick',
'green-tick'
];
percentages.sort();
var predictionWin = null;
if (prediction.winner) {
if (prediction.winner === -1) {
predictionWin = prediction.prediction_draw;
} else {
predictionWin = prediction.prediction_win_second;
if (prediction.first_team.id === prediction.winner) {
predictionWin = prediction.prediction_win_first;
}
}
return ticks[percentages.indexOf(predictionWin)];
}
return null;
}
$scope.parseDate = function (date) {
var locale = window.navigator.language.split('-')[0];
date = date.split(' ')[0].split('-');
if (locale === "fr") {
return date[2] + '/' + date[1] + '/' + date[0].substr(-2);
} else {
return date[1] + '/' + date[2] + '/' + date[0].substr(-2);
}
}
/**
End Scope functions
**/
/**
Controller functions
**/
this.parseScore = function (prediction) {
if (prediction.score) {
var score = prediction.score;
var scores = score.split('-');
prediction.score = scores;
}
}
/**
End Controller functions
**/
/**
Scope events
**/
deregistrationCallbackList.push(
$scope.$on('$ionicView.afterEnter', function(){
$ionicHistory.clearHistory();
})
);
$scope.$on('$destroy', function(){
angular.forEach(deregistrationCallbackList, function(deregistrationCallback){
deregistrationCallback();
});
deregistrationCallbackList = null;
});
$scope.$on("$ionicView.beforeEnter", function(event, data){
$ionicLoading.show({
template: '<ion-spinner icon="ripple" class="spinner-assertive"></ion-spinner>',
animation: 'fade-in',
showBackdrop: true,
});
var freePredictionsPromise = $http.get(constantConfig.apiUrl + 'predictions/football/free');
var leaguesPromise = $http.get(constantConfig.apiUrl + uuid + '/leagues/football');
$q.all([freePredictionsPromise, leaguesPromise]).then(function (data) {
var freePredictions = [];
data[0]['data'].forEach(function (prediction) {
self.parseScore(prediction);
freePredictions.push(prediction);
});
$scope.freePredictions = freePredictions;
$scope.leagues = data[1]['data'];
}).finally(function () {
$ionicLoading.hide();
});
});
/**
End Scope event
**/
}
angular.module('starter')
.controller('FootballController', FootballController);
})(angular);