all files / app/main/apps/scrumboard/directives/ms-sb-add-card/ ms-sb-add-card.directive.js

14.89% Statements 7/47
0% Branches 0/10
8.33% Functions 1/12
14.89% Lines 7/47
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                                                                                                                                                                                                                                                                                                        
(function ()
{
    'use strict';
 
    angular
        .module('app.scrumboard')
        .controller('msSbAddCardController', msSbAddCardController)
        .directive('msSbAddCard', msSbAddCardDirective);
 
    /** @ngInject */
    function msSbAddCardController($scope, $timeout, BoardService, msUtils)
    {
        var vm = this;
 
        vm.newCardName = '';
        vm.listId = $scope.msListId;
        vm.board = BoardService.data;
        vm.cards = vm.board.cards;
        vm.list = vm.board.lists.getById(vm.listId);
 
        // Methods
        vm.addNewCard = addNewCard;
 
        /////
 
        /**
         * Add New Card
         */
        function addNewCard()
        {
            if ( vm.newCardName === '' )
            {
                return;
            }
 
            var newCardId = msUtils.guidGenerator();
 
            vm.cards.push({
                id               : newCardId,
                name             : vm.newCardName,
                description      : '',
                idAttachmentCover: '',
                idMembers        : [],
                idLabels         : [],
                attachments      : [],
                subscribed       : false,
                checklists       : [],
                checkItems       : 0,
                checkItemsChecked: 0,
                comments         : [],
                activities       : [],
                due              : null
            });
 
            vm.list.idCards.push(newCardId);
 
            $timeout(function ()
            {
                $scope.scrollListContentBottom();
            });
 
            vm.newCardName = '';
        }
    }
 
    /** @ngInject */
    function msSbAddCardDirective($document, $window, $timeout)
    {
        return {
            restrict   : 'E',
            controller : 'msSbAddCardController as vm',
            templateUrl: 'app/main/apps/scrumboard/directives/ms-sb-add-card/ms-sb-add-card.html',
            scope      : {
                msListId: '='
            },
            link       : function (scope, iElement)
            {
                scope.formActive = false;
                scope.toggleForm = toggleForm;
                scope.scrollListContentBottom = scrollListContentBottom;
 
                var buttonEl = iElement.find('.ms-sb-add-card-button'),
                    formEl = iElement.find('.ms-sb-add-card-form'),
                    listCards = iElement.parent().prev().find('.list-cards');
 
                /**
                 * Click Event
                 */
                buttonEl.on('click', toggleForm);
 
                /**
                 * Toggle Form
                 */
                function toggleForm()
                {
                    scope.$evalAsync(function ()
                    {
                        scope.formActive = !scope.formActive;
 
                        if ( scope.formActive )
                        {
                            $timeout(function ()
                            {
                                formEl.find('input').focus();
 
                                scrollListContentBottom();
                            });
 
                            $document.on('click', outSideClick);
                        }
                        else
                        {
                            PerfectScrollbar.update(listCards[0]);
                            $document.off('click', outSideClick);
                        }
 
                        $timeout(function ()
                        {
                            // IE list-content max-height hack
                            if ( angular.element('html').hasClass('explorer') )
                            {
                                angular.element($window).trigger('resize');
                            }
                        });
 
                    });
                }
 
                /**
                 * Scroll List to the Bottom
                 */
                function scrollListContentBottom()
                {
                    listCards[0].scrollTop = listCards[0].scrollHeight;
                }
 
                /**
                 * Click Outside Event Handler
                 * @param event
                 */
                var outSideClick = function (event)
                {
                    var isChild = formEl.has(event.target).length > 0;
                    var isSelf = formEl[0] === event.target;
                    var isInside = isChild || isSelf;
 
                    if ( !isInside )
                    {
                        toggleForm();
                    }
                };
            }
        };
    }
})();