Guest User

Untitled

a guest
Oct 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  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. // Add Book To List
  12. UI.prototype.addBookToList = function(book){
  13. const list = document.getElementById('book-list');
  14. // Create tr element
  15. const row = document.createElement('tr');
  16. // Insert cols
  17. row.innerHTML = `
  18. <td>${book.title}</td>
  19. <td>${book.author}</td>
  20. <td>${book.isbn}</td>
  21. <td><a href="#" class="delete">X<a></td>
  22. `;
  23.  
  24. list.appendChild(row);
  25. }
  26.  
  27. // Show Alert
  28. UI.prototype.showAlert = function(message, className){
  29. // create Div
  30. const div= document.createElement('div');
  31. // add Classes
  32. div.className = `alert ${className} `;
  33. // add text
  34. div.appendChild(document.createTextNode(message));
  35. // Get parent
  36. const container = document.querySelector('.container');
  37.  
  38. // get the form
  39. const form = document.querySelector('#book-form');
  40.  
  41. container.insertBefore(div, form);
  42.  
  43. // Timeout after 3 sec
  44. setTimeout(()=>{
  45. document.querySelector('.alert').remove();
  46. }, 3000);
  47. }
  48.  
  49. //Delete book
  50. UI.prototype.deleteBook = function(target){
  51. if(target.className === 'delete'){
  52. target.parentElement.parentElement.remove();
  53. }
  54. }
  55.  
  56.  
  57. // Clear Fields
  58. UI.prototype.clearFields = function() {
  59. document.getElementById('title').value = '';
  60. document.getElementById('author').value = '';
  61. document.getElementById('isbn').value = '';
  62. }
  63.  
  64. // Event Listeners
  65. document.getElementById('book-form').addEventListener('submit', function(e){
  66. // Get form values
  67. const title = document.getElementById('title').value,
  68. author = document.getElementById('author').value,
  69. isbn = document.getElementById('isbn').value
  70.  
  71. // Instantiate book
  72. const book = new Book(title, author, isbn);
  73.  
  74. // Instantiate UI
  75. const ui = new UI();
  76.  
  77. // Validate
  78. if(title === '' || author === '' || isbn === '') {
  79. // Error alert
  80. ui.showAlert('Please fill in all fields', 'error');
  81. } else {
  82. // Add book to list
  83. ui.addBookToList(book);
  84.  
  85. // Show success
  86. ui.showAlert('Book Added!', 'success');
  87.  
  88. // Clear fields
  89. ui.clearFields();
  90. }
  91.  
  92. e.preventDefault();
  93. });
  94.  
  95. // event litsiner for delete
  96.  
  97. document.getElementById('book-list').addEventListener('click', (e) => {
  98.  
  99. const ui = new UI();
  100.  
  101. //Delete book
  102. ui.deleteBook(e.target);
  103.  
  104. // show message
  105. ui.showAlert('book got Removed!', 'success')
  106.  
  107. e.preventDefault();
  108. })
Add Comment
Please, Sign In to add comment