all files / app/main/apps/contacts/dialogs/contact/ contact-dialog.controller.js

19.44% Statements 7/36
0% Branches 0/4
14.29% Functions 1/7
19.44% Lines 7/36
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                                                                                                                                                                                                                  
(function ()
{
    'use strict';
 
    angular
        .module('app.contacts')
        .controller('ContactDialogController', ContactDialogController);
 
    /** @ngInject */
    function ContactDialogController($mdDialog, Contact, Contacts, User, msUtils)
    {
        var vm = this;
 
        // Data
        vm.title = 'Edit Contact';
        vm.contact = angular.copy(Contact);
        vm.contacts = Contacts;
        vm.user = User;
        vm.newContact = false;
        vm.allFields = false;
 
        if ( !vm.contact )
        {
            vm.contact = {
                'id'      : msUtils.guidGenerator(),
                'name'    : '',
                'lastName': '',
                'avatar'  : 'assets/images/avatars/profile.jpg',
                'nickname': '',
                'company' : '',
                'jobTitle': '',
                'email'   : '',
                'phone'   : '',
                'address' : '',
                'birthday': null,
                'notes'   : ''
            };
 
            vm.title = 'New Contact';
            vm.newContact = true;
            vm.contact.tags = [];
        }
 
        // Methods
        vm.addNewContact = addNewContact;
        vm.saveContact = saveContact;
        vm.deleteContactConfirm = deleteContactConfirm;
        vm.closeDialog = closeDialog;
        vm.toggleInArray = msUtils.toggleInArray;
        vm.exists = msUtils.exists;
 
        //////////
 
        /**
         * Add new contact
         */
        function addNewContact()
        {
            vm.contacts.unshift(vm.contact);
 
            closeDialog();
        }
 
        /**
         * Save contact
         */
        function saveContact()
        {
            // Dummy save action
            for ( var i = 0; i < vm.contacts.length; i++ )
            {
                if ( vm.contacts[i].id === vm.contact.id )
                {
                    vm.contacts[i] = angular.copy(vm.contact);
                    break;
                }
            }
 
            closeDialog();
        }
 
        /**
         * Delete Contact Confirm Dialog
         */
        function deleteContactConfirm(ev)
        {
            var confirm = $mdDialog.confirm()
                .title('Are you sure want to delete the contact?')
                .htmlContent('<b>' + vm.contact.name + ' ' + vm.contact.lastName + '</b>' + ' will be deleted.')
                .ariaLabel('delete contact')
                .targetEvent(ev)
                .ok('OK')
                .cancel('CANCEL');
 
            $mdDialog.show(confirm).then(function ()
            {
 
                vm.contacts.splice(vm.contacts.indexOf(Contact), 1);
 
            });
        }
 
        /**
         * Close dialog
         */
        function closeDialog()
        {
            $mdDialog.hide();
        }
 
    }
})();