Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;(function($) {
- 'use strict';
- //competition variables
- var competition1;
- var competition2;
- var competition3;
- var comp = [];
- var teamstest = [];
- var teams = [];
- var initiateCalendar = function(fixtures) {
- $(document).ready(function() {
- console.log(fixtures);
- $('#calendar').fullCalendar({
- header: {
- left: 'prev,next,today',
- center: 'title',
- right: ''
- },
- defaultDate: '2015-08-22',
- navLinks: false, // can click day/week names to navigate views
- selectable: false,
- selectHelper: false,
- eventClick: function(calEvent, jsEvent, view) {
- $('.home').css('float', 'left').css('position', 'relative').css('font-size', '2rem');
- $('.away').css('float', 'right').css('position', 'relative').css('font-size', '2rem');
- $('.title').css('font-weight','400').css('position', 'relative').css('font-size', '3rem');
- $('.title-home').css('float', 'left').css('left','25%');
- $('.title-away').css('float', 'right').css('right','25%');
- $('.id').css('display','none');
- $('#date').text(calEvent.date).css('bottom', '1rem');
- $('#leftHome').text(check(calEvent.homeName)).css('margin-top', '6rem').css('left', '5%').css('margin-bottom','2rem');
- $('#rightAway').text(check(calEvent.awayName)).css('margin-top', '6rem').css('right', '5%').css('margin-bottom','2rem');
- $("#imageHome").attr("src", calEvent.imageHome).css('float', 'left').css('left', '10%').css('position', 'relative');
- $("#imageAway").attr("src", calEvent.imageAway).css('float', 'right').css('right', '10%').css('position', 'relative');
- $('#result').text(calEvent.result).css('font-weight','700').css('text-align', 'center')
- .css('font-size', '3.2rem').css('position', 'absolute').css('left', '45%').css('right', '45%');
- $('#close').css('float','right').css('margin-top', '-4px').css('top', '0').css('right', '0').css('position', 'absolute');
- $('#modalContent').css('max-width','55rem').css('max-height', '30rem').css('height', '30rem');
- $('#myModal').css('display', 'block' ).css('max-height','100%');
- },
- select: function(start, end) {
- var title = prompt('Event Title:');
- var eventData;
- if (title) {
- eventData = {
- title: title,
- start: start,
- id: id
- };
- $('#calendar').fullCalendar('renderEvent', eventData, true); // stick? = true
- }
- $('#calendar').fullCalendar('unselect');
- },
- editable: false,
- eventLimit: true, // allow "more" link when too many events
- events: fixtures
- });
- });
- };
- //This function is triggered on the selection of a different value in the #league_selector
- //this also applies the live filtering
- $('#league_selector').change(function() {
- if ($('#league_selector :selected').text() == "All") {
- $('.one').show();
- $('.two').show();
- $('.three').show();
- $('.fc-more').show();
- };
- if ($('#league_selector :selected').text() == "Bundesliga 1") {
- $('.one').show();
- $('.two').hide();
- $('.three').hide();
- $('.fc-more').hide();
- };
- if ($('#league_selector :selected').text() == "Bundesliga 2") {
- $('.one').hide()
- $('.two').show();
- $('.three').hide();
- $('.fc-more').hide();
- };
- if ($('#league_selector :selected').text() == "Bundesliga 3") {
- $('.one').hide();
- $('.two').hide();
- $('.three').show();
- $('.fc-more').hide();
- };
- });
- //This function give the html elements an ID which is needed for live filtering
- var setFilterId = function() {
- $('#league_selector').css('padding','0.4rem').css('float','right').css('margin-right','20%');
- var htmlEventArray = $('.fc-event-container');
- var color;
- var childArray = [];
- var count = 0;
- for (var i = 0; i < htmlEventArray.length; i++) {
- childArray.push(htmlEventArray[i].children);
- count++;
- };
- for (var i = 0; i < childArray.length; i++) {
- var color = convertToHex($(childArray[i]).css('background-color'));
- if (color == '#cc00cc') {
- $(htmlEventArray[i]).addClass('one');
- }
- if (color == '#009999') {
- $(htmlEventArray[i]).addClass('two');
- }
- if (color == '#9933ff') {
- $(htmlEventArray[i]).addClass('three');
- }
- };
- };
- //this function converts the RGB value of a color to a HEX value
- var convertToHex = function(colorval) {
- var color;
- var colorInHex;
- var rex = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
- delete(rex[0]);
- for (var i = 1; i <= 3; ++i) {
- rex[i] = parseInt(rex[i]).toString(16);
- if (rex[i].length == 1) rex[i] = '0' + rex[i];
- }
- color = '#' + rex.join('');
- return color;
- }
- //this function adds the data to a common array for events to be loaded in the calendar
- var addData = function(data) {
- for (var fixture in data) {
- var color;
- if (data[fixture].id == 394) {
- color = '#cc00cc';
- }
- if (data[fixture].id == 395) {
- color = '#009999';
- }
- if (data[fixture].id == 403) {
- color = '#9933ff';
- }
- comp.push({
- color: color,
- title: data[fixture].title,
- start: data[fixture].start,
- id: data[fixture].id,
- homeName: data[fixture].homeName,
- awayName: data[fixture].awayName,
- result: data[fixture].result,
- time: data[fixture].time,
- imageHome: data[fixture].homeImage,
- imageAway: data[fixture].awayImage
- });
- }
- };
- //this returns the common array
- var getData = function(ok) {
- return comp;
- };
- //this function will assing values and filter the freshly acquired data, the id will give the color to the event, one color for every league
- var filterData = function(fixtures, id) {
- var matchesArray = [];
- var i;
- for (i = 0; i < fixtures.count; i++) {
- matchesArray.push({
- id: id,
- title: check(fixtures.fixtures[i].homeTeamName) + " Vs. " + check(fixtures.fixtures[i].awayTeamName),
- start: fixtures.fixtures[i].date.substring(0,10),
- homeName: fixtures.fixtures[i].homeTeamName,
- awayName: fixtures.fixtures[i].awayTeamName,
- result: fixtures.fixtures[i].result.goalsHomeTeam +" - " + fixtures.fixtures[i].result.goalsAwayTeam,
- time: fixtures.fixtures[i].date.substring(11,16),
- homeImage: '',
- awayImage: ''
- });
- }
- return matchesArray;
- };
- //this function corrects the team name, sometimes a faulty teamname is acquired which starts with '1. ', this part needs to be sliced off
- var check = function(string) {
- if (string.charAt(0) == '1') {
- string = string.slice(3, string.length-1);
- }
- return string;
- };
- $(document).ready(function () {
- // On ready the document will use AJAX to get the event values
- // there are six requests, 3 for the the three leagues and another
- // 3 for the images of each team. I tried to place the code under here
- // in a separate method but this did not work for some reason, I think
- // because it had no access to the arrays.
- $.ajax({
- headers: { 'X-Auth-Token': 'ef19f6ab395040a2b85eaaa259d05da7' },
- url: 'http://api.football-data.org/v1/competitions/394/teams',
- dataType: 'json',
- type: 'GET',
- }).done(function(response) {
- //this code will put the imagelink in a array with teams
- for (var team in response.teams) {
- teams.push({
- teamName: check(response.teams[team].name),
- imageLink: response.teams[team].crestUrl
- });
- };
- });
- $.ajax({
- headers: { 'X-Auth-Token': 'ef19f6ab395040a2b85eaaa259d05da7' },
- url: 'http://api.football-data.org/v1/competitions/395/teams',
- dataType: 'json',
- type: 'GET',
- }).done(function(response) {
- for (var team in response.teams) {
- teams.push({
- teamName: check(response.teams[team].name),
- imageLink: response.teams[team].crestUrl
- });
- };
- });
- $.ajax({
- headers: { 'X-Auth-Token': 'ef19f6ab395040a2b85eaaa259d05da7' },
- url: 'http://api.football-data.org/v1/competitions/403/teams',
- dataType: 'json',
- type: 'GET',
- }).done(function(response) {
- for (var team in response.teams) {
- teams.push({
- teamName: check(response.teams[team].name),
- imageLink: response.teams[team].crestUrl
- });
- };
- });
- $.ajax({
- headers: { 'X-Auth-Token': 'ef19f6ab395040a2b85eaaa259d05da7' },
- url: 'http://api.football-data.org/v1/competitions/394/fixtures',
- dataType: 'json',
- type: 'GET',
- }).done(function(response) {
- var matches = filterData(response, 394);
- //this code will assign the imagelinks from the teamsarray to the matches
- for (var match in matches) {
- for (var team in teams) {
- if (matches[match].homeName === check(teams[team].teamName)) {
- matches[match].homeImage = teams[team].imageLink;
- };
- };
- for (var team in teams) {
- if (matches[match].awayName == check(teams[team].teamName)) {
- matches[match].awayImage = teams[team].imageLink;
- };
- };
- };
- addData(matches);
- });
- $.ajax({
- headers: { 'X-Auth-Token': 'ef19f6ab395040a2b85eaaa259d05da7' },
- url: 'http://api.football-data.org/v1/competitions/395/fixtures',
- dataType: 'json',
- type: 'GET',
- }).done(function(response) {
- var matches = filterData(response, 395);
- for (var match in matches) {
- for (var team in teams) {
- if (matches[match].homeName === check(teams[team].teamName)) {
- matches[match].homeImage = teams[team].imageLink;
- };
- };
- for (var team in teams) {
- if (matches[match].awayName == check(teams[team].teamName)) {
- matches[match].awayImage = teams[team].imageLink;
- };
- };
- };
- addData(matches);
- });
- $.ajax({
- headers: { 'X-Auth-Token': 'ef19f6ab395040a2b85eaaa259d05da7' },
- url: 'http://api.football-data.org/v1/competitions/403/fixtures',
- dataType: 'json',
- type: 'GET',
- }).done(function(response) {
- var matches = filterData(response, 403);
- for (var match in matches) {
- for (var team in teams) {
- if (matches[match].homeName === check(teams[team].teamName)) {
- matches[match].homeImage = teams[team].imageLink;
- };
- };
- for (var team in teams) {
- if (matches[match].awayName == check(teams[team].teamName)) {
- matches[match].awayImage = teams[team].imageLink;
- };
- };
- };
- //add final data, initiate calendar and set the filter ID's
- addData(matches);
- initiateCalendar(getData());
- setFilterId();
- });
- });
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement