Advertisement
yerzhik

ScheduleView

Oct 7th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MyScheduleView;
  2. FriendScheduleView;
  3. SelectedScheduleView;
  4. var current_selected_friend_team = null;
  5. var _is_shown = false;
  6. preloaderGlobal; // globalVariables.js
  7.  
  8. var ScheduleView = Backbone.View.extend({
  9.     initialize: function() {
  10.         this.$el.hide();
  11.         this.$el.append($("<div>").attr("id", "schedule_myschedule_view"));
  12.         this.$el.append($("<div>").attr("id", "schedule_friendschedules_view"));
  13.         this.$el.append($("<div>").attr("id", "schedule_selectedschedule_view"));
  14.  
  15.         this.mySchedule = new MyScheduleView({
  16.             el: "#schedule_myschedule_view"
  17.         });
  18.         this.friendSchedule = new FriendScheduleView({
  19.             el: "#schedule_friendschedules_view"
  20.         });
  21.         this.selectedSchedule = new SelectedScheduleView({
  22.             el: "#schedule_selectedschedule_view"
  23.         });
  24.         this.render();
  25.     },
  26.     mySchedule: null,
  27.     friendSchedule: null,
  28.     selectedSchedule: null,
  29.     events: {
  30.         "click #schedule_myschedule": "myScheduleClick",
  31.         "click #schedule_friendschedule": "friendScheduleClick",
  32.         "click .schedule_boxbutton": "boxScoreClick"
  33.     },
  34.     shownItem: 0, // zero for my schedule 1 for friend schedule
  35.     shownItemForFriend: 0, // 0 for friend list, 1 for selected friend
  36.     /* event functions */
  37.     myScheduleClick: function(e) {
  38.         $(e.target).addClass('active').siblings('.active').removeClass('active');
  39.         if (this.shownItem != 0) {
  40.             this.mySchedule.show();
  41.             this.friendSchedule.hide();
  42.             this.selectedSchedule.hide();
  43.             this.shownItem = 0;
  44.         }
  45.         fecthTeam();
  46.     },
  47.     friendScheduleClick: function(e) {
  48.         $(e.target).addClass('active').siblings('.active').removeClass('active');
  49.         if (this.shownItem == 0) {
  50.             if (this.shownItemForFriend == 0) {
  51.                 this.friendSchedule.show();
  52.                 this.mySchedule.hide();
  53.                 this.selectedSchedule.hide();
  54.             } else {
  55.                 if (current_selected_friend_team)
  56.                     this.selectedSchedule.show(current_selected_friend_team);
  57.                 this.mySchedule.hide();
  58.                 this.friendSchedule.hide();
  59.             }
  60.         } else {
  61.             this.friendSchedule.show();
  62.             this.mySchedule.hide();
  63.             this.selectedSchedule.hide();
  64.             this.shownItemForFriend = 0;
  65.             _is_shown = false;
  66.         }
  67.         this.shownItem = 1;
  68.     },
  69.  
  70.     friendSelected: function(teamId) {
  71.         this.friendSchedule.hide();
  72.         this.selectedSchedule.show(teamId);
  73.         this.shownItemForFriend = 1;
  74.     },
  75.  
  76.     isShown: 0,
  77.     removeScroll: function() {
  78.         var scroll = $('#schedule_pagescroll');
  79.         var jspData = scroll.data('jsp');
  80.         if (jspData != null && jspData != undefined) {
  81.             jspData.destroy();
  82.         }
  83.     },
  84.     reinitialiseScroll: function() {
  85.         var scroll = $('#schedule_pagescroll');
  86.         var jspData = scroll.data('jsp');
  87.         if (jspData != null && jspData != undefined) {
  88.             scroll.data('jsp').reinitialise();
  89.         }
  90.     },
  91.     addScroll: function() {
  92.         var scroll = $('#schedule_pagescroll');
  93.         scroll.jScrollPane();
  94.     },
  95.     show: function() {
  96.         if (this.isShown == 0) {
  97.             commonShow(this.$el);
  98.             this.friendSchedule.hide();
  99.             this.selectedSchedule.hide();
  100.             this.mySchedule.show();
  101.             this.isShown = 1;
  102.             this.reinitialiseScroll();
  103.         }
  104.     },
  105.  
  106.     hide: function() {
  107.         if (this.isShown == 1) {
  108.             commonHide(this.$el);
  109.             this.isShown = 0;
  110.         }
  111.     },
  112.  
  113.     generateImage: function(url, custom_css) {
  114.         var img = '<img style=' + custom_css + ' src="' + url + '">';
  115.         return img;
  116.     },
  117.  
  118.     loadDefaultLogo: function(logo_file_name, custom_css) {
  119.         custom_css = custom_css || '"height: 20px; width: 20px; float: left; position: absolute; left: 10px;"';
  120.         var url = 'images/store_logos/' + logo_file_name.toLowerCase();
  121.         return this.generateImage(url, custom_css);
  122.     },
  123.  
  124.     loadCustomLogo: function(logo1_file_name, logo2_file_name, logo3_file_name, type, custom_css) {
  125.         custom_css = custom_css || '"height: 8px; width: 9px; float: left; position: absolute; left: 15px; top: 8px;"';
  126.         var common_url = 'images/customlogo/' + 'typeface_' + type + '/';
  127.         var innerImg = '';
  128.         if (logo1_file_name)
  129.             var base_path = 'images/customlogo/' + 'bases' + '/';
  130.         var url = base_path + logo1_file_name.toLowerCase();
  131.         innerImg = innerImg + this.generateImage(url, '"height: 20px; width: 25px; float: left; position: absolute; left: 8px; top: 2px"');
  132.         if (logo2_file_name)
  133.             var url = common_url + logo2_file_name.toLowerCase();
  134.         innerImg = innerImg + this.generateImage(url, custom_css);
  135.         if (logo3_file_name)
  136.             var url = common_url + logo3_file_name.toLowerCase();
  137.         innerImg = innerImg + this.generateImage(url, custom_css);
  138.         return innerImg;
  139.     },
  140.  
  141.     displayTeamLogo: function(team, class_css) {
  142.         var myTeamClass = "";
  143.         if (team.id === app.myTeam.get("id")) {
  144.             team = app.myTeam.toJSON();
  145.             myTeamClass = "myTeam";
  146.         }
  147.  
  148.         class_css = class_css || "";
  149.         var result = '<div id="small-team-logo-div" class = "' + myTeamClass + " " + class_css + '">';
  150.         if (!team.isLogoCustom) {
  151.             result = result + this.loadDefaultLogo(team.logo.image);
  152.         } else {
  153.             result = result + this.loadCustomLogo(team.logoBase, team.logoLayer2, team.logoLayer3, team.logoType)
  154.         }
  155.         result = result + '</div>';
  156.         return result;
  157.     },
  158.  
  159.     displayTeamFlag: function(team, custom_size) {
  160.         custom_size = custom_size || 'height="18px" width="18px"';
  161.         var url = 'images/flags/' + team.get("country").get("flag");
  162.         var img = '<img ' + custom_size + ' src="' + url + '">';
  163.         return img;
  164.     },
  165.  
  166.     addGame: function(gameInfo) {
  167.         gameInfo['team1Logo'] = this.displayTeamLogo(gameInfo.team1.attributes, 'team1-css');
  168.         gameInfo['team2Logo'] = this.displayTeamLogo(gameInfo.team2.attributes, 'team2-css');
  169.         gameInfo['team1Flag'] = this.displayTeamFlag(gameInfo.team1);
  170.         gameInfo['team2Flag'] = this.displayTeamFlag(gameInfo.team2);
  171.         if (gameInfo.team1.get('id') === app.myTeamId) {
  172.             gameInfo.team1.myTeam = 'myTeam';
  173.             gameInfo.team2.myTeam = '';
  174.         }
  175.         if (gameInfo.team2.get('id') === app.myTeamId) {
  176.             gameInfo.team2.myTeam = 'myTeam';
  177.             gameInfo.team1.myTeam = '';
  178.         }
  179.         gameInfo.displayLogo = this.getGameDisplayLogo(gameInfo);
  180.         var sHtml = window.JST["schedule/gamelist/inner_row"](gameInfo);
  181.         var tableRow = $("<tr>").append(sHtml);
  182.         tableRow.data("id", gameInfo.id);
  183.         return tableRow;
  184.     },
  185.  
  186.     getGameDisplayLogo: function(game) {
  187.         var logo = "",
  188.             isPlayOff = game.isPlayOff,
  189.             isInternationalGame = game.isInternationalGame,
  190.             playOffCode = game.playOffCode;
  191.         switch (true) {
  192.             //Training
  193.             case (isPlayOff == false && isInternationalGame == false && playOffCode != null):
  194.                 logo = "logo_training.png";
  195.                 break;
  196.                 //League - regular season
  197.             case (isPlayOff == false && isInternationalGame == false):
  198.                 logo = "logo_league.png";
  199.                 break;
  200.                 //League - playoffs
  201.             case (isPlayOff == true && isInternationalGame == false):
  202.                 logo = "logo_playoffs.png";
  203.                 break;
  204.                 //Intl Play (WBT) - group stage
  205.             case (isPlayOff == false && isInternationalGame == true):
  206.                 logo = "logo_intlplay.png";
  207.                 break;
  208.                 //Intl Play (WBT) - playoffs
  209.             case (isPlayOff == true && isInternationalGame == true):
  210.                 logo = "logo_intlplay_playoffs.png";
  211.                 break;
  212.         }
  213.         return logo;
  214.     },
  215.  
  216.     teamInfoFirstClick: function(event) {
  217.         var teamId = event.currentTarget.id;
  218.         if (teamId == app.myTeamId) {
  219.             bottomPanel.frontOffice();
  220.         } else {
  221.             teamInfo.show(teamId);
  222.         }
  223.     },
  224.  
  225.     teamInfoSecondClick: function(event) {
  226.         var teamId = event.currentTarget.id;
  227.         if (teamId == app.myTeamId) {
  228.             bottomPanel.frontOffice();
  229.         } else {
  230.             preloaderGlobal.show();
  231.             setTimeout(function() {
  232.                 teamInfo.show(teamId);
  233.             }, 0);
  234.         }
  235.     },
  236.  
  237.     boxScoreClick: function(event) {
  238.         preloaderGlobal.show();
  239.         boxScoreView.show(event.currentTarget.id);
  240.         preloaderGlobal.hide();
  241.     },
  242.  
  243.     competitionLogoClick: function(event) {
  244.         bottomPanel.competitions();
  245.     },
  246.  
  247.     setCommonLinks: function() {
  248.         $(".myschedule_first_team").click(this.teamInfoFirstClick);
  249.         $(".myschedule_second_team").click(this.teamInfoSecondClick);
  250.         $(".schedule_boxcomplogo").click(this.competitionLogoClick);
  251.     },
  252.  
  253.     render: function() {
  254.  
  255.     },
  256.  
  257.     getGroupGameInfoBaseDate: function(models) {
  258.         var groupGames = new Array();
  259.         var gameInfo = "";
  260.         for (var ind = 0; ind < models.length; ind++) {
  261.             gameInfo = models[ind].attributes;
  262.             var datetime = gameInfo.date;
  263.             var date = new Date(datetime);
  264.  
  265.             var sDate = date.format("dd mmm");
  266.             var time = date.format("H:MM");
  267.             models[ind].set({
  268.                 "time": time,
  269.                 "sDate": sDate
  270.             });
  271.             if (!(groupGames[sDate] instanceof Array)) {
  272.                 groupGames[sDate] = new Array();
  273.             }
  274.             groupGames[sDate].push(models[ind]);
  275.         }
  276.         return groupGames;
  277.     }
  278. });
  279.  
  280. var MyScheduleView = ScheduleView.extend({
  281.     initialize: function() {
  282.         this.$el.attr("id", "schedule_pagescroll");
  283.         this.$el.hide();
  284. //        $("#middle_schedule").append(this.$el);
  285. //        this.$el.append($("<div>").attr("id", "schedule_pagescroll"));
  286.         this.collection = app.myTeam.schedule;
  287.         console.log(this.collection.toJSON());
  288.         this.addScroll();
  289.         this.generateGroupGamesAndRender();
  290.     },
  291.     el: "",
  292.  
  293.     isShown: 0,
  294.  
  295.     generateGroupGamesAndRender: function() {
  296.         this.groupGames = this.getGroupGameInfoBaseDate(this.collection.models);
  297.         this.render();
  298.     },
  299.     show: function() {
  300.         if (this.isShown == 0) {
  301.             this.$el.show();
  302.             this.isShown = 1;
  303.         }
  304.  
  305.         if (app.myTeam.get("logo") != null)
  306.             $("#schedule_pagescroll").find("div.myTeam").find("img").attr("src", "images/store_logos/" + app.myTeam.get("logo").image);
  307.         else {
  308.             var team = app.myTeam.toJSON();
  309.             var newCustomLogoInnerHtml = this.loadCustomLogo(team.logoBase, team.logoLayer2, team.logoLayer3, team.logoType);
  310.             $("#schedule_pagescroll").find("div.myTeam").html(newCustomLogoInnerHtml);
  311.         }
  312.     },
  313.  
  314.     hide: function() {
  315.         if (this.isShown == 1) {
  316.             this.$el.hide();
  317.             this.isShown = 0;
  318.         }
  319.     },
  320.  
  321.     addNewTable: function(date, gameInfos, number) {
  322.         var scroll = $('#schedule_pagescroll');
  323.         var pane = scroll.data('jsp');
  324.         var content = pane.getContentPane();
  325.  
  326.         if (gameInfos.length > 0) {
  327.             var gameDate = new Date(gameInfos[0].attributes.date);
  328.             var dateString = gameDate.format('dd mmm');
  329.         } else {
  330.             var dateString = date;
  331.         }
  332.         content.append($("<table>").attr("class", "schedule_table").attr("cellspacing", "0")
  333.             .append($("<thead>")
  334.                 .append($("<tr>")
  335.                     .append($("<td>").attr("width", "668px").text(dateString))))
  336.             .append($("<tbody>").attr("id", "schedule_mytable_tbody" + number.toString())));
  337.         var gameInfo = "";
  338.         for (var ind = 0; ind < gameInfos.length; ++ind) {
  339.             gameInfo = gameInfos[ind].attributes;
  340.             gameInfo.dateInfo = new Date(gameInfo.date);
  341.             var element = this.addGame(gameInfo);
  342.             $("#schedule_mytable_tbody" + number).append(element);
  343.  
  344.         }
  345.  
  346.         this.setCommonLinks();
  347.     },
  348.  
  349.  
  350.     render: function() {
  351.         var scroll = $('#schedule_pagescroll');
  352.         var pane = scroll.data('jsp');
  353.         var content = pane.getContentPane();
  354.         content.children().remove();
  355.         var ind = 0;
  356.         for (var key in this.groupGames) {
  357.             this.addNewTable(key, this.groupGames[key], ind++);
  358.         }
  359.         pane.reinitialise();
  360.     }
  361. });
  362.  
  363. var FriendScheduleView = ScheduleView.extend({
  364.     initialize: function() {
  365.         this.$el.hide();
  366.         $("#middle_schedule").append(this.$el);
  367.         this.$el.append($("<div>").attr("id", "schedulepick_pagescroll"));
  368.         $("#schedulepick_pagescroll").append($("<table>").attr("cellspacing", "0")
  369.             .append($("<thead>")
  370.                 .append($("<tr>")
  371.                     .append($("<td>").attr({
  372.                         "id": "schedulepick_pagetitle",
  373.                         "width": "650px"
  374.                     }).text("FRIEND LIST"))))
  375.             .append($("<tbody>").attr("id", "schedulepick_tbody")));
  376.         this.collection = app.myManager.get('friends');
  377.         this.render();
  378.     },
  379.     events: {},
  380.  
  381.     isShown: 0,
  382.  
  383.     show: function() {
  384.         if (this.isShown == 0) {
  385.             this.$el.show();
  386.             this.isShown = 1;
  387.             this.$el.find('#schedulepick_tbody').jScrollPane();
  388.         }
  389.     },
  390.  
  391.     hide: function() {
  392.         if (this.isShown == 1) {
  393.             this.$el.hide();
  394.             this.isShown = 0;
  395.         }
  396.     },
  397.  
  398.     displayTeamLogo: function(team, custom_class) {
  399.         console.log("BBBB displayTeamLogo 325");
  400.         if (team.id === app.myTeam.get("id")) {
  401.             team = app.myTeam.toJSON();
  402.         }
  403.  
  404.         custom_class = custom_class || "";
  405.         var result = '<div id="small-team-logo-div" class = ' + custom_class + '>';
  406.         if (!team.isLogoCustom) {
  407.             result = result + this.loadDefaultLogo(team.logo.image);
  408.         } else {
  409.             result = result + this.loadCustomLogo(team.logoBase, team.logoLayer2, team.logoLayer3, team.logoType)
  410.         }
  411.         result = result + '</div>';
  412.         return result;
  413.     },
  414.  
  415.     addFriend: function(friendInfo) {
  416.         //Currently hard code for get logo of team, it must be changed by API
  417.         //TODO Need to fix return data in app.myManager.get('friends')
  418.         var team = app.teams.getTeam(friendInfo.teamID);
  419.         team.globalLoadLogo('schedule-team-logo-friend', '100px', '20px', true);
  420.         friendInfo.teamLogo = team.get("displayLogo");
  421.         var sHtml = JST["schedule/friendlist/row"](friendInfo);
  422.         $("#schedulepick_tbody").append(sHtml);
  423.     },
  424.  
  425.     render: function() {
  426.         $("#schedulepick_tbody").children().remove();
  427.  
  428.         for (var ind = 0; ind < this.collection.models.length; ++ind) {
  429.             if (this.collection.models[ind].get("appUser")) {
  430.                 this.addFriend(this.collection.models[ind].attributes);
  431.             }
  432.         }
  433.  
  434.         $("#schedulepick_tbody tr:odd").addClass("schedule_table_odd");
  435.         $("#schedulepick_tbody tr:even").addClass("schedule_table_even");
  436.  
  437.         $("#schedulepick_tbody tr").off("click");
  438.         $("#schedulepick_tbody tr").click(function(event) {
  439.             current_selected_friend_team = event.currentTarget.id;
  440.             scheduleView.friendSelected(event.currentTarget.id);
  441.         });
  442.     }
  443. });
  444.  
  445.  
  446. var SelectedScheduleView = ScheduleView.extend({
  447.     initialize: function() {
  448.         this.$el.hide();
  449.         this.$el.append($("<span>").attr("id", "schedulefriend_top"));
  450.         this.$el.append($("<div>").attr("id", "schedulefriend_pagescroll"));
  451.     },
  452.  
  453.     isShown: 0,
  454.  
  455.     show: function(teamId) {
  456.         if (_is_shown == false) {
  457.             this.loadData(teamId);
  458.             this.render();
  459.             this.$el.show();
  460.             this.isShown = 1;
  461.             _is_shown = true;
  462.             $('#schedulefriend_pagescroll').jScrollPane({
  463.                 autoReinitialise: true
  464.             });
  465.         } else {
  466.             this.$el.show();
  467.             this.isShown = 1;
  468.             _is_shown = true;
  469.         }
  470.     },
  471.  
  472.     loadData: function(teamId) {
  473.         var selected_friend_schedule = new App.FriendSchedule({
  474.             id: teamId
  475.         });
  476.         selected_friend_schedule.fetch({
  477.             async: false
  478.         });
  479.         var groupGames = new Array();
  480.         var gameInfo = null;
  481.         $.each(selected_friend_schedule.attributes, function(key, value) {
  482.             if (value.date) {
  483.                 gameInfo = value;
  484.                 var datetime = gameInfo.date;
  485.                 var date = new Date(datetime);
  486.                 var sDate = date.format("dd mmm");
  487.                 var time = date.format("H:MM");
  488.                 gameInfo.time = time;
  489.                 gameInfo.sDate = sDate;
  490.                 if (!(groupGames[sDate] instanceof Array)) {
  491.                     groupGames[sDate] = new Array();
  492.                 }
  493.                 groupGames[sDate].push(gameInfo);
  494.             }
  495.         });
  496.         this.groupGames = groupGames;
  497.     },
  498.  
  499.     hide: function() {
  500.         if (this.isShown == 1) {
  501.             this.$el.hide();
  502.             this.isShown = 0;
  503.         }
  504.     },
  505.  
  506.     displayTeamFlag: function(team, custom_size) {
  507.         custom_size = custom_size || 'height="18px" width="18px"';
  508.         var url = 'images/flags/' + team.country.flag;
  509.         var img = '<img ' + custom_size + ' src="' + url + '">';
  510.         return img;
  511.     },
  512.  
  513.     addGame: function(gameInfo) {
  514.         gameInfo['team1Logo'] = this.displayTeamLogo(gameInfo.team1, 'team1-css');
  515.         gameInfo['team2Logo'] = this.displayTeamLogo(gameInfo.team2, 'team2-css');
  516.         gameInfo['team1Flag'] = this.displayTeamFlag(gameInfo.team1);
  517.         gameInfo['team2Flag'] = this.displayTeamFlag(gameInfo.team2);
  518.         if (gameInfo.team1.id == current_selected_friend_team) {
  519.             gameInfo.team1.myTeam = 'myTeam';
  520.             gameInfo.team2.myTeam = '';
  521.         }
  522.         if (gameInfo.team2.id == current_selected_friend_team) {
  523.             gameInfo.team2.myTeam = 'myTeam';
  524.             gameInfo.team1.myTeam = '';
  525.         }
  526.         gameInfo.displayLogo = this.getGameDisplayLogo(gameInfo);
  527.         var sHtml = window.JST["schedule/gamelist/friend_inner_row"](gameInfo);
  528.         var tableRow = $("<tr>").append(sHtml);
  529.         tableRow.data("id", gameInfo.id);
  530.         return tableRow;
  531.     },
  532.  
  533.     addTable: function(date, gameInfos, number, selector) {
  534.         $(selector).append($("<table>").attr("class", "schedule_table").attr("cellspacing", "0")
  535.             .append($("<thead>")
  536.                 .append($("<tr>")
  537.                     .append($("<td>").attr("width", "668px").text(date))))
  538.             .append($("<tbody>").attr("id", "schedfriend_table_tbody" + number.toString())));
  539.  
  540.         var gameInfo = "";
  541.         for (var ind = 0; ind < gameInfos.length; ++ind) {
  542.             gameInfo = gameInfos[ind];
  543.             gameInfo.dateInfo = new Date(gameInfo.date);
  544.             var element = this.addGame(gameInfo);
  545.             $("#schedfriend_table_tbody" + number).append(element);
  546.         }
  547.  
  548.         this.setCommonLinks();
  549.     },
  550.  
  551.     render: function() {
  552.         $("#schedulefriend_top").text("MY FRIEND'S SCHEDULE");
  553.         var selector = "";
  554.         if ($("#schedulefriend_pagescroll .jspContainer .jspPane").length == 0) {
  555.             $("#schedulefriend_pagescroll").children().remove();
  556.             selector = "#schedulefriend_pagescroll";
  557.         } else {
  558.             $("#schedulefriend_pagescroll .jspContainer .jspPane").children().remove();
  559.             selector = "#schedulefriend_pagescroll .jspContainer .jspPane"
  560.         }
  561.         var ind = 0;
  562.         for (var key in this.groupGames) {
  563.             this.addTable(key, this.groupGames[key], ind++, selector);
  564.         }
  565.     }
  566. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement