120 lines
5.6 KiB
JavaScript
120 lines
5.6 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Created by viktor.shevchenko on 7/27/15.
|
|
*/
|
|
/*
|
|
* Recommended Templates directive - loads templates for creation from Smart Recorder
|
|
* Very possible that tickets are just incidents, todo: verify and rename to incidents
|
|
*
|
|
*/
|
|
angular.module('resourceModule')
|
|
.directive('rsTemplates', ['ticketTemplateModel', 'events',
|
|
function (ticketTemplateModel, events) {
|
|
return {
|
|
require: '^rs',
|
|
restrict: 'E',
|
|
scope: {},
|
|
templateUrl: 'views/resource/rs-templates.html',
|
|
link: function (scope, element, attrs, rsController) {
|
|
angular.extend(scope, rsController);
|
|
var context = scope.context, state = {
|
|
itemLimit: 4,
|
|
isDataLoading: false,
|
|
watchersOn: false
|
|
}, initialItemLimit = 4;
|
|
scope.templates = [];
|
|
scope.toggleTemplateSelection = function (template) {
|
|
if (scope.savedTemplateId !== template.id) {
|
|
selectTemplate(template);
|
|
}
|
|
else {
|
|
unSelectTemplate();
|
|
}
|
|
};
|
|
scope.$on(events.ON_SELECTED_LIVE_CHAT, function (event, data) {
|
|
if (data && data.searchText) {
|
|
scope.summarySearchText = data.searchText;
|
|
}
|
|
else {
|
|
scope.summarySearchText = '';
|
|
}
|
|
scope.templates = [];
|
|
init();
|
|
});
|
|
function init() {
|
|
var searchTemplatesParams = {
|
|
searchText: context.summary || scope.summarySearchText,
|
|
companyName: context.company,
|
|
customerId: context.customerId
|
|
};
|
|
if (scope.context.type === EntityVO.TYPE_LIVE_CHAT) {
|
|
searchTemplatesParams.isFromLiveChat = true;
|
|
searchTemplatesParams.type = scope.context.searchTicketTypes || scope.searchTicketTypes;
|
|
}
|
|
if (!searchTemplatesParams.searchText || (searchTemplatesParams.type && searchTemplatesParams.type[0] === 'none')) {
|
|
scope.templates = [];
|
|
return;
|
|
}
|
|
state.itemLimit = initialItemLimit;
|
|
state.isDataLoading = true;
|
|
ticketTemplateModel.getTemplatesForSmartRecorder(searchTemplatesParams).then(function (templates) {
|
|
scope.templates = templates;
|
|
scope.$emit(events.RESOURCES_FOUND, { data: scope.templates, type: 'templates' });
|
|
!state.watchersOn && initWatchers();
|
|
}).finally(function () {
|
|
state.isDataLoading = false;
|
|
});
|
|
}
|
|
function initWatchers() {
|
|
state.watchersOn = true;
|
|
scope.$watchCollection('context', function (newValue, oldValue) {
|
|
var watchProps = ['summary', 'company', 'customerId'], i, l = watchProps.length;
|
|
if (newValue.template && newValue.template.id) {
|
|
if ((newValue.template.id !== oldValue.template.id) || (newValue.template.id === oldValue.template.id && !scope.savedTemplateId)) {
|
|
scope.savedTemplateId = newValue.template.id;
|
|
sortListBySelectedTemplate();
|
|
}
|
|
}
|
|
else if (scope.context.type === EntityVO.TYPE_LIVE_CHAT && scope.context.selectedTemplate) {
|
|
scope.savedTemplateId = scope.context.selectedTemplate.id;
|
|
}
|
|
else {
|
|
scope.savedTemplateId = '';
|
|
}
|
|
for (i = 0; i < l; i++) {
|
|
if (newValue[watchProps[i]] !== oldValue[watchProps[i]]) {
|
|
init();
|
|
break;
|
|
}
|
|
}
|
|
}, true);
|
|
}
|
|
function selectTemplate(template) {
|
|
scope.savedTemplateId = template.id;
|
|
sortListBySelectedTemplate();
|
|
scope.$emit(events.SELECT_TEMPLATE_FROM_RS, template);
|
|
}
|
|
function unSelectTemplate() {
|
|
scope.savedTemplateId = '';
|
|
scope.$emit(events.UNSELECT_TEMPLATE_FROM_RS);
|
|
}
|
|
function sortListBySelectedTemplate() {
|
|
//Scroll position is getting changed on selecting templates due to splice and unshift method so created a copy
|
|
//and made changes and assign it back
|
|
var localTemplate = angular.copy(scope.templates);
|
|
_.find(localTemplate, function (template, index) {
|
|
if (scope.savedTemplateId === template.id && index !== 0) {
|
|
localTemplate.splice(index, 1);
|
|
localTemplate.unshift(template);
|
|
return true;
|
|
}
|
|
});
|
|
scope.templates = localTemplate;
|
|
}
|
|
scope.context = context;
|
|
scope.state = state;
|
|
init();
|
|
}
|
|
};
|
|
}]);
|