-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
178 lines (135 loc) · 5.39 KB
/
script.js
File metadata and controls
178 lines (135 loc) · 5.39 KB
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
$(document).ready(function () {
// test flag
const test = false;
// get times from moment
const now = moment().format('MMMM Do YYYY');
// commented out for test in non-standard hours
let nowHour24 = moment().format('H');
let nowHour12 = moment().format('h');
// set times for testing after hours
if (test) {
nowHour24 = 13;
nowHour12 = 1;
}
let $dateHeading = $('#navbar-subtitle');
$dateHeading.text(now);
// using font awesome icon https://fontawesome.com/license
// change description here - none
const saveIcon = "./images/save-regular.svg";
// Get stored to dos from localStorage
// Parsing the JSON string to an object
let storedPlans = JSON.parse(localStorage.getItem("storedPlans"));
if (test) { console.log(storedPlans); }
// If plans were retrieved from localStorage, update the plan array to it
if (storedPlans !== null) {
planTextArr = storedPlans;
} else {
// this should only occur on first time the app is loaded in the browser
// helpfully remind user that lunch is important
planTextArr = new Array(9);
planTextArr[4] = "Let me help plan your day";
}
if (test) { console.log("full array of planed text", planTextArr); }
// set variable referencing planner element
let $plannerDiv = $('#plannerContainer');
// clear existing elements
$plannerDiv.empty();
if (test) { console.log("current time", nowHour12); }
// build calendar by row for fix set of hours
for (let hour = 9; hour <= 17; hour++) {
// index for array use offset from hour
let index = hour - 9;
// build row components
let $rowDiv = $('<div>');
$rowDiv.addClass('row');
$rowDiv.addClass('plannerRow');
$rowDiv.attr('hour-index', hour);
// Start building Time box portion of row
let $col2TimeDiv = $('<div>');
$col2TimeDiv.addClass('col-md-2');
// create timeBox element (contains time)
const $timeBoxSpn = $('<span>');
// can use this to get value
$timeBoxSpn.attr('class', 'timeBox');
// format hours for display
let displayHour = 0;
let amPm = "";
if (hour > 12) {
displayHour = hour - 12;
amPm = "pm";
} else {
displayHour = hour;
amPm = "am";
}
// populate timeBox with time
$timeBoxSpn.text(`${displayHour} ${amPm}`);
$rowDiv.append($col2TimeDiv);
$col2TimeDiv.append($timeBoxSpn);
// STOP building Time box portion of row
// START building input portion of row
// build row components
let $dailyPlanSpn = $('<input>');
$dailyPlanSpn.attr('id', `input-${index}`);
$dailyPlanSpn.attr('hour-index', index);
$dailyPlanSpn.attr('type', 'text');
$dailyPlanSpn.attr('class', 'dailyPlan');
// access index from data array for hour
$dailyPlanSpn.val(planTextArr[index]);
// create col to control width
let $col9IptDiv = $('<div>');
$col9IptDiv.addClass('col-md-9');
// add col width and row component to row
$rowDiv.append($col9IptDiv);
$col9IptDiv.append($dailyPlanSpn);
// STOP building Time box portion of row
// START building save portion of row
let $col1SaveDiv = $('<div>');
$col1SaveDiv.addClass('col-md-1');
let $saveBtn = $('<i>');
$saveBtn.attr('id', `saveId-${index}`);
$saveBtn.attr('save-id', index);
$saveBtn.attr('class', "far fa-save saveIcon");
$rowDiv.append($col1SaveDiv);
$col1SaveDiv.append($saveBtn);
// set row color based on time
updateRowColor($rowDiv, hour);
// add row to planner container
$plannerDiv.append($rowDiv);
};
// function to update row color
function updateRowColor($hourRow, hour) {
if (test) { console.log("rowColor ", nowHour24, hour); }
if (hour < nowHour24) {
// $hourRow.css('')
if (test) { console.log("lessThan"); }
$hourRow.css("background-color", "darkGray")
} else if (hour > nowHour24) {
if (test) { console.log("greaterThan"); }
$hourRow.css("background-color", "lightGreen")
} else {
if (test) { console.log("equal"); }
$hourRow.css("background-color", "lightPink")
}
};
$(document).on('click', 'i', function (event) {
event.preventDefault();
if (test) { console.log('click pta before ' + planTextArr); }
let $index = $(this).attr('save-id');
let inputId = '#input-' + $index;
let $value = $(inputId).val();
planTextArr[$index] = $value;
if (test) { console.log('value ', $value); }
if (test) { console.log('index ', $index); }
if (test) { console.log('click pta after ' + planTextArr); }
// id tag for save button
$(`#saveId-${$index}`).removeClass('shadowPulse');
localStorage.setItem("storedPlans", JSON.stringify(planTextArr));
});
$(document).on('change', 'input', function (event) {
event.preventDefault();
if (test) { console.log('onChange'); }
if (test) { console.log('id', $(this).attr('hour-index')); }
let i = $(this).attr('hour-index');
$(`#saveId-${i}`).addClass('shadowPulse');
});
});