Advertisement
Guest User

Untitled

a guest
Jul 10th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 4.47 KB | None | 0 0
  1. (function ($) {
  2.     // firestore ref
  3.     var db;
  4.  
  5.     // auth and setup event handlers
  6.     var init = function () {
  7.         auth();
  8.  
  9.         $('#ContactTable').on('click', 'button.edit', edit);
  10.         $('#ContactTable').on('click', 'button.remove', remove);
  11.         $('#ContactAdd').click(add);
  12.         $('#ContactForm').submit(save);
  13.     };
  14.  
  15.     // init on doc ready
  16.     $(document).ready(init);
  17.  
  18.     // sign-in anonymously
  19.     var auth = function () {
  20.         firebase.auth().signInAnonymously()
  21.         .then(function (result) {
  22.      db = firebase.firestore();
  23.      db.settings({ timestampsInSnapshots: true });
  24.  
  25.      list();
  26.         })
  27.         .catch(function (error) {
  28.      alert("failed to anonymously sign-in");
  29.         });
  30.     };
  31.  
  32.  
  33.     var listTempLi;
  34.     // load list
  35.     var list = function () {
  36.         var ulBody = $('#contacts');
  37.         //remove any data rows
  38.         ulBody.find('li.data').remove();
  39.         //get template row
  40.         var tempLi = ulBody.find('li.data-temp').removeClass('data-temp').addClass('data').remove();
  41.         if (tempLi.length) {
  42.             listTempLi = tempLi;
  43.         } else {
  44.             tempLi = listTempLi;
  45.         }
  46.  
  47.         // get collection of Contacts
  48.         db.collection("contacts").get().then(function (querySnapshot) {
  49.             querySnapshot.forEach(function (doc) {
  50.                 // clone template row and append to table body
  51.                 var li = tempLi.clone();
  52.                 li.data('id', doc.id);
  53.                 var data = doc.data();
  54.                 // set cell values from Contact data
  55.                 li.find('span[data-prop]').each(function () {
  56.                     var span = $(this);
  57.                     span.text(data[span.data('prop')] || '');
  58.                 });
  59.                 ulBody.append(li);
  60.             });
  61.         });
  62.     };
  63.  
  64.  
  65.  
  66.  
  67.     db.collection("contacts").where("name", "Ruben Kroes", true)
  68.     .get()
  69.     .then(function(querySnapshot) {
  70.         querySnapshot.forEach(function(doc) {
  71.             // doc.data() is never undefined for query doc snapshots
  72.             console.log(doc.id, " => ", doc.data());
  73.         });
  74.     })
  75.     .catch(function(error) {
  76.         console.log("Error getting documents: ", error);
  77.     });
  78.  
  79.  
  80.  
  81.  
  82.     // on remove
  83.     var remove = function (e) {
  84.         e.preventDefault();
  85.         var id = $(this).parents('tr:first').data('id');
  86.         db.collection("contacts").doc(id).delete().then(function () {
  87.             // reload list
  88.             list();
  89.         })
  90.         .catch(function (error) {
  91.      alert("failed to remove contact");
  92.         });
  93.     };
  94.  
  95.     // on add
  96.     var add = function (e) {
  97.         e.preventDefault();
  98.         open('');
  99.     };
  100.  
  101.     // on edit
  102.     var edit = function (e) {
  103.         e.preventDefault();
  104.         var id = $(this).parents('tr:first').data('id');
  105.         open(id);
  106.     };
  107.  
  108.     // open form modal
  109.     var open = function (id) {
  110.         var modal = $('#ContactModal');
  111.         // set current Contact id
  112.         modal.data('id', id);
  113.         // reset all inputs
  114.         modal.find('input').val('');
  115.         modal.modal('show');
  116.  
  117.         if (!id) return;
  118.  
  119.         // get Contact to edit
  120.         db.collection("contacts").doc(id).get().then(function (doc) {
  121.             if (doc.exists) {
  122.                 var data = doc.data();
  123.                 //set form inputs from Contact data
  124.                 modal.find('input[data-prop]').each(function () {
  125.                     var inp = $(this);
  126.                     inp.val(data[inp.data('prop')] || '');
  127.                 });
  128.  
  129.             } else {
  130.                 alert("No such record");
  131.             }
  132.         }).catch(function (error) {
  133.             alert("failed to read contact");
  134.         });
  135.     };
  136.  
  137.     // update or add
  138.     var save = function (e) {
  139.         e.preventDefault();
  140.  
  141.         var modal = $('#ContactModal');
  142.         var id = modal.data('id');
  143.         var data = {};
  144.         //read values from form inputs
  145.         modal.find('input[data-prop]').each(function () {
  146.             var inp = $(this);
  147.             data[inp.data('prop')] = inp.val();
  148.         });
  149.  
  150.         // update or add
  151.         (id ? db.collection("contacts").doc(id).update(data) : db.collection("contacts").add(data)).then(function (result) {
  152.             // hide modal and reload list
  153.             modal.modal('hide');
  154.             list();
  155.         })
  156.         .catch(function (error) {
  157.      alert("failed to save contact");
  158.         });
  159.     };
  160.  
  161. }(jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement