Advertisement
Guest User

Untitled

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