This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaleandar.js
343 lines (318 loc) · 12.9 KB
/
caleandar.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const { ipcRenderer } = require('electron');
const { remote } = require('electron');
currWindow = remote.getCurrentWindow();
window.$ = window.jQuery = require('jquery');
$( document ).ready(function() {
var element = document.getElementById('caleandar');
caleandar(element, events, settings);
});
var Calendar = function(model, options, date){
// Default Values
this.Options = {
Color: '',
LinkColor: '',
NavShow: true,
NavVertical: false,
NavLocation: '',
DateTimeShow: true,
DateTimeFormat: 'mmm, yyyy',
DatetimeLocation: '',
EventClick: '',
EventTargetWholeDay: false,
DisabledDays: [],
ModelChange: model
};
// Overwriting default values
for(var key in options){
this.Options[key] = typeof options[key]=='string'?options[key].toLowerCase():options[key];
}
model?this.Model=model:this.Model={};
this.Today = new Date();
this.Selected = this.Today
this.Today.Month = this.Today.getMonth();
this.Today.Year = this.Today.getFullYear();
if(date){this.Selected = date}
this.Selected.Month = this.Selected.getMonth();
this.Selected.Year = this.Selected.getFullYear();
this.Selected.Days = new Date(this.Selected.Year, (this.Selected.Month + 1), 0).getDate();
this.Selected.FirstDay = new Date(this.Selected.Year, (this.Selected.Month), 1).getDay();
this.Selected.LastDay = new Date(this.Selected.Year, (this.Selected.Month + 1), 0).getDay();
this.Prev = new Date(this.Selected.Year, (this.Selected.Month - 1), 1);
if(this.Selected.Month==0){this.Prev = new Date(this.Selected.Year-1, 11, 1);}
this.Prev.Days = new Date(this.Prev.getFullYear(), (this.Prev.getMonth() + 1), 0).getDate();
};
function createCalendar(calendar, element, adjuster){
if(typeof adjuster !== 'undefined'){
var newDate = new Date(calendar.Selected.Year, calendar.Selected.Month + adjuster, 1);
calendar = new Calendar(calendar.Model, calendar.Options, newDate);
element.innerHTML = '';
}else{
for(var key in calendar.Options){
typeof calendar.Options[key] != 'function' && typeof calendar.Options[key] != 'object' && calendar.Options[key]?element.className += " " + key + "-" + calendar.Options[key]:0;
}
}
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function AddSidebar(){
var sidebar = document.createElement('div');
sidebar.className += 'cld-sidebar';
var monthList = document.createElement('ul');
monthList.className += 'cld-monthList';
for(var i = 0; i < months.length - 3; i++){
var x = document.createElement('li');
x.className += 'cld-month';
var n = i - (4 - calendar.Selected.Month);
// Account for overflowing month values
if(n<0){n+=12;}
else if(n>11){n-=12;}
// Add Appropriate Class
if(i==0){
x.className += ' cld-rwd cld-nav';
x.addEventListener('click', function(){
typeof calendar.Options.ModelChange == 'function'?calendar.Model = calendar.Options.ModelChange():calendar.Model = calendar.Options.ModelChange;
createCalendar(calendar, element, -1);});
x.innerHTML += '<svg height="15" width="15" viewBox="0 0 100 75" fill="rgba(255,255,255,0.5)"><polyline points="0,75 100,75 50,0"></polyline></svg>';
}
else if(i==months.length - 4){
x.className += ' cld-fwd cld-nav';
x.addEventListener('click', function(){
typeof calendar.Options.ModelChange == 'function'?calendar.Model = calendar.Options.ModelChange():calendar.Model = calendar.Options.ModelChange;
createCalendar(calendar, element, 1);} );
x.innerHTML += '<svg height="15" width="15" viewBox="0 0 100 75" fill="rgba(255,255,255,0.5)"><polyline points="0,0 100,0 50,75"></polyline></svg>';
}
else{
if(i < 4){x.className += ' cld-pre';}
else if(i > 4){x.className += ' cld-post';}
else{x.className += ' cld-curr';}
//prevent losing var adj value (for whatever reason that is happening)
(function () {
var adj = (i-4);
//x.addEventListener('click', function(){createCalendar(calendar, element, adj);console.log('kk', adj);} );
x.addEventListener('click', function(){
typeof calendar.Options.ModelChange == 'function'?calendar.Model = calendar.Options.ModelChange():calendar.Model = calendar.Options.ModelChange;
createCalendar(calendar, element, adj);} );
x.setAttribute('style', 'opacity:' + (1 - Math.abs(adj)/4));
x.innerHTML += months[n].substr(0,3);
}()); // immediate invocation
if(n==0){
var y = document.createElement('li');
y.className += 'cld-year';
if(i<5){
y.innerHTML += calendar.Selected.Year;
}else{
y.innerHTML += calendar.Selected.Year + 1;
}
monthList.appendChild(y);
}
}
monthList.appendChild(x);
}
sidebar.appendChild(monthList);
if(calendar.Options.NavLocation){
document.getElementById(calendar.Options.NavLocation).innerHTML = "";
document.getElementById(calendar.Options.NavLocation).appendChild(sidebar);
}
else{element.appendChild(sidebar);}
}
var mainSection = document.createElement('div');
mainSection.className += "cld-main";
function AddDateTime(){
var datetime = document.createElement('div');
datetime.className += "cld-datetime";
if(calendar.Options.NavShow && !calendar.Options.NavVertical){
var rwd = document.createElement('div');
rwd.className += " cld-rwd cld-nav";
rwd.addEventListener('click', function(){createCalendar(calendar, element, -1);} );
rwd.innerHTML = '<svg height="15" width="15" viewBox="0 0 75 100" fill="rgba(0,0,0,0.5)"><polyline points="0,50 75,0 75,100"></polyline></svg>';
datetime.appendChild(rwd);
}
var today = document.createElement('div');
today.className += ' today';
today.innerHTML = months[calendar.Selected.Month] + ", " + calendar.Selected.Year;
datetime.appendChild(today);
if(calendar.Options.NavShow && !calendar.Options.NavVertical){
var fwd = document.createElement('div');
fwd.className += " cld-fwd cld-nav";
fwd.addEventListener('click', function(){createCalendar(calendar, element, 1);} );
fwd.innerHTML = '<svg height="15" width="15" viewBox="0 0 75 100" fill="rgba(0,0,0,0.5)"><polyline points="0,0 75,50 0,100"></polyline></svg>';
datetime.appendChild(fwd);
}
if(calendar.Options.DatetimeLocation){
document.getElementById(calendar.Options.DatetimeLocation).innerHTML = "";
document.getElementById(calendar.Options.DatetimeLocation).appendChild(datetime);
}
else{mainSection.appendChild(datetime);}
}
function AddLabels(){
var labels = document.createElement('ul');
labels.className = 'cld-labels';
var labelsList = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
for(var i = 0; i < labelsList.length; i++){
var label = document.createElement('li');
label.className += "cld-label";
label.innerHTML = labelsList[i];
labels.appendChild(label);
}
mainSection.appendChild(labels);
}
function AddDays(){
// Create Number Element
function DayNumber(n){
var number = document.createElement('p');
number.className += "cld-number";
number.innerHTML += n;
return number;
}
var days = document.createElement('ul');
days.className += "cld-days";
// Previous Month's Days
for(var i = 0; i < (calendar.Selected.FirstDay); i++){
var day = document.createElement('li');
day.className += "cld-day prevMonth";
//Disabled Days
var d = i%7;
for(var q = 0; q < calendar.Options.DisabledDays.length; q++){
if(d==calendar.Options.DisabledDays[q]){
day.className += " disableDay";
}
}
var number = DayNumber((calendar.Prev.Days - calendar.Selected.FirstDay) + (i+1));
day.appendChild(number);
days.appendChild(day);
}
// Current Month's Days
for(var i = 0; i < calendar.Selected.Days; i++){
var day = document.createElement('li');
day.className += "cld-day currMonth";
//Disabled Days
var d = (i + calendar.Selected.FirstDay)%7;
for(var q = 0; q < calendar.Options.DisabledDays.length; q++){
if(d==calendar.Options.DisabledDays[q]){
day.className += " disableDay";
}
}
var number = DayNumber(i+1);
// Check Date against Event Dates
for(var n = 0; n < calendar.Model.length; n++){
var evDate = calendar.Model[n].Date;
var toDate = new Date(calendar.Selected.Year, calendar.Selected.Month, (i+1));
if(evDate.getTime() == toDate.getTime()){
number.className += " eventday";
var title = document.createElement('span');
title.className += "cld-title";
if(typeof calendar.Model[n].Link == 'function' || calendar.Options.EventClick){
var a = document.createElement('a');
a.setAttribute('href', '#');
a.innerHTML += calendar.Model[n].Title;
if(calendar.Options.EventClick){
var z = calendar.Model[n].Link;
if(typeof calendar.Model[n].Link != 'string'){
a.addEventListener('click', calendar.Options.EventClick.bind.apply(calendar.Options.EventClick, [null].concat(z)) );
if(calendar.Options.EventTargetWholeDay){
day.className += " clickable";
day.addEventListener('click', calendar.Options.EventClick.bind.apply(calendar.Options.EventClick, [null].concat(z)) );
}
}else{
a.addEventListener('click', calendar.Options.EventClick.bind(null, z) );
if(calendar.Options.EventTargetWholeDay){
day.className += " clickable";
day.addEventListener('click', calendar.Options.EventClick.bind(null, z) );
}
}
}else{
a.addEventListener('click', calendar.Model[n].Link);
if(calendar.Options.EventTargetWholeDay){
day.className += " clickable";
day.addEventListener('click', calendar.Model[n].Link);
}
}
title.appendChild(a);
}else{
title.innerHTML += '<a href="' + calendar.Model[n].Link + '">' + calendar.Model[n].Title + '</a>';
}
number.appendChild(title);
}
}
day.appendChild(number);
// If Today..
if((i+1) == calendar.Today.getDate() && calendar.Selected.Month == calendar.Today.Month && calendar.Selected.Year == calendar.Today.Year){
day.className += " today";
}
days.appendChild(day);
}
// Next Month's Days
// Always same amount of days in calander
var extraDays = 13;
if(days.children.length>35){extraDays = 6;}
else if(days.children.length<29){extraDays = 20;}
for(var i = 0; i < (extraDays - calendar.Selected.LastDay); i++){
var day = document.createElement('li');
day.className += "cld-day nextMonth";
//Disabled Days
var d = (i + calendar.Selected.LastDay + 1)%7;
for(var q = 0; q < calendar.Options.DisabledDays.length; q++){
if(d==calendar.Options.DisabledDays[q]){
day.className += " disableDay";
}
}
var number = DayNumber(i+1);
day.appendChild(number);
days.appendChild(day);
}
mainSection.appendChild(days);
}
if(calendar.Options.Color){
mainSection.innerHTML += '<style>.cld-main{color:' + calendar.Options.Color + ';}</style>';
}
if(calendar.Options.LinkColor){
mainSection.innerHTML += '<style>.cld-title a{color:' + calendar.Options.LinkColor + ';}</style>';
}
element.appendChild(mainSection);
if(calendar.Options.NavShow && calendar.Options.NavVertical){
AddSidebar();
}
if(calendar.Options.DateTimeShow){
AddDateTime();
}
AddLabels();
AddDays();
}
function caleandar(el, data, settings){
var obj = new Calendar(data, settings);
createCalendar(obj, el);
}
var events = [
{'Date': new Date(2019, 10, 7), 'Title': 'Make food'},
];
var settings = {
Color: '',
LinkColor: '',
NavShow: true,
NavVertical: false,
NavLocation: '',
DateTimeShow: true,
DateTimeFormat: 'mmm, yyyy',
DatetimeLocation: '',
EventClick: '',
EventTargetWholeDay: true,
DisabledDays: [],
ModelChange: model
};
/*
document.getElementById('addEvent').addEventListener('click', function(){
console.log("hello");
ipcRenderer.send('addEvent', "");
})
ipcRenderer.on('addEvent', (event, arg) => {
if(arg.length === 0){
var empty = document.createElement("P");
empty.innerHTML = "No recipes added yet";
document.form.appendChild(empty);
}
else{
for(var i = 0; i < arg.length; i++){
var newRecipe = document.createElement("BUTTON");
newRecipe.innerHTML = arg[i];
document.form.appendChild(newRecipe);
}
}
})*/