motmPoll.js 2.29 KB
import '../../vendor/jquery.scrollTo-min';

angular.module('footyroom')

.directive('motmPoll', function () {
    return {
        templateUrl: '/views/ng/motm/poll.html?1',
        scope: {
            matchId: '=',
        },
        controllerAs: 'ctrl',
        bindToController: true,
        controller: ['$http', function ($http) {
            var formationsEl = $('[motm-poll-formations]');
            var ctrl = formationsEl.controller('formations').motm = this;

            ctrl.poll = DataStore.manOfTheMatchPoll || {};

            ctrl.hasVotes = function () {
                return ctrl.poll.totalVotes > 0;
            };

            ctrl.vote = function () {
                $('body').scrollTo(formationsEl, 300);

                ctrl.voting = true;
            };

            ctrl.close = function () {
                ctrl.voting = false;
            };

            ctrl.clickPlayer = function (player) {
                $http.post('/vote', {
                    results: true,
                    limit: 5,
                    pollRef: 'motm-' + ctrl.matchId,
                    choices: [{
                        value: player.participant.name,
                        valueType: 'participant',
                        valueRef: player.participant.enetId,
                    }],
                })

                .success(function (response) {
                    ctrl.poll = response;
                });

                ctrl.close();
            };
        }],
    };
})

.directive('motmPollFormations', function () {
    return {
        compile: function (tElement) {
            var el = tElement.find('.formations');

            el.attr(
                'ng-class',
                '[{"formations-motm": ctrl.motm.voting},' + el.attr('ng-class') + ']'
            );

            el.prepend(
                '<div class="motm-formations-header" ng-if="ctrl.motm.voting">' +
					'<span class="icn-close pointer" ng-click="ctrl.motm.close()"></span>' +
					'<div>' +
						'Cast your vote for Man of The Match by choosing any player below.' +
					'</div>' +
				'</div>'
            );

            el.find('.player--field, .substitute, .squad-player')
            .attr('ng-click', 'ctrl.motm.voting && ctrl.motm.clickPlayer(player)')
            .addClass('motm-eligible');
        },
    };
});