-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditNotes.js
More file actions
72 lines (54 loc) · 2.05 KB
/
editNotes.js
File metadata and controls
72 lines (54 loc) · 2.05 KB
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
//first task get the notes from local storage
let notes = getNotes()
//populate the textArea and text with the value from notes
//to get the id passed from index.html
const noteId = location.hash.substring(1)
//get the individual note
let note = notes.find(note => note.id === noteId)
//if notes not found redirect to index.html
if (note === undefined) {
location.assign("index.html")
}
//else populate the text and text area
const txtEl = document.querySelector('#editTxt')
const textareEl = document.querySelector('#editNotesTxtArea')
const spanEl = document.querySelector('#lastEdited')
txtEl.value = note.text
textareEl.value = note.textArea
const createdDate = note.createdAt
const modifiedDate = note.updatedAt
if (createdDate !== modifiedDate) {
spanEl.textContent = latestModifiedTime(modifiedDate)
}
//when a user clicks on back button he should be redirected to index.html page
document.querySelector('#backButton').addEventListener('click', () => {
location.assign('index.html')
})
//when a user clicks on delete button, the note will be deleted and he should be redirected to index.html page
document.querySelector('#deleteButton').addEventListener('click', () => {
deleteNote(noteId)
location.assign('index.html')
})
//modify the text area and save to local storage
textareEl.addEventListener('input', e => {
const text = e.target.value
const modifiedAt = moment().valueOf()
note.textArea = text
note.updatedAt = modifiedAt
saveNotes(notes)
//add the time to div when the last modification was done
spanEl.textContent = latestModifiedTime(note.modifiedAt)
})
//to reflect changes to a window when opened in more than 1 widow
window.addEventListener('storage', e => {
console.log('arun'+ e.newValue)
if (e.key == 'notes') {
notes = JSON.parse(e.newValue)
note = notes.find(note => note.id === noteId)
if (note === undefined) {
location.assign("index.html")
}
textareEl.value = note.textArea
spanEl.textContent = latestModifiedTime(note.updatedAt)
}
})