Commit 0935e4c9 authored by webchick's avatar webchick

Issue #2215049 by sun: Stop duplicating entire test list table into JavaScript.

parent 18698102
......@@ -82,10 +82,15 @@ public function buildForm(array $form, array &$form_state) {
'#title' => $this->t('Collapse'),
'#suffix' => '<a href="#" class="simpletest-collapse">(' . $this->t('Collapse') . ')</a>',
);
$js = array(
'images' => array(
drupal_render($image_collapsed),
drupal_render($image_extended),
$form['tests']['#attached']['js'][] = array(
'type' => 'setting',
'data' => array(
'simpleTest' => array(
'images' => array(
drupal_render($image_collapsed),
drupal_render($image_extended),
),
),
),
);
......@@ -108,13 +113,13 @@ public function buildForm(array $form, array &$form_state) {
$form['tests'][$group]['select'] = array(
'#wrapper_attributes' => array(
'id' => $group_class,
'class' => array('simpletest-select-all'),
'class' => array('simpletest-group-select-all'),
),
);
$form['tests'][$group]['title'] = array(
// Expand/collapse image.
'#prefix' => '<div class="simpletest-image" id="simpletest-test-group-' . $group_class . '"></div>',
'#markup' => '<label for="' . $group_class . '-select-all" class="simpletest-group-label">' . $group . '</label>',
'#markup' => '<label for="' . $group_class . '-group-select-all">' . $group . '</label>',
'#wrapper_attributes' => array(
'class' => array('simpletest-group-label'),
),
......@@ -126,15 +131,6 @@ public function buildForm(array $form, array &$form_state) {
),
);
// Add individual tests to group.
$current_js = array(
'testClass' => $group_class . '-test',
'testNames' => array(),
// imageDirection maps to the 'images' index in the $js array.
'imageDirection' => 0,
'clickActive' => FALSE,
);
// Sort test classes within group alphabetically by name/label.
uasort($tests, function ($a, $b) {
return SortArray::sortByKeyString($a, $b, 'name');
......@@ -142,10 +138,6 @@ public function buildForm(array $form, array &$form_state) {
// Cycle through each test within the current group.
foreach ($tests as $class => $info) {
$test_id = drupal_clean_id_identifier($class);
$test_checkbox_id = 'edit-tests-' . $test_id;
$current_js['testNames'][] = $test_checkbox_id;
$form['tests'][$class] = array(
'#attributes' => array('class' => array($group_class . '-test', 'js-hide')),
);
......@@ -168,16 +160,8 @@ public function buildForm(array $form, array &$form_state) {
),
);
}
$js['simpletest-test-group-' . $group_class] = $current_js;
}
// Add JavaScript array of settings.
$form['tests']['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('simpleTest' => $js),
);
// Action buttons.
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
......
......@@ -3,98 +3,56 @@
"use strict";
/**
* Add the cool table collapsing on the testing overview page.
* Collapses table rows followed by group rows on the test listing page.
*/
Drupal.behaviors.simpleTestMenuCollapse = {
Drupal.behaviors.simpleTestGroupCollapse = {
attach: function (context) {
var timeout = null;
// Adds expand-collapse functionality.
$('div.simpletest-image').once('simpletest-image', function () {
var $this = $(this);
var direction = drupalSettings.simpleTest[this.id].imageDirection;
$this.html(drupalSettings.simpleTest.images[direction]);
// Adds group toggling functionality to arrow images.
$this.on('click', function () {
var trs = $this.closest('tbody').children('.' + drupalSettings.simpleTest[this.id].testClass);
var direction = drupalSettings.simpleTest[this.id].imageDirection;
var row = direction ? trs.length - 1 : 0;
// If clicked in the middle of expanding a group, stop so we can switch directions.
if (timeout) {
clearTimeout(timeout);
}
// Function to toggle an individual row according to the current direction.
// We set a timeout of 20 ms until the next row will be shown/hidden to
// create a sliding effect.
function rowToggle() {
if (direction) {
if (row >= 0) {
$(trs[row]).hide();
row--;
timeout = setTimeout(rowToggle, 20);
}
}
else {
if (row < trs.length) {
$(trs[row]).removeClass('js-hide').show();
row++;
timeout = setTimeout(rowToggle, 20);
}
}
}
// Kick-off the toggling upon a new click.
rowToggle();
// Toggle the arrow image next to the test group title.
$this.html(drupalSettings.simpleTest.images[(direction ? 0 : 1)]);
drupalSettings.simpleTest[this.id].imageDirection = !direction;
});
$(context).find('.simpletest-group').once('simpletest-group-collapse', function () {
var $group = $(this);
var $image = $group.find('.simpletest-image');
$image
.html(drupalSettings.simpleTest.images[0])
.on('click', function () {
var $tests = $group.nextUntil('.simpletest-group');
var expand = !$group.hasClass('expanded');
$group.toggleClass('expanded', expand);
$tests.toggleClass('js-hide', !expand);
$image.html(drupalSettings.simpleTest.images[+expand]);
});
});
}
};
/**
* Select/deselect all the inner checkboxes when the outer checkboxes are
* selected/deselected.
* Toggles test checkboxes to match the group checkbox.
*/
Drupal.behaviors.simpleTestSelectAll = {
attach: function (context) {
$('td.simpletest-select-all').once('simpletest-select-all', function () {
var testCheckboxes = drupalSettings.simpleTest['simpletest-test-group-' + $(this).attr('id')].testNames;
var groupCheckbox = $('<input type="checkbox" class="form-checkbox" id="' + $(this).attr('id') + '-select-all" />');
// Each time a single-test checkbox is checked or unchecked, make sure
// that the associated group checkbox gets the right state too.
function updateGroupCheckbox() {
var checkedTests = 0;
for (var i = 0; i < testCheckboxes.length; i++) {
if ($('#' + testCheckboxes[i]).prop('checked')) {
checkedTests++;
}
}
$(groupCheckbox).prop('checked', (checkedTests === testCheckboxes.length));
}
// Have the single-test checkboxes follow the group checkbox.
groupCheckbox.on('change', function () {
$(context).find('.simpletest-group').once('simpletest-group-select-all', function () {
var $group = $(this);
var $cell = $group.find('.simpletest-group-select-all');
var $groupCheckbox = $('<input type="checkbox" id="' + $cell.attr('id') + '-group-select-all" class="form-checkbox" />');
var $testCheckboxes = $group.nextUntil('.simpletest-group').find('input[type=checkbox]');
$cell.append($groupCheckbox);
// Toggle the test checkboxes when the group checkbox is toggled.
$groupCheckbox.on('change', function () {
var checked = $(this).prop('checked');
for (var i = 0; i < testCheckboxes.length; i++) {
$('#' + testCheckboxes[i]).prop('checked', checked);
}
$testCheckboxes.prop('checked', checked);
});
// Have the group checkbox follow the single-test checkboxes.
for (var i = 0; i < testCheckboxes.length; i++) {
$('#' + testCheckboxes[i]).on('change', updateGroupCheckbox);
// Update the group checkbox when a test checkbox is toggled.
function updateGroupCheckbox() {
var allChecked = true;
$testCheckboxes.each(function () {
if (!$(this).prop('checked')) {
allChecked = false;
return false;
}
});
$groupCheckbox.prop('checked', allChecked);
}
// Initialize status for the group checkbox correctly.
updateGroupCheckbox();
$(this).append(groupCheckbox);
$testCheckboxes.on('change', updateGroupCheckbox);
});
}
};
......@@ -102,9 +60,6 @@
/**
* Filters the test list table by a text input search string.
*
* Additionally accounts for multiple tables being wrapped in "package" details
* elements.
*
* Text search input: input.table-filter-text
* Target table: input.table-filter-text[data-table]
* Source text: .table-filter-text-source
......@@ -139,14 +94,8 @@
else if (searched) {
searched = false;
$('#simpletest-form-table thead th.select-all input').show();
// Hide all rows and then show groups.
$rows.hide();
$rows.filter('.simpletest-group').show().each(function () {
var id = 'simpletest-test-group-' + $(this).children().first().attr('id');
if (drupalSettings.simpleTest[id].imageDirection) {
$(this).closest('tbody').children('.' + drupalSettings.simpleTest[id].testClass).show();
}
});
// Restore all rows to their original display state.
$rows.css('display', '');
}
}
......
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