]> git.ktnx.net Git - lsl.git/commitdiff
next step in web-app: adding of lists and list items, marking items as done/pending
authorDamyan Ivanov <dmn@debian.org>
Tue, 15 Feb 2022 21:13:33 +0000 (21:13 +0000)
committerDamyan Ivanov <dmn@debian.org>
Tue, 15 Feb 2022 21:13:33 +0000 (21:13 +0000)
public/css/style.css
public/javascripts/lsl.js
views/index.tt

index 80f143517cdcec139021d4754db533654ae2240c..98ef56559d391dc93f8ccd3a466d9437b98d0d6d 100644 (file)
@@ -5,12 +5,12 @@ margin-bottom: 25px;
 padding: 0;
 background-color: #ddd;
 font-family: "Bitstream Vera Sans", sans-serif;
-font-size: 12pt;
+font-size: 14pt;
 color: #333;
 }
 
 input {
-    font-size: 12pt;
+    font-size: 14pt;
 }
 
 h1 {
@@ -107,10 +107,17 @@ font-family: sans-serif;
 font-size: 10px;
 }
 
-#lists-sidebar-item, .have-lists #no-lists-sidebar-item {
+#lists-sidebar-item,
+.have-lists .have-no-lists {
     display: none;
 }
 .have-lists #lists-sidebar-item { display: block; }
+div.needs-lists { display: none; }
+.have-lists div.needs-lists { display: block; }
+.have-list-items #list-is-empty { display: none; }
+#list-is-empty {
+    font-style: italic;
+}
 .table-2-col {
   display: grid;
   grid-template-columns: auto auto;
@@ -119,15 +126,44 @@ font-size: 10px;
 .table-2-col > .arrow {
   align-self: center;
 }
-#list-contents { display: none; }
 .have-list #list-contents {
     display: block;
 }
-#new-list-sidebar-item > div {
+.input-with-button {
     display: grid;
     grid-template-columns: auto auto;
     column-gap: 1ex;
 }
+.input-with-button > button {
+    max-width: max-content;
+}
+
+.list-item-row {
+    display: grid;
+    grid-template-columns: max-content auto max-content;
+    column-gap: 1ex;
+    background: hsla(0, 0%, 90%, 0);
+    transition: 0.5s;
+}
+.list-item-row:hover {
+    background: hsla(0, 0%, 90%, 1);
+}
+.list-item-row .description { cursor: text; min-width: 2em; }
+.list-item-row .edit-trigger {
+    cursor: pointer;
+    visibility: hidden;
+    opacity: 0;
+    transition: 0.5s;
+    background: hsla(0, 0%, 80%, 0);
+}
+.list-item-row:hover .edit-trigger {
+    visibility: visible;
+    opacity: 1;
+    padding: 0 0.5ex;
+}
+.edit-trigger:hover {
+    background: hsla(0, 0%, 80%, 1);
+}
 
 #busy-screen {
     display: none;
@@ -156,4 +192,17 @@ font-size: 10px;
     background: hsla(0, 0%, 100%, 75%);
     border-radius: 1ex;
 }
-
+ul#lists > li {
+    cursor: pointer;
+    border: 1px solid transparent;
+    transition: 0.25s;
+}
+ul#lists > li:hover {
+    border: 1px solid black;
+}
+ul#lists > li.selected {
+    background: hsl(0, 0%, 70%);
+}
+#new-list-item {
+    margin-top: 1em;
+}
index 8ecf08e1fb9ae15097133a8d1907943f61fce9c6..a21cb3ba1b53514ec4b635a8c70b08cb5b37e73f 100644 (file)
 "use strict";
 (function(){
 var ui_icon_class_re = new RegExp('\\bui-icon-\\S+\\b');
-var uri_base;
-var lists_version;
+var uri_base;   // filled on page load from an HTML attribute
+var lists_version = -1;
 var lists = [];
-var current_list;
+var selected_list;
 
+function add_list_item(data) {
+    var item = $('<li>').addClass('list-item-row').data('item', data);
+    var cb = $('<input type="checkbox">');
+    if (data.done) cb.prop('checked', true);
+    item.append(cb);
+    item.append($('<span class="description">').text(data.description || ''));
+    item.append($('<span class="edit-trigger">').text('…'));
+
+    $('#list-items').append(item).addClass('have-list-items');
+}
+function got_lists_version(new_version) {
+    if (new_version != lists_version)
+        window.setTimeout(load_lists);
+}
+function load_list_items(uri, target) {
+    $.get(uri)
+    .done(item_data => {
+        target.data('items', item_data);
+        if (selected_list)
+            selected_list.removeClass('selected');
+
+        var item_list = $('#list-items').empty().removeClass('have-list-items');
+
+        $.each(item_data.items, (i,item) => {
+            add_list_item(item);
+        });
+
+        got_lists_version(item_data.lists_version);
+    });
+}
+function select_list(new_selected_list) {
+    if (new_selected_list == selected_list)
+        return;
+
+    var id_data = new_selected_list.data('id');
+
+    load_list_items(id_data.uri, new_selected_list);
+    selected_list = new_selected_list;
+    selected_list.addClass('selected');
+    $('#selected-list-name').text(id_data.name);
+
+}
 function load_lists() {
-    console.log('TODO');
+    $.get(uri_base + '/api/v1/list')
+    .done(data => {
+        lists_version = data.lists_version;
+
+        if (data.lists.length) {
+            var lists = $('#lists');
+            var new_selected_list;
+
+            $.each(data.lists, (i, list) => {
+                var my_id = 'shopping-list-id'+i;
+                var list_item = $('<li>')
+                    .data('id', list)
+                    .prop('id', my_id)
+                    .text(list.name);
+
+                lists.append(list_item);
+
+                if (!new_selected_list
+                    ||selected_list && selected_list.uri == list.uri)
+                    new_selected_list = list_item;
+
+            });
+
+            select_list(new_selected_list);
+
+            $('#page').addClass('have-lists');
+            selected_list = new_selected_list;
+        }
+        else {
+            $('#page').removeClass('have-lists');
+            selected_list = null;
+        }
+    });
+}
+function new_list_submission_done(data) {
+    if (data.lists_version != lists_version) {
+        load_lists();
+        return;
+    }
+
+    console.log("TODO: easy-add new list");
+
 }
 function handle_new_list_submission(){
     $.post(uri_base + '/api/v1/list',
         JSON.stringify({name:$('input[name="list_name"]').val()}))
-    .done(load_lists);
+    .done(new_list_submission_done);
     return false;
 }
+function new_list_item_submission_done(data) {
+    if (data.lists_version != lists_version) {
+        load_lists();
+        return;
+    }
+
+    var item_data = selected_list.data('items');
+
+    if (data.list_version != item_data.version + 1) {
+        load_list_items(current_list);
+    }
+    else {
+        var new_item = {
+            description: $('#new-list-item input[type="text"]').val().trim(),
+            done: $('#new-list-item input[type="checkbox"]').prop('checked'),
+            version: 1,
+        };
+        item_data.items.push(new_item);
+        item_data.version = data.list_version;
+        add_list_item(new_item);
+    }
+
+    $('#new-list-item input').val('');
+}
+function handle_new_list_item_submission(){
+    var description = $('#new-list-item input[type="text"]').val().trim();
+    if (description.length == 0) return;
+
+    var id_data = selected_list.data('id');
+    $.post(id_data.uri,
+        JSON.stringify({
+            description: description,
+            done: $('#new-list-item input[type="checkbox"]').prop('checked')
+        }),
+    )
+    .done(new_list_item_submission_done);
+}
+function handle_list_item_state_changed(ev) {
+    var item = $(ev.target).closest('li');
+    var cb = item.find('input[type="checkbox"]');
+    var item_data = item.data('item');
+
+    $.ajax(item_data.uri,
+        {   type: 'PUT',
+            data: JSON.stringify({
+                version: item_data.version,
+                done: cb.prop('checked')})
+        }
+    );
+}
 $(function(){
     uri_base = $('#page').attr('lsl-uri-base');
     $(document).ajaxStart(function(){
@@ -49,6 +182,18 @@ $(function(){
 
         return true;
     });
+    $('ul#lists').on('click', 'li', ev=>{
+        select_list($(ev.target));
+    });
+    $('#new-list-item button').on('click', handle_new_list_item_submission);
+    $('#new-list-item input').on('keypress', ev => {
+        if (13 == ev.keyCode) {
+            handle_new_list_item_submission();
+            return false;
+        }
+        return true;
+    });
+    $('#list-items').on('change', '.list-item-row input[type="checkbox"]', handle_list_item_state_changed);
     load_lists();
 });
 })();
index 6f32f0c189b70a91defbb1aa24cbf7cc346f2465..0b7a8cedfa54fefdb22be88405c634747591e2e0 100644 (file)
@@ -3,17 +3,17 @@
     <ul id="sidebar-items">
       <li id="lists-sidebar-item">
         <h3>Shopping lists</h3>
-        <ul class="lists">
+        <ul id="lists">
         </ul>
       </li>
 
-      <li id="no-lists-sidebar-item">
+      <li id="no-lists-sidebar-item" class="have-no-lists">
         <h3>No shopping lists</h3>
       </li>
 
       <li id="new-list-sidebar-item">
         <h3>New shopping list</h3>
-        <div>
+        <div class="input-with-button">
           <input name="list_name" size="20" maxlength="60" />
           <button class="ui-icon-plusthick"></button>
         </div>
       </div>
     </div>
 
-
-    <div id="list-contents" class="have-lists">
+    <div id="list-contents" class="needs-lists">
       <ul id="list-items" class="have-list-items">
       </ul>
-      <div id="new-list-item">
+      <div id="list-is-empty">
+        The list has no items
+      </div>
+      <div id="new-list-item" class="input-with-button">
         <input type="text" size="15" maxlength="80" name="new_list_item" />
         <button id="add-list-item" class="ui-icon-plusthick">Add</button>
       </div>