-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdateDisplay.js
200 lines (177 loc) · 6.91 KB
/
dateDisplay.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
/**
* @author Thomas Lane
* @requires Date.js
* @version $Revision: 229 $
* $Date: 2011-01-16 19:08:49 -0800 (Sun, 16 Jan 2011) $
* @modified G.E. Eidsness
* @version $Revision: 030 $
* $Date: 2023-11-17 00:46:39 -0700 (Fri, 17 Nov 2023) $
* $Date: 2024-09-24 10:06:39 -0700 (Tue, 24 Sept 2024) $
*/
/**
* @class DateDisplay
* Implements a weekly and monthly display of days of week
*/
class DateDisplay {
/**
* Creates a new DateDisplay instance.
*
* @param {HTMLElement} dateNode - The output HTMLElement for displaying the calendar.
* @param {HTMLElement} titleNode - The output HTMLElement for displaying the date display title.
* @param {HTMLElement} detailsNode - The output HTMLElement for displaying the selected movie showing.
* @param {Date} activeDay - The active date for the date display.
*/
constructor(dateNode, titleNode, detailsNode, activeDay) {
this._displayNode = dateNode;
this._titleNode = titleNode;
this._detailsNode = detailsNode;
this.setDate(activeDay);
};
/**
* Removes all of the textnode children from the displayNode
* post: _displayNode has no children
* @private
*/
_clearDisplayDate() {
while (this._displayNode.firstChild) {
this._displayNode.removeChild(this._displayNode.firstChild);
}
};
/**
* Appends a 2 letter day of the week abbreviations to the top of the date display
* @private
*/
_appendHeading(type) {
const dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",];
let heading = document.createElement("div");
heading.id = "headingContainer";
heading.className = type;
let fragment = document.createDocumentFragment();
for (let i in dayNames) {
let name = document.createTextNode(dayNames[i]);
let div = document.createElement("div");
div.className = "dayName";
div.appendChild(name);
fragment.appendChild(div);
}
heading.appendChild(fragment);
this._displayNode.appendChild(heading);
};
/**
* Appends a series of child textnodes each day of the a week to the _displayNode element
* followed by a single br element. Also sets the Title for the date display to the
* active days month and year
* @private
* @param {Date} day the date object used to loop through days in the week
*/
_appendWeek(day, type) {
let weekContainer = document.createElement("div");
let currentDate = new Date().getDelimDate();
let currentMonth = new Date().getMonth();
let currentYear = new Date().getFullYear();
weekContainer.id = "weekContainer";
weekContainer.className = type;
let fragment = document.createDocumentFragment();
for (let weekIterator = day.getWeekStart(); weekIterator <= day.getWeekEnd(); weekIterator.incrementByDay()) {
let dayText = weekIterator.getDate();
let weekIteratorDate = weekIterator.getDelimDate();
let weekIteratorMonth = weekIterator.getMonth();
let weekIteratorYear = weekIterator.getFullYear();
let dateNumber = document.createTextNode(dayText.toString().padStart(2, "0") + " ");
let divSpacer = document.createElement("div");
divSpacer.className = "divSpacer";
let dateNumDiv = document.createElement("div");
dateNumDiv.className = "dateNum";
dateNumDiv.appendChild(dateNumber);
let div = document.createElement("div");
div.appendChild(divSpacer);
div.appendChild(dateNumDiv);
let showings = jsonShowings[weekIteratorDate];
if (showings) {
// Convert showings object to array and sort by date ascending
let sortedShowings = Object.values(showings).sort((a, b) => {
return new Date(a.date) - new Date(b.date);
});
for (let show of sortedShowings) {
let showingsShowDate = new Date(show.date);
let hours = showingsShowDate.getUTCHours().toString().padStart(2, '0');
let minutes = showingsShowDate.getMinutes().toString().padStart(2, '0');
let movieData = `${hours}:${minutes} - ${show.title}`;
let listing = `<div id="${showingsShowDate.toISOString()}" class="listing">${movieData}</div>`;
if (type == "week") {
let movieDescription = `${show.descr.substring(0, 120)}...`;
listing += `${movieDescription}`;
}
div.innerHTML += listing;
}
}
div.id = weekIteratorDate;
if (div.id == currentDate && weekIteratorMonth == this._date.getMonth() && weekIteratorYear == this._date.getFullYear()) {
div.className = "today";
requestAnimationFrame(() => {
div.style.backgroundColor = "#ede2c5";
});
} else if (type == "week" || weekIteratorMonth == this._date.getMonth()) {
div.className = "dateBlock";
} else {
div.className = "blankDateBlock";
}
fragment.appendChild(div);
}
weekContainer.appendChild(fragment);
this._displayNode.appendChild(weekContainer);
this._displayNode.className = type;
};
/**
* Sets the _titleNode to the Month Name and Year for the active date
* @private
*/
_setTitle() {
if (this._titleNode.firstChild) {
this._titleNode.removeChild(this._titleNode.firstChild);
} // current month and year
this._titleNode.appendChild(document.createTextNode(
this._date.getMonthWord() + " " + this._date.getFullYear()
)
);
};
/**
* Clears all children from _displayNode
* Appends a series of child nodes for each week of the month to _displayNode
* and sets the Title for the date display to the active days month and year.
*/
displayMonth() {
let type = "month";
this._clearDisplayDate();
this._appendHeading(type);
for (
let monthIterator = this._date.getMonthStart(); //start at beginning of month
monthIterator <= this._date.getMonthEnd().getWeekEnd(); //loop until the end of the last week that the month end falls in
monthIterator.incrementByWeek() //increment the iterator by a week
) {
this._appendWeek(monthIterator, type);
}
//this._loadTodaysListing();
this._setTitle();
};
/**
* Clears all children from _displayNode
* Appends a series of child nodes for each day of the week to _displayNode
* and sets the Title for the date display to the active days month and year.
*/
displayWeek() {
//If the display has previously been populated clear it
let type = "week";
this._clearDisplayDate();
this._appendHeading(type);
this._appendWeek(this._date, type);
this._setTitle();
};
/**
* Sets the active date for the date display
* @param {Date} date used used for setting date of date display
*/
setDate(date) {
this._date = new Date(date);
};
};