-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.php
More file actions
115 lines (90 loc) · 3.92 KB
/
Copy pathfilter.php
File metadata and controls
115 lines (90 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/*
* Copyright (C) 2016 Ryan Nutt <ryan@classcube.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
defined( 'MOODLE_INTERNAL' ) || die();
class filter_bootstraptabs extends \moodle_text_filter {
/**
* Counter to keep track of each individual tab set so that this
* will work with multiples.
*
* @var int
*/
private $current_tabset = 0;
public function filter( $text, array $options = array() ) {
$text = preg_replace_callback( '/\[(tabs|pills)\]([\s\S]*?)\[\/\1\]/', function($matches) {
return $this->tab_callback( $matches );
}, $text );
return $text;
}
private function tab_callback( $matches ) {
$class_name = "nav-tabs";
if ( $matches[ 1 ] == 'tabs' ) {
$class_name = 'nav-tabs';
}
else if ( $matches[ 1 ] == 'pills' ) {
$class_name = 'nav-pills';
}
else {
/* Neither, so just return it back */
return $matches[ 0 ];
}
/* Get the tab titles */
$tab_info = preg_split( '/\[[tab|pill].*?title=["\']?(.*?)["\']?\s*?\]/', $matches[ 2 ], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
/* Need to clean up any html between the opening [tabs|pills] and the
* first tab.
*/
if ( empty( trim( strip_tags( $tab_info[ 0 ] ) ) ) ) {
unset( $tab_info[ 0 ] );
/* Reindex the array so it starts at 0 again */
$tab_info = array_values( $tab_info );
}
$html = '';
/* First step, build the tab bar */
$html .= '<ul class="nav ' . $class_name . '">';
$divs = '';
for ( $i = 0; $i < count( $tab_info ); $i += 2 ) {
/* The first tab needs to be flagged as active to start */
$html .= '<li role="presentation"' . ($i == 0 ? ' class="active"' : '') . '><a href="#" data-cc-tab="' . $i . '" data-cc-set="' . $this->current_tabset . '">' . $tab_info[ $i ] . '</a></li>';
$divs .= '<div ' . ($i == 0 ? '' : ' style="display:none;" ') . ' data-cc-set="' . $this->current_tabset . '" data-cc-tab="' . $i . '">';
$divs .= $tab_info[ $i + 1 ];
$divs .= '</div>';
}
$html .= '</ul>';
$html .= $divs;
if ( $this->current_tabset == 0 ) {
?>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
require(['jquery'], function ($) {
$('a[data-cc-tab]').click(function() {
var tab = $(this).data('cc-tab');
var set = $(this).data('cc-set');
$('a[data-cc-set="' + set + '"]').parents('li').removeClass('active');
$(this).parents('li').addClass('active');
$('div[data-cc-set="' + set + '"]').hide();
$('div[data-cc-set="' + set + '"][data-cc-tab="' + tab + '"]').show();
return false;
});
});
}, false);
</script>
<?php
}
$this->current_tabset++;
return $html;
}
}