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#import <NGObjWeb/NGObjWeb.h>
23#import <Foundation/Foundation.h>
24#ifdef __APPLE__
25#  import <NGObjWeb/WEClientCapabilities.h>
26#else
27#  import <WEExtensions/WEClientCapabilities.h>
28#endif
29#import "JSMenuItem.h"
30
31@implementation JSMenuItem
32
33- (id)initWithName:(NSString *)_name
34  associations:(NSDictionary *)_config
35  template:(WOElement *)_subs
36{
37  if ((self = [super initWithName:_name associations:_config template:_subs]))
38  {
39    self->action = OWGetProperty(_config,@"action");
40    self->href   = OWGetProperty(_config,@"href");
41    self->string = OWGetProperty(_config,@"string");
42
43    self->template = [_subs retain];
44  }
45  return self;
46}
47
48- (void)dealloc {
49  [self->action   release];
50  [self->href     release];
51  [self->string   release];
52  [self->template release];
53  [super dealloc];
54}
55
56- (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
57  if (![[_ctx elementID] isEqualToString:[_ctx senderID]]) {
58    NSLog(@"ERROR: elementID %@ and senderID %@ do not match.",
59          [_ctx elementID], [_ctx senderID]);
60    return nil;
61  }
62  return [self->action valueInComponent:[_ctx component]];
63}
64
65- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
66  WOComponent          *comp;
67  NSString             *tmp;
68  NSString             *url;
69  WEClientCapabilities *ccaps;
70  BOOL                 ie, ns;
71  NSString             *eid;
72
73  if ([_ctx isRenderingDisabled]) {
74    [self->template appendToResponse:_response inContext:_ctx];
75    return;
76  }
77
78  comp  = [_ctx component];
79  ccaps = [[_ctx request] clientCapabilities];
80  ie    = [ccaps isJavaScriptBrowser] && [ccaps isInternetExplorer];
81  ns    = [ccaps isJavaScriptBrowser] && [ccaps isNetscape];
82  eid   = [_ctx objectForKey:@"eid"];
83
84  NSAssert(self->action != nil || self->href != nil,
85           @"ERROR: no action or href defined...");
86  if (self->action != nil)
87    url = [_ctx componentActionURL];
88  else if (self->href != nil)
89    url = [self->href stringValueInComponent:comp];
90  else
91    url = nil;
92
93  if (ie) {
94    tmp  = [[NSString alloc] initWithFormat:
95                     @"<div align=\"left\" class=\"menuItem\" url=\"%@\">"
96                     @"%@</div>",
97                     url, [self->string stringValueInComponent:comp]];
98  }
99#if 0
100  else if (ns)
101    tmp = [[NSString alloc] initWithFormat:
102                    @"m%@.addMenuItem(\"%@\",\"top.window.location='%@'\");",
103                    //@"m%@.addMenuItem(\"%@\",\"alert('%@')\");",
104                    eid, [self->string stringValueInComponent:comp], url];
105#endif
106  else {
107    return;
108#if 0
109    tmp = [[NSString alloc] initWithFormat:
110                    @"<a href=\"%@\">%@</a>", url,
111                    [self->string stringValueInComponent:[_ctx component]]];
112#endif
113  }
114
115  [_response appendContentString:tmp];
116  [tmp release];
117}
118
119@end /* JSMenuItem */
120