]> err.no Git - sope/blob - sope-appserver/WOExtensions/WOTabPanel.m
added svn:keywords and svn:ignore where appropriate. removed CVS artifacts.
[sope] / sope-appserver / WOExtensions / WOTabPanel.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
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 <NGObjWeb/WODynamicElement.h>
24
25 @interface WOTabPanel : WODynamicElement
26 {
27   WOAssociation *tabs;
28   WOAssociation *selectedTab;
29   WOAssociation *tabNameKey;
30   WOAssociation *nonSelectedBgColor;
31   WOAssociation *bgcolor;
32   WOAssociation *textColor;
33   WOAssociation *submitActionName;
34   
35   WOElement *template;
36 }
37 @end
38
39 #include "common.h"
40
41 @interface WOContext(NGPrivates)
42 - (void)addActiveFormElement:(WOElement *)_element;
43 @end
44
45 @implementation WOTabPanel
46
47 - (id)initWithName:(NSString *)_name
48   associations:(NSDictionary *)_config
49   template:(WOElement *)_c
50 {
51   if ((self = [super initWithName:_name associations:_config template:_c])) {
52     self->tabs               = WOExtGetProperty(_config, @"tabs");
53     self->selectedTab        = WOExtGetProperty(_config, @"selectedTab");
54     self->tabNameKey         = WOExtGetProperty(_config, @"tabNameKey");
55     
56     self->nonSelectedBgColor = 
57       WOExtGetProperty(_config, @"nonSelectedBgColor");
58     self->bgcolor            = WOExtGetProperty(_config, @"bgcolor");
59     self->textColor          = WOExtGetProperty(_config, @"textColor");
60     
61     self->submitActionName   = WOExtGetProperty(_config, @"submitActionName");
62
63     self->template = [_c retain];
64   }
65   return self;
66 }
67
68 - (void)dealloc {
69   [self->submitActionName release];
70   [self->textColor   release];
71   [self->bgcolor     release];
72   [self->nonSelectedBgColor release];
73   [self->tabNameKey  release];
74   [self->selectedTab release];
75   [self->tabs        release];
76   [self->template    release];
77   [super dealloc];
78 }
79
80 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
81   /* check whether a (form) tab was clicked */
82   [_ctx appendElementIDComponent:@"tab"];
83   {
84     WOComponent *sComponent;
85     NSArray *ttabs;
86     unsigned i;
87     
88     sComponent = [_ctx component];
89     ttabs = [self->tabs valueInComponent:sComponent];
90     
91     [_ctx appendZeroElementIDComponent];
92     
93     for (i = 0; i < [ttabs count]; i++) {
94       if ([_req formValueForKey:[_ctx elementID]]) {
95         /* found active tab */
96         [self->selectedTab setValue:[ttabs objectAtIndex:i]
97                            inComponent:sComponent];
98         [_ctx addActiveFormElement:self];
99         break;
100       }
101       [_ctx incrementLastElementIDComponent];
102     }
103     
104     [_ctx deleteLastElementIDComponent];
105   }
106   [_ctx deleteLastElementIDComponent];
107
108   /* let content take values */
109   [_ctx appendElementIDComponent:@"content"];
110   [self->template takeValuesFromRequest:_req inContext:_ctx];
111   [_ctx deleteLastElementIDComponent];
112 }
113
114 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
115   id result;
116   NSString *section;
117
118   section = [_ctx currentElementID];
119   if ([section isEqualToString:@"tab"]) {
120     WOComponent *sComponent;
121     NSArray *ttabs;
122     int idx;
123     
124     [_ctx consumeElementID]; // consume 'tab'
125     
126     sComponent = [_ctx component];
127     ttabs = [self->tabs valueInComponent:sComponent];
128
129     idx = [[_ctx currentElementID] intValue];
130     [_ctx consumeElementID]; // consume index
131
132     if (idx >= (int)[ttabs count]) {
133       /* index out of range */
134       idx = 0;
135     }
136     
137     [self->selectedTab setValue:[ttabs objectAtIndex:idx]
138                        inComponent:sComponent];
139     
140     result = [_ctx page];
141   }
142   else if ([section isEqualToString:@"content"]) { 
143     [_ctx consumeElementID]; // consume 'content'
144     
145     [_ctx appendElementIDComponent:@"content"];
146     result = [self->template invokeActionForRequest:_req inContext:_ctx];
147     [_ctx deleteLastElementIDComponent];
148   }
149   else {
150     NSLog(@"%s: missing section id !", __PRETTY_FUNCTION__);
151     result = [_ctx page];
152   }
153   return result;
154 }
155
156 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
157   WOComponent *sComponent;
158   NSArray     *ttabs;
159   BOOL        isInForm;
160   unsigned    i, selIdx;
161   NSString    *selColor, *unselColor, *s;
162   
163   sComponent = [_ctx component];
164   ttabs = [self->tabs valueInComponent:sComponent];
165
166   selColor = self->bgcolor
167     ? [self->bgcolor stringValueInComponent:sComponent]
168     : @"#CCCCCC";
169   
170   unselColor = self->nonSelectedBgColor
171     ? [self->nonSelectedBgColor stringValueInComponent:sComponent]
172     : @"#AAAAAA";
173   
174   if ([ttabs count] < 1) {
175     /* no tabs configured .. */
176     [self->template appendToResponse:_response inContext:_ctx];
177     return;
178   }
179   
180   [_response appendContentString:@"<table border='0'>"];
181   [_response appendContentString:@"<tr>"];
182
183   isInForm = [_ctx isInForm];
184   
185   /* cannot use -indexOfObjectIdenticalTo:, since this doesn't work
186      with language bridges (base types crossing a bridge aren't
187      enforced to be identical ... */
188   selIdx = [ttabs indexOfObject:
189                     [self->selectedTab valueInComponent:sComponent]];
190   if (selIdx == NSNotFound)
191     selIdx = 0;
192   
193   [_ctx appendElementIDComponent:@"tab"];
194   [_ctx appendZeroElementIDComponent];
195   
196   for (i = 0; i < [ttabs count]; i++) {
197     id       tab;
198     BOOL     isCurrentSelected;
199     NSString *title;
200     
201     tab = [ttabs objectAtIndex:i];
202     isCurrentSelected = i == selIdx;
203     
204     [self->selectedTab setValue:tab inComponent:sComponent];
205
206     title = (self->tabNameKey)
207       ? [self->tabNameKey stringValueInComponent:sComponent]
208       : [tab stringValue];
209     
210     [_response appendContentString:@"<td"];
211     [_response appendContentString:@" bgcolor=\""];
212     [_response appendContentString:isCurrentSelected ? selColor : unselColor];
213     [_response appendContentString:@"\""];
214     [_response appendContentString:@">"];
215     
216     if (isInForm) {
217       /* gen submit button */
218       [_response appendContentString:@"<input type='submit' name=\""];
219       [_response appendContentHTMLAttributeValue:[_ctx elementID]];
220       [_response appendContentString:@"\" value=\""];
221       [_response appendContentString:title];
222       [_response appendContentString:@"\" />"];
223     }
224     else {
225       /* gen link */
226       [_response appendContentString:@"<a href=\""];
227       [_response appendContentHTMLAttributeValue:[_ctx componentActionURL]];
228       [_response appendContentString:@"\" title=\""];
229       [_response appendContentHTMLAttributeValue:title];
230       [_response appendContentString:@"\">"];
231       
232       if (self->textColor) {
233         [_response appendContentString:@"<font color=\""];
234         [_response appendContentHTMLAttributeValue:
235                      [self->textColor stringValueInComponent:sComponent]];
236         [_response appendContentString:@"\">"];
237       }
238       
239       [_response appendContentHTMLString:title];
240
241       if (self->textColor)
242         [_response appendContentString:@"</font>"];
243       [_response appendContentString:@"</a>"];
244     }
245     
246     [_response appendContentString:@"</td>"];
247     
248     [_ctx incrementLastElementIDComponent]; /* increment index */
249   }
250   [_ctx deleteLastElementIDComponent]; /* del index */
251   [_ctx deleteLastElementIDComponent]; /* del 'tab' */
252
253   [self->selectedTab setValue:[ttabs objectAtIndex:selIdx]
254                      inComponent:sComponent];
255   
256   [_response appendContentString:@"</tr><tr><td colspan=\""];
257   s = [[NSString alloc] initWithFormat:@"%d",[ttabs count]];
258   [_response appendContentString:s];
259   [s release];
260   [_response appendContentString:@"\" bgcolor=\""];
261   [_response appendContentString:selColor];
262   [_response appendContentString:@"\">"];
263   
264   [_ctx appendElementIDComponent:@"content"];
265   [self->template appendToResponse:_response inContext:_ctx];
266   [_ctx deleteLastElementIDComponent];
267   
268   [_response appendContentString:@"</td></tr></table>"];
269 }
270
271 @end /* WOTabPanel */