(js) Improve Component service & controller

Fixes #4527
This commit is contained in:
Francis Lachapelle
2018-08-22 14:52:44 -04:00
parent 9f654d223b
commit 3e5a486a1b
2 changed files with 197 additions and 223 deletions
@@ -6,63 +6,52 @@
/**
* @ngInject
*/
ComponentController.$inject = ['$rootScope', '$mdDialog', 'Calendar', 'Component', 'AddressBook', 'Alarm', 'Account', 'stateComponent'];
function ComponentController($rootScope, $mdDialog, Calendar, Component, AddressBook, Alarm, Account, stateComponent) {
ComponentController.$inject = ['$rootScope', '$q', '$mdDialog', 'Calendar', 'Component', 'AddressBook', 'Alarm', 'Account', 'stateComponent'];
function ComponentController($rootScope, $q, $mdDialog, Calendar, Component, AddressBook, Alarm, Account, stateComponent) {
var vm = this, component;
vm.calendarService = Calendar;
vm.service = Component;
vm.component = stateComponent;
vm.close = close;
vm.highPriority = highPriority;
vm.cardFilter = cardFilter;
vm.newMessageWithAllRecipients = newMessageWithAllRecipients;
vm.newMessageWithRecipient = newMessageWithRecipient;
vm.edit = edit;
vm.editAllOccurrences = editAllOccurrences;
vm.reply = reply;
vm.replyAllOccurrences = replyAllOccurrences;
vm.deleteOccurrence = deleteOccurrence;
vm.deleteAllOccurrences = deleteAllOccurrences;
vm.toggleRawSource = toggleRawSource;
vm.copySelectedComponent = copySelectedComponent;
vm.moveSelectedComponent = moveSelectedComponent;
this.$onInit = function () {
this.calendarService = Calendar;
this.service = Component;
this.component = stateComponent;
// Put organizer in an array to display it as an mdChip
vm.organizer = [stateComponent.organizer];
// Put organizer in an array to display it as an mdChip
this.organizer = [stateComponent.organizer];
};
function close() {
this.close = function () {
$mdDialog.hide();
}
};
function highPriority() {
return (vm.component &&
vm.component.priority &&
vm.component.priority < 5);
}
this.highPriority = function () {
return (this.component &&
this.component.priority &&
this.component.priority < 5);
};
// Autocomplete cards for attendees
function cardFilter($query) {
this.cardFilter = function ($query) {
return AddressBook.$filterAll($query);
}
};
function newMessageWithAllRecipients($event) {
var recipients = _.map(vm.component.attendees, function(attendee) {
this.newMessageWithAllRecipients = function ($event) {
var recipients = _.map(this.component.attendees, function(attendee) {
return attendee.name + " <" + attendee.email + ">";
});
_newMessage($event, recipients);
}
};
function newMessageWithRecipient($event, name, email) {
this.newMessageWithRecipient = function ($event, name, email) {
_newMessage($event, [name + " <" + email + ">"]);
}
};
function _newMessage($event, recipients) {
Account.$findAll().then(function(accounts) {
var account = _.find(accounts, function(o) {
if (o.id === 0)
return o;
});
}),
onCompleteDeferred = $q.defer();
// We must initialize the Account with its mailbox
// list before proceeding with message's creation
@@ -77,9 +66,15 @@
templateUrl: '../Mail/UIxMailEditor',
controller: 'MessageEditorController',
controllerAs: 'editor',
onComplete: function (scope, element) {
return onCompleteDeferred.resolve(element);
},
locals: {
stateAccount: account,
stateMessage: message
stateMessage: message,
onCompletePromise: function () {
return onCompleteDeferred.promise;
}
}
});
});
@@ -90,8 +85,8 @@
$event.stopPropagation();
}
function edit() {
var type = (vm.component.component == 'vevent')? 'Appointment':'Task';
this.edit = function () {
var type = (this.component.component == 'vevent')? 'Appointment':'Task';
$mdDialog.hide().then(function() {
// UI/Templates/SchedulerUI/UIxAppointmentEditorTemplate.wox or
// UI/Templates/SchedulerUI/UIxTaskEditorTemplate.wox
@@ -108,29 +103,29 @@
}
});
});
}
};
function editAllOccurrences() {
component = Calendar.$get(vm.component.pid).$getComponent(vm.component.id);
this.editAllOccurrences = function () {
component = Calendar.$get(this.component.pid).$getComponent(this.component.id);
component.$futureComponentData.then(function() {
vm.component = component;
edit();
vm.edit();
});
}
};
function reply(component) {
var c = component || vm.component;
this.reply = function (component) {
var c = component || this.component;
c.$reply().then(function() {
$rootScope.$emit('calendars:list');
Alarm.getAlarms();
$mdDialog.hide();
});
}
};
function replyAllOccurrences() {
this.replyAllOccurrences = function () {
// Retrieve master event
component = Calendar.$get(vm.component.pid).$getComponent(vm.component.id);
component = Calendar.$get(this.component.pid).$getComponent(this.component.id);
component.$futureComponentData.then(function() {
// Propagate the participant status and alarm to the master event
component.reply = vm.component.reply;
@@ -138,26 +133,26 @@
component.$hasAlarm = vm.component.$hasAlarm;
component.alarm = vm.component.alarm;
// Send reply to the server
reply(component);
vm.reply(component);
});
}
};
function deleteOccurrence() {
vm.component.remove(true).then(function() {
this.deleteOccurrence = function () {
this.component.remove(true).then(function() {
$rootScope.$emit('calendars:list');
$mdDialog.hide();
});
}
};
function deleteAllOccurrences() {
vm.component.remove().then(function() {
this.deleteAllOccurrences = function () {
this.component.remove().then(function() {
$rootScope.$emit('calendars:list');
$mdDialog.hide();
});
}
};
function toggleRawSource($event) {
Calendar.$$resource.post(vm.component.pid + '/' + vm.component.id, "raw").then(function(data) {
this.toggleRawSource = function ($event) {
Calendar.$$resource.post(this.component.pid + '/' + this.component.id, "raw").then(function(data) {
$mdDialog.hide();
$mdDialog.show({
parent: angular.element(document.body),
@@ -189,21 +184,21 @@
};
}
});
}
};
function copySelectedComponent(calendar) {
vm.component.copyTo(calendar).then(function() {
this.copySelectedComponent = function (calendar) {
this.component.copyTo(calendar).then(function() {
$mdDialog.hide();
$rootScope.$emit('calendars:list');
});
}
};
function moveSelectedComponent(calendar) {
vm.component.moveTo(calendar).then(function() {
this.moveSelectedComponent = function (calendar) {
this.component.moveTo(calendar).then(function() {
$mdDialog.hide();
$rootScope.$emit('calendars:list');
});
}
};
}
/**
@@ -213,93 +208,71 @@
function ComponentEditorController($rootScope, $scope, $log, $timeout, $mdDialog, focus, User, CalendarSettings, Calendar, Component, AddressBook, Card, Alarm, stateComponent) {
var vm = this, component, oldStartDate, oldEndDate, oldDueDate;
vm.service = Calendar;
vm.component = stateComponent;
vm.categories = {};
vm.showRecurrenceEditor = vm.component.$hasCustomRepeat;
vm.toggleRecurrenceEditor = toggleRecurrenceEditor;
vm.recurrenceMonthDaysAreRequired = recurrenceMonthDaysAreRequired;
vm.showAttendeesEditor = vm.component.attendees && vm.component.attendees.length;
vm.toggleAttendeesEditor = toggleAttendeesEditor;
//vm.searchText = null;
vm.changeFrequency = changeFrequency;
vm.changeCalendar = changeCalendar;
vm.cardFilter = cardFilter;
vm.addAttendee = addAttendee;
vm.removeAttendee = removeAttendee;
vm.addAttachUrl = addAttachUrl;
vm.priorityLevel = priorityLevel;
vm.changeAlarmRelation = changeAlarmRelation;
vm.onAlarmChange = onAlarmChange;
vm.reset = reset;
vm.cancel = cancel;
vm.edit = edit;
vm.save = save;
vm.attendeeConflictError = false;
vm.attendeesEditor = {
days: getDays(),
hours: getHours()
this.$onInit = function () {
this.service = Calendar;
this.component = stateComponent;
this.categories = {};
this.showRecurrenceEditor = this.component.$hasCustomRepeat;
this.showAttendeesEditor = this.component.attendees && this.component.attendees.length;
//this.searchText = null;
this.attendeeConflictError = false;
this.attendeesEditor = {
days: getDays(),
hours: getHours()
};
if (this.component.start)
oldStartDate = new Date(this.component.start.getTime());
if (this.component.end)
oldEndDate = new Date(this.component.end.getTime());
if (this.component.due)
oldDueDate = new Date(this.component.due.getTime());
};
vm.addStartDate = addStartDate;
vm.removeStartDate = removeStartDate;
vm.addDueDate = addDueDate;
vm.removeDueDate = removeDueDate;
// Synchronize start and end dates
vm.adjustStartTime = adjustStartTime;
vm.adjustEndTime = adjustEndTime;
vm.adjustDueTime = adjustDueTime;
if (vm.component.start)
oldStartDate = new Date(vm.component.start.getTime());
if (vm.component.end)
oldEndDate = new Date(vm.component.end.getTime());
if (vm.component.due)
oldDueDate = new Date(vm.component.due.getTime());
function addAttachUrl() {
var i = vm.component.addAttachUrl('');
this.addAttachUrl = function () {
var i = this.component.addAttachUrl('');
focus('attachUrl_' + i);
}
};
function toggleRecurrenceEditor() {
vm.showRecurrenceEditor = !vm.showRecurrenceEditor;
vm.component.$hasCustomRepeat = vm.showRecurrenceEditor;
}
this.toggleRecurrenceEditor = function () {
this.showRecurrenceEditor = !this.showRecurrenceEditor;
this.component.$hasCustomRepeat = this.showRecurrenceEditor;
};
function toggleAttendeesEditor() {
vm.showAttendeesEditor = !vm.showAttendeesEditor;
}
this.toggleAttendeesEditor = function () {
this.showAttendeesEditor = !this.showAttendeesEditor;
};
function recurrenceMonthDaysAreRequired() {
return vm.component &&
vm.component.repeat.frequency == 'monthly' &&
vm.component.repeat.month.type == 'bymonthday';
}
this.recurrenceMonthDaysAreRequired = function () {
return this.component &&
this.component.repeat.frequency == 'monthly' &&
this.component.repeat.month.type == 'bymonthday';
};
function changeFrequency() {
if (vm.component.repeat.frequency == 'custom')
vm.showRecurrenceEditor = true;
}
this.changeFrequency = function () {
if (this.component.repeat.frequency == 'custom')
this.showRecurrenceEditor = true;
};
function changeCalendar() {
var updateRequired = (vm.component.attendees && vm.component.attendees.length > 0);
this.changeCalendar = function () {
var updateRequired = (this.component.attendees && this.component.attendees.length > 0);
if (updateRequired)
vm.component.initOrganizer(Calendar.$get(vm.component.destinationCalendar));
}
this.component.initOrganizer(Calendar.$get(this.component.destinationCalendar));
};
// Autocomplete cards for attendees
function cardFilter($query) {
this.cardFilter = function ($query) {
AddressBook.$filterAll($query);
return AddressBook.$cards;
}
};
function addAttendee(card) {
var initOrganizer = (!vm.component.attendees || vm.component.attendees.length === 0),
destinationCalendar = Calendar.$get(vm.component.destinationCalendar),
this.addAttendee = function (card, partial) {
var initOrganizer = (!this.component.attendees || this.component.attendees.length === 0),
destinationCalendar = Calendar.$get(this.component.destinationCalendar),
options = initOrganizer? { organizerCalendar: destinationCalendar } : {};
var emailRE = /([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)/i,
i, address;
if (partial) options.partial = partial;
function createCard(str) {
var match = str.match(emailRE),
@@ -323,7 +296,7 @@
card.charCodeAt(i) == 44 || // ,
card.charCodeAt(i) == 59) && // ;
emailRE.test(address)) {
vm.component.addAttendee(createCard(address), options);
this.component.addAttendee(createCard(address), options);
address = '';
}
else {
@@ -331,59 +304,60 @@
}
}
if (address)
vm.component.addAttendee(createCard(address), options);
this.component.addAttendee(createCard(address), options);
}
else {
vm.component.addAttendee(card, options);
vm.showAttendeesEditor |= initOrganizer;
this.component.addAttendee(card, options);
this.showAttendeesEditor |= initOrganizer;
}
}
};
function removeAttendee(attendee, form) {
vm.component.deleteAttendee(attendee);
if (vm.component.attendees.length === 0)
vm.showAttendeesEditor = false;
this.removeAttendee = function (attendee, form) {
this.component.deleteAttendee(attendee);
if (this.component.attendees.length === 0)
this.showAttendeesEditor = false;
form.$setDirty();
}
};
function priorityLevel() {
if (vm.component && vm.component.priority) {
if (vm.component.priority > 5)
this.priorityLevel = function () {
if (this.component && this.component.priority) {
if (this.component.priority > 5)
return l('low'); // 6-7-8-9
else if (vm.component.priority > 4)
else if (this.component.priority > 4)
return l('normal'); // 5
else
return l('high'); // 1-2-3-4
}
}
};
function changeAlarmRelation(form) {
if (vm.component.type == 'task' && vm.component.$hasAlarm &&
((!vm.component.start && vm.component.alarm.relation == 'START') ||
(!vm.component.due && vm.component.alarm.relation == 'END'))) {
this.changeAlarmRelation = function (form) {
if (this.component.type == 'task' && this.component.$hasAlarm &&
(this.component.start || this.component.due) &&
((!this.component.start && this.component.alarm.relation == 'START') ||
(!this.component.due && this.component.alarm.relation == 'END'))) {
form.alarmRelation.$setValidity('alarm', false);
}
else {
form.alarmRelation.$setValidity('alarm', true);
}
}
};
function onAlarmChange(form) {
if (vm.component.type !== 'task') {
this.onAlarmChange = function (form) {
if (this.component.type !== 'task') {
return;
}
if (!vm.component.start && vm.component.alarm.relation == 'START') {
vm.component.alarm.relation = 'END';
} else if (!vm.component.due && vm.component.alarm.relation == 'END') {
vm.component.alarm.relation = 'START';
if (!this.component.start && this.component.alarm.relation == 'START') {
this.component.alarm.relation = 'END';
} else if (!this.component.due && this.component.alarm.relation == 'END') {
this.component.alarm.relation = 'START';
}
changeAlarmRelation(form);
}
this.changeAlarmRelation(form);
};
function save(form, options) {
changeAlarmRelation(form);
this.save = function (form, options) {
this.changeAlarmRelation(form);
if (form.$valid) {
vm.component.$save(options)
this.component.$save(options)
.then(function(data) {
$rootScope.$emit('calendars:list');
Alarm.getAlarms();
@@ -396,21 +370,21 @@
edit(form);
});
}
}
};
function reset(form) {
vm.component.$reset();
this.reset = function (form) {
this.component.$reset();
form.$setPristine();
}
};
function cancel(form) {
reset(form);
if (vm.component.isNew) {
this.cancel = function (form) {
this.reset(form);
if (this.component.isNew) {
// Cancelling the creation of a component
vm.component = null;
this.component = null;
}
$mdDialog.hide();
}
};
function edit(form) {
vm.attendeeConflictError = false;
@@ -438,77 +412,77 @@
return hours;
}
function addStartDate(form) {
vm.component.$addStartDate();
oldStartDate = new Date(vm.component.start.getTime());
if (!vm.component.due) {
vm.component.alarm.relation = 'START';
this.addStartDate = function (form) {
this.component.$addStartDate();
oldStartDate = new Date(this.component.start.getTime());
if (!this.component.due) {
this.component.alarm.relation = 'START';
}
changeAlarmRelation(form);
}
this.changeAlarmRelation(form);
};
function removeStartDate(form) {
vm.component.$deleteStartDate();
if (vm.component.due) {
vm.component.alarm.relation = 'END';
this.removeStartDate = function (form) {
this.component.$deleteStartDate();
if (this.component.due) {
this.component.alarm.relation = 'END';
}
changeAlarmRelation(form);
}
this.changeAlarmRelation(form);
};
function addDueDate(form) {
vm.component.$addDueDate();
oldDueDate = new Date(vm.component.due.getTime());
if (!vm.component.start) {
vm.component.alarm.relation = 'END';
this.addDueDate = function (form) {
this.component.$addDueDate();
oldDueDate = new Date(this.component.due.getTime());
if (!this.component.start) {
this.component.alarm.relation = 'END';
}
changeAlarmRelation(form);
}
this.changeAlarmRelation(form);
};
function removeDueDate(form) {
vm.component.$deleteDueDate();
if (vm.component.start) {
vm.component.alarm.relation = 'START';
this.removeDueDate = function (form) {
this.component.$deleteDueDate();
if (this.component.start) {
this.component.alarm.relation = 'START';
}
changeAlarmRelation(form);
}
this.changeAlarmRelation(form);
};
function adjustStartTime() {
if (vm.component.start) {
this.adjustStartTime = function () {
if (this.component.start) {
// Preserve the delta between the start and end dates
var delta;
delta = oldStartDate.valueOf() - vm.component.start.valueOf();
delta = oldStartDate.valueOf() - this.component.start.valueOf();
if (delta !== 0) {
oldStartDate = new Date(vm.component.start.getTime());
if (vm.component.type === 'appointment') {
vm.component.end = new Date(vm.component.start.getTime());
vm.component.end.addMinutes(vm.component.delta);
oldEndDate = new Date(vm.component.end.getTime());
oldStartDate = new Date(this.component.start.getTime());
if (this.component.type === 'appointment') {
this.component.end = new Date(this.component.start.getTime());
this.component.end.addMinutes(this.component.delta);
oldEndDate = new Date(this.component.end.getTime());
}
updateFreeBusy();
}
}
}
};
function adjustEndTime() {
if (vm.component.end) {
this.adjustEndTime = function () {
if (this.component.end) {
// The end date must be after the start date
var delta = oldEndDate.valueOf() - vm.component.end.valueOf();
var delta = oldEndDate.valueOf() - this.component.end.valueOf();
if (delta !== 0) {
delta = vm.component.start.minutesTo(vm.component.end);
delta = this.component.start.minutesTo(this.component.end);
if (delta < 0)
vm.component.end = new Date(oldEndDate.getTime());
this.component.end = new Date(oldEndDate.getTime());
else {
vm.component.delta = delta;
oldEndDate = new Date(vm.component.end.getTime());
this.component.delta = delta;
oldEndDate = new Date(this.component.end.getTime());
}
updateFreeBusy();
}
}
}
};
function adjustDueTime() {
oldDueDate = new Date(vm.component.due.getTime());
}
this.adjustDueTime = function () {
oldDueDate = new Date(this.component.due.getTime());
};
function updateFreeBusy() {
vm.attendeesEditor.days = getDays();