-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathSwatAccordion.php
More file actions
164 lines (134 loc) · 4.1 KB
/
Copy pathSwatAccordion.php
File metadata and controls
164 lines (134 loc) · 4.1 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* Accordion widget containing {@link SwatNoteBookPage} pages.
*
* This widget is like a notebook but instead of tabs, pages are displayed
* stacked and open and close like disclosures. It sounds like a ye-olde
* squeezebox.
*
* @package Swat
* @copyright 2011-2016 silverorange
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @see SwatNoteBookPage
*/
class SwatAccordion extends SwatNoteBook
{
// {{{ public properties
/**
* Whether or not to animate the opening/closing of the accordion
*
* @var boolean
*/
public $animate = true;
/**
* Whether or not one page of the accordion is always open
*
* If false, the accordion can collapse to an all-closed state.
*
* @var boolean
*/
public $always_open = false;
// }}}
// {{{ public function __construct()
/**
* Creates a new accordion view
*
* @param string $id a non-visable unique id for this widget.
*/
public function __construct($id = null)
{
parent::__construct($id);
$this->requires_id = true;
}
// }}}
// {{{ public function display()
/**
* Displays this notebook
*/
public function display()
{
if (!$this->visible) {
return;
}
SwatWidget::display();
$li_counter = 0;
$ul_tag = new SwatHtmlTag('ul');
$ul_tag->id = $this->id;
$ul_tag->class = 'swat-accordion';
$ul_tag->open();
foreach ($this->pages as $page) {
if (!$page->visible) {
continue;
}
$li_counter++;
$li_tag = new SwatHtmlTag('li');
$li_tag->id = $page->id;
if ($page->id === $this->selected_page) {
$li_tag->class = 'swat-accordion-page selected';
} else {
$li_tag->class = 'swat-accordion-page';
}
$li_tag->class .= ' ' . implode(' ', $page->classes);
$li_tag->open();
// toggle link
$title = $page->title === null ? '' : $page->title;
$wrapper_span = new SwatHtmlTag('span');
$wrapper_span->tabindex = '0';
$wrapper_span->{'aria-role'} = 'tab';
$wrapper_span->class = 'swat-accordion-page-toggle';
$wrapper_span->open();
$anchor_tag = new SwatHtmlTag('a');
$anchor_tag->class = 'swat-accordion-page-link';
$anchor_tag->href = '#' . $page->id;
$em_tag = new SwatHtmlTag('em');
$em_tag->setContent($title, $page->title_content_type);
$anchor_tag->open();
$em_tag->display();
$anchor_tag->close();
$wrapper_span->close();
// content
echo '<div class="swat-accordion-page-animation">';
echo '<div class="swat-accordion-page-content">';
$page->display();
echo '</div>';
echo '</div>';
$li_tag->close();
}
$ul_tag->close();
Swat::displayInlineJavaScript($this->getInlineJavaScript());
}
// }}}
// {{{ protected function getInlineJavaScript()
/**
* Gets the inline JavaScript used by this accordion view
*
* @return string the inline JavaScript used by this accordion view.
*/
protected function getInlineJavaScript()
{
$javascript = sprintf(
"var %1\$s_obj = new %2\$s('%1\$s', %3\$s);",
$this->id,
$this->getJavascriptClassName(),
$this->animate ? 'true' : 'false'
);
$javascript .= sprintf(
"\n%s_obj.animate = %s;",
$this->id,
$this->animate ? 'true' : 'false'
);
$javascript .= sprintf(
"\n%s_obj.always_open = %s;",
$this->id,
$this->always_open ? 'true' : 'false'
);
return $javascript;
}
// }}}
// {{{ protected function getJavaScriptClassName()
protected function getJavaScriptClassName()
{
return 'SwatAccordion';
}
// }}}
}