From dc7ab638d5b930ce89f77883d20ee18676ac8d98 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Tue, 19 Feb 2019 09:02:33 +0000
Subject: [PATCH] Limit options in the dropdown based on selection

---
 opentech/apply/funds/tables.py                |  9 ++-
 .../includes/table_filter_and_search.html     |  2 +-
 .../src/javascript/apply/batch-actions.js     | 66 ++++++++++++-------
 3 files changed, 52 insertions(+), 25 deletions(-)

diff --git a/opentech/apply/funds/tables.py b/opentech/apply/funds/tables.py
index a515aa6b5..198373c99 100644
--- a/opentech/apply/funds/tables.py
+++ b/opentech/apply/funds/tables.py
@@ -1,3 +1,4 @@
+import json
 import textwrap
 
 from django import forms
@@ -25,6 +26,12 @@ def make_row_class(record):
     return css_class
 
 
+def render_actions(table, record):
+    user = table.context['user']
+    actions = record.get_actions_for_user(user)
+    return json.dumps([slugify(action) for _, action in actions])
+
+
 def render_title(record):
     return textwrap.shorten(record.title, width=30, placeholder="...")
 
@@ -33,7 +40,7 @@ class SubmissionsTable(tables.Table):
     """Base table for listing submissions, do not include admin data to this table"""
     title = tables.LinkColumn('funds:submissions:detail', text=render_title, args=[A('pk')], orderable=True, attrs={'td': {'data-tooltip': lambda record: record.title, 'class': 'js-title'}})
     submit_time = tables.DateColumn(verbose_name="Submitted")
-    phase = tables.Column(verbose_name="Status", order_by=('status',))
+    phase = tables.Column(verbose_name="Status", order_by=('status',), attrs={'td': {'data-actions': render_actions, 'class': 'js-actions'}})
     stage = tables.Column(verbose_name="Type", order_by=('status',))
     fund = tables.Column(verbose_name="Fund", accessor='page')
     comments = tables.Column(accessor='comment_count', verbose_name="Comments")
diff --git a/opentech/apply/funds/templates/funds/includes/table_filter_and_search.html b/opentech/apply/funds/templates/funds/includes/table_filter_and_search.html
index 8ee45d03e..2b4727e2b 100644
--- a/opentech/apply/funds/templates/funds/includes/table_filter_and_search.html
+++ b/opentech/apply/funds/templates/funds/includes/table_filter_and_search.html
@@ -11,7 +11,7 @@
                 <div class="actions-bar__inner actions-bar__inner--batch-actions">
                     <p class="actions-bar__total"><span class="js-total-actions">0</span> Selected</p>
 
-                    <button data-fancybox data-src="#batch-progress" class="button button--action button--batch-status js-batch-button" type="button">Status</button>
+                    <button data-fancybox data-src="#batch-progress" class="button button--action button--batch-status js-batch-button js-batch-progress" type="button">Status</button>
 
                     <button data-fancybox data-src="#batch-update-reviewers" class="button button--action button--reviewers js-batch-button" type="button">Reviewers</button>
                 </div>
diff --git a/opentech/static_src/src/javascript/apply/batch-actions.js b/opentech/static_src/src/javascript/apply/batch-actions.js
index 64c310378..1e9a75582 100644
--- a/opentech/static_src/src/javascript/apply/batch-actions.js
+++ b/opentech/static_src/src/javascript/apply/batch-actions.js
@@ -6,6 +6,7 @@
     const $checkbox = $('.js-batch-select');
     const $allCheckboxInput = $('.js-batch-select-all');
     const $batchButtons = $('.js-batch-button');
+    const $batchProgress = $('.js-batch-progress');
     const $batchTitlesList = $('.js-batch-titles');
     const $batchTitleCount = $('.js-batch-title-count');
     const $hiddenIDlist = $('#id_submission_ids');
@@ -50,31 +51,25 @@
     // append selected project titles to batch update reviewer modal
     $batchButtons.each(function () {
         $(this).click(function () {
-            $batchTitlesList.html('');
-            $batchTitleCount.html('');
-            $batchTitlesList.addClass(closedClass);
-            $toggleBatchList.html('Show');
-
-            let selectedIDs = [];
-
-            $checkbox.each(function () {
-                if ($(this).is(':checked')) {
-                    const href = $(this).parents('tr').find('.js-title').find('a').attr('href');
-                    const title = $(this).parents('tr').find('.js-title').data('tooltip');
-
-                    $batchTitlesList.append(`
-                        <a href="${href}" class="modal__list-item" target="_blank" rel="noopener noreferrer" title="${title}">
-                            ${title}
-                            <svg class="modal__open-link-icon"><use xlink:href="#open-in-new-tab"></use></svg>
-                        </a>
-                    `);
-                    selectedIDs.push($(this).parents('tr').data('record-id'));
-                }
-            });
+            prepareBatchListing();
+        });
+    });
 
-            $batchTitleCount.append(`${selectedIDs.length} submissions selected`);
-            $hiddenIDlist.val(selectedIDs.join(','));
+    $batchProgress.click(function () {
+        const $actionOptions = $('#id_action option');
+        var actions = $actionOptions.map(function() { return this.value; }).get();
+        $checkbox.filter(':checked').each(function () {
+            let newActions = $(this).parents('tr').find('.js-actions').data('actions');
+            actions = actions.filter(action => newActions.includes(action));
+        });
+        $actionOptions.each(function() {
+            if (!actions.includes(this.value)) {
+                $(this).attr("disabled", "disabled");
+            } else {
+                $(this).removeAttr("disabled");
+            }
         });
+        $actionOptions.filter(':enabled:first').prop('selected', true);;
     });
 
     // show/hide the list of actions
@@ -91,6 +86,31 @@
         $batchTitlesList.toggleClass(closedClass);
     });
 
+    function prepareBatchListing() {
+        $batchTitlesList.html('');
+        $batchTitleCount.html('');
+        $batchTitlesList.addClass(closedClass);
+        $toggleBatchList.html('Show');
+
+        let selectedIDs = [];
+
+        $checkbox.filter(':checked').each(function () {
+            const href = $(this).parents('tr').find('.js-title').find('a').attr('href');
+            const title = $(this).parents('tr').find('.js-title').data('tooltip');
+
+            $batchTitlesList.append(`
+                <a href="${href}" class="modal__list-item" target="_blank" rel="noopener noreferrer" title="${title}">
+                    ${title}
+                    <svg class="modal__open-link-icon"><use xlink:href="#open-in-new-tab"></use></svg>
+                </a>
+            `);
+            selectedIDs.push($(this).parents('tr').data('record-id'));
+        });
+
+        $batchTitleCount.append(`${selectedIDs.length} submissions selected`);
+        $hiddenIDlist.val(selectedIDs.join(','));
+    }
+
     function toggleBatchActions() {
         if ($('.js-batch-select:checked').length) {
             $body.addClass(activeClass);
-- 
GitLab