]> err.no Git - sope/blob - sope-appserver/WOExtensions/WOCollapsibleComponentContent.m
updated framework version
[sope] / sope-appserver / WOExtensions / WOCollapsibleComponentContent.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 <NGObjWeb/NGObjWeb.h>
23 #include "common.h"
24
25 @interface WOCollapsibleComponentContent : WODynamicElement
26 {
27 @protected
28   WOAssociation *condition;
29   WOAssociation *visibility;
30   WOAssociation *openedImageFileName;
31   WOAssociation *closedImageFileName;
32   WOAssociation *framework;
33   WOAssociation *openedLabel;
34   WOAssociation *closedLabel;
35   WOAssociation *submitActionName;
36
37   WOElement *template;
38 }
39 @end
40
41 @interface WOContext(WOExtensionsPrivate)
42 - (void)addActiveFormElement:(WOElement *)_element;
43 @end
44
45 @implementation WOCollapsibleComponentContent
46
47 - (id)initWithName:(NSString *)_name
48   associations:(NSDictionary *)_config
49   template:(WOElement *)_temp
50 {
51   if ((self = [super initWithName:_name associations:_config template:_temp])) {
52     self->condition           = WOExtGetProperty(_config, @"condition");
53     self->visibility          = WOExtGetProperty(_config, @"visibility");
54     self->openedImageFileName = 
55       WOExtGetProperty(_config, @"openedImageFileName");
56     self->closedImageFileName = 
57       WOExtGetProperty(_config, @"closedImageFileName");
58     self->framework           = WOExtGetProperty(_config, @"framework");
59     self->openedLabel         = WOExtGetProperty(_config, @"openedLabel");
60     self->closedLabel         = WOExtGetProperty(_config, @"closedLabel");
61     self->submitActionName    = WOExtGetProperty(_config, @"submitActionName");
62
63     if (WOExtGetProperty(_config, @"condition"))
64       NSLog(@"WARNING: WOCollapsibleComponent does not support 'condition'");
65
66     if (self->visibility == nil)
67       NSLog(@"WARNING: WOCollapsibleComponent 'visibility' not set");
68
69     if (self->visibility && ![self->visibility isValueSettable])
70       NSLog(@"WARNING: WOCollapsibleComponent 'visibility' is not settable");
71
72     ASSIGN(self->template, _temp);
73   }
74   return self;
75 }
76
77 - (void)dealloc {
78   RELEASE(self->condition);
79   RELEASE(self->visibility);
80   RELEASE(self->openedImageFileName);
81   RELEASE(self->closedImageFileName);
82   RELEASE(self->framework);
83   RELEASE(self->openedLabel);
84   RELEASE(self->closedLabel);
85   RELEASE(self->submitActionName);
86
87   RELEASE(self->template);
88   
89   [super dealloc];
90 }
91
92 // responder
93
94 - (void)takeValuesFromRequest:(WORequest *)_request
95   inContext:(WOContext *)_ctx
96 {
97   NSString *eid;
98
99   eid = [_ctx elementID];
100
101   if ([self->visibility boolValueInComponent:[_ctx component]]) {
102     [_ctx appendZeroElementIDComponent];
103     [self->template takeValuesFromRequest:_request inContext:_ctx];
104     [_ctx deleteLastElementIDComponent];
105   }
106   
107   if ([_request formValueForKey:[eid stringByAppendingString:@".c.x"]]) {
108     [_ctx addActiveFormElement:self];
109     [_ctx setRequestSenderID:[[_ctx senderID] stringByAppendingString:@".c"]];
110   }
111   else if ([_request formValueForKey:[eid stringByAppendingString:@".e.x"]]) {
112     [_ctx addActiveFormElement:self];
113     [_ctx setRequestSenderID:[[_ctx senderID] stringByAppendingString:@".e"]];
114   }
115 }
116
117 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
118   NSString *state;
119   NSString *eid;
120
121   state = [[_ctx currentElementID] stringValue];
122
123   eid = [_ctx elementID];
124
125   if (state) {
126     BOOL doForm;
127     
128     [_ctx consumeElementID]; // consume state-id (on or off)
129
130     doForm = ([_ctx isInForm] && self->submitActionName);
131     
132     if ([state isEqualToString:@"e"]) {
133       if ([self->visibility isValueSettable])
134         [self->visibility setBoolValue:NO inComponent:[_ctx component]];
135       if (doForm)
136         [self->submitActionName valueInComponent:[_ctx component]];
137     }
138     else if ([state isEqualToString:@"c"]) {
139       if ([self->visibility isValueSettable])
140         [self->visibility setBoolValue:YES inComponent:[_ctx component]];
141       if (doForm)
142         [self->submitActionName valueInComponent:[_ctx component]];
143     }
144     else {
145       id result;
146       
147       [_ctx appendElementIDComponent:state];
148       result = [self->template invokeActionForRequest:_request inContext:_ctx];
149       [_ctx deleteLastElementIDComponent];
150
151       return result;
152     }
153   }
154   return nil;
155 }
156
157 - (void)appendToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
158   BOOL        isCollapsed;
159   BOOL        doForm;
160   WOComponent *comp;
161   NSString    *img;
162   NSString    *label;
163   
164   comp = [_ctx component];
165
166   if ([self->visibility valueInComponent:comp] == nil) {
167     isCollapsed = ![self->condition boolValueInComponent:comp];
168     if ([self->visibility isValueSettable])
169       [self->visibility setBoolValue:!isCollapsed inComponent:comp];
170   }
171   else
172     isCollapsed = ![self->visibility boolValueInComponent:comp];
173
174   img = (isCollapsed)
175     ? [self->closedImageFileName stringValueInComponent:comp]
176     : [self->openedImageFileName stringValueInComponent:comp];
177
178   label = (isCollapsed)
179     ? [self->closedLabel stringValueInComponent:comp]
180     : [self->openedLabel stringValueInComponent:comp];
181
182   img = WOUriOfResource(img, _ctx);
183   
184   if (isCollapsed)
185     [_resp appendContentString:@"&nbsp;"];
186
187   doForm = ([_ctx isInForm] && self->submitActionName && img);
188
189   [_ctx appendElementIDComponent:(isCollapsed) ? @"c" : @"e"];
190   if (doForm) {
191     [_resp appendContentString:@"<INPUT TYPE=\"image\" BORDER=\"0\" NAME=\""];
192     [_resp appendContentString:[_ctx elementID]];
193     [_resp appendContentString:@"\" SRC=\""];
194     [_resp appendContentString:img];
195     [_resp appendContentString:@"\">"];
196   }
197   else {
198     [_resp appendContentString:@"<A HREF=\""];
199     [_resp appendContentString:[_ctx componentActionURL]];
200     [_resp appendContentString:@"\">"];
201
202     if (img) {
203       [_resp appendContentString:@"<IMG BORDER=0 SRC=\""];
204       [_resp appendContentString:img];
205       [_resp appendContentString:@"\""];
206       if (label) {
207         [_resp appendContentString:@" NAME=\""];
208         [_resp appendContentString:label];
209         [_resp appendContentString:@"\""];
210       }
211       [_resp appendContentString:@">"];
212     }
213     else
214       [_resp appendContentString:(isCollapsed) ? @"[+]" : @"[-]"];
215     [_resp appendContentString:@"</A>&nbsp;"];
216   }
217   
218   if (label) {
219     if (!doForm) {
220       [_resp appendContentString:@"<A HREF=\""];
221       [_resp appendContentString:[_ctx componentActionURL]];
222       [_resp appendContentString:@"\">"];
223     }
224     
225     [_resp appendContentString:label];
226
227     if (!doForm)
228       [_resp appendContentString:@"</A>"];
229   }
230   [_ctx deleteLastElementIDComponent];
231   
232   [_resp appendContentString:@"<BR>"];
233   
234   if (!isCollapsed) {
235     [_ctx appendZeroElementIDComponent];
236     [self->template appendToResponse:_resp inContext:_ctx];
237     [_ctx deleteLastElementIDComponent];
238   }
239 }
240
241 @end /* WOComponentContent */