diff --git a/README.txt b/README.txt index e687877..93788c0 100644 --- a/README.txt +++ b/README.txt @@ -30,8 +30,7 @@ IMPLEMENTATION Checklists are declared as multidimensional arrays using hook_checklistapi_checklist_info(). They can be altered using hook_checklistapi_checklist_info_alter(). Checklist API handles creation of menu -items and permissions. Progress details are saved in one Drupal variable per -checklist. (Note: it is the responsibility of implementing modules to remove -their own variables on hook_uninstall().) +items and permissions. Progress details are saved in one config file per +checklist. See checklistapi.api.php for more details. diff --git a/checklistapi.api.php b/checklistapi.api.php index 13b97c9..951d4e4 100644 --- a/checklistapi.api.php +++ b/checklistapi.api.php @@ -50,8 +50,8 @@ * - #default_value: (optional) The default checked state of the * item--TRUE for checked or FALSE for unchecked. Defaults to FALSE. * This is useful for automatically checking items that can be - * programmatically tested (e.g., a module is installed or a variable - * has a certain value). + * programmatically tested (e.g., a module is installed or a + * configuration setting has a certain value). * - #weight: (optional) A floating point number used to sort the list of * items before being output. Lower numbers appear before higher * numbers. diff --git a/checklistapi.js b/checklistapi.js index 95cfad4..946a01d 100644 --- a/checklistapi.js +++ b/checklistapi.js @@ -2,12 +2,34 @@ "use strict"; /** + * Updates the progress bar as checkboxes are changed. + */ + Drupal.behaviors.checklistapiUpdateProgressBar = { + attach: function (context) { + var total_items = $(':checkbox.checklistapi-item', context).size(), + progress_bar = $('#checklistapi-checklist-form .progress__bar', context), + progress_percentage = $('#checklistapi-checklist-form .progress__percentage', context); + $(':checkbox.checklistapi-item', context).change(function () { + var num_items_checked = $(':checkbox.checklistapi-item:checked', context).size(), + percent_complete = Math.round(num_items_checked / total_items * 100), + args = {}; + progress_bar.css('width', percent_complete + '%'); + args['@complete'] = num_items_checked; + args['@total'] = total_items; + args['@percent'] = percent_complete; + progress_percentage.html(Drupal.t('@complete of @total (@percent%)', args)); + }); + } + }; + + /** * Provides the summary information for the checklist form vertical tabs. */ Drupal.behaviors.checklistapiFieldsetSummaries = { attach: function (context) { $('#checklistapi-checklist-form .vertical-tabs-panes > details', context).drupalSetSummary(function (context) { - var total = $(':checkbox.checklistapi-item', context).size(), args = {}; + var total = $(':checkbox.checklistapi-item', context).size(), + args = {}; if (total) { args['@complete'] = $(':checkbox.checklistapi-item:checked', context).size(); args['@total'] = total; @@ -53,10 +75,10 @@ return Drupal.t('Your changes will be lost if you leave the page without saving.'); } }); - $('#checklistapi-checklist-form').submit(function() { + $('#checklistapi-checklist-form').submit(function () { $(window).unbind('beforeunload'); }); - $('#checklistapi-checklist-form .clear-saved-progress').click(function() { + $('#checklistapi-checklist-form .clear-saved-progress').click(function () { $(window).unbind('beforeunload'); }); } diff --git a/checklistapi.module b/checklistapi.module index 8948558..7340158 100644 --- a/checklistapi.module +++ b/checklistapi.module @@ -31,7 +31,7 @@ use Drupal\Core\Render\Element; function checklistapi_checklist_access($id, $operation = 'any') { $all_operations = array('view', 'edit', 'any'); if (!in_array($operation, $all_operations)) { - throw new ChecklistapiException(sprintf('No such operation "%s"', $operation)); + throw new \InvalidArgumentException(sprintf('No such operation "%s"', $operation)); } $current_user = \Drupal::currentUser(); @@ -61,19 +61,14 @@ function checklistapi_checklist_load($id) { * Compact mode shows checklist forms with less description text. * * Whether the user is in compact mode is determined by a cookie, which is set - * for the user by checklistapi_compact_page(). - * - * If the user does not have the cookie, the default value is given by the - * system variable 'checklistapi_compact_mode', which itself defaults to FALSE. - * This does not have a user interface to set it: it is a hidden variable which - * can be set in the settings.php file. + * for the user by ChecklistapiController::setCompactMode(). If the user does + * not have the cookie, the setting defaults to off. * * @return bool * TRUE when in compact mode, or FALSE when in expanded mode. */ function checklistapi_compact_mode() { - $config = \Drupal::config('checklistapi.progress'); - return isset($_COOKIE['Drupal_visitor_checklistapi_compact_mode']) ? $_COOKIE['Drupal_visitor_checklistapi_compact_mode'] : $config->get('checklistapi_compact_mode'); + return isset($_COOKIE['Drupal_visitor_checklistapi_compact_mode']) ? (bool) $_COOKIE['Drupal_visitor_checklistapi_compact_mode'] : FALSE; } /** diff --git a/lib/Drupal/checklistapi/ChecklistapiChecklist.php b/lib/Drupal/checklistapi/ChecklistapiChecklist.php index 633835d..f22cc5f 100644 --- a/lib/Drupal/checklistapi/ChecklistapiChecklist.php +++ b/lib/Drupal/checklistapi/ChecklistapiChecklist.php @@ -205,7 +205,8 @@ class ChecklistapiChecklist { /** * Clears the saved progress for the checklist. * - * Deletes the Drupal variable containing the checklist's saved progress. + * Deletes the Drupal configuration object containing the checklist's saved + * progress. */ public function clearSavedProgress() { $this->config->delete(); diff --git a/lib/Drupal/checklistapi/ChecklistapiException.php b/lib/Drupal/checklistapi/ChecklistapiException.php deleted file mode 100644 index 1b83bc0..0000000 --- a/lib/Drupal/checklistapi/ChecklistapiException.php +++ /dev/null @@ -1,13 +0,0 @@ - ($mode == 'on'))); - return $this->redirect(''); + + // Redirect to the checklist. + // @todo There must be a better way than this. + $path = explode('/', current_path()); + array_pop($path); + array_pop($path); + $checklist_path = implode('/', $path); + return $this->redirect(new Route($checklist_path)); } } diff --git a/tests/Drupal/checklistapi/Tests/ChecklistapiModuleTest.php b/tests/Drupal/checklistapi/Tests/ChecklistapiModuleTest.php index bc50036..4998eec 100644 --- a/tests/Drupal/checklistapi/Tests/ChecklistapiModuleTest.php +++ b/tests/Drupal/checklistapi/Tests/ChecklistapiModuleTest.php @@ -109,7 +109,7 @@ class ChecklistapiModuleTest extends UnitTestCase { /** * Tests that checklistapi_checklist_access() rejects an invalid mode. * - * @expectedException \Drupal\checklistapi\ChecklistapiException + * @expectedException \InvalidArgumentException * @expectedExceptionMessage No such operation "invalid operation" */ public function testChecklistapiChecklistAccessInvalidMode() {