Advertisement
Guest User

yeet

a guest
Oct 10th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // As we'll be calling the glasgow function constantly, we'll give it a short and special name:
  2. import gg from "https://cdn.jsdelivr.net/npm/glasgow@0.9/glasgow.js";
  3.  
  4. /*
  5.  * This is just an example of Glasgow in action. After you're done playing with it, you should
  6.  * remove it -- it has nothing to do with the actual assignment.
  7.  */
  8. async function api(method, path, body) {
  9.   let opts = { method };
  10.   if (body != null) {
  11.     opts.headers = { "Content-Type": "application/json" };
  12.     opts.body = JSON.stringify(body);
  13.   }
  14.   let response = await fetch("/api" + path, opts);
  15.   let data = await response.json();
  16.  
  17.   gg.refresh();
  18.   if (data.error) throw new Error(data.error);
  19.   return data;
  20. }
  21.  
  22. let lists;
  23. let editing;
  24. let currentList;
  25.  
  26. class Main {
  27.   async start() {
  28.     lists = lists || (await api("GET", "/lists"));
  29.     currentList = lists[0];
  30.   }
  31.  
  32.   render() {
  33.     if (!lists) return gg("main", gg("em", "Loading..."));
  34.     if (currentList != null && lists.indexOf(currentList) < 0)
  35.       currentList = null;
  36.     let sortedLists = lists.sort((a, b) => (a.name < b.name ? -1 : +1));
  37.     return gg(
  38.       "main",
  39.       gg("nav", this.getTab(), sortedLists.map(this.getTab.bind(this))),
  40.       currentList
  41.         ? gg(List, { key: currentList.id, list: currentList })
  42.         : gg(AddList),
  43.       editing && gg(EditModal, editing)
  44.     );
  45.   }
  46.   getTab(list) {
  47.     let name = list ? list.name : gg("i.material-icons", "note_add");
  48.     return gg("a", name, {
  49.       onclick: function() {
  50.         currentList = list;
  51.       },
  52.       className: currentList === list ? "active" : null,
  53.       key: list ? "~" + list.name : "+"
  54.     });
  55.   }
  56. }
  57.  
  58. class AddList {
  59.   render() {
  60.     return gg(
  61.       "div.modalBg",
  62.       gg(
  63.         "div.modal",
  64.         gg("h2", "Add a list"),
  65.         gg(
  66.           "div.inputRow",
  67.           gg("input", { type: "text", placeholder: "List name" })
  68.         ),
  69.         gg(
  70.           "div.buttonRow",
  71.           gg("button", "Cancel"),
  72.           gg("button.primary", "Create")
  73.         )
  74.       )
  75.     );
  76.   }
  77. }
  78.  
  79. class List {
  80.   async start() {
  81.     this.list.items =
  82.       this.list.items || (await api("GET", `/lists/${this.list.id}/items`));
  83.   }
  84.   render() {
  85.     if (!this.list.items)
  86.       return gg("section.card", gg("em", `loading "${this.list.name}"...`));
  87.  
  88.     return [
  89.       gg(
  90.         "section.card.special",
  91.         gg(
  92.           ".inputRow",
  93.           gg("i.material-icons", "delete", { onclick: this.deleteList })
  94.         )
  95.       ),
  96.       this.getItems("high", "High priority"),
  97.       this.getItems("medium", "Medium priority"),
  98.       this.getItems("low", "Low priority")
  99.     ];
  100.   }
  101.   getItems(priority, label) {
  102.     let items = this.list.items
  103.       .filter(item => item.priority === priority)
  104.       .sort((a, b) => (a.name < b.name ? -1 : +1));
  105.     if (!items.length) return;
  106.  
  107.     return gg(
  108.       "section.priority",
  109.       {
  110.         key: priority,
  111.         oncreate: gg.fadeIn,
  112.         onremove: gg.fadeOut
  113.       },
  114.       gg("h4", label),
  115.       items.map(item => {
  116.         return gg(Item, { key: "~" + item.name, item, list: this.list });
  117.       })
  118.     );
  119.   }
  120.   deleteList() {
  121.     lists.splice(lists.indexOf(this.list), 1);
  122.     api("DELETE", `/lists/${this.list.id}`);
  123.   }
  124.   addItem() {
  125.     editing = { list: this.list, item: { name: "", priority: "medium" } };
  126.   }
  127. }
  128.  
  129. class Item {
  130.   render() {
  131.     return gg(
  132.       "section.card",
  133.       { onclick: this.toggleChecked },
  134.       gg(
  135.         ".inputRow",
  136.         gg(
  137.           "i.material-icons",
  138.           this.item.checked ? "check_box" : "check_box_outline_blank"
  139.         ),
  140.         gg("label", this.item.name),
  141.         gg("i.material-icons", "edit", { onclick: this.edit })
  142.       ),
  143.       {
  144.         oncreate: gg.fadeIn,
  145.         onremove: gg.fadeOut
  146.       }
  147.     );
  148.   }
  149.  
  150.   toggleChecked() {
  151.     this.item.checked = !this.item.checked;
  152.     api("PUT", `/lists/${this.list.id}/items/${this.item.id}`, {
  153.       checked: this.item.checked
  154.     });
  155.   }
  156.  
  157.   edit() {
  158.     editing = { item: this.item, list: this.list };
  159.   }
  160. }
  161.  
  162. class Cards {
  163.   async start() {
  164.     this.$divElements = [];
  165.     if (currentList) {
  166.       this.$divElements = await api("GET", `/lists/${currentList.id}/items`);
  167.     }
  168.   }
  169.   render() {
  170.     return gg(
  171.       "div",
  172.       this.$divElements.map(card =>
  173.         gg(
  174.           "section.card",
  175.           gg(
  176.             "div.inputRow",
  177.             gg("i.material-icons", "check_box_outline_blank"),
  178.             gg("label", card.name),
  179.             gg("i.material-icons", "edit")
  180.           )
  181.         )
  182.       )
  183.     );
  184.   }
  185. }
  186.  
  187. // Show the main component
  188. gg.mount(document.body, Main);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement