-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
280 lines (220 loc) · 8.32 KB
/
script.js
File metadata and controls
280 lines (220 loc) · 8.32 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//data formatter, adapted from: https://codereview.stackexchange.com/questions/184459/getting-the-date-on-yyyymmdd-format
function createDTStamp(date) {
let x = date;
let y = x.getFullYear().toString();
let m = (x.getMonth() + 1).toString();
let d = x.getDate().toString();
let h = x.getHours().toString();
let min = x.getMinutes().toString();
(min.length == 1) && (min = '0' + min);
(h.length == 1) && (h = '0' + h);
(d.length == 1) && (d = '0' + d);
(m.length == 1) && (m = '0' + m);
let DTStamp= y + m + d + "T" + h + min + "00";
return DTStamp;
}
//FORM VALIDATION
function submitForm() {
const summary = document.getElementById('summary').value;
const startDate = document.getElementById('startDate').value;//.replace(/-/g,'');
const startTime = document.getElementById('startTime').value;//.replace(':','');
const endDate = document.getElementById('endDate').value;//.replace(/-/g,'');
const endTime = document.getElementById('endTime').value;//.replace(':','');
console.log(startDate+startTime);
console.log(endDate+endTime);
if (summary == "") {
alert('Events must have a title.')
}
else if (startDate == "" || endDate == "") {
alert(`Events must be created with a START time and an END time.`);
}
else if (startDate+startTime >= endDate+endTime) {
alert(`Error: ${endTime} on ${endDate} comes before ${startTime} on ${startDate}`);
}
else {
createFile(createEvent());
}
}
//EVENT CREATOR
function createEvent() {
const date = new Date()
let event = "BEGIN:VCALENDAR" +
"\nVERSION:2.0" +
"\nPRODID:Team-Pine-Line" +
"\nCALSCALE:GREGORIAN" +
"\nBEGIN:VEVENT";
/*
"\nBEGIN:VTIMEZONE";
"TZID:Pacific/Honolulu\n" +
"TZURL:http://tzurl.org/zoneinfo-outlook/Pacific/Honolulu\n" +
"X-LIC-LOCATION:Pacific/Honolulu\n" +
"BEGIN:STANDARD\n" +
"TZOFFSETFROM:-1000\n" +
"TZOFFSETTO:-1000\n" +
"END:STANDARD\n" +
*/
//TZID
const tzSelect = document.getElementById('timezones');
let tzid = tzSelect.options[tzSelect.selectedIndex].getAttribute('timeZoneID');
let tzOffset = "";
let tzOffsetFrom = "";
let tzOffsetTo = "";
//GET DEFAULT/USER TIMEZONE
if (tzid === "Etc/UTC") {
tzOffset = (date.getTimezoneOffset() / -60).toString();
console.log(tzOffset);
for (let i = 0; i < tzSelect.length; i++) {
//console.log(tzSelect.options[i].value);
if (tzOffset === tzSelect.options[i].value.toString()) {
tzid = tzSelect.options[i].getAttribute('timeZoneId');
/*
tzOffsetFrom = tzSelect.options[i].getAttribute('gmtOffset');
if (tzSelect.options[i].getAttribute('useDaylightTime') == 1) {
tzOffsetTo = (Number(tzOffsetFrom) + 100).toString();
} else {
tzOffsetTo = tzOffsetFrom;
}
*/
break;
}
else {
tzid = "Etc/UTC";
}
}
}
/*
else {
tzOffsetFrom = tzSelect.options[tzSelect.selectedIndex].getAttribute('gmtOffset');
if (tzSelect.options[tzSelect.selectedIndex].getAttribute('useDaylightTime') == "1") {
tzOffsetTo = (Number(tzOffsetFrom) + 100).toString();
} else {
tzOffsetTo = tzOffsetFrom;
}
}
*/
event = event.concat(`\nTZID:${tzid}`);
//TIMEZONE OFFSET ADJUSTMENT
/*
event = event.concat("\nTZURL:TZURL:http://tzurl.org/zoneinfo-outlook/" + tzid);
event = event.concat("\nBEGIN:STANDARD");
event = event.concat("\nTZOFFSETFROM:${tzOffsetFrom);
event = event.concat("\nTZOFFSETTO:${tzOffsetTo);
event = event.concat("\nEND:STANDARD");
event = event.concat("\nEND:VTIMEZONE");
*/
//DTSTAMP
const DTStamp = createDTStamp(date);
console.log(DTStamp);
event = event.concat(`\nDTSTAMP:${DTStamp}`);
//UID
var sentBy = document.getElementById('sentBy').value;
(sentBy === "") ? (sentBy = "none@none") : "";
const UID = DTStamp + "--" + sentBy.replace(/\s/g, '_');
console.log(UID);
event = event.concat(`\nUID:${UID}`);
//SUMMARY
const summary = document.getElementById('summary').value;
console.log(summary);
event = event.concat(`\nSUMMARY:${summary}`);
//LOCATION
const location = document.getElementById('location').value;
console.log(location);
(location !== "") ? event = event.concat(`\nLOCATION:${location}`) : "";
(location !== "") ? event = event.concat(`\nURL:${link(location)}`) : "";
//SENT-BY (declaration under UID)
//const sentBy = document.getElementById('sentBy').value;
console.log(sentBy);
(sentBy !== "none@none") ? event = event.concat(`\nSENT-BY:${sentBy}`) : "";
//RSVP
var rsvpVar = document.getElementsByName('rsvp');
for(i = 0; i < rsvpVar.length; i++) {
if(rsvpVar[i].checked)
var rsvpVal = rsvpVar[i].value;
}
event = event.concat('\nRSVP:' + rsvpVal);
//DTSTART
const startDate = document.getElementById('startDate').value.replace(/-/g,'');
const startTime = document.getElementById('startTime').value.replace(':','');
console.log(startDate);
console.log(startTime);
const DTStart = startDate + "T" + startTime + "00";
event = event.concat(`\nDTSTART:${DTStart}`);
//DTEND
const endDate = document.getElementById('endDate').value.replace(/-/g,'');
const endTime = document.getElementById('endTime').value.replace(':','');
console.log(endDate);
console.log(endTime);
const DTEnd = endDate + "T" + endTime + "00";
event = event.concat(`\nDTEND:${DTEnd}`);
//RRULE
var rrule = "\nRRULE:";
var recurrence = document.getElementsByName('recurrence');
var interval = document.getElementsByName('interval');
var untilDate = document.getElementById('u_repeat').value.replace(/-/g,'');
console.log(recurrence);
console.log(interval);
console.log(untilDate);
if(!recurrence[0].checked) {
for (let i = 1; i < recurrence.length; i++) {
if (recurrence[i].checked) {
rrule = rrule.concat(`FREQ=${recurrence[i].value};`);
}
}
if (interval) {
rrule = rrule.concat(`INTERVAL=${interval[0].value};`);
}
if (untilDate) {
rrule = rrule.concat(`UNTIL=${untilDate}T235900`);
}
event = event.concat(rrule)
}
//PRIORITY
var prioElement = document.getElementsByName('priority'); // fetches radio buttons by name
for(let i = 0; i < prioElement.length; i++) { // fetches value if radio button selected
if(prioElement[i].checked)
var priority = prioElement[i].value;
}
event = event.concat('\nPRIORITY:' + priority);
//CLASSIFICATION
var classif = document.getElementsByName('classification');
for(let i = 0; i < classif.length; i++) {
if(classif[i].checked)
var classVal = classif[i].value;
}
event = event.concat('\nCLASS:' + classVal);
//RESOURCES
var resources = "\nRESOURCES:";
var res = "";
var resBool = 0;
var resData = document.getElementsByName('resources');
for (let i = 0; i < resData.length; i++) {
if(resData[i].checked) {
resBool = 1;
res = res.concat(`${resData[i].value},`);
}
}
res = res.slice(0,-1);
resources = resources.concat(res);
console.log(resources);
if(resBool == 1) {
event = event.concat(resources);
}
//EVENT END
event = event.concat("\nEND:VEVENT" + "\nEND:VCALENDAR");
//TEST
console.log(event);
return event;
}
function link(location){
let url="https://www.google.com/maps/search/?api=1&query=";//link of map search
let encodelocation = encodeURI(location);//encode the location
let final=url+encodelocation;//combine encoded location and link
return final;//return the hyperlink
}
function createFile(data) {
console.log(data);
const blob = new Blob([data], {type: "text/plain;charset=utf-8"});
// saveAs(blob, "event.ics");
saveAs(blob, `${document.getElementById('summary').value}.ics`);
// saveAs(blob, document.getElementById('summary').value.ics);
}