Commit 651f97c8 authored by markfullmer's avatar markfullmer Committed by Mark Fullmer

Issue #2919397 by mark_fullmer: Minify JS, remove invalid switcher option, add...

Issue #2919397 by mark_fullmer: Minify JS, remove invalid switcher option, add support for overriding on a per-table basis
parent 54b78532
This diff is collapsed.
......@@ -4,7 +4,7 @@ tablesaw-filter:
theme:
css/tablesaw.css: {}
js:
js/tablesaw.js: {}
js/tablesaw.min.js: {}
js/tablesaw-init.js: {}
dependencies:
- core/jquery
......@@ -20,11 +20,17 @@ use Drupal\Core\Form\FormStateInterface;
*/
class FilterResponsiveTablesFilter extends FilterBase {
public static $modes = [
'stack' => "Stack Mode",
'columntoggle' => "Column Toggle Mode",
'swipe' => "Swipe Mode",
];
/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
if ($filtered = $this->responsive_tables_filter($text)) {
if ($filtered = $this->runFilter($text)) {
$result = new FilterProcessResult($filtered);
// Attach Tablesaw library assets to this page.
$result->setAttachments([
......@@ -44,15 +50,10 @@ class FilterResponsiveTablesFilter extends FilterBase {
public function settingsForm(array $form, FormStateInterface $form_state) {
$form['tablesaw_type'] = [
'#type' => 'select',
'#title' => $this->t('Tablesaw Types'),
'#title' => $this->t('Default mode'),
'#default_value' => $this->settings['tablesaw_type'],
'#description' => $this->t('Tablesaw styles availabe on mobile. Documentation: https://github.com/filamentgroup/tablesaw'),
'#options' => [
'stack' => $this->t('Stack Mode'),
'columntoggle' => $this->t('Column Toggle Mode'),
'swipe' => $this->t('Swipe Mode'),
'mode_switcher' => $this->t('Mode Switcher'),
],
'#description' => $this->t('This will apply by default to tables in WYSIWYGs, but can be overridden on an individual basis by adding the <code>class</code> "tablesaw-stack", "tablesaw-columntoggle", or "tablesaw-swipe" to the <code>table</code> tag. See documentation: https://github.com/filamentgroup/tablesaw'),
'#options' => self::$modes,
];
return $form;
}
......@@ -60,7 +61,7 @@ class FilterResponsiveTablesFilter extends FilterBase {
/**
* Business logic for adding classes & attributes to <table> tags.
*/
public function responsive_tables_filter($text) {
public function runFilter($text) {
// Older versions of libxml always add DOCTYPE, <html>, and <body> tags.
// See http://www.php.net/manual/en/libxml.constants.php.
// Sometimes, PHP is >= 5.4, but libxml is old enough that the constants are
......@@ -89,13 +90,19 @@ class FilterResponsiveTablesFilter extends FilterBase {
// Find existing class attributes, if any, and append tablesaw class.
$existing_classes = $table->getAttribute('class');
if (strpos($existing_classes, 'no-tablesaw') === FALSE) {
$new_classes = !empty($existing_classes) ? $existing_classes . ' tablesaw tablesaw-' . $this->settings['tablesaw_type'] : 'tablesaw tablesaw-' . $this->settings['tablesaw_type'];
$type = $this->settings['tablesaw_type'];
// Allow for class-based override of default.
foreach (array_keys(self::$modes) as $mode) {
if (strpos($existing_classes, "tablesaw-" . $mode) !== FALSE) {
$type = $mode;
break;
}
}
$new_classes = !empty($existing_classes) ? $existing_classes . ' tablesaw tablesaw-' . $type : 'tablesaw tablesaw-' . $type;
$table->setAttribute('class', $new_classes);
// Force data-tablesaw-mode attribute to be "stack".
$table->setAttribute('data-tablesaw-mode', $this->settings['tablesaw_type']);
// Set data-tablesaw-mode & minimap.
$table->setAttribute('data-tablesaw-mode', $type);
$table->setAttribute('data-tablesaw-minimap', NULL);
}
}
// Get innerHTML of root node.
......
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