-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
103 lines (65 loc) · 3.13 KB
/
main.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
let dates_field= document.getElementById('dates_field');
let addBtn= document.getElementById('addBtn');
let submitBtn= document.getElementById('submitBtn');
let date= document.createElement('div');
let flag= false;
let count=0;
let workSheet= document.createElement('p');
if (addBtn){ // add btn pressed
addBtn.addEventListener('click', (event) => {
count++;
console.log(count);
let date= document.createElement('div');
date.setAttribute('id',`date${count}`);
date.innerHTML = `<input type="datetime-local" id="startDate" />
<input type="datetime-local" id="endDate" />`
date.children[0].setAttribute('onblur',"compare(`${this.parentElement.id}`,'startDate',`${this.value}`);");
date.children[1].setAttribute('onblur',"compare(`${this.parentElement.id}`,'endDate',`${this.value}`);");
dates_field.append(date);
});
}
function compare(parent,startOrEnd,valueOfSelectedDate)
{
flag=true;
let parentID = parseInt(parent.replace ( /[^\d.]/g, '' ),10);
if(parentID===0){
let currentDate=document.getElementById(`date0`).children;
var startDt = currentDate[0].value;
var endDt = currentDate[1].value;
if( (new Date(startDt).getTime() > new Date(endDt).getTime()) && startDt!="" && endDt!=""){
flag=false;
alert('Wrong Input');
}
}else{
for(let i=0 ; i<parentID+1;i++){
let currentDate=document.getElementById(`date${i}`).children;
var startDt = currentDate[0].value;
var endDt = currentDate[1].value;
if( (new Date(startDt).getTime() > new Date(valueOfSelectedDate).getTime())||
(new Date(endDt).getTime() > new Date(valueOfSelectedDate).getTime()))
{
flag=false;
console.log('wrongInput')
alert('Wrong Input');
break;
}
}
}
}
if(submitBtn){ //submit putton press
submitBtn.addEventListener('click', () =>{
if(flag){
workSheet.innerHTML='';
for(let i=0 ; i<dates_field.childNodes.length-2;i++){
let currentDate=document.getElementById(`date${i}`).children;
var startEndText = document.createTextNode(`Start:${currentDate[0].value} End:${currentDate[1].value}`);
if(currentDate[0].value!="" && currentDate[1].value!=""){ //checking if not empty
workSheet.appendChild(startEndText);
workSheet.appendChild(document.createElement('br'));
}
}
document.getElementById('main').appendChild(workSheet);
}
//console.log(`Start:${currentDate.getElementById('startDate')} End:${currentDate.getElementById('endDate')}`);
});
}