]> git.ktnx.net Git - lsl.git/blobdiff - public/javascripts/lsl.js
hide the mass-action button on broken streak
[lsl.git] / public / javascripts / lsl.js
index 3e66db6ede1441b20f9e361e286b30d5a8659af9..de2da715f83f6828daa8843b56ab33f32083c699 100644 (file)
@@ -8,6 +8,7 @@ var uri_id_re = new RegExp('/(\\d+)$');
 var lists_version = -1;
 var lists = [];
 var selected_list;
+var last_item_action, last_item_action_stamp, item_action_streak = 0;
 
 function debug(...args) {
     if (devel_env)
@@ -92,6 +93,14 @@ function edit_list() {
                 .append(
                     $('<legend>').text('List name'),
                     $('<input type="text" size="10">')
+                    .on('keypress', (ev) => {
+                        if (13 == ev.keyCode) {
+                            save_list(d);
+                            return false;
+                        }
+
+                        return true;
+                    })
                     .val(selected_list.data('lsl-name'))
                 )
         );
@@ -101,7 +110,10 @@ function edit_list() {
         autoOpen: true,
         modal: true,
         title: 'Edit list',
-        width: 'max-content',
+        width: 'min(calc(100% - 2em), 20em)',
+        close: (ev) => {
+            $(ev.target).dialog('destroy');
+        },
         buttons: [
             {
                 class: 'btn-delete',
@@ -110,10 +122,6 @@ function edit_list() {
                     delete_list(d);
                 },
             },
-            {
-                text: 'Cancel',
-                click: ()=>{ d.dialog('destroy'); },
-            },
             {
                 icon: 'ui-icon-disk',
                 text: 'OK',
@@ -327,7 +335,10 @@ function edit_list_item(li) {
         autoOpen: true,
         modal: true,
         title: 'Edit item',
-        width: 'max-content',
+        width: 'min(calc(100% - 2em), 20em)',
+        close: (ev) => {
+            $(ev.target).dialog('destroy');
+        },
         buttons: [
             {
                 class: 'btn-delete',
@@ -336,10 +347,6 @@ function edit_list_item(li) {
                     delete_list_item(d, li);
                 },
             },
-            {
-                text: 'Cancel',
-                click: ()=>{ d.dialog('destroy'); },
-            },
             {
                 icon: 'ui-icon-disk',
                 text: 'OK',
@@ -367,10 +374,57 @@ function handle_new_list_item_submission(){
     )
     .done(new_list_item_submission_done);
 }
+function reposition_item_multi_action() {
+    var box = $('#item-multi-action');
+    var cnt = $('#content');
+    var cnt_x = cnt.offset().left;
+    var cnt_w = cnt.css('width').replace('px', '');
+    var box_w = box.width();
+
+    box.css('left', cnt_x + cnt_w / 2 - box_w / 2);
+}
 function handle_list_item_state_changed(ev) {
     var item = $(ev.target).closest('li');
     var cb = item.find('input[type="checkbox"]');
 
+    var action = cb.prop('checked') ? 'check' : 'clear';
+    var now = new Date();
+    var since_last_action = now - last_item_action_stamp;
+
+    console.debug(action, last_item_action, now - last_item_action_stamp, item_action_streak);
+    if (last_item_action
+        && last_item_action == action
+        && since_last_action < 3000)
+    {
+        item_action_streak ++;
+
+        if (item_action_streak > 2
+            && ( action == 'check' && $('#list-items input[type="checkbox"]').not(':checked').length
+                || action == 'clear' && $('#list-items input[type="checkbox"]').filter(':checked').length
+            )
+        ) {
+            var box = $('#item-multi-action')
+                .removeClass('checking clearing')
+                .addClass(action+'ing')
+                .addClass('in-view');
+            reposition_item_multi_action();
+            var old_timer = box.data('lsl-hide-timer');
+            if (old_timer)
+                window.clearTimeout(old_timer);
+
+            box.data('lsl-hide-timer', window.setTimeout(() => {
+                box.removeClass('in-view').data('lsl-hide-timer', false);
+            }, 3000));
+        }
+    }
+    else {
+        $('#item-multi-action').removeClass('in-view');
+        item_action_streak = 1;
+    }
+
+    last_item_action = action;
+    last_item_action_stamp = now;
+
     $.ajax(item.data('lsl-uri'),
         {   type: 'PUT',
             data: JSON.stringify({
@@ -440,6 +494,23 @@ $(function(){
         return true;
     });
     $('#list-items').on('change', '.list-item-row input[type="checkbox"]', handle_list_item_state_changed);
+    $('#list-items').on('click', '.list-item-row > .description', (ev)=>{
+        $(ev.target).prev('input').trigger('click');
+    });
     load_lists();
+    $('body').on('click', '.ui-widget-overlay', (ev) => {
+        $(ev.target).siblings('.ui-dialog').find('.ui-dialog-content').dialog('close');
+    });
+    $(window).on('resize', () => { reposition_item_multi_action(); });
+    $('#btn-check-all').click((ev) => {
+        $('#list-items input[type="checkbox"]').not(':checked').prop('checked', true).trigger('change');
+        $('#item-multi-action').removeClass('in-view');
+        return false;
+    });
+    $('#btn-clear-all').click((ev) => {
+        $('#list-items input[type="checkbox"]').filter(':checked').prop('checked', false).trigger('change');
+        $('#item-multi-action').removeClass('in-view');
+        return false;
+    });
 });
 })();