all files / app/main/apps/notes/directives/ms-new-note/ ms-new-note.directive.js

23.53% Statements 8/34
0% Branches 0/9
9.09% Functions 1/11
23.53% Lines 8/34
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                                                                                                                                                                                                          
(function ()
{
    'use strict';
 
    angular
        .module('app.notes')
        .controller('msNewNoteController', msNewNoteController)
        .directive('msNewNoteButton', msNewNoteButtonDirective)
        .directive('msNewNote', msNewNoteDirective);
 
    /** @ngInject */
    function msNewNoteController($scope, $document)
    {
        var MsNewNote = this;
 
        //Data
        MsNewNote.formVisible = false;
        MsNewNote.form = {
            title      : '',
            description: ''
        };
 
        //Methods
        MsNewNote.open = open;
        MsNewNote.close = close;
        MsNewNote.element = [];
 
        //////
 
        /**
         * Close Trigger
         */
        $scope.$on('MsNewNote:close', function ()
        {
            close();
        });
 
        /**
         * Open new note form
         */
        function open()
        {
            MsNewNote.element.addClass('form-visible');
            MsNewNote.element.find('textarea').focus();
            $document.on('click', outSideClick);
        }
 
        /**
         * Close new note form
         */
        function close()
        {
            MsNewNote.element.removeClass('form-visible');
            $scope.$broadcast('MsNewNote:closed');
 
            $document.off('click', outSideClick);
        }
 
        /**
         * Click Outside Event Handler
         * @param event
         */
        function outSideClick(event)
        {
            var isChild = MsNewNote.element.has(event.target).length > 0;
            var isSelf = MsNewNote.element[0] === event.target;
            var isMenu = angular.element(event.target).closest('md-menu-content').length > 0;
            var isCalendar = angular.element(event.target).closest('md-calendar').length > 0 || angular.element(event.target).closest('.md-datepicker-calendar-pane').length > 0 || angular.element(event.target).hasClass('md-scroll-mask');
 
            var isInside = isChild || isSelf || isMenu || isCalendar;
 
            if ( !isInside )
            {
                close();
            }
        }
    }
 
    /** @ngInject */
    function msNewNoteDirective()
    {
        return {
            restrict    : 'E',
            controller  : 'msNewNoteController',
            controllerAs: 'MsNewNote',
            templateUrl : 'app/main/apps/notes/directives/ms-new-note/ms-new-note.html',
            link        : function (scope, element, attributes, MsNewNote)
            {
                MsNewNote.element = element;
            }
        };
    }
 
    /** @ngInject */
    function msNewNoteButtonDirective()
    {
        return {
            restrict: 'EA',
            require : '^msNewNote',
            link    : function (scope, element, attributes, msNewNote)
            {
                element.on('click', function ()
                {
                    msNewNote.open();
                });
            }
        };
    }
})();