Commit 50b6a6c1 authored by TravisCarden's avatar TravisCarden

By TravisCarden: Removed route-based compact mode functionality in favor of JavaScript-based.

parent ae5e2eab
......@@ -45,6 +45,9 @@
*/
Drupal.behaviors.checklistapiCompactModeLink = {
attach: function (context) {
var is_compact_mode = $('#checklistapi-checklist-form', context).hasClass('compact-mode');
var text = is_compact_mode ? Drupal.t('Show item descriptions') : Drupal.t('Hide item descriptions');
$('#checklistapi-checklist-form .compact-link', context).html('<a href="#">' + text + '</a>');
$('#checklistapi-checklist-form .compact-link a', context).click(function () {
$(this).closest('#checklistapi-checklist-form').toggleClass('compact-mode');
var is_compact_mode = $(this).closest('#checklistapi-checklist-form').hasClass('compact-mode');
......
......@@ -12,7 +12,6 @@ use Drupal\checklistapi\ChecklistapiChecklist;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* Access callback: Checks the current user's access to a given checklist.
......@@ -69,8 +68,9 @@ function checklistapi_checklist_load($id) {
* @return bool
* TRUE when in compact mode, or FALSE when in expanded mode.
*/
function checklistapi_compact_mode() {
return isset($_COOKIE['Drupal_visitor_checklistapi_compact_mode']) ? (bool) $_COOKIE['Drupal_visitor_checklistapi_compact_mode'] : FALSE;
function checklistapi_compact_mode_is_on() {
// PHP converts dots into underscores in cookie names.
return (bool) \Drupal::request()->cookies->get('Drupal_visitor_checklistapi_compact_mode', FALSE);
}
/**
......@@ -194,10 +194,6 @@ function checklistapi_strtolowercamel($string) {
*/
function checklistapi_theme() {
return array(
'checklistapi_compact_link' => array(
'template' => 'checklistapi-compact-link',
'variables' => array('link' => ''),
),
'checklistapi_progress_bar' => array(
'path' => drupal_get_path('module', 'checklistapi') . '/templates',
'template' => 'checklistapi-progress-bar',
......@@ -210,33 +206,3 @@ function checklistapi_theme() {
),
);
}
/**
* Prepares variables for checklist compact link templates.
*
* Default template: checklistapi-compact-link.html.twig.
*
* @param array $variables
* An empty array.
*/
function template_preprocess_checklistapi_compact_link(array &$variables) {
if (checklistapi_compact_mode()) {
$text = t('Show item descriptions');
$mode = 'off';
$title = t('Expand layout to include item descriptions.');
}
else {
$text = t('Hide item descriptions');
$mode = 'on';
$title = t('Compress layout by hiding item descriptions.');
}
$current_route = \Drupal::routeMatch()->getRouteName();
$route_name = "{$current_route}.compact";
$route_parameters = array('mode' => $mode);
$options = array('query' => drupal_get_destination());
$url = Url::fromRoute($route_name, $route_parameters, $options)
->setOption('attributes', array('title' => $title));
$variables['link'] = \Drupal::l($text, $url);
}
......@@ -8,8 +8,6 @@
namespace Drupal\checklistapi\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Route;
use Drupal\Core\Url;
/**
......@@ -91,33 +89,4 @@ class ChecklistapiController extends ControllerBase {
return $output;
}
/**
* Sets whether the admin menu is in compact mode or not.
*
* @param string $mode
* The mode to set compact mode to. Accepted values are "on" and "off".
*
* @throws NotFoundHttpException
* Throws an exception if an invalid mode is supplied.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* Return a redirect response object.
*/
public function setCompactMode($mode) {
$all_modes = array('on', 'off');
if (!in_array($mode, $all_modes)) {
throw new NotFoundHttpException();
}
// Persist the setting for the current user.
user_cookie_save(array('checklistapi_compact_mode' => ($mode == 'on')));
// Redirect to the checklist.
// @todo There must be a better way than this.
$path = explode('/', Url::fromRoute('<current>'));
array_pop($path);
array_pop($path);
$checklist_path = implode('/', $path);
return $this->redirect(new Route($checklist_path));
}
}
......@@ -50,10 +50,10 @@ class ChecklistapiChecklistForm implements FormInterface {
);
// Compact mode.
if (checklistapi_compact_mode()) {
if (checklistapi_compact_mode_is_on()) {
$form['#attributes']['class'] = array('compact-mode');
}
$compact_link = array('#theme' => 'checklistapi_compact_link');
$compact_link = array('#markup' => '<div class="compact-link"></div>');
$form['compact_mode_link'] = array(
'#markup' => $renderer->render($compact_link),
);
......
......@@ -30,13 +30,15 @@ class ChecklistapiRouteSubscriber extends RouteSubscriberBase {
continue;
}
$requirements = array('_checklistapi_access' => 'TRUE');
// View/edit checklist.
$routes["checklistapi.checklists.{$id}"] = new Route($definition['#path'], array(
'_title' => $definition['#title'],
'_form' => '\Drupal\checklistapi\Form\ChecklistapiChecklistForm',
'checklist_id' => $id,
'op' => 'any',
), $requirements = array('_checklistapi_access' => 'TRUE'));
), $requirements);
// Clear saved progress.
$routes["checklistapi.checklists.{$id}.clear"] = new Route("{$definition['#path']}/clear", array(
......@@ -46,15 +48,6 @@ class ChecklistapiRouteSubscriber extends RouteSubscriberBase {
'op' => 'edit',
), $requirements);
// Toggle compact mode.
$routes["checklistapi.checklists.{$id}.compact"] = new Route("{$definition['#path']}/compact/{mode}", array(
'_title' => 'Compact mode',
'_controller' => '\Drupal\checklistapi\Controller\ChecklistapiController::setCompactMode',
'checklist_id' => $id,
'op' => 'any',
'mode' => 'off',
), $requirements);
return $routes;
}
}
......@@ -63,4 +56,5 @@ class ChecklistapiRouteSubscriber extends RouteSubscriberBase {
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {}
}
......@@ -78,33 +78,6 @@ class ChecklistapiTest extends WebTestBase {
$this->assertRaw('This checklist based on', 'Created per-checklist help block.');
}
/**
* Tests compact mode.
*/
public function testCompactMode() {
$this->drupalGet('admin/config/development/checklistapi-example');
$this->assertTrue($this->isCompactModeEffective(), 'Compact mode disabled by default.');
$this->assertLink('Hide item descriptions', 0, 'Enable compact mode link present.');
$this->clickLink('Hide item descriptions');
$this->assertFalse($this->isCompactModeEffective(), 'Compact mode in effect.');
$this->assertLink('Show item descriptions', 0, 'Disable compact mode link present.');
$this->clickLink('Show item descriptions');
$this->assertTrue($this->isCompactModeEffective(), 'Compact mode back in effect.');
$this->assertLink('Hide item descriptions', 0, 'Enable compact mode link present again.');
}
/**
* Determines whether compact mode has taken effect or not.
*
* @return bool
* Returns TRUE if compact mode is effective, or FALSE if not.
*/
public function isCompactModeEffective() {
return !$this->cssSelect('#checklistapi-checklist-form.compact-mode');
}
/**
* Tests permissions.
*/
......
{#
/**
* @file
* Default theme implementation of a checklist compact link.
*
* Available variables:
* - link: The link, already formatted by \Drupal::l().
*
* @see template_preprocess_checklistapi_compact_link()
*
* @ingroup themeable
*/
#}
{% spaceless %}
<div class="compact-link">
{{ link }}
</div>
{% endspaceless %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment