').addClass('fc-event-title').text(event.title))));
const userIdList = event.extendedProps.userIdList || [];
userIdList.forEach(userId => {
const userName = event.extendedProps.userNameMap[userId] || '';
let avatarHtml = this.getHelper().getAvatarHtml(userId, 'small', 13);
if (avatarHtml) {
avatarHtml += ' ';
}
const $div = $('
').addClass('user').css({
overflow: 'hidden'
}).append(avatarHtml).append($('').text(userName));
$content.append($div);
});
return {
html: $content.get(0).innerHTML
};
};
}
if (!this.options.height) {
options.contentHeight = this.getCalculatedHeight();
} else {
options.aspectRatio = 1.62;
}
if (this.date) {
options.initialDate = this.date;
} else {
this.$el.find('button[data-action="today"]').addClass('active');
}
setTimeout(() => {
this.calendar = new FullCalendar.Calendar(this.$calendar.get(0), options);
this.calendar.render();
this.handleScrollToNow();
this.updateDate();
if (this.$container && this.$container.length) {
this.adjustSize();
}
}, 150);
}
/**
* @private
*/
handleScrollToNow() {
if (!(this.mode === 'agendaWeek' || this.mode === 'agendaDay')) {
return;
}
if (!this.isToday()) {
return;
}
const scrollHour = this.getDateTime().getNowMoment().hours() - Math.floor(this.slotDuration * this.scrollToNowSlots / 60);
if (scrollHour < 0) {
return;
}
this.calendar.scrollToTime(scrollHour + ':00');
}
/**
* @param {{
* [allDay]: boolean,
* [dateStart]: string,
* [dateEnd]: string,
* [dateStartDate]: ?string,
* [dateEndDate]: ?string,
* }} [values]
*/
async createEvent(values) {
values = values || {};
if (!values.dateStart && this.date !== this.getDateTime().getToday() && (this.mode === 'day' || this.mode === 'agendaDay')) {
values.allDay = true;
values.dateStartDate = this.date;
values.dateEndDate = this.date;
}
const attributes = {};
if (this.options.userId) {
attributes.assignedUserId = this.options.userId;
attributes.assignedUserName = this.options.userName || this.options.userId;
}
const scopeList = this.enabledScopeList.filter(it => !this.onlyDateScopeList.includes(it));
Espo.Ui.notifyWait();
const view = await this.createView('dialog', 'crm:views/calendar/modals/edit', {
attributes: attributes,
enabledScopeList: scopeList,
scopeList: this.scopeList,
allDay: values.allDay,
dateStartDate: values.dateStartDate,
dateEndDate: values.dateEndDate,
dateStart: values.dateStart,
dateEnd: values.dateEnd
});
let added = false;
this.listenTo(view, 'before:save', () => {
if (this.options.onSave) {
this.options.onSave();
}
});
this.listenTo(view, 'after:save', model => {
if (!added) {
this.addModel(model);
added = true;
return;
}
this.updateModel(model);
});
await view.render();
Espo.Ui.notify();
}
/**
* @private
* @param {string} from
* @param {string} to
* @param {function} callback
*/
fetchEvents(from, to, callback) {
let url = `Activities?from=${from}&to=${to}`;
if (this.options.userId) {
url += '&userId=' + this.options.userId;
}
url += '&scopeList=' + encodeURIComponent(this.enabledScopeList.join(','));
if (this.teamIdList && this.teamIdList.length) {
url += '&teamIdList=' + encodeURIComponent(this.teamIdList.join(','));
}
const agenda = this.mode === 'agendaWeek' || this.mode === 'agendaDay';
url += '&agenda=' + encodeURIComponent(agenda);
if (!this.suppressLoadingAlert) {
Espo.Ui.notifyWait();
}
Espo.Ajax.getRequest(url).then(data => {
const events = this.convertToFcEvents(data);
callback(events);
Espo.Ui.notify(false);
});
this.fetching = true;
this.suppressLoadingAlert = false;
setTimeout(() => this.fetching = false, 50);
}
/**
* @private
* @param {import('model').default} model
*/
addModel(model) {
const attributes = model.getClonedAttributes();
attributes.scope = model.entityType;
const event = this.convertToFcEvent(attributes);
// true passed to prevent duplicates after re-fetch.
this.calendar.addEvent(event, true);
}
/**
* @private
* @param {import('model').default} model
*/
updateModel(model) {
const eventId = model.entityType + '-' + model.id;
const event = this.calendar.getEventById(eventId);
if (!event) {
return;
}
const attributes = model.getClonedAttributes();
attributes.scope = model.entityType;
const data = this.convertToFcEvent(attributes);
this.applyPropsToEvent(event, data);
}
/**
* @private
* @param {EventImpl} event
* @return {module:modules/crm/views/calendar/calendar~FcEvent}
*/
obtainPropsFromEvent(event) {
const props = {};
for (const key in event.extendedProps) {
props[key] = event.extendedProps[key];
}
props.allDay = event.allDay;
props.start = event.start;
props.end = event.end;
props.title = event.title;
props.id = event.id;
props.color = event.color;
return props;
}
/**
* @private
* @param {EventImpl} event
* @param {{start?: Date, end?: Date, allDay: boolean} & Record} props
*/
applyPropsToEvent(event, props) {
if ('start' in props) {
if (!props.allDay && props.end && props.end.getTime() === props.start.getTime()) {
// Otherwise, 0-duration event would disappear.
props.end = (0, _moment.default)(props.end).add(1, 'hour').toDate();
}
event.setDates(props.start, props.end, {
allDay: props.allDay
});
}
for (const key in props) {
const value = props[key];
if (key === 'start' || key === 'end' || key === 'allDay') {
continue;
}
if (key === 'className') {
event.setProp('classNames', value);
continue;
}
if (this.extendedProps.includes(key)) {
event.setExtendedProp(key, value);
continue;
}
event.setProp(key, value);
}
}
/**
* @private
* @param {import('model').default} model
*/
removeModel(model) {
const event = this.calendar.getEventById(model.entityType + '-' + model.id);
if (!event) {
return;
}
event.remove();
}
/**
* @param {{suppressLoadingAlert: boolean}} [options]
*/
actionRefresh(options) {
if (options && options.suppressLoadingAlert) {
this.suppressLoadingAlert = true;
}
this.calendar.refetchEvents();
}
actionPrevious() {
this.calendar.prev();
this.handleScrollToNow();
this.updateDate();
}
actionNext() {
this.calendar.next();
this.handleScrollToNow();
this.updateDate();
}
/**
* @private
* @param {string} scope
* @return {string|undefined}
*/
getColorFromScopeName(scope) {
const additionalColorList = this.getMetadata().get('clientDefs.Calendar.additionalColorList') || [];
if (!additionalColorList.length) {
return;
}
const colors = this.getMetadata().get('clientDefs.Calendar.colors') || {};
const scopeList = this.getConfig().get('calendarEntityList') || [];
let index = 0;
let j = 0;
for (let i = 0; i < scopeList.length; i++) {
if (scopeList[i] in colors) {
continue;
}
if (scopeList[i] === scope) {
index = j;
break;
}
j++;
}
index = index % additionalColorList.length;
this.colors[scope] = additionalColorList[index];
return this.colors[scope];
}
actionToday() {
if (this.isToday()) {
this.actionRefresh();
return;
}
this.calendar.today();
this.handleScrollToNow();
this.updateDate();
}
}
var _default = _exports.default = CalendarView;
});