-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
177 lines (140 loc) · 4.23 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const RANDOM_IMAGE_URL = "https://source.unsplash.com/random/{resolution}/?Sunset";
let backgroundImageUrl;
let
doctors,
searchInput,
dropdown,
dropdownElements;
window.addEventListener("load", e => {
setRandomBackgroundImage();
doctors = createDoctors(25).sort((a, b) => a.name.localeCompare(b.name));
dropdown = document.querySelector("#dropdown");
dropdownElements = doctors.map(e => createDropdownElement(e, dropdown));
searchInput = document.querySelector("#searchInput");
searchInput.addEventListener("keyup", handleSearch);
});
function handleSearch(e){
let value = searchInput.value;
if(value.length > 0)
dropdown.classList.add("show");
else{
dropdown.classList.remove("show");
return;
}
const MAX_HITS = 5;
let n = 0;
for(let element of dropdownElements)
if(n >= MAX_HITS)
element.hide();
else if(element.filter(value))
n++;
dropdownElements.sort((a, b) => {
let p = b.matchScore - a.matchScore;
if(p != 0)
return p;
return a.data.name.localeCompare(b.data.name);
});
for(let element of dropdownElements)
dropdown.appendChild(element);
}
function createDropdownElement(doctor, parent = null){
let element = createElement("div.dropdown-item", parent);
element.data = doctor;
element.show = function(){
this.classList.remove("hide");
};
element.hide = function(){
this.classList.add("hide");
};
element.filter = function(text){
let searchValues = text.replace(/[^A-Za-z ]/g, "").split(" ");
this.matchScore = 0;
const INDEX_PROPERTIES = ["name", "specialization", "hospital"];
for(let i = 0; i < INDEX_PROPERTIES.length; i++)
for(let token of searchValues)
if(this.data[INDEX_PROPERTIES[i]].replace(/[^A-Za-z ]/g, "").match(new RegExp(token, "i"))){
this.show();
this.matchScore = INDEX_PROPERTIES.length - i;
return true;
}
this.hide();
return false;
};
element.addEventListener("click", bookAppointment);
createElement("span.name", element, {innerText: doctor.name});
createElement("span.specialization", element, {innerText: doctor.specialization});
createElement("span.hospital", element, {innerText: doctor.hospital});
return element;
}
function bookAppointment(e){
document.querySelector("#welcome").classList.add("hide");
document.querySelector("#Search").classList.remove("show");
document.querySelector("#Appointment").classList.add("show");
let data = e.target.closest("div").data;
document.querySelector("#doctor").value =
"Dr. " +
data.name +
", " +
data.specialization;
}
async function setRandomBackgroundImage(){
let resolution = screen.availWidth + "x" + screen.availHeight;
let image = await fetch(RANDOM_IMAGE_URL.replace("{resolution}", resolution));
backgroundImageUrl = image.url;
document.body.style.setProperty("--background-image", "url('" + backgroundImageUrl + "')");
let dark = await isImageDark(backgroundImageUrl)
if(dark)
document.body.classList.add("dark");
else
document.body.classList.remove("dark");
}
async function isImageDark(imageSrc){
const THRESHOLD = 0.1;
return new Promise((resolve, reject) => {
const image = new Image();
image.crossOrigin = "anonymous";
image.addEventListener("load", e => {
const canvas = new OffscreenCanvas(image.width, image.height);
const g = canvas.getContext("2d");
g.drawImage(image, 0, 0);
const data = g.getImageData(0, 0, canvas.width, canvas.height).data;
let light = 0, dark = 0;
for(let x = 0; x < data.length; x += 4)
if(Math.max(data[x], data[x + 1], data[x + 2]) < 128)
dark++;
else
light++;
let delta = (light - dark)/(canvas.width * canvas.height);
if(delta + THRESHOLD < 0)
resolve(true); //Dark
else
resolve(false); //Light
});
image.src = imageSrc;
});
}
function createElement(name, parent, options = {}){
let classList = name.split(".");
let tagName = classList.shift();
let id = tagName.split("#");
if(id.length > 1){
tagName = id[0];
id = id[1];
}
else
id = undefined;
let element = document.createElement(tagName);
if(id)
element.id = id;
if(classList.length > 0)
for(c of classList)
element.classList.add(c);
for(o in options)
if(o in element)
element[o] = options[o];
else
element.setAttribute(o, options[o]);
if(parent)
parent.appendChild(element);
return element;
}