-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework4.js
155 lines (129 loc) · 3.72 KB
/
homework4.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
/////////////////////////////////////////////////////////////
// Title: Homework 3
// Author: Brian Waters
/////////////////////////////////////////////////////////////
///////////////////////////////////////////
// ------------- Functions ------------- //
///////////////////////////////////////////
// ------------- Homework Functions ------------- //
// 1. Create div and table within that div
function createDatePicker(month, year) {
var tabObj, tabClassName = 'DP main_table';
tabObj = genTable(tabClassName);
appendHeader(tabObj, month, year);
appendDayTitleRow(tabObj);
fillDates(tabObj, month, year);
}
// ------------- Extra Functions ------------- //
// Create table with id = Name
function genTable(className) {
var table = document.createElement('table');
table = addDOMClassName(table, className);
document.body.appendChild(table);
return table;
}
// Generate header for Datepicker table
function appendHeader(table, month, year) {
var header = table.createTHead();
var row = header.insertRow();
var headerCell = document.createElement('th');
var monthNames = [ 'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'];
headerCell.colSpan = 7;
headerCell.innerHTML = '< ' + monthNames[month] + ' ' + year + ' >';
headerCell = addDOMClassName(headerCell, 'DP table_header');
row.appendChild(headerCell);
}
// Generate a row for the days of the week
function appendDayTitleRow(table) {
var row = table.insertRow();
var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
var daysLen = days.length;
var tmp;
for (var i=0; i<daysLen; i++) {
tmp = row.insertCell(i);
tmp.innerHTML = days[i];
}
row = addDOMClassName(row, 'DP border_bottom');
}
// Fill in dates for each day in the specified month
function fillDates(table, month, year) {
var day = new Date(year, month);
var initDay = day.getDay();
var currMonth = day.getMonth();
var row, cell;
// Ensure we have first day of the week
if (initDay !== 0) {
day.setDate(day.getDate() - initDay);
}
// Update day and check for
currDay = day.getDate();
while (currMonth === month) {
row = table.insertRow();
for (var i=0; i<=6; i++) {
cell = row.insertCell();
currMonth = day.getMonth();
currDay = day.getDate();
cell.innerHTML = currDay;
if (currMonth !== month) {
cell = addDOMClassName(cell,'DP faded_cell');
}
else {
cell = addDOMClassName(cell,'DP normal_cell');
}
day.setDate(day.getDate() + 1);
}
currMonth = day.getMonth();
}
}
// Add a class name to DOM object provided it doesn't already exist
function addDOMClassName(domObj, newClassName) {
var origClassName = domObj.className;
domObj.className = appendWordToString(origClassName, newClassName);
return domObj;
}
// Append word to string if that string doesn't
// already appear in the string
function appendWordToString(str, word) {
var arr,
cleanStr,
cleanWord,
hasWord;
// Clean the string
cleanStr = cleanWhitespace(str);
cleanWord = word.trim();
arr = cleanStr.split(' ');
// Check if arr has word already
hasWord = checkElementInArray(arr, cleanWord);
if (!hasWord) {
arr.push(cleanWord);
cleanStr = arr.join(' ');
}
return cleanStr;
}
// Trim beginning and end whitespace and remove multiple
// internal whitespaces
function cleanWhitespace(str) {
str = str.replace(/\s+/g,' ').trim();
return str;
}
// Check if element exists in array
function checkElementInArray(arr, elem) {
var index = arr.indexOf(elem);
if (index > -1) return true;
return false;
}
///////////////////////////////////////////
// --------------- Main ---------------- //
///////////////////////////////////////////
createDatePicker(07,2015);