-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
161 lines (134 loc) · 4.63 KB
/
script.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
const date = new Date()
var actividad=undefined
var fecha=[]
window.onload=principal;
function principal(){
document.querySelector(".prev").addEventListener("click", () => {
date.setMonth(date.getMonth() - 1)
date.setDate(document.querySelector("#today").innerHTML*1)
consulta()
});
document.querySelector(".next").addEventListener("click", () => {
date.setMonth(date.getMonth() + 1);
date.setDate(document.querySelector("#today").innerHTML*1)
consulta()
});
consulta()
}
function consulta(){
fetch("calendar.php")
.then(function (response){
return response.json()
})
.then(function (data) {
actividad=data;
for(let i=0;i<actividad.length;i++){
fecha[i]=actividad[i]['sFecha'].split("-")
}
renderCalendar();
})
}
function renderCalendar(){
console.log(actividad)
let dia=date.getDate()
date.setDate(1)
const monthDays = document.querySelector(".days");
const lastDay = new Date(
date.getFullYear(),
date.getMonth() + 1,
0
).getDate();
const prevLastDay = new Date(
date.getFullYear(),
date.getMonth(),
0
).getDate();
var firstDayIndex=0;
if(date.getDay()==0){
firstDayIndex = date.getDay() + 6;
} else { firstDayIndex = date.getDay() - 1; }
const lastDayIndex = new Date(
date.getFullYear(),
date.getMonth() + 1,
0
).getDay() - 1;
const nextDays = 7 - lastDayIndex - 1;
const months = [
"Enero",
"Febrero",
"Marzo",
"Abril",
"Mayo",
"Junio",
"Julio",
"Agosto",
"Septiembre",
"Octubre",
"Noviembre",
"Diciembre",
];
document.querySelector(".date h1").innerHTML = months[date.getMonth()];
let options = { weekday: 'short', year: 'numeric', month: 'long', day: 'numeric' };
document.querySelector(".date p").innerHTML = new Date().toLocaleDateString("es-ES", options);
let days = ""; let x=true;
for (let x = firstDayIndex; x > 0; x--) {
days += `<div class="prev-date">${prevLastDay - x + 1}</div>`;
}
for (let i = 1; i <= lastDay; i++) {
for(let j = 0; j<actividad.length; j++){
if(fecha[j][2]*1==i &&
fecha[j][1]-1==date.getMonth()
&& fecha[j][0]*1==date.getFullYear()) {
days += `<div class="selected" onclick="showInfo(${j}, ${i}, ${firstDayIndex})">${i}</div>`;
i++
}
}
//guarda el dia seleccionado, pero en el mes actual presenta los dos y luego sigue con el menor(primer id).
// habria que hacer otro if y ponerle un id y estilo distinto a el dia actual que al seleccionado
if (dia === i || i === new Date().getDate() &&
date.getMonth() === new Date().getMonth() &&
date.getFullYear() === new Date().getFullYear()) {
days += `<div id="today" onclick="selected(${i}, ${firstDayIndex}, ${x})">${i}</div>`;
} else {
days += `<div onclick="selected(${i}, ${firstDayIndex}, ${x})">${i}</div>`;
}
}
if(firstDayIndex+nextDays>7){
for (let j = 1; j <= nextDays; j++) {
days += `<div class="next-date">${j}</div>`;
monthDays.innerHTML = days;
}
} else {
for (let j = 1; j <= nextDays+7; j++) {
days += `<div class="next-date">${j}</div>`;
monthDays.innerHTML = days;
}
}
};
function selected(i,ini, x){
let n=ini+i;
if(document.querySelector("#today")!=null)
document.querySelector("#today").removeAttribute("id");
document.querySelector(".days div:nth-child("+n+")").setAttribute("id", "today");
let dia=i; let mes=date.getMonth(); let anio=date.getFullYear();
let fecha=new Date(anio,mes,dia);
let options = { weekday: 'short', year: 'numeric', month: 'long', day: 'numeric' };
document.querySelector("div.date p").innerHTML = fecha.toLocaleDateString("es-ES", options);
if( x==true && document.querySelector("#infoD")!=null )
document.querySelector("#infoD").removeAttribute("id");
}
function showInfo(indice, i, ini){
//si #today=.info showinfo.
let x=false
selected(i,ini,x)
if(document.querySelector("#infoD")==null){
document.querySelector(".info").setAttribute("id", "infoD");
} else if(document.querySelector("#infoD")!=null &&
document.querySelector(".info h2").innerHTML!=actividad[indice]['nombre']){
document.querySelector(".info").setAttribute("id", "infoD");
} else{
document.querySelector("#infoD").removeAttribute("id");
}
document.querySelector(".info").innerHTML= "<div class='card'><h2>"
+actividad[indice]['nombre']+"</h2> <p class='fechalugaractiviades'>"+actividad[indice]['sFecha']+" || "+actividad[indice]['sHora']+" || "+actividad[indice]['lugar']+"</p><img src='"+actividad[indice]['foto']+"' width='100%' /></div>";
}