1+ // Book Constructor
2+ function Book ( title , author , isbn ) {
3+ this . title = title ;
4+ this . author = author ;
5+ this . isbn = isbn ;
6+ }
7+
8+ // UI constructor
9+ function UI ( ) { }
10+
11+ // Clear Fields
12+ UI . prototype . clearFields = function ( ) {
13+ document . getElementById ( 'title' ) . value ;
14+ document . getElementById ( 'author' ) . value ;
15+ document . getElementById ( 'isbn' ) . value ;
16+ }
17+
18+ // Add Book Object
19+ UI . prototype . addBookToList = function ( book ) {
20+ const list = document . getElementById ( 'book-list' ) ;
21+ // Create TR element.
22+ const row = document . createElement ( 'tr' ) ;
23+
24+ // Inserting values
25+ row . innerHTML = `
26+ <td>${ book . title } </td>
27+ <td>${ book . author } </td>
28+ <td>${ book . isbn } </td>
29+ <td><a href = "#" class = "delete"> X </a></td>
30+ `
31+
32+ list . appendChild ( row ) ;
33+ }
34+
35+ // Delete Book
36+ UI . prototype . deleteBook = function ( target ) {
37+ if ( target . className === 'delete' ) {
38+ target . parentElement . parentElement . remove ( ) ;
39+ }
40+ }
41+
42+ // Show alert
43+ UI . prototype . showAlert = function ( message , className ) {
44+
45+ // Create Div
46+ const div = document . createElement ( 'div' ) ;
47+
48+ // Add classes
49+ div . className = `alert ${ className } ` ;
50+
51+ // Add text
52+ div . appendChild ( document . createTextNode ( message ) ) ;
53+
54+ // Get Parent and append
55+ const container = document . querySelector ( '.container' ) ;
56+ const form = document . querySelector ( '#book-form' ) ;
57+ container . insertBefore ( div , form ) ;
58+
59+ // Timeout after 2 seconds.
60+ setTimeout ( function ( ) {
61+ document . querySelector ( '.alert' ) . remove ( ) ;
62+ } , 2000 ) ;
63+ }
64+
65+ // Event Listeners
66+ document . getElementById ( 'book-form' ) . addEventListener ( 'submit' , function ( e ) {
67+
68+ // Getting form elements
69+ const title = document . getElementById ( 'title' ) . value ;
70+ const author = document . getElementById ( 'author' ) . value ;
71+ const isbn = document . getElementById ( 'isbn' ) . value ;
72+
73+ // Creating book object.
74+ const book = new Book ( title , author , isbn ) ;
75+ console . log ( book ) ;
76+
77+ // Creating UI object.
78+ const ui = new UI ( ) ;
79+
80+ // Validate
81+ if ( title === '' || isbn === '' || author === '' ) {
82+ ui . showAlert ( 'Please fill in all fields' , 'error' ) ;
83+ }
84+ else {
85+
86+ // Add book to list;
87+ ui . addBookToList ( book ) ;
88+
89+ // Show success
90+ ui . showAlert ( 'Book Added! ' , 'success' ) ;
91+
92+ e . preventDefault ( ) ;
93+ }
94+ } ) ;
95+
96+ // Event Listeners for deleting a book
97+ // Parent needs to be accessed --- Event delegation
98+ document . getElementById ( 'book-list' ) . addEventListener (
99+ 'click' , function ( e ) {
100+ const ui = new UI ( ) ;
101+ ui . deleteBook ( e . target ) ;
102+ ui . showAlert ( 'Book removed' , 'success' ) ;
103+ e . preventDefault ( ) ;
104+ }
105+ ) ;
0 commit comments