SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/common/user-availability.js

92 lines
5.6 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('myitsmApp')
.directive('userAvailability', ['personModel', 'chatModel', '$timeout', function (personModel, chatModel, $timeout) {
return {
restrict: 'A',
scope: {
userInfo: '=userAvailability'
},
priority: 99,
replace: true,
template: "<div class=\"chat-availability__holder\" ng-class=\"'availability__'+(currentProfile.available ? currentProfile.available.toLowerCase() : 'offline')\"></div>",
link: function (scope, $element, attr) {
scope.currentProfile = {};
var outerHeight = parseInt($element.css('height')), outerWidth = parseInt($element.css('width')), commonSize = (outerHeight >= outerWidth) ? outerHeight : outerWidth;
/*if(commonSize == 0){
commonSize = defaultSize;
}*/
$element.height(commonSize).width(commonSize);
attr.$observe('src', function (value) {
if (value) {
$element.css({ 'background-image': 'url(' + attr.src + ')', 'background-size': 'contain' });
}
});
var unbinder = scope.$watch(function () {
return !!chatModel.connected && scope.userInfo;
}, function (newVal) {
if (newVal) {
var personData = scope.userInfo, jid = personData.jid, personId = personData.loginId || personData.elementId, generatedJID = jid || chatModel.generateUserJID(personId), currentUserIdMatch = personId ? (chatModel.currentUser.elementId === personId || chatModel.currentUser.loginId === personId) : false, currentUserJidMatch = jid ? (chatModel.currentUser === jid) : false;
if (currentUserIdMatch || currentUserJidMatch) {
scope.currentProfile = chatModel.currentUser;
return;
}
if (jid) {
chatModel.cachedProfiles[jid] ? angular.extend(chatModel.cachedProfiles[jid], personData) : chatModel.cachedProfiles[jid] = personData;
scope.currentProfile = chatModel.cachedProfiles[jid];
chatModel.checkUserAvailability(jid);
return;
}
else if (personId) {
var cachedProfile;
if (chatModel.cachedProfiles[generatedJID]) {
personData.jid = generatedJID;
personData.available = chatModel.cachedProfiles[generatedJID].available;
cachedProfile = chatModel.cachedProfiles[generatedJID];
angular.extend(chatModel.cachedProfiles[generatedJID], personData);
}
else {
cachedProfile = _.find(chatModel.cachedProfiles, function (profile) {
return _.includes(profile, personId);
});
}
if (cachedProfile) {
scope.currentProfile = cachedProfile;
chatModel.checkUserAvailability(scope.currentProfile.jid);
return;
}
var promise = personModel.getPersonDetailsByID(personId)
.then(function (personDetails) {
jid = personDetails ? personDetails.jid : _.noop();
if (jid) {
personDetails.available = personDetails.available.toLowerCase();
chatModel.cachedProfiles[jid] ? angular.extend(chatModel.cachedProfiles[jid], personModel.personDetails) : chatModel.cachedProfiles[jid] = personModel.personDetails;
$timeout(function () {
scope.currentProfile = chatModel.cachedProfiles[jid];
if (!scope.currentProfile.available) {
chatModel.checkUserAvailability(scope.currentProfile.jid);
}
});
}
else {
chatModel.cachedProfiles.notFound = personDetails;
scope.currentProfile = personDetails;
}
});
chatModel.pendingProfileRequests[personId] = promise;
return;
}
}
});
scope.$on('destroy', function () {
unbinder();
});
/* function removeAvailability() {
$element.removeClass('chat-availability__holder');
}*/
}
};
}]);
})();