2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
22 #import <NGObjWeb/WODynamicElement.h>
24 @interface JSImageFlyover : WODynamicElement
26 WOAssociation *action;
27 WOAssociation *javaScriptFunction;
28 WOAssociation *pageName;
29 WOAssociation *selectedImage;
30 WOAssociation *unselectedImage;
31 WOAssociation *framework;
32 WOAssociation *targetWindow;
35 WOAssociation *directActionName;
36 WOAssociation *actionClass;
38 WOAssociation *queryDictionary;
39 NSDictionary *queryParameters;
46 #import <NGObjWeb/NGObjWeb.h>
49 @implementation JSImageFlyover
52 return [super version] + 0 /* v2 */;
55 NSAssert2([super version] == 2,
56 @"invalid superclass (%@) version %i !",
57 NSStringFromClass([self superclass]), [super version]);
60 - (NSDictionary *)extractQueryParameters: (NSDictionary *)_set {
61 NSMutableDictionary *paras = nil;
62 NSMutableArray *paraKeys = nil;
66 // locate query parameters
67 keys = [_set keyEnumerator];
68 while ((key = [keys nextObject])) {
69 if ([key hasPrefix:@"?"]) {
72 if ([key isEqualToString:@"?wosid"])
75 value = [_set objectForKey:key];
78 paraKeys = [NSMutableArray arrayWithCapacity:8];
80 paras = [NSMutableDictionary dictionaryWithCapacity:8];
82 [paraKeys addObject:key];
83 [paras setObject:value forKey:[key substringFromIndex:1]];
87 // remove query parameters
90 for (cnt = 0, count = [paraKeys count]; cnt < count; cnt++) {
91 [(NSMutableDictionary *)_set removeObjectForKey:
92 [paraKeys objectAtIndex:cnt]];
100 - (id)initWithName:(NSString *)_name
101 associations:(NSDictionary *)_config
102 template:(WOElement *)_subs
104 if ((self = [super initWithName:_name associations:_config template:_subs])){
106 WOAssociation *sidInUrlAssoc;
108 self->action = WOExtGetProperty(_config, @"action");
109 self->javaScriptFunction = WOExtGetProperty(_config, @"javaScriptFunction");
110 self->pageName = WOExtGetProperty(_config, @"pageName");
111 self->selectedImage = WOExtGetProperty(_config, @"selectedImage");
112 self->unselectedImage = WOExtGetProperty(_config, @"unselectedImage");
113 self->framework = WOExtGetProperty(_config, @"framework");
114 self->targetWindow = WOExtGetProperty(_config, @"targetWindow");
116 self->directActionName = WOExtGetProperty(_config, @"directActionName");
117 self->actionClass = WOExtGetProperty(_config, @"actionClass");
118 self->queryDictionary = WOExtGetProperty(_config, @"queryDictionary");
119 self->queryParameters = [self extractQueryParameters:_config];
122 if (self->action) funcCount++;
123 if (self->pageName) funcCount++;
124 if (self->javaScriptFunction) funcCount++;
125 if (self->directActionName) funcCount++;
128 NSLog(@"WARNING: JSImageFlyover: choose only one of "
129 @"action | pageName | javaScriptFunction | directActionName");
131 NSLog(@"WARNING: JSImageFlyover: no function declared - choose one of"
132 @"action | pageName | javaScriptFunction | directActionName");
133 if (!self->selectedImage)
134 NSLog(@"WARNING: JSImageFlyover: no value for 'selectedImage'");
135 if (!self->unselectedImage)
136 NSLog(@"WARNING: JSImageFlyover: no value for 'unselectedImage'");
138 /* for directActionName */
139 sidInUrlAssoc = WOExtGetProperty(_config, @"?wosid");
140 self->sidInUrl = (sidInUrlAssoc)
141 ? [sidInUrlAssoc boolValueInComponent:nil]
145 self->template = [_subs retain];
151 [self->action release];
152 [self->javaScriptFunction release];
153 [self->pageName release];
154 [self->selectedImage release];
155 [self->unselectedImage release];
156 [self->framework release];
157 [self->targetWindow release];
158 [self->template release];
160 [self->directActionName release];
161 [self->actionClass release];
162 [self->queryDictionary release];
163 [self->queryParameters release];
168 /* handle requests */
170 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
171 [self->template takeValuesFromRequest:_rq inContext:_ctx];
174 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
178 if (self->pageName) {
179 name = [self->pageName stringValueInComponent: [_ctx component]];
180 result = [[_ctx application] pageWithName:name inContext:_ctx];
182 else if (self->action) {
183 result = [self->action valueInComponent:[_ctx component]];
186 result = [self->template invokeActionForRequest:_rq inContext:_ctx];
191 /* generate response */
193 - (NSString *)imageByFilename:(NSString *)_name
194 inContext:(WOContext *)_ctx
195 framework:(NSString *)_framework
197 WOResourceManager *rm;
201 rm = [[_ctx application] resourceManager];
202 languages = [_ctx resourceLookupLanguages];
203 tmp = [rm urlForResourceNamed:_name
204 inFramework:_framework
206 request:[_ctx request]];
211 - (void)appendDirectActionURLToResponse:(WOResponse *)_response
212 inContext:(WOContext *)_ctx
217 NSMutableDictionary *qd;
222 comp = [_ctx component];
223 daName = [self->directActionName stringValueInComponent:comp];
224 daClass = [self->actionClass stringValueInComponent:comp];
228 if (![daClass isEqualToString:@"DirectAction"])
229 daName = [NSString stringWithFormat:@"%@/%@", daClass, daName];
235 qd = [NSMutableDictionary dictionaryWithCapacity:16];
237 if (self->queryDictionary) {
238 if ((tdict = [self->queryDictionary valueInComponent:comp]))
239 [qd addEntriesFromDictionary:tdict];
242 if (self->queryParameters) {
246 keys = [self->queryParameters keyEnumerator];
247 while ((key = [keys nextObject])) {
249 assoc = [self->queryParameters objectForKey:key];
250 value = [assoc stringValueInComponent:comp];
251 [qd setObject:(value ? value : @"") forKey:key];
255 if ((self->sidInUrl) && ([_ctx hasSession])) {
257 [qd setObject:[sn sessionID] forKey:WORequestValueSessionID];
258 if (![sn isDistributionEnabled]) {
259 [qd setObject:[[WOApplication application] number]
260 forKey:WORequestValueInstance];
264 s = [_ctx directActionURLForActionNamed:daName queryDictionary:qd];
265 [_response appendContentString:s];
268 - (void)appendToResponse:(WOResponse *)_response
269 inContext:(WOContext *)_ctx
273 NSString *tunselected, *tselected, *tframework;
278 comp = [_ctx component];
279 tunselected = [self->unselectedImage stringValueInComponent:comp];
280 tselected = [self->selectedImage stringValueInComponent:comp];
281 tframework = (self->framework)
282 ? [self->framework stringValueInComponent:comp]
283 : [comp frameworkName];
285 tunselected = [self imageByFilename: tunselected inContext:_ctx
286 framework: tframework];
287 tselected = [self imageByFilename: tselected inContext:_ctx
288 framework: tframework];
290 elID = [_ctx elementID];
291 /* javascript didn't work with #.#.#.# -> replacing to #x#x#x# */
292 ta = [[NSArray alloc] initWithArray:[elID componentsSeparatedByString:@"."]];
293 elID = [ta componentsJoinedByString:@"x"];
297 [self->template appendToResponse:_response inContext:_ctx];
300 [_response appendContentString:@"<script type=\"text/javascript\">\n<!--\n"];
302 if (![_ctx valueForKey:@"JSImageFlyoverScriptDone"]) {
303 tmp = @"var JSImageFlyoverImages = new Array();\n"
304 @"function JSImageFlyover(imgName,imgKind) {\n"
305 @" document.images[imgName].src = "
306 @"JSImageFlyoverImages[imgName][imgKind].src;\n"
308 [_response appendContentString:tmp];
309 [_ctx takeValue:[NSNumber numberWithBool:YES]
310 forKey:@"JSImageFlyoverScriptDone"];
313 tmp = @"JSImageFlyoverImages['%@'] = new Array; \n"
314 @"JSImageFlyoverImages['%@'][0] = new Image; "
315 @"JSImageFlyoverImages['%@'][0].src = '%@'; \n"
316 @"JSImageFlyoverImages['%@'][1] = new Image; "
317 @"JSImageFlyoverImages['%@'][1].src = '%@'; \n";
319 s = [[NSString alloc] initWithFormat:tmp,
320 elID, elID, elID, tunselected,
321 elID, elID, tselected];
322 [_response appendContentString:s];
325 [_response appendContentString:@"\n//-->\n</script>"];
327 /* link containing onMouseOver, onMouseOut and HREF */
328 [_response appendContentString:@"<a onmouseover=\"JSImageFlyover('"];
329 [_response appendContentString:elID];
330 [_response appendContentString:@"',1)\""];
331 [_response appendContentString:@" onmouseout=\"JSImageFlyover('"];
332 [_response appendContentString:elID];
333 [_response appendContentString:@"',0)\""];
334 [_response appendContentString:@" href=\""];
336 if (self->javaScriptFunction) {
337 [_response appendContentString:@"javascript:"];
338 [_response appendContentHTMLAttributeValue:
339 [self->javaScriptFunction stringValueInComponent:comp]];
341 else if (self->directActionName)
342 [self appendDirectActionURLToResponse:_response inContext:_ctx];
343 else /* component action */
344 [_response appendContentString:[_ctx componentActionURL]];
346 [_response appendContentString:@"\" "];
348 if (self->targetWindow) {
349 [_response appendContentString:@" target=\""];
350 [_response appendContentHTMLAttributeValue:
351 [self->targetWindow stringValueInComponent: comp]];
352 [_response appendContentString:@"\" "];
354 [_response appendContentString:@" >"];
356 /* the image itself */
358 [_response appendContentString:@"<img border='0' src=\""];
359 [_response appendContentString:tunselected];
360 [_response appendContentString:@"\" name=\""];
361 [_response appendContentString:elID];
362 [_response appendContentString:@"\" "];
363 [self appendExtraAttributesToResponse:_response inContext:_ctx];
365 [_response appendContentString:
366 (_ctx->wcFlags.xmlStyleEmptyElements ? @" />" : @">")];
369 [_response appendContentString:@"</a>"];
372 @end /* JSImageFlyover */