Skip to content
Snippets Groups Projects
Commit 22464422 authored by Vladimir Roudakov's avatar Vladimir Roudakov
Browse files

Issue #3340969 by teknocat, joelpittet: Automatic TOC patch causes TOC to show...

Issue #3340969 by teknocat, joelpittet: Automatic TOC patch causes TOC to show on all pages regardless of configuration
parents f38f0ce3 d62b65e6
No related branches found
No related tags found
No related merge requests found
......@@ -11,35 +11,35 @@
* @ingroup forms
*/
function toc_filter_admin_settings() {
$form['toc_filter_header_tag'] = array(
$form['toc_filter_header_tag'] = [
'#type' => 'select',
'#title' => t('Header tag'),
'#description' => t('The selected header tag is used to generate the table of contents when the TOC filter is assigned to an input format.'),
'#options' => array(
'#options' => [
'h1' => 'h1',
'h2' => 'h2',
'h3' => 'h3',
'h4' => 'h4',
'h5' => 'h5',
'h6' => 'h6',
),
],
'#required' => 1,
'#default_value' => variable_get('toc_filter_header_tag', 'h3'),
);
];
$form['toc_filter_default_top'] = array(
$form['toc_filter_default_top'] = [
'#type' => 'checkbox',
'#title' => t('Insert table of contents by default'),
'#description' => t('Check to automatically place a Table of Contents at the top.'),
'#default_value' => variable_get('toc_filter_default_top', '0'),
);
];
$form['toc_filter_default_type'] = array(
$form['toc_filter_default_type'] = [
'#type' => 'select',
'#title' => t('Default Type'),
'#description' => t('Select the type of list that should be inserted by default.'),
'#default_value' => variable_get('toc_filter_default_type', 'ul'),
'#options' => array(
'#options' => [
'faq' => 'faq',
'ol' => 'ol',
'number' => 'number',
......@@ -47,58 +47,58 @@ function toc_filter_admin_settings() {
'bullet',
'jump-menu',
'menu',
),
'#states' => array(
'disabled' => array(
':input[name="toc_filter_default_top"]' => array('checked' => FALSE),
),
),
);
],
'#states' => [
'disabled' => [
':input[name="toc_filter_default_top"]' => ['checked' => FALSE],
],
],
];
$form['toc_filter_bullet_default_title'] = array(
$form['toc_filter_bullet_default_title'] = [
'#type' => 'textfield',
'#title' => t('Un-order (bullet) list default title'),
'#default_value' => variable_get('toc_filter_bullet_default_title', ''),
);
];
$form['toc_filter_number_default_title'] = array(
$form['toc_filter_number_default_title'] = [
'#type' => 'textfield',
'#title' => t('Order (number) list default title'),
'#default_value' => variable_get('toc_filter_number_default_title', ''),
);
];
$form['toc_filter_faq_default_title'] = array(
$form['toc_filter_faq_default_title'] = [
'#type' => 'textfield',
'#title' => t('FAQ default title'),
'#default_value' => variable_get('toc_filter_faq_default_title', ''),
);
];
if (module_exists('ctools')) {
$form['toc_filter_jump_menu_default_title'] = array(
$form['toc_filter_jump_menu_default_title'] = [
'#type' => 'textfield',
'#title' => t('Jump menu default title'),
'#default_value' => variable_get('toc_filter_jump_menu_default_title', ''),
'#description' => t('The jump menu title will appear as the first item in the select menu.'),
);
];
}
$form['toc_filter_smooth_scroll'] = array(
$form['toc_filter_smooth_scroll'] = [
'#type' => 'checkbox',
'#title' => t('Enable smooth scrolling'),
'#description' => t('Smooth scrolling will be applied to all anchor links'),
'#default_value' => variable_get('toc_filter_smooth_scroll', '1'),
);
];
$form['toc_filter_smooth_scroll_duration'] = array(
$form['toc_filter_smooth_scroll_duration'] = [
'#type' => 'select',
'#title' => t('Smooth scrolling duration'),
'#default_value' => variable_get('toc_filter_smooth_scroll_duration', ''),
'#options' => array(
'#options' => [
'slow' => 'slow',
'' => 'medium',
'fast' => 'fast',
),
);
],
];
return system_settings_form($form);
}
......@@ -21,13 +21,14 @@ function toc_filter_uninstall() {
}
/**
* Change 'toc_filter_ol_default_title' and 'toc_filter_ul_default_title' to 'toc_filter_numbered_default_title' and 'toc_filter_bullet_default_title'
* Change 'toc_filter_ol_default_title' and 'toc_filter_ul_default_title' to
* 'toc_filter_numbered_default_title' and 'toc_filter_bullet_default_title'
*/
function toc_filter_update_7103() {
$variables = array(
$variables = [
'toc_filter_ol_default_title' => 'toc_filter_number_default_title',
'toc_filter_ul_default_title' => 'toc_filter_bullet_default_title',
);
];
foreach ($variables as $from => $to) {
if ($value = variable_get($from, '')) {
variable_set($to, $value);
......
......@@ -8,67 +8,68 @@
(function ($) {
Drupal.tocFilterScrollToOnClick = function() {
// Make sure links still has hash.
if (!this.hash || this.hash == '#') {
return true;
}
// Make sure the href is pointing to an anchor link on this page.
var href = this.href.replace(/#[^#]*$/, '');
var url = window.location.toString();
if (href && url.indexOf(href) === -1) {
return true;
}
// Scroll to the anchor
return Drupal.tocFilterScrollTo(this.hash);
}
Drupal.tocFilterScrollToOnClick = function () {
// Make sure links still has hash.
if (!this.hash || this.hash == '#') {
return true;
}
Drupal.tocFilterScrollTo = function(hash) {
// Find hash target.
var $a = $('a[name=' + hash.substring(1) + ']');
// Make sure the href is pointing to an anchor link on this page.
var href = this.href.replace(/#[^#]*$/, '');
var url = window.location.toString();
if (href && url.indexOf(href) === -1) {
return true;
}
// Make hash target is on the current page.
if (!$a.length) {
return true;
// Scroll to the anchor
return Drupal.tocFilterScrollTo(this.hash);
}
// Scroll to hash target
var duration = Drupal.settings.toc_filter_smooth_scroll_duration || 'medium';
$('html, body').animate({scrollTop: $a.offset().top}, duration);
Drupal.tocFilterScrollTo = function (hash) {
// Find hash target.
var $a = $('a[name=' + hash.substring(1) + ']');
// Move focus to targets back to top link.
// Target anchor not focused; breaks keyboard navigation https://drupal.org/node/2058875
$a.parent().prev('.toc-filter-back-to-top').find('a').focus();
// Make hash target is on the current page.
if (!$a.length) {
return true;
}
return false;
}
// Scroll to hash target
var duration = Drupal.settings.toc_filter_smooth_scroll_duration || 'medium';
$('html, body').animate({scrollTop: $a.offset().top}, duration);
Drupal.behaviors.tocFilterSmoothScroll = {
attach: function (context) {
// Only map <a href="#..."> links
$('a[href*="#"]', context).once('toc-filter').click(Drupal.tocFilterScrollToOnClick);
// Move focus to targets back to top link.
// Target anchor not focused; breaks keyboard navigation
// https://drupal.org/node/2058875
$a.parent().prev('.toc-filter-back-to-top').find('a').focus();
return false;
}
};
Drupal.behaviors.tocFilterSmoothScroll = {
attach: function (context) {
// Only map <a href="#..."> links
$('a[href*="#"]', context).once('toc-filter').click(Drupal.tocFilterScrollToOnClick);
}
};
// Override CToolsJumpMenu behavior for TOC filter jumpmenus.
Drupal.behaviors.tocFilterCToolsJumpMenu = {
attach: function(context) {
$('.toc-filter-jump-menu .ctools-jump-menu-change:not(.toc-filter-jump-menu-processed)')
.addClass('toc-filter-jump-menu-processed')
.unbind('change')
.change(function() {
// Find our sibling value.
var $select = $(this).parents('form').find('.ctools-jump-menu-select');
var hash = $select.val();
if (hash) {
Drupal.tocFilterScrollTo(hash);
}
$select.find('option:first').attr('selected', true);
return false;
});
}
};
Drupal.behaviors.tocFilterCToolsJumpMenu = {
attach: function (context) {
$('.toc-filter-jump-menu .ctools-jump-menu-change:not(.toc-filter-jump-menu-processed)')
.addClass('toc-filter-jump-menu-processed')
.unbind('change')
.change(function () {
// Find our sibling value.
var $select = $(this).parents('form').find('.ctools-jump-menu-select');
var hash = $select.val();
if (hash) {
Drupal.tocFilterScrollTo(hash);
}
$select.find('option:first').attr('selected', true);
return false;
});
}
};
})(jQuery)
......@@ -10,14 +10,20 @@
*/
function toc_filter_init() {
if (module_exists('ctools')) {
drupal_add_js(drupal_get_path('module', 'ctools') . '/js/jump-menu.js', array('type' => 'file', 'every_page' => TRUE));
drupal_add_js(drupal_get_path('module', 'ctools') . '/js/jump-menu.js', [
'type' => 'file',
'every_page' => TRUE,
]);
}
if (variable_get('toc_filter_smooth_scroll', '1')) {
drupal_add_js(drupal_get_path('module', 'toc_filter') . '/toc_filter.js', array('type' => 'file', 'every_page' => TRUE));
$settings = array(
drupal_add_js(drupal_get_path('module', 'toc_filter') . '/toc_filter.js', [
'type' => 'file',
'every_page' => TRUE,
]);
$settings = [
'toc_filter_smooth_scroll_duration' => variable_get('toc_filter_smooth_scroll_duration', ''),
);
];
drupal_add_js($settings, 'setting');
}
}
......@@ -26,15 +32,15 @@ function toc_filter_init() {
* Implements hook_menu().
*/
function toc_filter_menu() {
$items = array();
$items['admin/config/content/toc_filter'] = array(
$items = [];
$items['admin/config/content/toc_filter'] = [
'title' => 'TOC filter',
'page callback' => 'drupal_get_form',
'page arguments' => array('toc_filter_admin_settings'),
'access arguments' => array('administer site configuration'),
'page arguments' => ['toc_filter_admin_settings'],
'access arguments' => ['administer site configuration'],
'file' => 'toc_filter.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
];
return $items;
}
......@@ -43,20 +49,20 @@ function toc_filter_menu() {
*/
function toc_filter_filter_info() {
if (module_exists('ctools')) {
$description = t("Converts &lt;@header_tag&gt; tags to a linked table of contents or jump menu with an optional title. (i.e [TOC:(faq|numbered|jump-menu|menu) (title)]", array('@header_tag' => variable_get('toc_filter_header_tag', 'h3')));
$description = t("Converts &lt;@header_tag&gt; tags to a linked table of contents or jump menu with an optional title. (i.e [TOC:(faq|numbered|jump-menu|menu) (title)]", ['@header_tag' => variable_get('toc_filter_header_tag', 'h3')]);
}
else {
$description = t("Converts &lt;@header_tag&gt; tags to a linked table of contents with an optional title. (i.e [TOC:(faq|numbered) (title)]", array('@header_tag' => variable_get('toc_filter_header_tag', 'h3')));
$description = t("Converts &lt;@header_tag&gt; tags to a linked table of contents with an optional title. (i.e [TOC:(faq|numbered) (title)]", ['@header_tag' => variable_get('toc_filter_header_tag', 'h3')]);
}
$filters['toc_filter'] = array(
$filters['toc_filter'] = [
'title' => t('Table of contents'),
'description' => $description,
'default settings' => array(),
'default settings' => [],
'settings callback' => '_toc_filter_settings_callback',
'process callback' => '_toc_filter_process_callback',
'tips callback' => '_toc_filter_tips_callback',
);
];
return $filters;
}
......@@ -65,10 +71,10 @@ function toc_filter_filter_info() {
*/
function _toc_filter_tips_callback($delta, $format, $long = FALSE) {
if (module_exists('ctools')) {
return t("Adding [TOC:(faq|ol|number|ul|bullet|jump-menu|menu) (title)] will generate a table of contents or jump menu linked to all the &lt;@header_tag&gt; tags with an optional title.", array("@header_tag" => variable_get('toc_filter_header_tag', 'h3')));
return t("Adding [TOC:(faq|ol|number|ul|bullet|jump-menu|menu) (title)] will generate a table of contents or jump menu linked to all the &lt;@header_tag&gt; tags with an optional title.", ["@header_tag" => variable_get('toc_filter_header_tag', 'h3')]);
}
else {
return t("Adding [TOC:(faq|ol|number|ul|bullet) (title)] will generate a table of contents linked to all the &lt;@header_tag&gt; tags with an optional title.", array("@header_tag" => variable_get('toc_filter_header_tag', 'h3')));
return t("Adding [TOC:(faq|ol|number|ul|bullet) (title)] will generate a table of contents linked to all the &lt;@header_tag&gt; tags with an optional title.", ["@header_tag" => variable_get('toc_filter_header_tag', 'h3')]);
}
}
......@@ -86,7 +92,7 @@ function _toc_filter_process_callback($text) {
}
// Must track nested filter calls which can be created by drupal_get_form('ctools_jump_menu').
static $processing = array();
static $processing = [];
$md5 = md5($text);
if (isset($processing[$md5])) {
return $text;
......@@ -126,18 +132,22 @@ function _toc_filter_process_callback($text) {
$header_tag = variable_get('toc_filter_header_tag', 'h3');
preg_match_all('/(<' . $header_tag . '[^>]*>)(.*?)(<\/' . $header_tag . '>)/is', $text, $header_matches);
$targets = array();
$links = array();
$targets = [];
$links = [];
for ($i = 0, $len = count($header_matches[0]); $i < $len; $i++) {
$header_match = $header_matches[0][$i];
$open_tag = $header_matches[1][$i];
$header_title = $header_matches[2][$i];
if (empty($header_title)) {
continue;
}
$close_tag = $header_matches[3][$i];
$header_id = preg_replace('/[^-a-z0-9]+/', '-', drupal_strtolower(trim($header_title)));
// Add header class to open tag.
$open_tag_attributes = toc_filter_parse_tag_attributes($open_tag) + array('class' => '');
$open_tag_attributes = toc_filter_parse_tag_attributes($open_tag) + ['class' => ''];
$open_tag_attributes['class'] .= ((empty($open_tag_attributes['class'])) ? '' : ' ') . ' toc-header toc-header-' . $format;
$header_replace = '<' . $header_tag . drupal_attributes($open_tag_attributes) . '>' .
......@@ -151,7 +161,7 @@ function _toc_filter_process_callback($text) {
$links[] = '<a href="#' . $header_id . '">' . strip_tags($header_title, '<i><em><b><strong><br>') . '</a>';
// Add anchor before header.
$back_to_top = theme('toc_filter_back_to_top', array('class' => ($i == 0) ? 'first' : ''));
$back_to_top = theme('toc_filter_back_to_top', ['class' => ($i == 0) ? 'first' : '']);
$text = str_replace($header_match, $back_to_top . $header_replace, $text);
}
......@@ -162,12 +172,20 @@ function _toc_filter_process_callback($text) {
// Theme list.
$links_list_type = ($is_numbered) ? 'ol' : 'ul';
$toc_content = theme('item_list', array('items' => $links, 'title' => check_plain($title), 'type' => $links_list_type, 'attributes' => array('class' => 'toc-filter-links')));
$output = theme('toc_filter', array('type' => $type, 'content' => $toc_content));
$toc_content = theme('item_list', [
'items' => $links,
'title' => check_plain($title),
'type' => $links_list_type,
'attributes' => ['class' => 'toc-filter-links'],
]);
$output = theme('toc_filter', [
'format' => $format,
'content' => $toc_content,
]);
// Add jump menu with noscript list.
if ($format == 'jump_menu' && module_exists('ctools')) {
$options = array();
$options = [];
if ($title) {
$options['choose'] = check_plain($title);
}
......@@ -188,7 +206,7 @@ function _toc_filter_process_callback($text) {
$text = str_replace($match, $output, $text);
// Add closing back to top.
$text .= theme('toc_filter_back_to_top', array('class' => 'last'));
$text .= theme('toc_filter_back_to_top', ['class' => 'last']);
unset($processing[$md5]);
......@@ -199,14 +217,14 @@ function _toc_filter_process_callback($text) {
* TOC filter settings callback.
*/
function _toc_filter_settings_callback() {
$form = array();
$form['toc_filter_settings'] = array(
$form = [];
$form['toc_filter_settings'] = [
'#type' => 'fieldset',
'#title' => t('TOC filter'),
'#description' => t('To configure this filter, please goto the global <a href="@href">TOC filter site configuration form</a>.', array('@href' => url('admin/config/content/toc_filter'))),
'#description' => t('To configure this filter, please goto the global <a href="@href">TOC filter site configuration form</a>.', ['@href' => url('admin/config/content/toc_filter')]),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
];
return $form;
}
......@@ -219,7 +237,7 @@ function _toc_filter_settings_callback() {
*/
function toc_filter_parse_tag_attributes($tag) {
preg_match_all('/(\w+)\s*=\s*"([^"]+)"/', $tag, $matches);
$attributes = array();
$attributes = [];
for ($i = 0, $len = count($matches[1]); $i < $len; $i++) {
$attributes[$matches[1][$i]] = htmlspecialchars_decode($matches[2][$i], ENT_QUOTES);
}
......@@ -234,19 +252,19 @@ function toc_filter_parse_tag_attributes($tag) {
* Implements hook_theme().
*/
function toc_filter_theme() {
return array(
'toc_filter' => array(
'variables' => array(
return [
'toc_filter' => [
'variables' => [
'format' => '',
'content' => '',
),
),
'toc_filter_back_to_top' => array(
'variables' => array(
],
],
'toc_filter_back_to_top' => [
'variables' => [
'class' => '',
),
),
);
],
],
];
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment