forked from MMM-CalendarExt2/MMM-CalendarExt2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCALEXT2_Event.js
201 lines (188 loc) · 6.91 KB
/
CALEXT2_Event.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
class Event {
constructor(data, slot) {
this.data = data
this.locale = slot.locale
this.mStart = moment.unix(data.startDate).locale(this.locale)
this.mEnd = moment.unix(data.endDate).locale(this.locale)
this.sStart = moment(slot.start).locale(this.locale)
this.sEnd = moment(slot.end).locale(this.locale)
this.useEventTimeRelative = slot.useEventTimeRelative
this.relativeFormat = slot.relativeFormat
this.timeFormat = slot.timeFormat
this.dateFormat = slot.dateFormat
this.dateTimeFormat = slot.dateTimeFormat
this.dom = this.makeEventDom()
}
destroy() {
this.dom.remove()
for (var property in this) {
if (this.hasOwnProperty(property)) {
this[property] = null
}
}
}
draw(slot, targetDom) {
var useEventTimeRelative = slot.useEventTimeRelative
var hideOverflow = slot.hideOverflow
var eventDom = this.dom
eventDom.style.opacity = 0
targetDom.appendChild(eventDom)
if (hideOverflow) {
targetDom.classList.add("hideOverflow")
targetDom.style.maxHeight = slot.maxHeight
targetDom.style.height = slot.maxHeight
var contentRect = targetDom.getBoundingClientRect()
var eventRect = eventDom.getBoundingClientRect()
if (eventRect.bottom > contentRect.bottom || eventRect.right > contentRect.right) {
eventDom.classList.add("hiddenEvent")
return 1
}
}
eventDom.style.opacity = 1
return 0
}
drawSleeve(slot, sleeve) {
// to implement
}
makeEventDom() {
var event = this.data
var locale = this.locale
var now = moment().locale(locale)
var isEventMultiSlots = (event, mESX, mEEX, mSSX, mSEX) => {
var isMulti = false
if (event.duration > 86400) {
isMulti = true
} else {
if (mESX.isBefore(mSSX) || mEEX.isAfter(mSEX)) {
isMulti = true
}
}
return isMulti
}
var eventDom = document.createElement("div")
eventDom.classList.add("event")
var isPassed = (moment.unix(event.endDate).isBefore(now)) ? true : false
var isFullday = (event.isFullday) ? true : false
var isMultiSlots = isEventMultiSlots(event, this.mStart, this.mEnd, this.sStart, this.sEnd)
var isTargetDay = (this.mStart.isBefore(moment(now).endOf("day")) || this.mEnd.isAfter(moment(now).startOf("day"))) ? true : false
var isNow = (now.isBetween(this.mStart, this.mEnd, null, "[)")) ? true : false
if (isFullday) {
eventDom.classList.add("fullday")
} else {
eventDom.classList.add("notfullday")
}
if (isMultiSlots) eventDom.classList.add("overslot")
if (isTargetDay) eventDom.classList.add("targetday")
if (isNow) eventDom.classList.add("now")
if (isPassed) {
eventDom.classList.add("passed")
}
if (event.isMoment) eventDom.classList.add("moment")
if (event.isOneday) {
eventDom.classList.add("oneday")
} else {
eventDom.classList.add("overday")
}
if (event.isRecurring) {
eventDom.classList.add("recurred")
}
if (event.startHere) eventDom.classList.add("starthere")
if (event.endHere) eventDom.classList.add("endhere")
if (event.className) eventDom.classList.add(event.className)
eventDom.dataset.calendarName = event.calendarName
eventDom.dataset.calendarSeq = event.calendarSeq
eventDom.dataset.startDate = event.startDate
eventDom.dataset.endDate = event.endDate
eventDom.dataset.duration = event.duration
eventDom.dataset.title = event.title
eventDom.dataset.location = event.location
var mainDom = document.createElement("div")
mainDom.classList.add("eventMain")
if(event.icon) {
var iconDom = document.createElement("div")
iconDom.classList.add("iconify", "eventIcon")
iconDom.dataset.icon = event.icon
mainDom.appendChild(iconDom)
}
var title = document.createElement("div")
title.innerHTML = event.title
title.classList.add("eventTitle")
mainDom.appendChild(title)
var time = this.createEventTime()
mainDom.appendChild(time)
eventDom.appendChild(mainDom)
var subDom = document.createElement("div")
subDom.classList.add("eventSub")
var description = document.createElement("div")
description.classList.add("eventDescription")
description.innerHTML = event.description
subDom.appendChild(description)
var location = document.createElement("div")
location.classList.add("eventLocation")
location.innerHTML = event.location
subDom.appendChild(location)
eventDom.appendChild(subDom)
return eventDom
}
createEventTime() {
var locale = this.locale
var event = this.data
var makeEventTime = (innerHTML, classNameString) => {
var div = document.createElement("div")
var arr = classNameString.split(" ")
arr.forEach((className)=> {
div.classList.add(className)
})
div.innerHTML = innerHTML
return div
}
var dur = moment.duration(event.duration, "seconds").locale(locale)
var time = document.createElement("div")
time.classList.add("eventTime")
var now = moment().locale(locale)
if (this.useEventTimeRelative) {
var status = "current"
if (this.mEnd.isBefore(now)) status = "passed"
if (this.mStart.isAfter(now)) status = "future"
var timeDom = document.createElement("div")
timeDom.classList.add("relative")
timeDom.classList.add(status)
timeDom.innerHTML
= this.relativeFormat[status]
.replace("%ENDFROMNOW%", this.mEnd.fromNow())
.replace("%STARTFROMNOW%", this.mStart.fromNow())
.replace("%DURATION%", dur.humanize())
time.appendChild(timeDom)
} else {
var sd, ed, st, et, sdt, edt = null
if (typeof this.dateFormat == "object") {
sd = this.mStart.calendar(null, this.dateFormat)
ed = this.mEnd.calendar(null, this.dateFormat)
} else {
sd = this.mStart.format(this.dateFormat)
ed = this.mEnd.format(this.dateFormat)
}
if (typeof this.timeFormat == "object") {
st = this.mStart.calendar(null, this.timeFormat)
et = this.mEnd.calendar(null, this.timeFormat)
} else {
st = this.mStart.format(this.timeFormat)
et = this.mEnd.format(this.timeFormat)
}
if (typeof this.dateTimeFormat == "object") {
sdt = this.mStart.calendar(null, this.dateTimeFormat)
edt = this.mEnd.calendar(null, this.dateTimeFormat)
} else {
sdt = this.mStart.format(this.dateTimeFormat)
edt = this.mEnd.format(this.dateTimeFormat)
}
time.appendChild(makeEventTime(sd, "startDate start date"))
time.appendChild(makeEventTime(st, "startTime start time"))
time.appendChild(makeEventTime(sdt, "startDateTime start dateTime"))
time.appendChild(makeEventTime(ed, "endDate end date"))
time.appendChild(makeEventTime(et, "endTime end time"))
time.appendChild(makeEventTime(edt, "endDateTime end dateTime"))
}
return time
}
}