]> err.no Git - sope/blob - sope-appserver/WEExtensions/JSMenu.m
milli => micro
[sope] / sope-appserver / WEExtensions / JSMenu.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include "JSMenu.h"
24 #include "JSMenuItem.h"
25 #include <NGObjWeb/NGObjWeb.h>
26 #include <NGObjWeb/WEClientCapabilities.h>
27 #include "common.h"
28
29 @implementation JSMenu
30
31 - (id)initWithName:(NSString *)_name
32   associations:(NSDictionary *)_config
33   template:(WOElement *)_subs
34 {
35   if((self = [super initWithName:_name associations:_config template:_subs])) {
36     self->fgColor     = OWGetProperty(_config,@"fgColor");
37     self->bgColor     = OWGetProperty(_config,@"bgColor");
38     self->fgColorHigh = OWGetProperty(_config,@"fgColorHigh");
39     self->bgColorHigh = OWGetProperty(_config,@"bgColorHigh");
40     self->borderColor = OWGetProperty(_config,@"borderColor");
41     self->borderWidth = OWGetProperty(_config,@"borderWidth");
42     self->fontSize    = OWGetProperty(_config,@"fontSize");
43     self->width       = OWGetProperty(_config,@"width");
44     self->leftPadding = OWGetProperty(_config,@"leftPadding");
45     self->string      = OWGetProperty(_config,@"string");
46     self->bindAtId    = OWGetProperty(_config,@"bindAtId");
47     self->align       = OWGetProperty(_config,@"align");
48     self->tag         = OWGetProperty(_config,@"tag");
49
50     self->template = [_subs retain];
51   }
52   return self;
53 }
54
55 - (void)dealloc {
56   [self->fgColor     release];
57   [self->bgColor     release];
58   [self->fgColorHigh release];
59   [self->bgColorHigh release];
60   [self->borderWidth release];
61   [self->fontSize    release];
62   [self->width       release];
63   [self->leftPadding release];
64   [self->string      release];
65   [self->bindAtId    release];
66   [self->align       release];
67   [self->tag         release];
68   [self->template    release];
69   [super dealloc];
70 }
71
72 /* handling requests */
73
74 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
75   return [self->template invokeActionForRequest:_req inContext:_ctx];
76 }
77
78 /* generate response */
79
80 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
81   WOComponent          *comp;
82   NSString             *tmp;
83   NSString             *eid; // [_ctx elementID]
84   WEClientCapabilities *ccaps;
85   BOOL                 ie, ns;
86
87   eid   = [[[_ctx elementID] componentsSeparatedByString:@"."]
88                   componentsJoinedByString:@"_"];
89   comp  = [_ctx component];
90   ccaps = [[_ctx request] clientCapabilities];
91   ie    = [ccaps isJavaScriptBrowser] && [ccaps isInternetExplorer];
92   ns    = [ccaps isJavaScriptBrowser] && [ccaps isNetscape];
93
94   [_ctx setObject:eid forKey:@"eid"];
95
96   if (!ie) {
97     return;
98
99     [_response appendContentString:@"<font size=\"-1\">["];
100     [self->template appendToResponse:_response inContext:_ctx];
101     [_response appendContentString:@"]</font>"];
102     return;
103   }
104
105   if (ie) {
106     if ([_ctx objectForKey:@"jsmenu_included"] == nil) {
107       [_ctx setObject:@"done" forKey:@"jsmenu_included"];
108       tmp = [[NSString alloc] initWithFormat:
109                       @"<style>\n"
110                       @".menuItem {\n"
111                       @"  font:%@pt sans-serif;\n"
112                       @"  width:%@;\n"
113                       @"  padding-left:%@;\n"
114                       @"  background-Color:%@;\n"
115                       @"  color:%@;\n"
116                       @"  text-align:%@\n"
117                       @"}\n"
118                       @".highlightItem {\n"
119                       @"  font:%@pt sans-serif;\n"
120                       @"  width:%@;\n"
121                       @"  padding-left:%@;\n"
122                       @"  background-Color:%@;\n"
123                       @"  color:%@\n"
124                       @"  text-align:%@\n"
125                       @"}\n"
126                       @"</style>\n",
127                       [self->fontSize stringValueInComponent:comp],
128                       [self->width stringValueInComponent:comp],
129                       [self->leftPadding stringValueInComponent:comp],
130                       [self->bgColor stringValueInComponent:comp],
131                       [self->fgColor stringValueInComponent:comp],
132                       [self->align stringValueInComponent:comp],
133                       [self->fontSize stringValueInComponent:comp],
134                       [self->width stringValueInComponent:comp],
135                       [self->leftPadding stringValueInComponent:comp],
136                       [self->bgColorHigh stringValueInComponent:comp],
137                       [self->fgColorHigh stringValueInComponent:comp],
138                       [self->align stringValueInComponent:comp]];
139       [_response appendContentString:tmp];
140       [tmp release];
141
142       tmp = [[NSString alloc] initWithFormat:
143                       @"<script language=\"javascript\">\n"
144                       @"var menuOpened;\n"
145                       @"function displayMenu(m) {\n"
146                       @"  closeMenu();\n"
147                       @"  if(m.parentNode != event.srcElement)\n"
148                       @"    return false;\n"
149                       @"  menuOpened=m;\n"
150                       @"  m.style.posLeft=event.clientX+document.body.scrollLeft;\n"
151                       @"  m.style.posTop=event.clientY+document.body.scrollTop;\n"
152                       @"  if(document.body.clientWidth<event.clientX+%@+5)\n"
153                       @"    m.style.posLeft-=%@;\n"
154                       @"  m.style.display=\"\";\n"
155                       @"  m.setCapture();\n"
156                       @"  return false;\n"
157                       @"}\n"
158                       @"function switchMenu() {\n"
159                       @"  el=event.srcElement;\n"
160                       @"  if(el.className==\"menuItem\")\n"
161                       @"    el.className=\"highlightItem\";\n"
162                       @"  else if(el.className==\"highlightItem\")\n"
163                       @"    el.className=\"menuItem\";\n"
164                       @"}\n"
165                       @"function clickMenu(m) {\n"
166                       @"  m.releaseCapture();\n"
167                       @"  m.style.display=\"none\";\n"
168                       @"  el=event.srcElement;\n"
169                       @"  if(m==el.parentNode)window.location=el.url;\n"
170                       @"  return false;\n"
171                       @"}\n"
172                       @"function closeMenu() {\n"
173                       @"  if(menuOpened==null)"
174                       @"    return;\n"
175                       @"  menuOpened.releaseCapture();\n"
176                       @"  menuOpened.style.display=\"none\";\n"
177                       @"  menuOpened=null;\n"
178                       @"}\n"
179                       @"</script>\n",
180                       [self->width stringValueInComponent:comp],
181                       [self->width stringValueInComponent:comp]];
182       [_response appendContentString:tmp];
183       [tmp release];
184     }
185     tmp = [[NSString alloc] initWithFormat:
186                     @"<div id=\"m%@\" onclick=\"return clickMenu(m%@);\" "
187                     @"onmouseover=\"switchMenu();\" "
188                     @"onmouseout=\"switchMenu();\" "
189                     @"style=\"position:absolute;display:none;width:%@;"
190                     @"background-Color:%@;border:outset %@px %@;"
191                     @"text-decoration:none\">",
192                     eid, eid,
193                     [self->width       stringValueInComponent:comp],
194                     [self->bgColor     stringValueInComponent:comp],
195                     [self->borderWidth stringValueInComponent:comp],
196                     [self->borderColor stringValueInComponent:comp]];
197     [_response appendContentString:tmp];
198     [tmp release];
199
200     [self->template appendToResponse:_response inContext:_ctx];
201
202     [_response appendContentString:@"</div>"];
203 #if 0
204     if ([self->tag stringValueInComponent:comp] != nil)
205       tmp = [[NSString alloc] initWithFormat:
206                       @"<script id=\"s%@\" language=\"javascript\">"
207                       @"function c%@(){return displayMenu(m%@);}"
208                       @"tmp=document.getElementById(\"s%@\");"
209                       @"i=5;"
210                       @"while((tmp.tagName!=\"%@\")&&i--)"
211                       @"tmp=tmp.parentNode;"
212                       @"tmp.oncontextmenu=c%@;"
213                       @"</script>",
214                       eid, eid, eid, eid,
215                       [self->tag stringValueInComponent:comp], eid];
216     else
217 #endif
218       tmp = [[NSString alloc] initWithFormat:
219                       @"<script id=\"s%@\" language=\"javascript\">"
220                       @"function c%@() { return displayMenu(m%@); }"
221                       @"s%@.parentNode.oncontextmenu=c%@;"
222                       @"</script>",
223                       eid, eid, eid, eid, eid];
224     [_response appendContentString:tmp];
225     [tmp release];
226   }
227 #if 0
228   if (ns) {
229     if ([_ctx objectForKey:@"jsmenu_included"] == nil) {
230       tmp = [[NSString alloc] initWithFormat:
231                       @"<script language=\"javascript1.2\" "
232                       @"src=\"http://inster:9000/sascha/menu07.js\">"
233                       @"</script>\n"
234                       @"<script language=\"javascript1.2\">"
235                       @"function onLoad(){m%@.writeMenus();}</script>\n",
236                       eid];
237       [_response appendContentString:tmp];
238       [tmp release];
239     }
240
241     tmp = [[NSString alloc] initWithFormat:
242                     @"<script language=\"javascript1.2\">\n"
243                     @"m%@=new Menu();",eid];
244     [_response appendContentString:tmp];
245     [tmp release];
246
247     [self->template appendToResponse:_response inContext:_ctx];
248
249     tmp = [[NSString alloc] initWithFormat:
250                     @"</script>\n"
251                     @"<a href=\"#\" onclick=\"showMenu(m%@);return false;\">"
252                     @"M</a>", eid];
253     [_response appendContentString:tmp];
254     [tmp release];
255
256     [_ctx setObject:@"done" forKey:@"jsmenu_included"];
257   }
258 #endif
259 }
260
261 @end /* JSMenu */