Framework  3.9
draggable_list.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8 Copyright (c) 2017 Sonjara, Inc
9 
10 Permission is hereby granted, free of charge, to any person
11 obtaining a copy of this software and associated documentation
12 files (the "Software"), to deal in the Software without
13 restriction, including without limitation the rights to use,
14 copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the
16 Software is furnished to do so, subject to the following
17 conditions:
18 
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
21 
22 Except as contained in this notice, the name(s) of the above
23 copyright holders shall not be used in advertising or otherwise
24 to promote the sale, use or other dealings in this Software
25 without prior written authorization.
26 
27 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 OTHER DEALINGS IN THE SOFTWARE.
35 
36 *****************************************************************/
37 
47 {
48  var $items;
49  var $id;
50  var $format;
51  var $listTag = "ul";
52  var $CSSclass = null;
53  var $styles = null;
54  var $emptyList = "No items in the list";
55 
56  var $reorderHandler = null;
57  var $dragText = "";
58 
59  function __construct($items, $id = "draggable_list", $format = null, $handler = null)
60  {
61  $this->items = $items;
62  $this->id = $id;
63  $this->format = $format;
64  $this->reorderHandler = $handler;
65  }
66 
67  function enableDragReorder($handler)
68  {
69  $this->reorderHandler = $handler;
70  }
71 
72  function writeScript()
73  {
74  if (!count($this->items)) return;
75  $pk = $this->items[0]->getPrimaryKey();
76 
77  $script = <<<ENDSCRIPT
78 <script type='text/javascript'>
79 window.addEvent('domready', function()
80 {
81  new DraggableList('{$this->id}', '{$this->reorderHandler}', '{$pk}');
82 });
83 </script>
84 ENDSCRIPT;
85  return $script;
86  }
87 
88  function drawList()
89  {
90  if (!count($this->items))
91  {
92  echo "<p>{$this->emptyList}</p>";
93  return;
94  }
95 
96  $attrs = "";
97  if ($this->CSSclass)
98  {
99  $attrs .= " class='draggable {$this->CSSclass}'";
100  }
101  else
102  {
103  $attrs .= " class='draggable'";
104  }
105 
106  if ($this->styles) $attrs .= " style='{$this->styles}'";
107 ?>
108 
109  <? echo $this->dragText;?>
110  <<?echo $this->listTag?> id="<?echo $this->id?>"<?echo $attrs?>>
111 <?
112  foreach($this->items as $item)
113  {
114  $pkTag = " data-pk='".$item->getPrimaryKeyValue()."'";
115 
116  echo "<li{$pkTag}>";
117 
118  if (is_callable($this->format))
119  {
120  echo call_user_func($this->format, $item);
121  }
122  else echo $item->format($this->format);
123 
124  echo "</li>\n";
125  }
126  echo "</{$this->listTag}>\n";
127  }
128 }?>
DraggableList provides a simple user interface for sorting a list of items.
__construct($items, $id="draggable_list", $format=null, $handler=null)
enableDragReorder($handler)
$CSSclass
CSS class(es) to be applied to the list.
$listTag
The HTML list tag type.
$dragText
Optional text to display above reorderable table.
$emptyList
Message to display when the list is empty.
$styles
Specific CSS styles to be applied to the list.
$reorderHandler
Handler script that implements reordering records.