-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
122 lines (112 loc) · 3.89 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//variables
const addMarksContainer=document.querySelector(".add-bookmark"),
closeBtn=document.querySelector(".nav i"),
form=document.querySelector("form"),
url=document.querySelector("#url"),
websiteName=document.querySelector("#title"),
saveBtn=document.querySelector(".save"),
bookmarksCont=document.querySelector(".bookmarks"),
addMarkBtn=document.querySelector(".add-bookmark-btn");
let websiteNameValue,urlValue;
let allItems=[]
let clearHideClass=(ele)=>{
ele.classList.remove("hide")
}
let addHideClass=(ele)=>{
ele.classList.add("hide")
}
//for form validation
function formvalidation(urlValue){
//regex for validate the form
let expression =/(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/
const regex=new RegExp(expression)
//check if there is no value
if(!urlValue || !websiteNameValue){
alert("Please fill both required fields!!")
return false
}
//check if url is valid
if (!urlValue.match(regex)){
alert("please write right URL !!")
return false
}
return true
}
//add elements to document
function bookMarkElements(urlValue, websiteNameValue) {
const bookmarkCon = document.createElement("div");
bookmarkCon.className = "bookmark";
const cancelEl = document.createElement("i");
cancelEl.className="fa-solid fa-xmark"
cancelEl.id="cancelBtn"
cancelEl.addEventListener("click",clearBookMark)
const link = document.createElement("a");
const linkText=document.createTextNode(websiteNameValue)
link.href = `${urlValue}`;
const logo = document.createElement("img");
logo.src=`https://www.google.com/s2/favicons?domain=${urlValue}&sz=256`
console.log(urlValue);
link.setAttribute("target","_blank")
logo.id="logo"
link.append(logo)
link.append(linkText)
bookmarkCon.append(cancelEl)
bookmarkCon.append(link)
bookmarksCont.appendChild(bookmarkCon)
}
//localstorage and update items
function addInfoToLocalStorage(){
//state of there are data in local storage
if(localStorage.getItem("data")){
allItems= JSON.parse( localStorage.getItem("data"))
//updating data
allItems.forEach((data,i) => {
urlValue=`${data.url}`
websiteNameValue=`${data.name}`
bookMarkElements(urlValue,websiteNameValue)}
);
}
}
//clear bookmarks from window and local storage
function clearBookMark(e){
let selectedUrl= e.target.parentElement.querySelector("a").href
allItems=JSON.parse(localStorage.getItem("data"))
//check if url of slecteditem identical to url in local storage then delete it from local store and ui
allItems.map(item=>{
if(`${item.url}/`===selectedUrl||item.url===selectedUrl){
let i=allItems.indexOf(item)
if (i > -1) {
e.target.parentElement.remove()
allItems.splice(i,1)
localStorage.setItem("data",JSON.stringify(allItems))
}
}
})
}
//add Events
addInfoToLocalStorage()
addMarkBtn.addEventListener("click",()=>{addMarksContainer.classList.remove("hide")})
closeBtn.addEventListener("click",()=>{addMarksContainer.classList.add("hide")})
form.addEventListener("submit",(e)=>{
e.preventDefault()
websiteNameValue= websiteName.value
urlValue= url.value
//check if url doesnt contain http....
if(!urlValue.includes("https://","http://")){
urlValue =`https://${urlValue}`
}
if(!formvalidation(urlValue,websiteNameValue)){
return false
}
//formvalidation works=true
let StoredData ={
url:urlValue,
name:websiteNameValue
}
allItems.push(StoredData)
localStorage.setItem("data",JSON.stringify(allItems))
//add bookmarks
bookMarkElements(urlValue, websiteNameValue);
form.reset()
websiteName.focus()
})