Blame view

resources/js/livescores/frLsMatchGroupDirective.js 6.73 KB
e77200db5   nologostudio.ru   Initial commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  import frLsMatchGroup from '../../../public/views/ng/common/frLsMatchGroup.html';
  
  export default function () {
      return {
          restrict: 'A',
          template: frLsMatchGroup,
          scope: true,
          controller: ['$scope', '$attrs', function ($scope, $attrs) {
              var stagesByParent = $scope.stagesByParent;
              var stagesById = $scope.stagesById;
              var competition = $scope.competition;
              var matchesByStage = $scope.matchesByStage;
  
              console.time('build');
              (function () {
                  var groups = [];
                  var matchGroupsIndex = {};
  
                  function walkTree(stage) {
                      // Case: Competition
                      // - Create a match group, indexed by competition (stage) id.
                      // - We don't use the name of competition, we use name of the season for the match group.
                      if (stage.type == 'competition') {
                          matchGroupsIndex[stage.id] = {
                              name: '',
                              url: stage.meta.url,
                              subgroups: [],
                          };
  
                          groups.push(matchGroupsIndex[stage.id]);
                      }
  
                      // Case: Season
                      // - Modify the name of the match group by the name of the season.
                      // Note: `season` must always be an immidiate child of `competition`.
                      // Note: This implementation assumes there's only one season per competition.
                      if (stage.type == 'season') {
                          matchGroupsIndex[stage.parent].name = stage.name;
                          matchGroupsIndex[stage.id] = matchGroupsIndex[stage.parent];
                      }
  
                      // Case: Tournament Proper in normal league type.
                      // - Make it a subgroup of main group.
                      // - Don't set name of the subgroup.
                      if (stage.type == 'stage' && stage.parent && stagesById[stage.parent].type == 'season' && !stageHasManyChildren(stage.parent) && !stageHasChildren(stage.id)) {
                          matchGroupsIndex[stage.parent].subgroups.push({
                              matches: matchesByStage[stage.id],
                          });
                      }
  
                      // Case: A stage that is a single immidiate child of a season and a parent to other stages.
                      // - Change the name of the match group to include the name of this stage.
                      if (stage.type == 'stage' && stagesById[stage.parent].type == 'season' && !stageHasManyChildren(stage.parent) && stageHasChildren(stage.id)) {
                          matchGroupsIndex[stage.parent].name += ' - ' + stage.name;
                          matchGroupsIndex[stage.id] = {
                              name: '',
                          };
                      }
  
                      // Case: A stage that is one of multiple immidiate children of a season and a parent to other stages.
                      // - Index this stage
                      if (stage.type == 'stage' && stagesById[stage.parent].type == 'season' && stageHasManyChildren(stage.parent) && stageHasChildren(stage.id)) {
                          matchGroupsIndex[stage.id] = {
                              name: stage.name,
                          };
                      }
  
                      // Case:  A stage that is a child of another stage and is a parent to other stages.
                      // - Index this stage.
                      // - Concat the name with the parent.
                      if (stage.type == 'stage' && stagesById[stage.parent].type != 'season' && stageHasChildren(stage.id)) {
                          matchGroupsIndex[stage.id] = {
                              name: matchGroupsIndex[stage.parent] && matchGroupsIndex[stage.parent].name ? matchGroupsIndex[stage.parent].name + ' - ' + stage.name : stage.name,
                          };
                      }
  
                      // Case: A leaf stage that is a child of another stage and is not a parent to any stages. Also not a group.
                      // - Add the stage as a subgroup to the main group.
                      // - The name of the subgroup is a tree of stages before it, that are not in main group, plus it's own name.
                      if (stage.type == 'stage' && stagesById[stage.parent].type != 'season' && !stageHasChildren(stage.id)) {
                          groups[0].subgroups.push({
                              name: matchGroupsIndex[stage.parent].name ? matchGroupsIndex[stage.parent].name + ' - ' + stage.name : stage.name,
                              matches: matchesByStage[stage.id],
                          });
                      }
  
                      // Case: A stage that is a group stage, ie has children that are groups.
                      // - Concat the name with the parent.
                      // - Add all matches that bellong to the groups.
                      if (stageIsGroupStage(stage.id)) {
                          groups[0].subgroups.push({
                              name: stageHasManyChildren(stage.parent) ?
                                  (matchGroupsIndex[stage.parent].name ? matchGroupsIndex[stage.parent].name + ' - ' + stage.name : stage.name) : '',
                              matches: _(stagesByParent[stage.id]).reduce(function (matches, stage) {
                                  [].push.apply(matches, matchesByStage[stage.id]);
                                  return matches;
                              }, []),
                          });
                      }
  
                      // Case: Group
                      // - Precalculate group name and save it
                      if (stage.type == 'group' && stage.name) {
                          stage.groupName = stage.name.replace('Group ', '');
                      }
  
                      // Keep walking.
                      _(stagesByParent[stage.id]).each(walkTree);
                  }
  
                  function stageHasChildren(id) {
                      return stagesByParent[id] && stagesByParent[id].length > 0;
                  }
  
                  function stageHasManyChildren(id) {
                      return stagesByParent[id] && stagesByParent[id].length > 1;
                  }
  
                  function stageIsGroupStage(id) {
                      return stagesByParent[id] && stagesByParent[id][0].type == 'group';
                  }
  
                  walkTree(competition);
  
                  $scope.group = _(groups).uniq()[0];
  
                  _($scope.group.subgroups).each(function (group) {
                      group.matches = _(group.matches).sortBy(function (match) {
                          return match.datetime;
                      });
                  });
              }());
              console.timeEnd('build');
          }],
      };
  }