Mike07
@Mike07

Как реализовать открытие и закрытие всех спойлеров разом на веб странице?

Имеется публикация, скажем, с 60 спойлерами. Как можно реализовать возможность раскрыть и закрыть разом все спойлеры для пользователей? Может есть плагин какой-нибудь? Или кнопку какую сделать?

<?php function bbspoiler_shortcode($atts, $content) {
	extract(shortcode_atts(array(
		'title' => __('Spoiler', 'bbspoiler'),
		'state' => 'folded',
        'style' => 'default',
		'collapse_link' => 'true'
	), $atts));

	$title = esc_attr($title);
	$head_class = (esc_attr($state) == 'folded')?'':' unfolded';
	$body_class = (esc_attr($state) == 'folded')?' folded':'';

	$output  = "\n<div class=\"sp-wrap sp-wrap-".$style."\">\n";
	$output .= "<div class=\"sp-head".$head_class."\" title=\"". __('Expand', 'bbspoiler') ."\">\n";
	$output .= $title;
	$output .= "\n</div>\n";
	$output .= "<div class=\"sp-body".$body_class."\">\n";
	$output .= wpautop(do_shortcode($content));
	if ($collapse_link == 'true') {
		$output .= "<div class=\"spdiv\">[". __('collapse', 'bbspoiler') . "]</div>\n";
	}
	$output .= "</div>\n</div>\n";

	return $output;
}
add_shortcode ('spoiler', 'bbspoiler_shortcode');

function bbspoiler_shortcode2($atts, $content) {
	extract(shortcode_atts(array(
		'title' => __('Spoiler', 'bbspoiler'),
		'state' => 'folded',
        'style' => 'default',
		'collapse_link' => 'true'
	), $atts));

	$title = esc_attr($title);
	$head_class = (esc_attr($state) == 'folded')?'':' unfolded';
	$body_class = (esc_attr($state) == 'folded')?' folded':'';

	$output  = "\n<div class=\"sp-wrap sp-wrap-".$style."\">\n";
	$output .= "<div class=\"sp-head".$head_class."\" title=\"". __('Expand', 'bbspoiler') ."\">\n";
	$output .= $title;
	$output .= "\n</div>\n";
	$output .= "<div class=\"sp-body".$body_class."\">\n";
	$output .= wpautop(do_shortcode($content));
	if ($collapse_link == 'true') {
		$output .= "<div class=\"spdiv\">[". __('collapse', 'bbspoiler') . "]</div>\n";
	}
	$output .= "</div>\n</div>\n";

	return $output;
}
add_shortcode ('spoiler2', 'bbspoiler_shortcode2');

function bbspoiler_files() {
	$purl = plugins_url();
	
	wp_register_script('bbspoiler', $purl . '/bbspoiler/inc/bbspoiler.js');  
	wp_register_style( 'bbspoiler', $purl . '/bbspoiler/inc/bbspoiler.css' );
	
	if(!wp_script_is('jquery')) {wp_enqueue_script('jquery');}
	wp_enqueue_script('bbspoiler');
	wp_enqueue_style('bbspoiler');
	
	$lang_array = array('unfolded' 	=> __('Expand', 'bbspoiler'), 
						'folded' 	=> __('Collapse', 'bbspoiler'));
	wp_localize_script('bbspoiler', 'title', $lang_array);   
}
add_action('wp_enqueue_scripts', 'bbspoiler_files');

function bbspoiler_admin_print_scripts() {
?>
<script type='text/javascript'>
var bbbutton = {
    "title":"<?php _e('Title', 'bbspoiler'); ?>",
    "spoiler":"<?php _e('Spoiler', 'bbspoiler'); ?>",
    "text":"<?php _e('Text', 'bbspoiler'); ?>",
    "showlink":"<?php _e('Show collapse link?', 'bbspoiler'); ?>",
    "style":"<?php _e('Style', 'bbspoiler'); ?>",
    "default2":"<?php _e('Default', 'bbspoiler'); ?>",
    "green":"<?php _e('Green', 'bbspoiler'); ?>",
    "red":"<?php _e('Red', 'bbspoiler'); ?>",
    "blue":"<?php _e('Blue', 'bbspoiler'); ?>",
    "yellow":"<?php _e('Yellow', 'bbspoiler'); ?>",
    "orange":"<?php _e('Orange', 'bbspoiler'); ?>",
    "brown":"<?php _e('Brown', 'bbspoiler'); ?>",
    "purple":"<?php _e('Purple', 'bbspoiler'); ?>",
    "cyan":"<?php _e('Cyan', 'bbspoiler'); ?>",
    "lime":"<?php _e('Lime', 'bbspoiler'); ?>",
    "steelblue":"<?php _e('SteelBlue', 'bbspoiler'); ?>",
    };
</script>
<?php }    
add_action('admin_head', 'bbspoiler_admin_print_scripts');

function bbspoiler_setup(){
    load_plugin_textdomain('bbspoiler');
}
add_action('init', 'bbspoiler_setup');

add_action('admin_print_footer_scripts','eg_quicktags'); 
function eg_quicktags(){
if (wp_script_is('quicktags')){ ?>
<script type="text/javascript" charset="utf-8">
buttonSpoiler = edButtons.length;
edButtons[edButtons.length] = new edButton('spoiler','spoiler','[spoiler title=\'Title\']Text[/spoiler]\n');

jQuery(document).ready(function($){
    jQuery("#ed_toolbar").append('<input type="button" value="spoiler" id="ed_spoiler" class="ed_button" onclick="edInsertTag(edCanvas, buttonSpoiler);" title="Spoiler" />');
});
</script>
<?php } }

function bbspoiler_add_tinymce() {
    global $typenow;
    if(!in_array($typenow, array('post', 'page')))
        return ;
    add_filter('mce_external_plugins', 'bbspoiler_add_tinymce_plugin');
    add_filter('mce_buttons', 'bbspoiler_add_tinymce_button');
}
add_action('admin_head', 'bbspoiler_add_tinymce');

function bbspoiler_add_tinymce_plugin($plugin_array) {
	$plugin_array['bbspoiler_button'] = plugins_url('/inc/bbbutton.js', __FILE__);
    return $plugin_array;
}

// Add the button key for address via JS
function bbspoiler_add_tinymce_button($buttons) {
    array_push($buttons, 'bbspoiler_button_button_key');
    return $buttons;
}

add_filter('bbp_get_reply_content', 'bb_enable_shortcode', 10,2);
add_filter('bbp_get_topic_content', 'bb_enable_shortcode', 10,2);
function bb_enable_shortcode($content) {
	return do_shortcode($content);
}

?>


( function() {
    tinymce.PluginManager.add( 'bbspoiler_button', function( editor, url ) {

        // Add a button that opens a window
        editor.addButton( 'bbspoiler_button_button_key', {
			title : bbbutton.spoiler,
            text: false,
			image : url + '/spoiler.png',
            
            onclick: function() {
                // Open window
                editor.windowManager.open( {
                    title: 'BBSpoiler',
                    body: [
					{type: 'textbox', name: 'title', label: bbbutton.title},
					{type: 'textbox', name: 'text', label: bbbutton.text, 'multiline': 'true', 'minWidth': 380, 'minHeight': 140},
                    {type: 'listbox', name: 'style', label: bbbutton.style, 
                        'values': [
                            {text: bbbutton.default2, value: 'default'},
                            {text: bbbutton.green, value: 'green'},
                            {text: bbbutton.blue, value: 'blue'},
                            {text: bbbutton.yellow, value: 'yellow'},
                            {text: bbbutton.orange, value: 'orange'},
                            {text: bbbutton.brown, value: 'brown'},
                            {text: bbbutton.purple, value: 'purple'},
                            {text: bbbutton.cyan, value: 'cyan'},
                            {text: bbbutton.lime, value: 'lime'},
                            {text: bbbutton.steelblue, value: 'steelblue'}
                        ]
                    },
					{name: 'collapse_link', type: 'checkbox', checked: 'true', label: bbbutton.showlink}
					],
                    onsubmit: function( e ) {
                        // Insert content when the window form is submitted
                        editor.insertContent('[spoiler title=\'' + e.data.title + '\'' + ' style=\'' + e.data.style + '\'' + ' collapse_link=\'' + e.data.collapse_link + '\']' + e.data.text + '[/spoiler]');
                    }
                } );
            }
        } );
    } );
} )();


jQuery(function(){
	jQuery(".sp-body.folded").hide();

	jQuery(".sp-head").on('click', function(event){
		$this = jQuery(this);
		if($this.hasClass("unfolded")) {
			$this.removeClass("unfolded");
			$this.next().slideUp("fast");
			$this.next().addClass("folded");
			$this.prop('title', title.unfolded);
		} else {
			$this.addClass("unfolded");
			$this.next().slideDown("fast");
			$this.next().removeClass("folded");
			$this.prop('title', title.folded);
		}
	});
	
	
	jQuery(".spdiv").on('click', function(event){
	$this = jQuery(this).closest("div.sp-body").prev();
		if($this.hasClass("unfolded")) {
			$this.removeClass("unfolded");
			$this.next().slideUp("fast");
			$this.next().addClass("folded");
			$this.prop('title', title.unfolded);
		} 
	});
});
  • Вопрос задан
  • 480 просмотров
Решения вопроса 1
erge
@erge
Примус починяю
попробуйте
либо в начале JavaScript'a удалить
jQuery(".sp-body.folded").hide();

и/или

в конце JavaScript'a перед последней });
вставить:

jQuery(".sp-head").each(function(){
  $this = jQuery(this);
  $this.addClass("unfolded");
  $this.next().slideDown("fast");
  $this.next().removeClass("folded");
  $this.prop('title', title.folded);
});


UPD:
а, если действие-переключение надо производить по кнопке, то
сделать кнопку, на onclick повесить функцию toggleSpoilers()

добавить в JS в самом конце
var isUnfolded = false;

function toggleSpoilers() {
  jQuery(".sp-head").each(function(){
    $this = jQuery(this);
    if(isUnfolded) {
      $this.removeClass("unfolded");
      $this.next().slideUp("fast");
      $this.next().addClass("folded");
      $this.prop('title', title.unfolded);
    } else {
      $this.addClass("unfolded");
      $this.next().slideDown("fast");
      $this.next().removeClass("folded");
      $this.prop('title', title.folded);
    }
  });
  isUnfolded = !isUnfolded;
}


как-то так...
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы