SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/common/person-avatar-menu.js

180 lines
9.6 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('myitsmApp')
.directive('personMenu', ['$http', '$templateCache', '$parse', '$compile', '$timeout', 'systemAlertService', 'chatModel', '$state', 'ticketModel',
function ($http, $templateCache, $parse, $compile, $timeout, systemAlertService, chatModel, $state, ticketModel) {
return {
restrict: 'A',
priority: 99,
replace: false,
link: function (scope, $elem) {
var $originalTemplate, lastClickedElement, menuHtml;
scope.userIsOnline = false;
scope.avatarMenu = {
isActive: false,
originalTemplate: $originalTemplate,
//disabledActions: ['sendEmail','makeCall'],
actionsList: [
{
name: 'viewProfile',
label: 'person.avatarMenu.viewProfile.label',
disabledAction: false,
iconClass: 'icon-user_o',
hiddenAction: false
},
{
name: 'startChat',
label: 'person.avatarMenu.startChat.label',
disabledAction: false,
iconClass: 'icon-comments',
hiddenAction: false,
genericText: 'fullName || (firstName+" "+lastName)'
},
{
name: 'sendEmail',
label: '',
disabledAction: true,
iconClass: 'icon-envelope',
hiddenAction: true
},
{
name: 'makeCall',
label: '',
disabledAction: true,
iconClass: 'icon-phone',
hiddenAction: true
}
]
};
if (!$originalTemplate) {
var template = $templateCache.get('views/person/person-avatar-menu.html');
if (template) {
$originalTemplate = angular.element(template);
}
else {
$http.get('views/person/person-avatar-menu.html')
.then(function (response) {
if (response && response.data) {
var template = response.data;
$originalTemplate = angular.element(template);
}
})
.catch(function (e) {
var template = $templateCache.get('views/person/person-avatar-menu.html');
$originalTemplate = angular.element(template);
});
}
}
scope.checkUserStatus = function () {
return scope.avatarMenu.menuContext.available ? scope.avatarMenu.menuContext.available === EntityVO.CHAT_STATUS_ONLINE : false;
};
scope.getGenericText = function (genericText) {
var parsingFn = $parse(genericText);
return parsingFn(scope.avatarMenu.menuContext);
};
scope.performAction = function (action) {
if (action.name === 'startChat') {
var chatContext = ticketModel.cache[$state.params.id];
//use ng-init, with personMenuContext definition, on person-menu element to set custom context for chat
if (scope.personMenuContext) {
chatContext = { type: scope.personMenuContext.type, id: scope.personMenuContext.id };
}
var jid = scope.avatarMenu.menuContext.jid;
chatModel.createAssignedChatRoomWithUser(jid, chatContext)
.catch(function (e) {
throw e;
});
}
};
//TODO: remove hardcoded references. Create a directive which will be listening for element click events
$elem.on('click', '.chat-availability__holder', handleAvatarClick);
$elem.on('click', '.profile-action-bar__item-menu_start-chat', handleMenuClick);
angular.element('body').on('click', bodyClickHandler);
function handleAvatarClick(e) {
var $this = angular.element(e.target || e.currentTarget);
var isolatedScope = $this.data('$isolateScope');
var userProfile;
if (isolatedScope) {
userProfile = isolatedScope.userInfo;
}
if (userProfile && !e.ctrlKey && !e.shiftKey) {
e.preventDefault();
e.stopPropagation();
}
else {
return;
}
var cachedProfile = chatModel.cachedProfiles[userProfile.jid];
if (!cachedProfile) {
// try by id && loginId when userprofile jid is null
cachedProfile = _.find(_.values(chatModel.cachedProfiles), function (obj) { return obj.id === userProfile.id && obj.loginId === userProfile.loginId; });
userProfile.jid = cachedProfile ? cachedProfile.jid : userProfile.jid;
}
scope.avatarMenu.menuContext = userProfile;
scope.userIsOnline = userProfile.jid ? (cachedProfile && cachedProfile.available === EntityVO.CHAT_STATUS_ONLINE) : false;
scope.avatarMenu.actionsList[1].disabledAction = !scope.userIsOnline;
scope.avatarMenu.actionsList[1].hiddenAction = !chatModel.connected;
scope.avatarMenu.isActive = true;
if (!menuHtml) {
menuHtml = $compile($originalTemplate)(scope);
}
$timeout(function () {
menuHtml.insertAfter($this);
lastClickedElement = $this[0];
});
}
function bodyClickHandler(event) {
if (event.target !== lastClickedElement) {
$timeout(function () {
scope.avatarMenu.isActive = false;
});
}
}
function handleMenuClick() {
var type = $state.current.name, chatRelation = {};
if (type === EntityVO.TYPE_PERSON) {
var personJid = scope.person.jid, selfChat = chatModel.currentUser.loginId === (scope.person.loginId || scope.person.elementId);
if (personJid && !selfChat) {
chatModel.createChatRoom(personJid);
}
else {
var textParam = selfChat ? 'self' : scope.person.fullName, text = "Can't start conversation with " + textParam;
$timeout(function () {
systemAlertService.warning({
text: text,
icon: 'icon-comments',
clear: true,
hide: 10000
});
});
}
return true;
}
else if (type === EntityVO.TYPE_ASSET) {
chatRelation = {
type: type,
ticketType: type,
id: $state.params.assetId,
reconciliationId: $state.params.assetId,
classId: $state.params.assetClassId
};
}
else {
chatRelation = { type: type, id: $state.params.id };
}
chatModel.createAssignedChatRoom(chatRelation);
return true;
}
scope.$on('$destroy', function () {
console.log("personAvatar: unbind events");
$elem.off('click', handleAvatarClick);
$elem.off('click', handleMenuClick);
angular.element('body').off('click', bodyClickHandler);
lastClickedElement = null;
});
}
};
}]);
})();