frLsMatchDirective.js 2.77 KB
import frLsMatch from '../../../public/views/ng/common/frLsMatch.html';
import '../vendor/jquery.scrollTo-min';
import '../common/services/frMatchViewHelper';
import '../common/directives/formations';

angular.module('footyroom').requires.push('fr.matchViewHelper', 'fr.formations');

angular.module('footyroom').directive('frLsMatch', [function () {
    return {
        restrict: 'A',
        scope: true,
        template: frLsMatch,
        controller: ['$scope', '$element', 'matchViewHelper', '$rootScope', function ($scope, $element, matchViewHelper, $rootScope) {
            var minutesJob;

            $scope.incidentIdToName = $rootScope.incidentIdToName;

            init();

            function init() {
                // Normalize score.
                matchViewHelper.setScore($scope.match);

                // Update minutes automatically.
                minutesJob = matchViewHelper.startMinutesJob($scope.match);

                // Add watcher on match incidents so that we can process them everytime they change.
                $scope.$watchCollection('match.incidents', function (newValue, oldValue) {
                    if (newValue === oldValue) {
                        return;
                    }

                    matchViewHelper.setMainIncidents($scope.match);
                });

                // Process incidents for the first time.
                matchViewHelper.setMainIncidents($scope.match);
            }

            $scope.$on('$destroy', function () {
                matchViewHelper.stopMinutesJob(minutesJob);
            });

            $scope.onClick = function () {
                if ($scope.openedMatch === $scope.match) {
                    $scope.openedMatch = null;
                } else {
                    $scope.openedMatch = $scope.match;
                }
            };

            $scope.clickMatchCenter = function ($event) {
                if ($scope.isMatchCenterOpen()) {
                    $scope.joinMatch($scope.match.matchId);
                    $element.find('ls-join-match').litelay({ spinner: true });
                }
            };

            $scope.isMatchCenterOpen = function () {
                return (new Date($scope.match.datetime) - new Date() <= 1800000);
            };

            $scope.$watch('match._subscribed', function (newValue, oldValue) {
                if (newValue === true) {
                    $scope.incidents = $scope.match.incidents;

                    $element.find('ls-join-match').litelay({ off: true });

                    setTimeout(function () {
                        $('body').scrollTo($element, 400, { offset: -65 });
                    }, 10);
                } else {
                    $scope.incidents = $scope.mainIncidents;
                }
            });
        }],
    };
}]);