Framework  3.9
expanding_list.inc
Go to the documentation of this file.
1 <?php
5 /**************************************************************
6 
7  Copyright (c) 2007-2010 Sonjara, Inc
8 
9  Permission is hereby granted, free of charge, to any person
10  obtaining a copy of this software and associated documentation
11  files (the "Software"), to deal in the Software without
12  restriction, including without limitation the rights to use,
13  copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following
16  conditions:
17 
18  The above copyright notice and this permission notice shall be
19  included in all copies or substantial portions of the Software.
20 
21  Except as contained in this notice, the name(s) of the above
22  copyright holders shall not be used in advertising or otherwise
23  to promote the sale, use or other dealings in this Software
24  without prior written authorization.
25 
26  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  OTHER DEALINGS IN THE SOFTWARE.
34 
35 *****************************************************************/
36 
44 {
45  var $items;
46  var $id;
49  var $CSSclass = "expanding_list";
50  var $styles = null;
51  var $emptyMessage = "No items in the list";
52  var $groupTag = "h4";
53  var $grouped = false;
54  var $dynamicLoad = false;
55  var $titleIDFormat = null;
56  var $bodyIDFormat = null;
57  var $scrollOnExpand = false;
58  var $scrollOffset = 0;
59  var $isItemExpanded = null;
60  var $onItemStart = null;
61  var $mode = "accordion";
62 
71  function ExpandingList($items, $id = "expanding_list", $titleFormat = null, $bodyFormat = null, $dynamicLoad = false)
72  {
73  $this->items = $items;
74  $this->id = $id;
75  $this->titleFormat = $titleFormat;
76  $this->bodyFormat = $bodyFormat;
77  $this->dynamicLoad = $dynamicLoad;
78  }
79 
84  function writeScript()
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  }
160 
164  function drawList()
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  }
243 
247  function drawGroupedList()
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  }
321 }?>
The ExpandingList class provides a simple User Interface control to allow the display of list items b...
$styles
Specific CSS styles to be applied to the list.
drawGroupedList()
Draws a set of grouped expanding lists.
$emptyMessage
Message to display when the list is empty.
$grouped
Flag indicating whether list should be grouped.
$CSSclass
CSS class(es) to be applied to the list.
$bodyFormat
The formatter used to display the list item bodies (either a format string or a callback function or ...
$isItemExpanded
Callback hook to determine if an item should be shown expanded by default. Callback is passed the ite...
$onItemStart
Callback hook called when starting to display each item. Return false to skip the item.
$titleIDFormat
Format template for title ID field.
ExpandingList($items, $id="expanding_list", $titleFormat=null, $bodyFormat=null, $dynamicLoad=false)
Creates a new ExpandingList control.
$titleFormat
The formatter used to display the list item titles (either a format string or a callback function or ...
drawList()
Generates the HTML for the expanding list.
$scrollOnExpand
Flag indicating whether to scroll the expanding item to the top of the screen.
$bodyIDFormat
Format template for body ID field.
$id
The HTML id of the list.
$items
The items to be displayed in the list.
$groupTag
Grouping tag type.
$scrollOffset
Offset from top of element when scrolling (to take into account fixed navigation bars,...
$dynamicLoad
Flag indicating whether list should load content on demand. If set to true, bodyFormat should provide...
writeScript()
Writes the supporting Javascript for an expanding list control.
$mode
The expansion mode for the list - set to "accordion" to have only one item open at a time.