This repository has been archived by the owner on Jun 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 367
/
mwlCalendarMonth.js
119 lines (97 loc) · 3.27 KB
/
mwlCalendarMonth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict';
var angular = require('angular');
angular
.module('mwl.calendar')
.controller('MwlCalendarMonthCtrl', function($scope, moment, calendarHelper, calendarConfig) {
var vm = this;
vm.calendarConfig = calendarConfig;
vm.openRowIndex = null;
$scope.$on('calendar.refreshView', function() {
vm.weekDays = calendarHelper.getWeekDayNames();
vm.view = calendarHelper.getMonthView(vm.events, vm.viewDate, vm.cellModifier);
var rows = Math.floor(vm.view.length / 7);
vm.monthOffsets = [];
for (var i = 0; i < rows; i++) {
vm.monthOffsets.push(i * 7);
}
//Auto open the calendar to the current day if set
if (vm.cellIsOpen && vm.openRowIndex === null) {
vm.openDayIndex = null;
vm.view.forEach(function(day) {
if (day.inMonth && moment(vm.viewDate).startOf('day').isSame(day.date)) {
vm.dayClicked(day, true);
}
});
}
});
vm.dayClicked = function(day, dayClickedFirstRun, $event) {
if (!dayClickedFirstRun) {
vm.onTimespanClick({
calendarDate: day.date.toDate(),
$event: $event
});
if ($event && $event.defaultPrevented) {
return;
}
}
vm.openRowIndex = null;
var dayIndex = vm.view.indexOf(day);
if (dayIndex === vm.openDayIndex) { //the day has been clicked and is already open
vm.openDayIndex = null; //close the open day
vm.cellIsOpen = false;
} else {
vm.openDayIndex = dayIndex;
vm.openRowIndex = Math.floor(dayIndex / 7);
vm.cellIsOpen = true;
}
};
vm.highlightEvent = function(event, shouldAddClass) {
vm.view.forEach(function(day) {
delete day.highlightClass;
if (shouldAddClass) {
var dayContainsEvent = day.events.indexOf(event) > -1;
if (dayContainsEvent) {
day.highlightClass = 'day-highlight dh-event-' + event.type;
}
}
});
};
vm.handleEventDrop = function(event, newDayDate, draggedFromDate) {
var newStart = moment(event.startsAt)
.date(moment(newDayDate).date())
.month(moment(newDayDate).month());
var newEnd = calendarHelper.adjustEndDateFromStartDiff(event.startsAt, newStart, event.endsAt);
vm.onEventTimesChanged({
calendarEvent: event,
calendarDate: newDayDate,
calendarNewEventStart: newStart.toDate(),
calendarNewEventEnd: newEnd ? newEnd.toDate() : null,
calendarDraggedFromDate: draggedFromDate
});
};
})
.directive('mwlCalendarMonth', function(calendarConfig) {
return {
templateUrl: calendarConfig.templates.calendarMonthView,
restrict: 'E',
require: '^mwlCalendar',
scope: {
events: '=',
viewDate: '=',
onEventClick: '=',
onEditEventClick: '=',
onDeleteEventClick: '=',
onEventTimesChanged: '=',
editEventHtml: '=',
deleteEventHtml: '=',
cellIsOpen: '=',
onTimespanClick: '=',
cellModifier: '='
},
controller: 'MwlCalendarMonthCtrl as vm',
link: function(scope, element, attrs, calendarCtrl) {
scope.vm.calendarCtrl = calendarCtrl;
},
bindToController: true
};
});