SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/change/calendar-month-view-directi...

140 lines
7.5 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('changeModule')
.directive('calendarMonthView', ['$timeout', '$log', '$locale', '$rootScope', 'events', '$state',
function ($timeout, $log, $locale, $rootScope, events, $state) {
return {
restrict: 'E',
templateUrl: 'views/change/calendar-month-view.html',
require: '^calendar',
scope: {
id: '@',
viewType: '@'
},
link: function (scope, element, attrs, calendarCtrl) {
var calendar = scope[scope.id], localeCulture = $locale.culture, localeFromId = $locale.id + "-" + $locale.id;
//if locale is not supported in DayPilot default to 'en-us'
localeCulture = DayPilot.Locale.find(localeCulture) ? localeCulture
: (DayPilot.Locale.find(localeFromId) ? localeFromId : 'en-us');
scope.model = calendarCtrl.model;
scope.isChangeCalendar = ($state.current.name && $state.current.name.indexOf('changeCalendar') !== -1);
scope.config = {
heightSpec: 'Full',
theme: 'monthview',
cssClassPrefix: 'monthview',
weekStarts: 0,
viewType: scope.viewType,
weeks: 1,
sortDirections: ['asc'],
locale: localeCulture,
showToolTip: true,
onBeforeHeaderRender: function (args) {
args.header.html = moment(args.header.dayOfWeek, "d").format("dddd");
},
onTimeRangeSelected: function (args) {
if (scope.isChangeCalendar) {
args.preventDefault();
}
else {
if (calendar.timeRangeSelectedHandling !== calendarCtrl.handling.select.disabled) {
calendar.clearSelection();
if (scope.model.context && scope.model.context.timing && scope.model.context.timing.name !== "Latent") {
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
};
scope.$watch('model.showWeekends', function () {
calendar.showWeekend = scope.model.showWeekends;
updateMonthView();
});
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: 'monthview_event_collision',
changeNonCollisionCss: 'monthview_event_secondary',
outageCollisionCss: 'monthview_event_outage_collision',
outageNonCollisionCss: 'monthview_event_outage_secondary',
businessEventCollisionCss: 'monthview_event_business_event_collision',
businessEventNonCollisionCss: 'monthview_event_business_event_secondary',
updateCalendar: updateMonthView
});
}
function enqueueRescheduleChangeRequest() {
$timeout(function () {
rescheduleChangeRequest(scope.model.context.scheduledStartDate, scope.model.context.scheduledEndDate);
});
}
function rescheduleChangeRequest(start, end) {
if (calendarCtrl.rescheduleChangeRequest(calendar, start, end)) {
updateMonthView();
}
}
function updateMonthView() {
var start = scope.model.context.scheduledStartDate;
var end = scope.model.context.scheduledEndDate;
$log.log("update month view: start=" + start + ", end=" + end);
if (!start) {
start = new Date();
}
if (scope.config.locale === 'de-de' || scope.config.locale === 'es-es' || scope.config.locale === 'fr-fr') {
scope.config.weekStarts = 1;
}
calendar.startDate = new DayPilot.Date(start, true);
calendar.update();
}
updateMonthView();
}
};
}
]);
})();