SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/change/calendar-view-directive.js

161 lines
8.7 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('changeModule')
.directive('calendarView', ['$timeout', '$log', 'dateFilter', '$rootScope', 'events', '$locale', '$state',
function ($timeout, $log, dateFilter, $rootScope, events, $locale, $state) {
return {
restrict: 'E',
templateUrl: 'views/change/calendar-view.html',
require: '^calendar',
scope: {
id: '@',
viewType: '@'
},
link: function (scope, element, attrs, calendarCtrl) {
var calendar = scope[scope.id];
scope.model = calendarCtrl.model;
scope.isChangeCalendar = ($state.current.name && $state.current.name.indexOf('changeCalendar') !== -1);
scope.config = {
heightSpec: 'Full',
viewType: scope.viewType,
theme: 'calendarview',
cssClassPrefix: 'calendarview',
weekStarts: 0,
sortDirections: ["asc"],
onBeforeHeaderRender: function (args) {
var format = 'dayAndDate';
if (window.myitsmLocale === 'en' && $locale.id.toLowerCase() !== 'en-us') {
format = 'shortDate';
}
var htmlText = dateFilter(args.header.start.toDateLocal(), format);
if (window.myitsmLocale === 'en' && $locale.id.toLowerCase() !== 'en-us') {
if ($locale.id.toLowerCase() === 'en-ca') {
//remove yy- at the beginning
htmlText = htmlText.substring(htmlText.indexOf('-') + 1);
}
else if ($locale.id.toLowerCase() === 'en-za') {
//remove yy/ at the beginning
htmlText = htmlText.substring(htmlText.indexOf('/') + 1);
}
else {
//remove /yy at the end
htmlText = htmlText.substring(0, htmlText.lastIndexOf('/'));
}
}
args.header.html = htmlText;
args.header.toolTip = htmlText;
},
onTimeRangeSelected: function (args) {
if (scope.isChangeCalendar) {
args.preventDefault();
}
else {
calendar.clearSelection();
scope.model.context.scheduledStartDate = args.start.toDateLocal();
scope.model.context.scheduledEndDate = args.end.toDateLocal();
scope.$apply();
}
},
onEventMove: function (args) {
if (scope.isChangeCalendar || (args.e.resource() !== scope.model.currentRequestResourceId)) {
args.preventDefault();
}
},
onEventMoved: function (args) {
scope.model.context.scheduledStartDate = args.newStart.toDateLocal();
scope.model.context.scheduledEndDate = args.newEnd.toDateLocal();
scope.$apply();
},
onEventResize: function (args) {
if (scope.isChangeCalendar || (args.e.resource() !== scope.model.currentRequestResourceId)) {
args.preventDefault();
}
},
onEventResized: function (args) {
scope.model.context.scheduledStartDate = args.newStart.toDateLocal();
scope.model.context.scheduledEndDate = args.newEnd.toDateLocal();
scope.$apply();
},
onEventClicked: calendarCtrl.onEventClicked,
onBeforeTimeHeaderRender: function (args) {
if (args.header.hasOwnProperty('hours')) {
var actualHour = args.header.hours;
if (args.header.html.indexOf("AM") !== -1 && actualHour === 12) {
actualHour = 0;
}
else if (args.header.html.indexOf("PM") !== -1 && actualHour !== 12) {
actualHour += 12;
}
args.header.html = dateFilter(moment().hour(actualHour).minute(args.header.minutes).second(0).toDate(), "shortestTime");
}
}
};
scope.$watch('model.showWeekends', function () {
var Week = 'Week', WorkWeek = 'WorkWeek';
if (scope.viewType === Week) {
calendar.viewType = scope.model.showWeekends ? Week : WorkWeek;
updateCalendarView();
}
});
scope.$watch('model.context.scheduledStartDate', function () {
enqueueRescheduleChangeRequest();
});
scope.$watch('model.context.scheduledEndDate', function () {
enqueueRescheduleChangeRequest();
});
scope.$watch('model.context', function () {
calendarCtrl.updateCalendarHandling(calendar);
}, true);
scope.$watch('model.collisions', function () {
displayCollisions();
}, true);
scope.$watch('model.calendarTypes', function () {
displayCollisions();
}, true);
var off = $rootScope.$on(events.CHANGE_COLLISION_STATUS_CHANGED, function (event, addressedCollisions) {
calendarCtrl.applyAddressedCollisions(addressedCollisions);
displayCollisions();
});
scope.$on('$destroy', function () {
off();
});
function displayCollisions() {
calendarCtrl.displayCollisions({
calendar: calendar,
resources: scope.config.resources,
changeCollisionCss: 'calendarview_event_collision',
changeNonCollisionCss: 'calendarview_event_secondary',
outageCollisionCss: 'calendarview_event_outage_collision',
outageNonCollisionCss: 'calendarview_event_outage_secondary',
businessEventCollisionCss: 'calendarview_event_business_event_collision',
businessEventNonCollisionCss: 'calendarview_event_business_event_secondary',
updateCalendar: updateCalendarView
});
}
function enqueueRescheduleChangeRequest() {
$timeout(function () {
rescheduleChangeRequest(scope.model.context.scheduledStartDate, scope.model.context.scheduledEndDate);
});
}
function rescheduleChangeRequest(start, end) {
if (calendarCtrl.rescheduleChangeRequest(calendar, start, end)) {
updateCalendarView();
}
}
function updateCalendarView() {
var start = scope.model.context.scheduledStartDate, end = scope.model.context.scheduledEndDate;
$log.log("update " + scope.viewType + " view: start=" + start + ", end=" + end);
if (!start) {
start = new Date();
}
calendar.startDate = new DayPilot.Date(start, true);
calendar.update();
}
updateCalendarView();
}
};
}
]);
})();