repetition.js

angular.module('homepage').
directive('repetition', function () {
    return {
        controller: 'repetitionController',
        restrict: 'E',
        replace: true,
        // Variables within this scope will be submitted back to server
        scope: {
            repetition: '=',
            defaults: '='
        },
        transclude: true,
        templateUrl: 'components/common-components/repetition/repetition.html'
    }
}).
controller('repetitionController', function ($scope) {
    function Repetition() {
        return {
            repeats: '',
            chooseday: {
                type: '',
                days: []
            },
            choosemonth: {
                type: '',
                months: []
            },
            count: {
                nth: '',
                isMonthly: false
            }
        }
    }
    // Initialise defaults
    $scope.defaults = $scope.defaults || {};
    $scope.repetition = new Repetition();
    $scope.repetition.repeats = $scope.defaults.repeat || 'DALY';

    $scope.selectOption = function (code) {
        var repetition = new Repetition();

        switch(code) {
            // repeats
            case 'ONCE':
            case 'DALY':
            case 'WEEK':
            case 'ANLY': {
                repetition.repeats = code;
                break;
            }
            case 'MTLY': {
                repetition.repeats = code;
                repetition.choosemonth.type = 'ALL';
                break;   
            }
            // choose days
            case 'WKDY':
            case 'WKED':
            case 'HLDY': {
                repetition.repeats = 'WEEK';
                repetition.chooseday.type = code;
                break;
            }
            case 'SUN':
            case 'MON':
            case 'TUE':
            case 'WED':
            case 'THU':
            case 'FRI':
            case 'SAT': {
                repetition.repeats = 'WEEK';
                repetition.chooseday.type = 'CHSE';
                repetition.chooseday.days = $scope.repetition.chooseday.days;
                var index = $scope.repetition.chooseday.days.indexOf(code);
                if(index > -1) {
                    repetition.chooseday.days.splice(index, 1);   
                }
                else {
                    repetition.chooseday.days.push(code);
                }
                break;
            }
            // select month options
            case 'ALL':
            case 'CHSE':
            case 'CONT': {
                repetition.repeats = 'MTLY';
                repetition.choosemonth.type = code;
                break;
            }
            // choose months
            case 'JAN':
            case 'FEB':
            case 'MAR':
            case 'APR':
            case 'MAY':
            case 'JUN':
            case 'JUL':
            case 'AUG':
            case 'SEP':
            case 'OCT':
            case 'NOV':
            case 'DEC': {
                repetition.repeats = 'MTLY';
                repetition.choosemonth.type = 'CHSE';
                repetition.choosemonth.months = $scope.repetition.choosemonth.months;
                var index = $scope.repetition.choosemonth.days.indexOf(code);
                if(index > -1) {
                    repetition.choosemonth.days.splice(index, 1);   
                }
                else {
                    repetition.choosemonth.days.push(code);
                }
                break;
            }
            // count
            // TODO
        }

        $scope.repetition = repetition;
    }
    // Default visibility of individual panel
    $scope.isVisible = {
        days: false,
        select: false,
        months: false,
        count: false
    };
});