Framework  3.9
context_menu.inc
Go to the documentation of this file.
1 <?php
2 /**************************************************************
3 
4  Copyright (c) 2007-2010 Sonjara, Inc
5 
6  Permission is hereby granted, free of charge, to any person
7  obtaining a copy of this software and associated documentation
8  files (the "Software"), to deal in the Software without
9  restriction, including without limitation the rights to use,
10  copy, modify, merge, publish, distribute, sublicense, and/or sell
11  copies of the Software, and to permit persons to whom the
12  Software is furnished to do so, subject to the following
13  conditions:
14 
15  The above copyright notice and this permission notice shall be
16  included in all copies or substantial portions of the Software.
17 
18  Except as contained in this notice, the name(s) of the above
19  copyright holders shall not be used in advertising or otherwise
20  to promote the sale, use or other dealings in this Software
21  without prior written authorization.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30  OTHER DEALINGS IN THE SOFTWARE.
31 
32 *****************************************************************/
33 
35 {
37  var $icon;
38  var $text;
39  var $action;
40  var $isURL;
41 
43  {
44  $this->command_id = $command_id;
45  $this->icon = $icon;
46  $this->text = $text;
47  $this->action = $action;
48  $this->isURL = $isURL;
49  }
50 
51  function draw()
52  {
53  if ($this->icon)
54  {
55  $output = "<img src='{$this->icon}' alt='' style='float: left; margin: 2px 0px 0px 1px'/>";
56  }
57 
58  if ($this->isURL)
59  {
60 
61  $output .= "<a href='{$this->action}'>";
62  }
63  else
64  {
65  $output .= "<a href=\"#\" onclick=\"{$this->action}; return false;\">";
66  }
67 
68  $output .= $this->text;
69  $output .= "</a>";
70  echo $output;
71  }
72 }
73 
75 {
77  {
78  }
79 
80  function draw()
81  {
82  echo "<hr/>";
83  }
84 }
85 
87 {
88  var $id;
89  var $cssClass;
90  var $items = array();
91  var $targets;
92  var $trigger = "contextmenu";
93  var $position = "pointer";
94 
95  function ContextMenu($id, $targets, $cssClass = "context_menu", $trigger = "contextmenu", $position = "pointer")
96  {
97  $this->id = $id;
98  $this->targets = $targets;
99  $this->cssClass = "context_menu";
100  $this->trigger = $trigger;
101  $this->position = $position;
102  }
103 
104  function command($id, $text, $action, $isURL = true, $icon = "")
105  {
106  $this->items[] = new ContextMenuItem($id, $text, $action, $icon, $isURL);
107  return $this;
108  }
109 
110  function separator()
111  {
112  $this->items[] = new ContextMenuSeparator();
113  return $this;
114  }
115 
116  function writeScript()
117  {
118  $s = <<<ENDSCRIPT
119 <script type="text/javascript">
120 window.addEvent('domready', function() {
121 new ContextMenu('$this->id', '$this->targets', '$this->trigger', {position: '{$this->position}'});
122 });
123 </script>
124 ENDSCRIPT;
125 
126  return $s;
127  }
128 
129  function writeMenu()
130  {
131  ob_start();
132 ?>
133  <ul id="<?echo $this->id?>" class="<?echo $this->cssClass?>">
134 <?
135  foreach($this->items as $item)
136  {
137  echo "<li class='{$item->command_id}'>";
138  $item->draw();
139  echo "</li>";
140  }
141 ?>
142  </ul>
143 <?
144  $menu = ob_get_contents();
145  ob_end_clean();
146  return $menu;
147  }
148 }
149 
151 {
152  var $menus;
153 
155  {
156  $this->menus = array();
157  }
158 
159  function addMenu($id, $targets, $cssClass = "context_menu", $trigger = "contextmenu", $position = "pointer")
160  {
161  $menu = new ContextMenu($id, $targets, $cssClass, $trigger, $position);
162  $this->menus[] = $menu;
163  return $menu;
164  }
165 
166  function writeScript()
167  {
168  $script = "";
169 
170  foreach($this->menus as $menu)
171  {
172  $script .= $menu->writeScript();
173  }
174 
175  return $script;
176  }
177 
178  function writeMenus()
179  {
180  $menus = "";
181 
182  foreach($this->menus as $menu)
183  {
184  $menus .= $menu->writeMenu();
185  }
186 
187  return $menus;
188  }
189 }
command($id, $text, $action, $isURL=true, $icon="")
ContextMenu($id, $targets, $cssClass="context_menu", $trigger="contextmenu", $position="pointer")
ContextMenuItem($command_id, $text, $action, $icon="", $isURL=true)
addMenu($id, $targets, $cssClass="context_menu", $trigger="contextmenu", $position="pointer")