all files / app/main/apps/scrumboard/views/calendar/ calendar-view.controller.js

21.15% Statements 11/52
0% Branches 0/8
6.25% Functions 1/16
21.15% Lines 11/52
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203                                                                                                                                                                                                                                                                                                                                                                                                
(function ()
{
    'use strict';
 
    angular
        .module('app.scrumboard')
        .controller('CalendarViewController', CalendarViewController);
 
    /** @ngInject */
    function CalendarViewController($scope, $document, $mdDialog, $mdSidenav, BoardService, DialogService)
    {
        var vm = this;
 
        // Data
        vm.board = BoardService.data;
        vm.eventSources = [];
 
        vm.calendarUiConfig = {
            calendar: {
                editable                 : true,
                eventLimit               : true,
                header                   : '',
                handleWindowResize       : false,
                aspectRatio              : 1,
                dayNames                 : ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
                dayNamesShort            : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
                timezone                 : 'local',
                eventDurationEditable    : false,
                defaultTimedEventDuration: '01:00:00',
                viewRender               : function (view)
                {
                    vm.calendarView = view;
                    vm.calendar = vm.calendarView.calendar;
                    vm.currentMonthShort = vm.calendar.getDate().format('MMM');
                },
                columnFormat             : {
                    month: 'ddd',
                    week : 'ddd D',
                    day  : 'ddd D'
                },
                eventClick               : function eventDetail(calendarEvent, ev)
                {
                    vm.openCardDialog(ev, calendarEvent.idCard);
                },
                eventDrop                : function (event)
                {
                    vm.board.cards.getById(event.idCard).due = moment.utc(event.start).format('x');
                },
                selectable               : true,
                selectHelper             : true,
                dayClick                 : function (date, ev)
                {
                    var offset = moment().utcOffset();
                    var corrDate = '';
 
                    if ( offset < 0 )
                    {
                        corrDate = moment.utc(date).subtract(offset, 'm').format('x');
                    }
                    else
                    {
                        corrDate = moment.utc(date).add(offset, 'm').format('x');
                    }
 
                    eventDialog(corrDate, ev);
                }
            }
        };
 
        // Methods
        vm.next = next;
        vm.prev = prev;
        vm.goToDate = goToDate;
        vm.openCardDialog = DialogService.openCardDialog;
        vm.toggleSidenav = toggleSidenav;
 
        //////////
 
        init();
 
        /**
         * Initialize
         */
        function init()
        {
            vm.cards = getScheduledCards();
            vm.eventSources[0] = vm.cards;
        }
 
        /**
         * Get scheduled cards and prepare
         * them to show on the calendar
         *
         * @returns {Array}
         */
        function getScheduledCards()
        {
            var cards = [];
 
            angular.forEach(vm.board.cards, function (card)
            {
                if ( card.due )
                {
                    cards.push({
                        idCard         : card.id,
                        title          : card.name,
                        start          : moment.utc(card.due, 'x'),
                        due            : card.due,
                        backgroundColor: getEventBgColor(card.due)
                    });
                }
            });
 
            return cards;
        }
 
        /**
         * Get background color
         *
         * @param cardDue
         * @returns {*}
         */
        function getEventBgColor(cardDue)
        {
            if ( moment() > moment(cardDue, 'x') )
            {
                return '#F44336';
            }
 
            return '#4CAF50';
        }
 
        /**
         * Watch board changes
         */
        $scope.$watch('vm.board', function (current, old)
        {
            if ( angular.equals(current, old) )
            {
                return;
            }
 
            init();
 
        }, true);
 
        /**
         * Go to Date
         *
         * @param date
         */
        function goToDate(date)
        {
            vm.calendarView.calendar.gotoDate(date);
            $mdSidenav('scheduled-tasks-sidenav').close();
        }
 
        /**
         * Go to next on current view (week, month etc.)
         */
        function next()
        {
            vm.calendarView.calendar.next();
        }
 
        /**
         * Go to previous on current view (week, month etc.)
         */
        function prev()
        {
            vm.calendarView.calendar.prev();
        }
 
        /**
         * Event Dialog
         */
        function eventDialog(date, ev)
        {
            $mdDialog.show({
                templateUrl        : 'app/main/apps/scrumboard/views/calendar/dialogs/event/event-dialog.html',
                controller         : 'ScrumboardCalendarEventDialogController',
                controllerAs       : 'vm',
                parent             : $document.find('#scrumboard'),
                targetEvent        : ev,
                clickOutsideToClose: true,
                locals             : {
                    dueDate: date
                }
            });
        }
 
        /**
         * Toggle sidenav
         *
         * @param sidenavId
         */
        function toggleSidenav(sidenavId)
        {
            $mdSidenav(sidenavId).toggle();
        }
 
    }
})();