]> git.ktnx.net Git - lsl.git/commitdiff
identify lists by their id (part of the URI)
authorDamyan Ivanov <dmn@debian.org>
Wed, 16 Feb 2022 20:32:20 +0000 (20:32 +0000)
committerDamyan Ivanov <dmn@debian.org>
Wed, 16 Feb 2022 20:32:20 +0000 (20:32 +0000)
public/javascripts/lsl.js

index 7d0c4e03e630921a2743fad380d5f9d0d905ce8b..bd767d3ce6a25b666e17d982ce7bdbda32844dbb 100644 (file)
@@ -1,11 +1,16 @@
 "use strict";
 (function(){
 var ui_icon_class_re = new RegExp('\\bui-icon-\\S+\\b');
+var uri_id_re = new RegExp('/(\\d+)$');
 var uri_base;   // filled on page load from an HTML attribute
 var lists_version = -1;
 var lists = [];
 var selected_list;
 
+function uri_id(uri) {
+    var m = uri.match(uri_id_re);
+    return m ? m[1] : null;
+}
 function add_list_item(data) {
     var item = $('<li>').addClass('list-item-row').data('item', data);
     var cb = $('<input type="checkbox">');
@@ -21,15 +26,19 @@ function got_lists_version(new_version) {
         window.setTimeout(load_lists);
 }
 function got_list_version(new_version) {
-    var id_data = selected_list.data('id');
-    if (new_version != id_data.version)
-        window.setTimeout(() => load_list_items(id_data.uri, selected_list));
+    if (new_version != selected_list.attr('lsl-version'))
+        window.setTimeout(
+            () => load_list_items(selected_list.data('lsl-uri'), selected_list));
 }
 function load_list_items(uri, target) {
     $.get(uri)
     .done(item_data => {
         target.data('items', item_data);
 
+        // TODO: merge new items into existing ones
+        // keep track of the last existing item, and append new one
+        // after it, prepending if there is no last existing item
+        // the newly prepended/inserted item becomes the last existing
         var item_list = $('#list-items').empty().removeClass('have-list-items');
 
         $.each(item_data.items, (i,item) => {
@@ -40,52 +49,60 @@ function load_list_items(uri, target) {
     });
 }
 function select_list(new_selected_list) {
-    if (new_selected_list == selected_list)
+    if (new_selected_list && selected_list
+        && new_selected_list.length && selected_list.length
+        && new_selected_list.get(0) == selected_list.get(0)
+    )
         return;
 
-    var id_data = new_selected_list.data('id');
+    if (new_selected_list && !new_selected_list.length)
+        new_selected_list = null;
+
+    $('#page').toggleClass('have-lists', !!new_selected_list);
 
     if (selected_list)
         selected_list.removeClass('selected');
-    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);
 
+    if (new_selected_list) {
+        load_list_items(new_selected_list.data('lsl-uri'), new_selected_list);
+        new_selected_list.addClass('selected');
+        $('#selected-list-name').text(new_selected_list.data('lsl-name'));
+    }
+
+    selected_list = new_selected_list;
 }
 function load_lists() {
     $.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 lists = $('#lists');
+        lists.find('>li').addClass('old');
+
+        $.each(data.lists, (i, list) => {
+            var list_id = uri_id(list.uri);
+            var existing = lists.find('li#list-'+list_id);
+            if (existing.length) {
+                existing.data('lsl-version', list.version)
+                .data('lsl-name', list.name)
+                .text(list.name)
+                .removeClass('old');
+            }
+            else {
                 var list_item = $('<li>')
-                    .data('id', list)
-                    .prop('id', my_id)
+                    .prop('id', 'list-'+list_id)
+                    .data('lsl-uri', list.uri)
+                    .data('lsl-version', list.version)
+                    .data('lsl-name', list.name)
                     .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);
+        if (!selected_list || selected_list.hasClass('old'))
+            select_list(lists.find('>li').eq(0));
 
-            $('#page').addClass('have-lists');
-            selected_list = new_selected_list;
-        }
-        else {
-            $('#page').removeClass('have-lists');
-            selected_list = null;
-        }
+        lists.find('li.old').remove();
     });
 }
 function new_list_submission_done(data) {
@@ -124,8 +141,7 @@ 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,
+    $.post(selected_list.data('lsl-uri'),
         JSON.stringify({
             description: description,
             done: $('#new-list-item input[type="checkbox"]').prop('checked')