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