(function () { var mod = angular.module('myMessages'); 'use strict'; var templatePath = modulesSharedResourcesUrl + 'Modules/MyMessages/Views/'; var directiveId = 'myMessages'; mod.directive(directiveId, [ 'myMessagesService', 'myMessagesContext', 'layoutService', 'fileReaderService', 'user', 'common', '$filter', '$modal', '$q', '$rootScope', '$timeout', '$window', myMessagesDirective ]); function myMessagesDirective(myMessagesService, myMessagesContext, layoutService, fileReaderService, user, common, $filter, $modal, $q, $rootScope, $timeout, $window) { return { restrict: 'E', templateUrl: templatePath + 'mymessages.html', link: myMessages }; function myMessages(vm, elem, attrs) { user.getProfile().then(function (data) { vm.user = data; }); var getLogFn = common.logger.getLogFn; var log = getLogFn(directiveId); var logSuccess = getLogFn(directiveId, 'success'); var logError = getLogFn(directiveId, 'error'); var logWarning = getLogFn(directiveId, 'warning'); var w = angular.element($window); vm.$watch( function () { return { width: $window.innerWidth, height: $window.innerHeight }; }, function (value) { vm.windowWidth = value.width; vm.windowHeight = value.height; vm.panelHeight = vm.windowHeight - 177; }, true ); w.bind('resize', function () { vm.$apply(); }); vm.ready = false; vm.pageTitle = 'Messages'; activate(); vm.getUserDetails = function (theUser) { if (theUser) { user.getUserProfile(theUser.userId).then(function (data) { theUser.details = data; }); } } vm.rightSideBar = layoutService.getRightSideBar; vm.toggleRightSideBar = function (openOrClose) { if (openOrClose === 'open') { layoutService.setRightSideBar(true); // add this to layoutService $rootScope.collapseSideBar = true; } else layoutService.setRightSideBar(false); }; function activate() { vm.loadingConversations = true; var setConversations = $q.defer(); var loadedConversations = $q.defer(); // check if conversations already exist in service, if so we don't need to use the set service if (!myMessagesService.getAllConversations()) { myMessagesService.setAllConversations().then(function () { setConversations.resolve(); }); } else setConversations.resolve(); // we need to make sure the setConversations promise has resolved as we cannot get conversations without the service having conversations to serve setConversations.promise.then(function () { vm.conversations = myMessagesService.getAllConversations().then(function (conversations) { vm.conversations = conversations; loadedConversations.resolve(); vm.loadingConversations = false; }); }); // next we can get the users for all users orgs and existing conversation participants loadedConversations.promise.then(myMessagesService.getUsers().then(function (users) { console.log(users) vm.users = users; setViewInitialLoad(); vm.tags.getTags(); })); // set the currentConversation view on load function setViewInitialLoad() { if (!vm.ready) { var clickedConversation = myMessagesService.getViewConversation(); if (clickedConversation === null) { // get the id of the latest conversation from context myMessagesContext.getLatestConversation().then(function (latestConvId) { angular.forEach(vm.conversations, function (conversation) { if (conversation.id === latestConvId) vm.setViewConversation(conversation); }); }); } else { vm.setViewConversation(clickedConversation); } vm.ready = true; } } } // sets the conversation for the main view and resets the user inputs vm.setViewConversation = function (conversation, newConversation) { vm.conversationReady = false; resetUserInputs(); vm.currentConversation = conversation if (newConversation) vm.toggleRightSideBar('open'); else vm.toggleRightSideBar('close'); if (conversation.isUnreadForUser) { myMessagesContext.setReadStatusForConversation(conversation); conversation.isUnreadForUser = false; } $timeout(function () { vm.ready = true; }, 300) vm.conversationReady = true; vm.scrollReady = true; vm.ready = true; function resetUserInputs() { vm.conversation.newMessageContent = null; vm.conversation.newGroupName = null; vm.conversation.participants = []; vm.conversation.requirements = null; vm.tags.newTag = null; vm.attachments.currentAttachments = null; } }; // all functions and vars for pinning / unpinning conversations vm.pin = { hoveredThisConversation: '', // sets conversation to hovered when user mouse overs, used for displaying the pin button hoveredConversation: function (conversation) { if (conversation) vm.pin.hoveredThisConversation = conversation.id; else vm.pin.hoveredThisConversation = null; }, // sets isPinnedForUser to opposite of current value and sends the corresponding conversation id to context pinConversation: function (conversation) { if (conversation.isPinnedForUser) conversation.isPinnedForUser = false; else conversation.isPinnedForUser = true; myMessagesContext.setPinStatusForConversation(conversation.id); logSuccess('Pin status changed successfully.'); } }; // all functions and vars for tags vm.tags = { newTag: '', allUserTags: [], tagsBarShown: false, tagsCloudShown: false, setTagFilter: function (tag) { if (vm.tags.tagFilter !== tag) { vm.tags.tagFilter = tag; } else { vm.tags.tagFilter = ''; } }, tagFilter: '', getTags: function () { myMessagesService.getAllUserTags().then(function (tags) { vm.tags.allUserTags = tags; }); }, openManageTags: function () { var participant = $filter('filter')(vm.currentConversation.participants, function (part) { return part.userId === vm.user.userId; })[0]; $modal.open({ templateUrl: templatePath + 'managetags.html', controller: manageTagsController, resolve: { participant: function () { return participant; } }, size: 'sm' }).result.then(function () { vm.tags.tagFilter = ''; vm.tags.getTags(); }); } }; function manageTagsController(common, $scope, $modalInstance, participant) { $scope.participant = participant; $scope.newTag = ''; $scope.addTag = function (tag) { myMessagesService.addTag(tag, $scope.participant); $scope.newTag = ''; }; $scope.removeTag = function (tag) { myMessagesService.removeTag(tag, $scope.participant); }, $scope.close = function () { $modalInstance.close(); }; } vm.currentConversation = {}; vm.conversation = { newMessageContent: null, newGroupName: null, lastConversation: {}, participants: [], requirements: null, replyToConversation: function (conversation, messageContent) { var newMessageObject = myMessagesService.replyToConversation(conversation, messageContent, vm.attachments.currentAttachments); vm.conversation.newMessageContent = null; vm.conversation.newGroupName = null; vm.attachments.currentAttachments = null; newMessageObject.then(function (newMessage) { conversation.messages.push(newMessage); conversation.lastMessage = newMessage; }); }, startNewConversation: function () { vm.conversation.lastConversation = vm.currentConversation; $modal.open({ templateUrl: templatePath + 'createnewconversation.html', controller: createNewConversationController, resolve: { currentUser: function () { return vm.user; } }, size: 'sm' }).result.then(function (conversationType) { vm.currentConversation = myMessagesService.startNewConversation(vm.conversation); vm.setViewConversation(vm.currentConversation, true); vm.conversation.requirements = myMessagesService.getRequirementsForSelectedConversationType(conversationType); vm.conversationInProgress = true; }); }, createNewConversation: function (messageContent) { var createNewConversation = myMessagesService.createNewConversation(vm.conversation.participants, messageContent, vm.conversation.newGroupName, vm.conversation.requirements, vm.attachments.currentAttachments); if (createNewConversation.then) createNewConversation.then(function (newConversation) { vm.toggleRightSideBar(); vm.conversations.push(newConversation); logSuccess('Conversation created successfully.'); vm.conversationInProgress = false; vm.setViewConversation(newConversation); }); else logWarning(createNewConversation); }, selectParticipant: function (selectedUser) { if (selectedUser.userId) selectedUser.userId = selectedUser.userId; vm.conversation.participants = myMessagesService.selectParticipant(vm.conversation.participants, selectedUser); if (vm.conversation.requirements.maxParticipants == vm.conversation.participants.length + 1) { var existing = myMessagesService.checkForExistingConversation(vm.conversations, vm.conversation.participants); if (existing) { //logWarning('You have an existing conversation with this person.') vm.currentConversation = existing; } vm.toggleRightSideBar(); } }, isParticipantSelected: function (user) { if (myMessagesService.isSelectedUserInParticipants(vm.conversation.participants, user)) return true; }, discardNewConversation: function () { if (vm.conversation.lastConversation) vm.setViewConversation(vm.conversation.lastConversation); } }; function createNewConversationController(common, $scope, $modalInstance, currentUser) { $scope.user = currentUser; $scope.select = function (selectedConversationType) { $modalInstance.close(selectedConversationType); }; $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; } vm.attachments = { currentAttachments: null, manageAttachments: function () { $modal.open({ templateUrl: templatePath + 'manageattachments.html', controller: manageAttachmentsController, size: 'sm', resolve: { existingFiles: function () { return vm.attachments.currentAttachments; } }, }).result.then(function (result) { vm.attachments.currentAttachments = result; }); } } function manageAttachmentsController(common, $scope, $modalInstance, existingFiles) { if (existingFiles) $scope.files = existingFiles; $scope.changedFiles = function (files) { $scope.files = files; angular.forEach($scope.files, function (file) { if (file.size > 40000000) { file.errorMessage = 'File size is too large, max 40MB'; } else { file.attachmentType = myMessagesService.getAttachmentType(file.type); if (file.attachmentType === 0) { fileReaderService.readAsDataURL(file, $scope).then(function (result) { file.src = result; }); } } }); } $scope.removeFileFromFiles = function (file) { $scope.files.splice($scope.files.indexOf(file), 1); } $scope.anyErrors = function () { var errors = 0; if (!$scope.files || !$scope.files.length) { errors++; return errors; } angular.forEach($scope.files, function (file) { if (file.errorMessage) errors++; }); return errors; } $scope.save = function () { $modalInstance.close($scope.files); } $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; } } } mod.directive('conversationListItem', function () { return { templateUrl: templatePath + 'conversationlistitem.html', restrict: 'E' }; }); // sets the background image of an element to the url and covers, you can also specify height and width of the element //
mod.directive('coverImg', function () { return function (scope, element, attrs) { attrs.$observe('coverImg', function (source) { element.css({ 'background-image': 'url(' + source + ')', 'background-size': 'cover', 'background-position': 'center' }); }); attrs.$observe('coverImgWidth', function (width) { //if attr is just number use px if (!isNaN(width)) { element.css({ 'width': width + 'px' }); } else { //all others have to be exact e.g. 50%, 5em, etc. element.css({ 'width': width }); } }); attrs.$observe('coverImgHeight', function (height) { if (!isNaN(height)) { element.css({ 'height': height + 'px' }); } else { element.css({ 'height': height }); } }); }; }); // creates a user avatar element given a user object (user.avatatUrl or user.id required) // mod.directive('userAvatar', function () { return { restrict: 'E', scope: { user: '=' }, template: '' + '' }; }); mod.directive('ngEnter', function () { return function (scope, element, attrs) { element.bind("keydown keypress", function (event) { if (event.which === 13) { scope.$apply(function () { scope.$eval(attrs.ngEnter); }); event.preventDefault(); } }); }; }); mod.directive('scrollTopAction', function () { return { restrict: 'A', link: function (scope, element, attrs) { if (element.scrollTop === 0) $timeout(function () { scope.$evalAsync(attrs.scrollTopAction); }, 50); } } }); mod.directive('conversationMessages', function () { return { restrict: 'E', scope: { conversation: '=conversation', user: '=user', getUserDetails: '=getUserDetails' }, templateUrl: templatePath + 'conversationmessages.html', link: function (scope, element, attrs) { scope.postRender = function () { var container = angular.element(document.querySelector('#conversation-messages-container')); container.context.scrollTop = 9999999; } } } }); mod.directive('postrenderAction', function ($timeout) { return { restrict: 'A', link: function (scope, element, attrs) { $timeout(function () { scope.$eval(attrs.postrenderAction); }); } } }) mod.directive('messageAttachmentItem', function () { return { restrict: 'E', scope: { attachment: '=' }, templateUrl: templatePath + 'messageattachmentitem.html', controller: messageAttachmentItemController }; }); function messageAttachmentItemController($scope, $modal) { var type = getType($scope.attachment); $scope.attachment.fileType = type; switch (type) { case 'word': $scope.attachment.icon = 'fa-file-word-o'; break; case 'excel': $scope.attachment.icon = 'fa-file-excel-o'; break; case 'powerpoint': $scope.attachment.icon = 'fa-file-powerpoint-o'; break; default: $scope.attachment.icon = 'fa-file-o'; } $scope.attachment.colour = 'panel-default'; function getType(attachment) { if (/^.*\.(doc|docx)$/.test(attachment.fileName)) return 'word'; else if (/^.*\.(xls|xlsx)$/.test(attachment.fileName)) return 'excel'; else if (/^.*\.(ppt|pptx)$/.test(attachment.fileName)) return 'powerpoint'; else return 'unknown'; } $scope.clickImage = function () { $modal.open({ templateUrl: templatePath + 'displayimageattachment.html', controller: function (common, $scope, $modalInstance) { $scope.closeImage = function () { $modalInstance.dismiss(); }; }, windowTopClass: 'widthAutoModal', scope: $scope }); }; } })();