main.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB
  2. $(document).ready(function () {
  3. // open database
  4. var request = indexedDB.open('customermanager', 1)
  5. // vai aumentar o numero da versao quando alterado algun valor
  6. request.onupgradeneeded = function (e) {
  7. var db = e.target.result
  8. if (!db.objectStoreNames.contains('customers')) {
  9. var os = db.createObjectStore('customers', { keyPath: "id", autoIncrement: true })
  10. // Create Index for Name
  11. os.createIndex('name', 'name', { unique: false })
  12. }
  13. }
  14. // Generating handlers - error ou sucess
  15. // success
  16. request.onsuccess = function (e) {
  17. console.log('Success: Opened Database....')
  18. db = e.target.result
  19. // show customers - chama a funçao
  20. showCustomers()
  21. }
  22. // Error
  23. request.onerror = function () {
  24. console.log('Error: Não possivel abrir o banco de dados....')
  25. }
  26. })
  27. // add customer
  28. function addCustomer() {
  29. var name = $('#name').val()
  30. var email = $('#email').val()
  31. var transaction = db.transaction(["customers"], "readwrite")
  32. // ask for objectStore
  33. var store = transaction.objectStore('customers')
  34. // define customer
  35. var customer = {
  36. name: name,
  37. email: email
  38. }
  39. // perform the add
  40. var request = store.add(customer)
  41. // success
  42. request.onsuccess = function (e) {
  43. window.location.href = "index.html"
  44. }
  45. // error
  46. request.onerror = function (e) {
  47. alert('Sorry, the customer was not added')
  48. console.log('Error', e.target.error.name)
  49. }
  50. }
  51. // show customers
  52. function showCustomers(e) {
  53. var transaction = db.transaction(["customers"], "readonly")
  54. // ask for objectStore
  55. var store = transaction.objectStore('customers')
  56. var index = store.index('name')
  57. // constuindo a saida
  58. var output = ''
  59. index.openCursor().onsuccess = function (e) {
  60. var cursor = e.target.result
  61. if (cursor) {
  62. output += "<tr id='customer_" + cursor.value.id + "'>"
  63. output += "<td>" + cursor.value.id + "</td>"
  64. output += "<td><span class='cursor customer' contenteditable='true' data-field='name' data-id='" + cursor.value.id + "'>" + cursor.value.name + "</span></td>"
  65. output += "<td><span class='cursor customer' contenteditable='true' data-field='email' data-id='" + cursor.value.id + "'>" + cursor.value.email + "</span></td>"
  66. output += "<td><a onclick='removeCustomer(" + cursor.value.id + ")' href=''>Delete</a></td>"
  67. output += "</tr>"
  68. cursor.continue()
  69. }
  70. $('#customers').html(output)
  71. }
  72. }
  73. // delete um customers
  74. function removeCustomer(id) {
  75. var transaction = db.transaction(["customers"], "readwrite")
  76. // ask for objectStore
  77. var store = transaction.objectStore('customers')
  78. var request = store.delete(id)
  79. // success
  80. request.onsuccess = function () {
  81. console.log('Customer ' + id + ' Deleted')
  82. $('.customer_' + id).remove()
  83. }
  84. // error
  85. request.onerror = function (e) {
  86. alert('Sorry, the customer was not removed')
  87. console.log('Error', e.target.error.name)
  88. }
  89. }
  90. // delete todos customers
  91. function clearCustomers() {
  92. indexedDB.deleteDatabase('customermanager')
  93. window.location.href = "index.html"
  94. }
  95. // update customers
  96. // quando clicar, editar e sair = blur event
  97. $('#customers').on('blur', '.customer', function () {
  98. // newly entered text
  99. var newText = $(this).html()
  100. // field
  101. var field = $(this).data('field') // data-field
  102. // customer id
  103. var id = $(this).data('id') // data-id
  104. // get transation para fazer o update
  105. var transaction = db.transaction(["customers"], "readwrite")
  106. // ask for objectStore
  107. var store = transaction.objectStore('customers')
  108. // vai pegar o id que foi atualizado
  109. var request = store.get(id)
  110. // se tiver sucesso de pegar o id (assim ele existe) vai atualizar com novo valor
  111. request.onsuccess = function () {
  112. var data = request.result
  113. if (field == 'name') {
  114. data.name = newText
  115. } else if (field == 'email') {
  116. data.email = newText
  117. }
  118. // store updated text
  119. var requestUpdate = store.put(data)
  120. requestUpdate.onsuccess = function () {
  121. console.log('Customer field Updated..')
  122. }
  123. requestUpdate.onerror = function () {
  124. console.log('Customer field NOT Updated..')
  125. }
  126. }
  127. })