Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.42 KB | None | 0 0
  1. ;(function($) {
  2. 'use strict';
  3.  
  4. //competition variables
  5. var competition1;
  6. var competition2;
  7. var competition3;
  8. var comp = [];
  9. var teamstest = [];
  10. var teams = [];
  11.  
  12. var initiateCalendar = function(fixtures) {
  13. $(document).ready(function() {
  14. console.log(fixtures);
  15. $('#calendar').fullCalendar({
  16. header: {
  17. left: 'prev,next,today',
  18. center: 'title',
  19. right: ''
  20. },
  21. defaultDate: '2015-08-22',
  22. navLinks: false, // can click day/week names to navigate views
  23. selectable: false,
  24. selectHelper: false,
  25.  
  26. eventClick: function(calEvent, jsEvent, view) {
  27. $('.home').css('float', 'left').css('position', 'relative').css('font-size', '2rem');
  28. $('.away').css('float', 'right').css('position', 'relative').css('font-size', '2rem');
  29. $('.title').css('font-weight','400').css('position', 'relative').css('font-size', '3rem');
  30. $('.title-home').css('float', 'left').css('left','25%');
  31. $('.title-away').css('float', 'right').css('right','25%');
  32. $('.id').css('display','none');
  33. $('#date').text(calEvent.date).css('bottom', '1rem');
  34. $('#leftHome').text(check(calEvent.homeName)).css('margin-top', '6rem').css('left', '5%').css('margin-bottom','2rem');
  35. $('#rightAway').text(check(calEvent.awayName)).css('margin-top', '6rem').css('right', '5%').css('margin-bottom','2rem');
  36. $("#imageHome").attr("src", calEvent.imageHome).css('float', 'left').css('left', '10%').css('position', 'relative');
  37. $("#imageAway").attr("src", calEvent.imageAway).css('float', 'right').css('right', '10%').css('position', 'relative');
  38. $('#result').text(calEvent.result).css('font-weight','700').css('text-align', 'center')
  39. .css('font-size', '3.2rem').css('position', 'absolute').css('left', '45%').css('right', '45%');
  40. $('#close').css('float','right').css('margin-top', '-4px').css('top', '0').css('right', '0').css('position', 'absolute');
  41. $('#modalContent').css('max-width','55rem').css('max-height', '30rem').css('height', '30rem');
  42. $('#myModal').css('display', 'block' ).css('max-height','100%');
  43. },
  44. select: function(start, end) {
  45. var title = prompt('Event Title:');
  46. var eventData;
  47. if (title) {
  48. eventData = {
  49. title: title,
  50. start: start,
  51. id: id
  52. };
  53. $('#calendar').fullCalendar('renderEvent', eventData, true); // stick? = true
  54. }
  55. $('#calendar').fullCalendar('unselect');
  56. },
  57. editable: false,
  58. eventLimit: true, // allow "more" link when too many events
  59.  
  60. events: fixtures
  61.  
  62. });
  63. });
  64. };
  65.  
  66. //This function is triggered on the selection of a different value in the #league_selector
  67. //this also applies the live filtering
  68. $('#league_selector').change(function() {
  69. if ($('#league_selector :selected').text() == "All") {
  70. $('.one').show();
  71. $('.two').show();
  72. $('.three').show();
  73. $('.fc-more').show();
  74. };
  75. if ($('#league_selector :selected').text() == "Bundesliga 1") {
  76. $('.one').show();
  77. $('.two').hide();
  78. $('.three').hide();
  79. $('.fc-more').hide();
  80. };
  81. if ($('#league_selector :selected').text() == "Bundesliga 2") {
  82. $('.one').hide()
  83. $('.two').show();
  84. $('.three').hide();
  85. $('.fc-more').hide();
  86. };
  87. if ($('#league_selector :selected').text() == "Bundesliga 3") {
  88. $('.one').hide();
  89. $('.two').hide();
  90. $('.three').show();
  91. $('.fc-more').hide();
  92. };
  93. });
  94.  
  95. //This function give the html elements an ID which is needed for live filtering
  96. var setFilterId = function() {
  97. $('#league_selector').css('padding','0.4rem').css('float','right').css('margin-right','20%');
  98. var htmlEventArray = $('.fc-event-container');
  99. var color;
  100. var childArray = [];
  101. var count = 0;
  102. for (var i = 0; i < htmlEventArray.length; i++) {
  103. childArray.push(htmlEventArray[i].children);
  104. count++;
  105. };
  106. for (var i = 0; i < childArray.length; i++) {
  107. var color = convertToHex($(childArray[i]).css('background-color'));
  108. if (color == '#cc00cc') {
  109. $(htmlEventArray[i]).addClass('one');
  110. }
  111. if (color == '#009999') {
  112. $(htmlEventArray[i]).addClass('two');
  113. }
  114. if (color == '#9933ff') {
  115. $(htmlEventArray[i]).addClass('three');
  116. }
  117. };
  118. };
  119.  
  120. //this function converts the RGB value of a color to a HEX value
  121. var convertToHex = function(colorval) {
  122. var color;
  123. var colorInHex;
  124. var rex = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
  125. delete(rex[0]);
  126. for (var i = 1; i <= 3; ++i) {
  127. rex[i] = parseInt(rex[i]).toString(16);
  128. if (rex[i].length == 1) rex[i] = '0' + rex[i];
  129. }
  130. color = '#' + rex.join('');
  131.  
  132. return color;
  133. }
  134.  
  135. //this function adds the data to a common array for events to be loaded in the calendar
  136. var addData = function(data) {
  137. for (var fixture in data) {
  138. var color;
  139. if (data[fixture].id == 394) {
  140. color = '#cc00cc';
  141. }
  142. if (data[fixture].id == 395) {
  143. color = '#009999';
  144. }
  145. if (data[fixture].id == 403) {
  146. color = '#9933ff';
  147. }
  148. comp.push({
  149. color: color,
  150. title: data[fixture].title,
  151. start: data[fixture].start,
  152. id: data[fixture].id,
  153. homeName: data[fixture].homeName,
  154. awayName: data[fixture].awayName,
  155. result: data[fixture].result,
  156. time: data[fixture].time,
  157. imageHome: data[fixture].homeImage,
  158. imageAway: data[fixture].awayImage
  159. });
  160. }
  161. };
  162.  
  163. //this returns the common array
  164. var getData = function(ok) {
  165. return comp;
  166. };
  167.  
  168. //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
  169. var filterData = function(fixtures, id) {
  170. var matchesArray = [];
  171. var i;
  172. for (i = 0; i < fixtures.count; i++) {
  173. matchesArray.push({
  174. id: id,
  175. title: check(fixtures.fixtures[i].homeTeamName) + " Vs. " + check(fixtures.fixtures[i].awayTeamName),
  176. start: fixtures.fixtures[i].date.substring(0,10),
  177. homeName: fixtures.fixtures[i].homeTeamName,
  178. awayName: fixtures.fixtures[i].awayTeamName,
  179. result: fixtures.fixtures[i].result.goalsHomeTeam +" - " + fixtures.fixtures[i].result.goalsAwayTeam,
  180. time: fixtures.fixtures[i].date.substring(11,16),
  181. homeImage: '',
  182. awayImage: ''
  183. });
  184. }
  185. return matchesArray;
  186. };
  187.  
  188. //this function corrects the team name, sometimes a faulty teamname is acquired which starts with '1. ', this part needs to be sliced off
  189. var check = function(string) {
  190. if (string.charAt(0) == '1') {
  191. string = string.slice(3, string.length-1);
  192. }
  193. return string;
  194. };
  195.  
  196.  
  197.  
  198. $(document).ready(function () {
  199. // On ready the document will use AJAX to get the event values
  200. // there are six requests, 3 for the the three leagues and another
  201. // 3 for the images of each team. I tried to place the code under here
  202. // in a separate method but this did not work for some reason, I think
  203. // because it had no access to the arrays.
  204.  
  205.  
  206. $.ajax({
  207. headers: { 'X-Auth-Token': 'ef19f6ab395040a2b85eaaa259d05da7' },
  208. url: 'http://api.football-data.org/v1/competitions/394/teams',
  209. dataType: 'json',
  210. type: 'GET',
  211. }).done(function(response) {
  212. //this code will put the imagelink in a array with teams
  213. for (var team in response.teams) {
  214. teams.push({
  215. teamName: check(response.teams[team].name),
  216. imageLink: response.teams[team].crestUrl
  217. });
  218. };
  219. });
  220.  
  221. $.ajax({
  222. headers: { 'X-Auth-Token': 'ef19f6ab395040a2b85eaaa259d05da7' },
  223. url: 'http://api.football-data.org/v1/competitions/395/teams',
  224. dataType: 'json',
  225. type: 'GET',
  226. }).done(function(response) {
  227. for (var team in response.teams) {
  228. teams.push({
  229. teamName: check(response.teams[team].name),
  230. imageLink: response.teams[team].crestUrl
  231. });
  232. };
  233. });
  234.  
  235. $.ajax({
  236. headers: { 'X-Auth-Token': 'ef19f6ab395040a2b85eaaa259d05da7' },
  237. url: 'http://api.football-data.org/v1/competitions/403/teams',
  238. dataType: 'json',
  239. type: 'GET',
  240. }).done(function(response) {
  241. for (var team in response.teams) {
  242. teams.push({
  243. teamName: check(response.teams[team].name),
  244. imageLink: response.teams[team].crestUrl
  245. });
  246. };
  247. });
  248.  
  249. $.ajax({
  250. headers: { 'X-Auth-Token': 'ef19f6ab395040a2b85eaaa259d05da7' },
  251. url: 'http://api.football-data.org/v1/competitions/394/fixtures',
  252. dataType: 'json',
  253. type: 'GET',
  254. }).done(function(response) {
  255. var matches = filterData(response, 394);
  256. //this code will assign the imagelinks from the teamsarray to the matches
  257. for (var match in matches) {
  258.  
  259. for (var team in teams) {
  260. if (matches[match].homeName === check(teams[team].teamName)) {
  261. matches[match].homeImage = teams[team].imageLink;
  262. };
  263. };
  264.  
  265. for (var team in teams) {
  266. if (matches[match].awayName == check(teams[team].teamName)) {
  267. matches[match].awayImage = teams[team].imageLink;
  268. };
  269. };
  270. };
  271. addData(matches);
  272. });
  273.  
  274. $.ajax({
  275. headers: { 'X-Auth-Token': 'ef19f6ab395040a2b85eaaa259d05da7' },
  276. url: 'http://api.football-data.org/v1/competitions/395/fixtures',
  277. dataType: 'json',
  278. type: 'GET',
  279. }).done(function(response) {
  280. var matches = filterData(response, 395);
  281. for (var match in matches) {
  282.  
  283. for (var team in teams) {
  284. if (matches[match].homeName === check(teams[team].teamName)) {
  285. matches[match].homeImage = teams[team].imageLink;
  286. };
  287. };
  288.  
  289. for (var team in teams) {
  290. if (matches[match].awayName == check(teams[team].teamName)) {
  291. matches[match].awayImage = teams[team].imageLink;
  292. };
  293. };
  294. };
  295. addData(matches);
  296. });
  297.  
  298.  
  299. $.ajax({
  300. headers: { 'X-Auth-Token': 'ef19f6ab395040a2b85eaaa259d05da7' },
  301. url: 'http://api.football-data.org/v1/competitions/403/fixtures',
  302. dataType: 'json',
  303. type: 'GET',
  304. }).done(function(response) {
  305. var matches = filterData(response, 403);
  306. for (var match in matches) {
  307.  
  308. for (var team in teams) {
  309. if (matches[match].homeName === check(teams[team].teamName)) {
  310. matches[match].homeImage = teams[team].imageLink;
  311. };
  312. };
  313.  
  314. for (var team in teams) {
  315. if (matches[match].awayName == check(teams[team].teamName)) {
  316. matches[match].awayImage = teams[team].imageLink;
  317. };
  318. };
  319. };
  320.  
  321. //add final data, initiate calendar and set the filter ID's
  322. addData(matches);
  323. initiateCalendar(getData());
  324. setFilterId();
  325. });
  326. });
  327.  
  328.  
  329. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement