Framework  3.9
ExpandingList Class Reference

The ExpandingList class provides a simple User Interface control to allow the display of list items by heading. More...

Public Member Functions

 ExpandingList ($items, $id="expanding_list", $titleFormat=null, $bodyFormat=null, $dynamicLoad=false)
 Creates a new ExpandingList control. More...
 
 writeScript ()
 Writes the supporting Javascript for an expanding list control. More...
 
 drawList ()
 Generates the HTML for the expanding list. More...
 
 drawGroupedList ()
 Draws a set of grouped expanding lists. More...
 

Public Attributes

 $items
 The items to be displayed in the list. More...
 
 $id
 The HTML id of the list. More...
 
 $titleFormat
 The formatter used to display the list item titles (either a format string or a callback function or method) More...
 
 $bodyFormat
 The formatter used to display the list item bodies (either a format string or a callback function or method) More...
 
 $CSSclass = "expanding_list"
 CSS class(es) to be applied to the list. More...
 
 $styles = null
 Specific CSS styles to be applied to the list. More...
 
 $emptyMessage = "No items in the list"
 Message to display when the list is empty. More...
 
 $groupTag = "h4"
 Grouping tag type. More...
 
 $grouped = false
 Flag indicating whether list should be grouped. More...
 
 $dynamicLoad = false
 Flag indicating whether list should load content on demand. If set to true, bodyFormat should provide the URL for each item. More...
 
 $titleIDFormat = null
 Format template for title ID field. More...
 
 $bodyIDFormat = null
 Format template for body ID field. More...
 
 $scrollOnExpand = false
 Flag indicating whether to scroll the expanding item to the top of the screen. More...
 
 $scrollOffset = 0
 Offset from top of element when scrolling (to take into account fixed navigation bars, etc) More...
 
 $isItemExpanded = null
 Callback hook to determine if an item should be shown expanded by default. Callback is passed the item and current index. Return true for expanded, false for collapsed. More...
 
 $onItemStart = null
 Callback hook called when starting to display each item. Return false to skip the item. More...
 
 $mode = "accordion"
 The expansion mode for the list - set to "accordion" to have only one item open at a time. More...
 

Detailed Description

The ExpandingList class provides a simple User Interface control to allow the display of list items by heading.

Clicking on a heading expands the body for that list item..

Author
andy

Definition at line 43 of file expanding_list.inc.

Member Function Documentation

◆ drawGroupedList()

ExpandingList::drawGroupedList ( )

Draws a set of grouped expanding lists.

Definition at line 247 of file expanding_list.inc.

248  {
249  $attrs = "";
250  if ($this->CSSclass) $attrs .= " class='{$this->CSSclass}'";
251  if ($this->styles) $attrs .= " style='{$this->styles}'";
252 
253  $groupIdx = 0;
254 
255  foreach($this->items as $group => $items)
256  {
257 ?>
258  <<?echo $this->groupTag?> class='expanding_list_group'><?echo $group?></<?echo $this->groupTag?>>
259  <dl id="<?echo $this->id?>_<?echo codify($group)?>"<?echo $attrs?>>
260 <?
261  $idx = 0;
262 
263  foreach($items as $item)
264  {
265  if (is_callable($this->onItemStart))
266  {
267  if (call_user_func($this->onItemStart, $item) === false) continue;
268  }
269 
270  $titleID = ($this->titleIDFormat) ? $item->format(" id='{$this->titleIDFormat}'") : "";
271  $bodyID = ($this->bodyIDFormat) ? $item->format(" id='{$this->bodyIDFormat}'") : "";
272 
273  $expanded = false;
274  if (is_callable($this->isItemExpanded))
275  {
276  $expanded = call_user_func($this->isItemExpanded, $item, $idx, $groupIdx);
277  }
278 
279  if ($expanded)
280  {
281  $dtState = "class='expanded'";
282  $ddState = "style=''";
283  }
284  else
285  {
286  $dtState = "";
287  $ddState = "style='display: none'";
288  }
289 
290  echo "<dt{$titleID} {$dtState} onclick='return {$this->id}_toggleDD(this);'>";
291 
292  if (is_callable($this->titleFormat))
293  {
294  echo call_user_func($this->titleFormat, $item);
295  }
296  else echo $item->format($this->titleFormat);
297 
298  echo "</dt>\n";
299 
300  if (is_callable($this->bodyFormat))
301  {
302  $body = call_user_func($this->bodyFormat, $item);
303  }
304  else $body = $item->format($this->bodyFormat);
305 
306  if ($this->dynamicLoad)
307  {
308  echo "<dd{$bodyID} {$dtState} {$ddState} data-url='{$body}'></dd>";
309  }
310  else
311  {
312  echo "<dd{$bodyID} {$ddState}>{$body}</dd>";
313  }
314 
315  ++$idx;
316  }
317  echo "</dl>\n";
318  ++$groupIdx;
319  }
320  }
$titleIDFormat
Format template for title ID field.
$bodyIDFormat
Format template for body ID field.
$items
The items to be displayed in the list.

◆ drawList()

ExpandingList::drawList ( )

Generates the HTML for the expanding list.

Definition at line 164 of file expanding_list.inc.

165  {
166  if (!count($this->items))
167  {
168  echo "<p>{$this->emptyMessage}</p>";
169  return;
170  }
171 
172  if ($this->grouped)
173  {
174  $this->drawGroupedList();
175  return;
176  }
177 
178  $attrs = "";
179  if ($this->CSSclass) $attrs .= " class='{$this->CSSclass}'";
180  if ($this->styles) $attrs .= " style='{$this->styles}'";
181 ?>
182  <dl id="<?echo $this->id?>"<?echo $attrs?>>
183 <?
184  $idx = 0;
185 
186  foreach($this->items as $item)
187  {
188  if (is_callable($this->onItemStart))
189  {
190  $ret = call_user_func($this->onItemStart, $item);
191  if ($ret === false) continue;
192  }
193 
194  $titleID = ($this->titleIDFormat) ? $item->format(" id='{$this->titleIDFormat}'") : "";
195  $bodyID = ($this->bodyIDFormat) ? $item->format(" id='{$this->bodyIDFormat}'") : "";
196 
197  $expanded = false;
198  if (is_callable($this->isItemExpanded))
199  {
200  $expanded = call_user_func($this->isItemExpanded, $item, $idx);
201  }
202 
203  if ($expanded)
204  {
205  $dtState = "class='expanded {$ret}'";
206  $ddState = "style='' class='{$ret}'";
207  }
208  else
209  {
210  $dtState = "class='{$ret}'";
211  $ddState = "style='display: none' class='{$ret}'";
212  }
213 
214  echo "<dt{$titleID} {$dtState} onclick='return {$this->id}_toggleDD(this);'>";
215 
216  if (is_callable($this->titleFormat))
217  {
218  echo call_user_func($this->titleFormat, $item);
219  }
220  else echo $item->format($this->titleFormat);
221 
222  echo "</dt>\n";
223 
224  if (is_callable($this->bodyFormat))
225  {
226  $body = call_user_func($this->bodyFormat, $item);
227  }
228  else $body = $item->format($this->bodyFormat);
229 
230  if ($this->dynamicLoad)
231  {
232  echo "<dd{$bodyID} {$dtState} {$ddState} data-url='{$body}'></dd>";
233  }
234  else
235  {
236  echo "<dd{$bodyID} {$ddState}>{$body}</dd>";
237  }
238 
239  ++$idx;
240  }
241  echo "</dl>\n";
242  }
drawGroupedList()
Draws a set of grouped expanding lists.

◆ ExpandingList()

ExpandingList::ExpandingList (   $items,
  $id = "expanding_list",
  $titleFormat = null,
  $bodyFormat = null,
  $dynamicLoad = false 
)

Creates a new ExpandingList control.

Parameters
array$itemsthe array of DataItems to be displayed in the list
string$idthe HTML id of the list
mixed$titleFormatThe formatter used to display the list item titles (either a format string or a callback function or method)
mixes$bodyFormatThe formatter used to display the list item bodies (either a format string or a callback function or method)
boolean$dynamicLoadSpecifies whether to load the contents of each list element on demand. If set to true, the $bodyFormat parameter should provide the URL for each item

Definition at line 71 of file expanding_list.inc.

72  {
73  $this->items = $items;
74  $this->id = $id;
75  $this->titleFormat = $titleFormat;
76  $this->bodyFormat = $bodyFormat;
77  $this->dynamicLoad = $dynamicLoad;
78  }
$bodyFormat
The formatter used to display the list item bodies (either a format string or a callback function or ...
$titleFormat
The formatter used to display the list item titles (either a format string or a callback function or ...
$id
The HTML id of the list.
$dynamicLoad
Flag indicating whether list should load content on demand. If set to true, bodyFormat should provide...

◆ writeScript()

ExpandingList::writeScript ( )

Writes the supporting Javascript for an expanding list control.

Returns
string

Definition at line 84 of file expanding_list.inc.

85  {
86  ob_start();
87 ?>
88  <script type="text/javascript">
89  function <?echo $this->id?>_toggleDD(elt)
90  {
91  elt = document.id(elt);
92  var body = elt.getNext("dd");
93 
94  if (elt.hasClass("expanded"))
95  {
96  body.dissolve();
97  elt.removeClass("expanded");
98  }
99  else
100  {
101 <?php
102  if ($this->mode == "accordion")
103  {
104 ?>
105  elt.getParent().getChildren("dt.expanded").each(function(e) { var b = e.getNext("dd"); b.dissolve(); e.removeClass("expanded"); });
106 <?php
107  }
108 ?>
109  if (body.get('data-url') && body.get('html') == '')
110  {
111  var cursor = elt.getStyle('cursor');
112 
113  elt.setStyle('cursor', 'progress');
114 
115  body.reload(function()
116  {
117  body.reveal();
118  elt.addClass("expanded");
119  elt.setStyle('cursor', cursor);
120 <?php
121  if ($this->scrollOnExpand)
122  {
123 ?>
124  window.scrollToElement(elt, <?php echo $this->scrollOffset?>);
125 <?php
126  }
127 ?>
128  });
129  }
130  else
131  {
132  body.reveal();
133  elt.addClass("expanded");
134  <?php
135  if ($this->scrollOnExpand)
136  {
137  ?>
138  window.scrollToElement(elt, <?php echo $this->scrollOffset?>);
139  <?php
140  }
141  ?>
142  }
143  }
144  }
145 
146  window.addEvent('domready', function()
147  {
148  document.getElements("dl.expanding_list dd.expanded[data-url!='']").each(function(panel)
149  {
150  panel.reload();
151  panel.removeClass('expanded');
152  });
153  });
154  </script>
155 <?
156  $script = ob_get_contents();
157  ob_end_clean();
158  return $script;
159  }

Member Data Documentation

◆ $bodyFormat

ExpandingList::$bodyFormat

The formatter used to display the list item bodies (either a format string or a callback function or method)

Definition at line 48 of file expanding_list.inc.

◆ $bodyIDFormat

ExpandingList::$bodyIDFormat = null

Format template for body ID field.

Definition at line 56 of file expanding_list.inc.

◆ $CSSclass

ExpandingList::$CSSclass = "expanding_list"

CSS class(es) to be applied to the list.

Definition at line 49 of file expanding_list.inc.

◆ $dynamicLoad

ExpandingList::$dynamicLoad = false

Flag indicating whether list should load content on demand. If set to true, bodyFormat should provide the URL for each item.

Definition at line 54 of file expanding_list.inc.

◆ $emptyMessage

ExpandingList::$emptyMessage = "No items in the list"

Message to display when the list is empty.

Definition at line 51 of file expanding_list.inc.

◆ $grouped

ExpandingList::$grouped = false

Flag indicating whether list should be grouped.

Definition at line 53 of file expanding_list.inc.

◆ $groupTag

ExpandingList::$groupTag = "h4"

Grouping tag type.

Definition at line 52 of file expanding_list.inc.

◆ $id

ExpandingList::$id

The HTML id of the list.

Definition at line 46 of file expanding_list.inc.

◆ $isItemExpanded

ExpandingList::$isItemExpanded = null

Callback hook to determine if an item should be shown expanded by default. Callback is passed the item and current index. Return true for expanded, false for collapsed.

Definition at line 59 of file expanding_list.inc.

◆ $items

ExpandingList::$items

The items to be displayed in the list.

Definition at line 45 of file expanding_list.inc.

◆ $mode

ExpandingList::$mode = "accordion"

The expansion mode for the list - set to "accordion" to have only one item open at a time.

Definition at line 61 of file expanding_list.inc.

◆ $onItemStart

ExpandingList::$onItemStart = null

Callback hook called when starting to display each item. Return false to skip the item.

Definition at line 60 of file expanding_list.inc.

◆ $scrollOffset

ExpandingList::$scrollOffset = 0

Offset from top of element when scrolling (to take into account fixed navigation bars, etc)

Definition at line 58 of file expanding_list.inc.

◆ $scrollOnExpand

ExpandingList::$scrollOnExpand = false

Flag indicating whether to scroll the expanding item to the top of the screen.

Definition at line 57 of file expanding_list.inc.

◆ $styles

ExpandingList::$styles = null

Specific CSS styles to be applied to the list.

Definition at line 50 of file expanding_list.inc.

◆ $titleFormat

ExpandingList::$titleFormat

The formatter used to display the list item titles (either a format string or a callback function or method)

Definition at line 47 of file expanding_list.inc.

◆ $titleIDFormat

ExpandingList::$titleIDFormat = null

Format template for title ID field.

Definition at line 55 of file expanding_list.inc.


The documentation for this class was generated from the following file: