(function () { 'use strict'; var app = angular.module('calendar'); var templatePath = modulesSharedResourcesUrl + 'Modules/Calendar/Views/'; app.directive('calendar', ['$injector', 'config', 'common', '$rootScope', '$location', '$modal', '$filter', 'calendarDataContext', 'user', 'myUsersDataContext', function ($injector, config, common, $rootScope, $location, $modal, $filter, calendarDataContext, user, myUsersDataContext) { return { restrict: 'E', templateUrl: templatePath + 'calendar.html', scope: { isStudent: '=' }, link: link }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var log = getLogFn('calendar'); var logSuccess = getLogFn('calendar', "success"); $scope.calendarView = 'week'; $scope.viewDate = $filter('date')(moment().toDate(), "yyyy-MM-dd"); $scope.excludedDays = [0, 6]; $scope.cellIsOpen = true; if (!$scope.isStudent) { var myFormsAdminDataContext = $injector.get('myFormsAdminDataContext'); } else { var myFormsAdminDataContext = $injector.get('myFormsDataContext'); } activate(); function getCalendars() { if (!$scope.isStudent) { myUsersDataContext.getOrganisationsFullFlat(2).then(function (groups) { $scope.calendars = groups; if (groups.length > 0) { $scope.setNewCalendar($scope.currentOrgId ? $scope.currentOrgId : groups[0].id); } }); } else { myUsersDataContext.getOrganisations().then(function (groups) { $scope.calendars = groups.filter(function (group) { return group.type == 2; }); if ($scope.calendars.length > 0) { $scope.setNewCalendar($scope.currentOrgId ? $scope.currentOrgId : $scope.calendars[0].id); } }); } } $scope.setNewCalendar = function (orgId) { $scope.currentOrgId = orgId; $scope.calendarLoaded = false; calendarDataContext.getCalendarByOrgId(orgId).then(function (data) { for (i in data.events) { data.events[i].startsAt = new Date(data.events[i].startsAt); data.events[i].endsAt = new Date(data.events[i].endsAt); data.events[i].dayHeight = moment(data.events[i].endsAt).diff(data.events[i].startsAt, 'minutes'); if ($scope.isStudent && data.events[i].type === 'meeting' && data.events[i].title !== ($scope.user.firstName + " " + $scope.user.lastName)) { data.events[i].title = 'Unavailable'; data.events[i].description = ''; data.events[i].color = { primary: "#444444", secondary: "#b4b4b4" }; data.events[i].disabled = true; } } user.getUserProfile(data.createdBy).then(function (data) { $scope.tutor = data; }); $scope.calendar = data; $scope.events = data.events; $scope.calendarLoaded = true; }); if (!$scope.isStudent) { myUsersDataContext.getUsers(orgId).then(function (data) { for (i in data) { data[i].name = data[i].firstName + " " + data[i].lastName; } $scope.users = data; }); } } function activate() { NProgress.done(); log('Activated Calendar View'); user.getProfile().then(function (data) { $scope.user = data; getCalendars(); }) } $scope.eventTimesChanged = function (event) { vm.viewDate = event.startsAt; alert('Dragged and dropped'); }; $scope.eventClicked = function (event) { if (event.disabled) return; if (event.type === 'available' && !event.editAvailability) { $scope.manageEvent({ type: 'meeting', startsAt: moment(event.startsAt).toDate(), title: $scope.user.firstName + " " + $scope.user.lastName, endsAt: moment(event.startsAt).add(1, 'hour').toDate(), draggable: true, resizable: true, factors: [], recursOn: null, color: { primary: "#009ee3", secondary: "#ccecf9" } }, false); } else { if (!$scope.isStudent) { $scope.manageEvent(event, true, event.type); } else { $scope.viewEvent(event); } } } $scope.timespanClicked = function (date) { if (!$scope.isStudent) { $scope.manageEvent({ type: 'meeting', startsAt: moment(date).toDate(), title: '', endsAt: moment(date).add(1, 'hour').toDate(), draggable: true, resizable: true, factors: [], recursOn: null, color: { primary: "#009ee3", secondary: "#ccecf9" } }, false); } }; $scope.manageEvent = function (event, isEdit, type) { $modal.open({ templateUrl: templatePath + 'addevent.html', controller: manageEventController, size: type === 'available' || $scope.isStudent ? 'sm' : 'lg', backdrop: 'static', resolve: { event: function () { return event; }, isEdit: function () { return isEdit; }, getCalendars: function () { return getCalendars; }, calendar: function () { return $scope.calendar; }, viewDate: function () { return $scope.viewDate; }, type: function () { return type; }, users: function () { return $scope.users; }, isStudent: function() { return $scope.isStudent; }, user: function () { return $scope.user; }, tutor: function () { return $scope.tutor; }, } }); } $scope.viewEvent = function (event) { $modal.open({ templateUrl: templatePath + 'viewevent.html', controller: viewEventController, size: 'sm', backdrop: 'static', resolve: { event: function () { return event; }, calendar: function () { return $scope.calendar; } } }); } var manageEventController = function (common, $scope, $modalInstance, event, isEdit, getCalendars, calendar, viewDate, type, users, isStudent, user, tutor) { $scope.event = event; $scope.viewDate = viewDate; $scope.users = users; $scope.user = user; $scope.isStudent = isStudent; $scope.tutor = tutor; $scope.eventUser = $scope.event && $scope.users ? $scope.users.find(function (element) { return element.name === $scope.event.title; }) : null; // Are we editing the event? $scope.isEdit = isEdit; if ($scope.event) { if ($scope.event.id) { $scope.isEdit = true; } } // Create blank event model if (!$scope.event) { $scope.event = { title: type === 'available' ? 'Available' : '', startsAt: moment($scope.viewDate).toDate(), endsAt: moment($scope.viewDate).add(1, 'hour').toDate(), draggable: true, resizable: true, type: type, factors: [], recursOn: null, color: type === 'meeting' ? { primary: "#009ee3", secondary: "#ccecf9" } : { primary: "#7abe5c", secondary: "#bddfae" } } } // Set the calendar ID for the event $scope.event.calendarId = calendar.id; $scope.toggle = function ($event, field, event) { $event.preventDefault(); $event.stopPropagation(); event[field] = !event[field]; }; // Open th start day popup $scope.openStart = function () { $scope.showStartDate = true; }; // Convert the hex to an translucent rgba function hexToRgbA(hex) { var c; if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) { c = hex.substring(1).split(''); if (c.length == 3) { c = [c[0], c[0], c[1], c[1], c[2], c[2]]; } c = '0x' + c.join(''); return 'rgba(' + [(c >> 16) & 255, (c >> 8) & 255, c & 255].join(',') + ',0.2)'; } // It's already in rgba format return hex; } // Get all the available forms for a user function getAvailableForms() { myFormsAdminDataContext.getTemplatesICanManage().then(function (data) { $scope.availableForms = []; for (i in data) { if (data[i].isPublished && data[i].formType != 5) { //don't include activity types $scope.availableForms.push(data[i]); } if ($scope.event.factors.length > 0 && data[i].id === $scope.event.factors[0].factorId) { $scope.selectedForm = data[i]; } } $scope.availableFormsLoaded = true; }); } if (!$scope.isStudent) getAvailableForms(); //Set the form template as the response type $scope.setSelectedForm = function (selectedForm) { $scope.selectedForm = selectedForm; if (selectedForm) { $scope.event.factors.push({ factorId: selectedForm.id }); } } // Save the event $scope.saveEvent = function () { if ($scope.event.type === 'meeting') $scope.event.title = $scope.isStudent ? $scope.user.firstName + " " + $scope.user.lastName : $scope.eventUser.name; if ($scope.event.endsAt == null) { $scope.event.endsAt = $scope.event.startsAt; } if ($scope.event.allDay) { $scope.event.startsAt = moment($scope.event.startsAt).startOf('day').add(1, 'minutes').toDate(); $scope.event.endsAt = moment($scope.event.endsAt).endOf('day').subtract(1, 'minutes').toDate(); } if ($scope.event.id) { calendarDataContext.updateCalendarEvent($scope.event).then(function (data) { logSuccess("Event " + $scope.event.title + " updated"); getCalendars(); $modalInstance.dismiss('cancel'); }); } else { calendarDataContext.createCalendarEvent($scope.event).then(function (data) { logSuccess("Event " + $scope.event.title + " created"); getCalendars(); $modalInstance.dismiss('cancel'); if ($scope.event.type === 'meeting') { var o365MeetingRequest = { subject: $scope.event.title, body: $scope.event.description, start: $scope.event.startsAt, end: $scope.event.endsAt, attendees: $scope.user.email.email + ";" + $scope.isStudent ? $scope.tutor.email.email : $scope.eventUser.email, creator: $scope.user.email.email }; calendarDataContext.createo365Event(o365MeetingRequest).then(function (data) { console.log('Created o365 event'); }); } }); } } // Remove an event $scope.removeEvent = function () { calendarDataContext.deleteCalendarEvent($scope.event).then(function (data) { logSuccess("Log " + $scope.event.title + " removed"); getCalendars(); $modalInstance.dismiss('cancel'); }); } //close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; } var viewEventController = function (common, $scope, $modalInstance, event, calendar) { $scope.event = event; user.getUserProfile($scope.event.createdBy).then(function (data) { $scope.creator = data; }); if ($scope.event.factors.length > 0) { myFormsAdminDataContext.getFormTemplate($scope.event.factors[0].factorId).then(function (form) { $scope.selectedForm = form }); } //close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; } } }]); })();