]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/Common/UIxTabItem.m
git-svn-id: http://svn.opengroupware.org/SOGo/trunk@174 d1b88da0-ebda-0310-925b-ed51d...
[scalable-opengroupware.org] / SOGo / UI / Common / UIxTabItem.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
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 "UIxTabView.h"
24 #include "common.h"
25
26 #if DEBUG
27 #  define DEBUG_JS 1
28 #endif
29
30 /* context keys */
31 extern NSString *UIxTabView_HEAD;
32 extern NSString *UIxTabView_BODY;
33 extern NSString *UIxTabView_KEYS;
34 extern NSString *UIxTabView_SCRIPT;
35 extern NSString *UIxTabView_ACTIVEKEY;
36 extern NSString *UIxTabView_COLLECT;
37
38 @implementation UIxTabItem
39
40 static Class StrClass = Nil;
41
42 + (int)version {
43   return [super version] + 0;
44 }
45 + (void)initialize {
46   StrClass = [NSString class];
47 }
48
49 static NSString *retStrForInt(int i) {
50   switch(i) {
51   case 0:  return @"0";
52   case 1:  return @"1";
53   case 2:  return @"2";
54   case 3:  return @"3";
55   case 4:  return @"4";
56   case 5:  return @"5";
57   case 6:  return @"6";
58   case 7:  return @"7";
59   case 8:  return @"8";
60   case 9:  return @"9";
61   case 10: return @"10";
62     // TODO: find useful count!
63   default:
64     return [[StrClass alloc] initWithFormat:@"%i", i];
65   }
66 }
67
68 - (id)initWithName:(NSString *)_name
69   associations:(NSDictionary *)_config
70   template:(WOElement *)_subs
71 {
72   if ((self = [super initWithName:_name associations:_config template:_subs])) {
73     self->key      = WOExtGetProperty(_config, @"key");
74     self->label    = WOExtGetProperty(_config, @"label");
75
76     self->isScript = WOExtGetProperty(_config, @"isScript");
77     self->href     = WOExtGetProperty(_config, @"href");
78
79     self->icon     = WOExtGetProperty(_config, @"icon");
80     self->action   = WOExtGetProperty(_config, @"action");
81
82     self->tabStyle         = WOExtGetProperty(_config, @"tabStyle");
83     self->selectedTabStyle = WOExtGetProperty(_config, @"selectedTabStyle");
84
85     self->tabIcon         = WOExtGetProperty(_config, @"tabIcon");
86     self->leftTabIcon     = WOExtGetProperty(_config, @"leftTabIcon");
87     self->selectedTabIcon = WOExtGetProperty(_config, @"selectedTabIcon");
88     
89     self->asBackground    = WOExtGetProperty(_config, @"asBackground");
90     self->width           = WOExtGetProperty(_config, @"width");
91     self->height          = WOExtGetProperty(_config, @"height");
92     self->activeBgColor   = WOExtGetProperty(_config, @"activeBgColor");
93     self->inactiveBgColor = WOExtGetProperty(_config, @"inactiveBgColor");
94     
95     self->template = [_subs retain];
96   }
97   return self;
98 }
99
100 - (void)dealloc {
101   [self->key      release];
102   [self->label    release];
103
104   [self->href   release];
105
106   [self->action   release];
107
108   [self->isScript release];
109   [self->template release];
110
111   [self->tabStyle release];
112   [self->selectedTabStyle release];
113
114   [self->icon     release];
115   [self->leftTabIcon     release];
116   [self->selectedTabIcon release];
117   [self->tabIcon         release];
118
119   [self->asBackground release];
120   [self->width        release];
121   [self->height       release];
122
123   [self->activeBgColor   release];
124   [self->inactiveBgColor release];
125   
126   [super dealloc];
127 }
128
129 /* responder */
130
131 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
132   NSString *activeTabKey;
133   NSString *myTabKey;
134   BOOL     doCheck;
135   
136   if ([_ctx objectForKey:UIxTabView_HEAD]) {
137     /* head clicks */
138     [[_ctx component] debugWithFormat:
139                         @"UIxTabItem: head takes (no) values, eid='%@'",
140                         [_ctx elementID]];
141     return;
142   }
143
144   if ((activeTabKey = [_ctx objectForKey:UIxTabView_BODY]) == nil) {
145     [[_ctx component] debugWithFormat:@"UIxTabItem: invalid state"];
146     [self->template takeValuesFromRequest:_rq inContext:_ctx];
147     return;
148   }
149   
150   myTabKey = [self->key      stringValueInComponent:[_ctx component]];
151   doCheck  = [self->isScript boolValueInComponent:[_ctx component]];
152     
153   if ([activeTabKey isEqualToString:myTabKey] || doCheck) {
154 #if ADD_OWN_ELEMENTIDS
155     [_ctx appendElementIDComponent:activeTabKey];
156 #endif
157       
158 #if DEBUG_TAKEVALUES
159     [[_ctx component] debugWithFormat:
160                           @"UIxTabItem: body takes values, eid='%@'",
161                           [_ctx elementID]];
162 #endif
163       
164     [self->template takeValuesFromRequest:_rq inContext:_ctx];
165 #if ADD_OWN_ELEMENTIDS
166     [_ctx deleteLastElementIDComponent];
167 #endif
168   }
169 #if DEBUG_TAKEVALUES
170   else {
171       [[_ctx component] debugWithFormat:
172                           @"UIxTabItem: body takes no values, eid='%@'",
173                           [_ctx elementID]];
174   }
175 #endif
176 }
177
178 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
179   id            result;
180   WOAssociation *tmp;
181   NSString      *activeTabKey;
182   
183   if ((tmp = [_ctx objectForKey:UIxTabView_HEAD])) {
184     /* click on tab icon */
185     NSString      *tabkey;
186     
187     tabkey = [_ctx currentElementID];
188     [_ctx consumeElementID];
189     [_ctx appendElementIDComponent:tabkey];
190     
191     if ([tmp isValueSettable])
192       [tmp setValue:tabkey inComponent:[_ctx component]];
193     
194 #if 0
195     result = [self->action valueInComponent:[_ctx component]];
196 #endif
197
198     [_ctx deleteLastElementIDComponent];
199   }
200   else if ((activeTabKey = [_ctx objectForKey:UIxTabView_BODY])) {
201     /* clicked somewhere in the (active) body */
202     result = [self->template invokeActionForRequest:_req inContext:_ctx];
203   }
204   else {
205     [[_ctx component] logWithFormat:@"UIxTabItem: invalid invoke state"];
206     result = [self->template invokeActionForRequest:_req inContext:_ctx];
207   }
208   
209   return result;
210 }
211
212 /* info collection */
213
214 - (void)_collectInContext:(WOContext *)_ctx key:(NSString *)k {
215   BOOL  isLeft = NO;
216   NSMutableArray *keys;
217   UIxTabItemInfo  *info;
218   WOComponent    *cmp;
219       
220   cmp  = [_ctx component];
221   keys = [_ctx objectForKey:UIxTabView_KEYS];
222   if (keys == nil) {
223     keys = [[[NSMutableArray alloc] init] autorelease];
224     [_ctx setObject:keys forKey:UIxTabView_KEYS];
225     isLeft = YES;
226   }
227       
228   if (k == nil) {
229     /* auto-assign a key */
230     k = retStrForInt([keys count]);
231   }
232   else
233     k = [k retain];
234   [_ctx appendElementIDComponent:k];
235   
236   info = [[UIxTabItemInfo alloc] init];
237   info->key      = [k copy];
238   info->label    = [[self->label stringValueInComponent:cmp] copy];
239   info->icon     = [[self->icon  stringValueInComponent:cmp] copy];
240 #if 0
241   info->uri      = [[_ctx componentActionURL] copy];
242 #else
243   info->uri      = [[self->href stringValueInComponent:cmp] copy];
244 #endif
245   info->isScript = [self->isScript boolValueInComponent:cmp];
246   info->tabIcon  = [[self->tabIcon stringValueInComponent:cmp] copy];
247   info->leftIcon = [[self->leftTabIcon stringValueInComponent:cmp] copy];
248   info->selIcon  = [[self->selectedTabIcon stringValueInComponent:cmp]
249                                            copy];
250   info->tabStyle         = [[self->tabStyle stringValueInComponent:cmp] copy];
251   info->selectedTabStyle = [[self->selectedTabStyle stringValueInComponent:cmp]
252                                                     copy];
253
254   if (self->asBackground == nil)
255     info->asBackground = 0;
256   else {
257     info->asBackground
258       = ([self->asBackground boolValueInComponent:cmp]) ? 1 : -1;
259   }
260   info->width        = [[self->width  stringValueInComponent:cmp] copy];
261   info->height       = [[self->height stringValueInComponent:cmp] copy];
262   info->activeBg     = [[self->activeBgColor stringValueInComponent:cmp]
263                                              copy];
264   info->inactiveBg   = [[self->inactiveBgColor stringValueInComponent:cmp]
265                                                copy];
266       
267   if (info->leftIcon == nil) info->leftIcon = [info->tabIcon copy];
268       
269   [keys addObject:info];
270   [info release];
271   [k release];
272       
273   [_ctx deleteLastElementIDComponent];
274 }
275
276 /* header generation */
277
278 - (void)_appendHeadToResponse:(WOResponse *)_response
279   inContext:(WOContext *)_ctx
280   activeKey:(NSString *)activeKey
281   key:(NSString *)k
282 {
283   /* head is currently generated in UIxTabView */
284 #if 0
285   // note: some associations can be inherited by UIxTabView !
286   BOOL        doImages;
287   WOComponent *comp;
288   BOOL        doBgIcon;
289   NSString    *label;
290   NSString    *w, *h;
291   
292   doImages = ![[[_ctx request] clientCapabilities] isTextModeBrowser];
293   comp     = [_ctx component];
294   
295   doBgIcon = self->asBackground && doImages
296     ? [self->asBackground boolValueInComponent:comp] ? YES : NO
297     : NO;
298   
299   if ((label = [self->label stringValueInComponent:comp]) == nil)
300     label = k;
301
302   if (doImages) {
303     /* lookup image */
304     NSString *imgName = nil;
305     // ...
306     
307     imgUri = WEUriOfResource(imgName, _ctx);
308     if ([imgUri length] < 1)
309       doImages = NO;
310   }
311   
312   // .... _isActive
313 #endif
314 }
315
316 /* body generation */
317
318 - (void)_appendBodyToResponse:(WOResponse *)_response
319   inContext:(WOContext *)_ctx
320   activeKey:(NSString *)tmp
321   key:(NSString *)k
322 {
323   BOOL doScript;
324   BOOL isScript_;
325   BOOL isActive;
326
327   doScript  = [[_ctx objectForKey:UIxTabView_SCRIPT] boolValue];
328   isScript_ = [self->isScript boolValueInComponent:[_ctx component]];
329   isActive  = [tmp isEqualToString:k];
330     
331   if (doScript && (isActive || isScript_)) {
332     [_response appendContentString:@"<div id=\""];
333     [_response appendContentString:k];
334     [_response appendContentString:@"TabLayer\" style=\"display: none;\">\n"];
335   }
336   
337   if (isActive || (doScript && isScript_)) {
338     /* content is active or used as layer*/
339 #if ADD_OWN_ELEMENTIDS
340     [_ctx appendElementIDComponent:k];
341 #endif
342 #if DEBUG && 0
343     NSLog(@"TAB: %@", k);
344 #endif
345     
346     [self->template appendToResponse:_response inContext:_ctx];
347     
348 #if ADD_OWN_ELEMENTIDS
349     [_ctx deleteLastElementIDComponent];
350 #endif
351   }
352     
353   if (doScript && (isActive || isScript_)) {
354     NSString *jsout;
355     [_response appendContentString:@"</div>"];
356
357     jsout = [NSString alloc];
358     jsout = [jsout initWithFormat:
359                    @"<script language=\"JavaScript\">\n<!--\n"
360                    @"%@Tab[\"Div\"] = %@TabLayer;\n",
361                    k, k];
362     
363     [_response appendContentString:jsout];
364     [jsout release];
365     
366 #if DEBUG_JS
367     jsout = [NSString alloc];
368     jsout = [jsout initWithFormat:
369                      @"if (%@Tab[\"Div\"].style==null) {"
370                      @"alert('missing style in div for tab %@');}",
371                      k, k];
372     
373     [_response appendContentString:jsout];
374     [jsout release];
375 #endif
376     
377     if (isActive) {
378       [_response appendContentString:@"showTab("];
379       [_response appendContentString:k];
380       [_response appendContentString:@"Tab);\n"];
381     }
382     [_response appendContentString:@"//-->\n</script>"];
383   }
384 }
385
386 /* master generation method */
387
388 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
389   NSString *k;
390   BOOL     doForm;
391   id       tmp;
392   
393   doForm = [_ctx isInForm];
394   k = [self->key stringValueInComponent:[_ctx component]];
395   
396   if ((tmp = [_ctx objectForKey:UIxTabView_HEAD])) {
397     if ([tmp isEqual:UIxTabView_COLLECT]) {
398       [self _collectInContext:_ctx key:k];
399     }
400     else {
401       [self _appendHeadToResponse:_response inContext:_ctx
402             activeKey:tmp key:k];
403     }
404   }
405   else if ((tmp = [_ctx objectForKey:UIxTabView_BODY])) {
406     [self _appendBodyToResponse:_response inContext:_ctx
407           activeKey:tmp key:k];
408   }
409   else {
410     NSLog(@"WARNING(%s): invalid UIxTabItem state !!!", __PRETTY_FUNCTION__);
411     [_response appendContentString:@"[invalid state]"];
412   }
413 }
414
415 @end /* UIxTabItem */
416
417 @implementation UIxTabItemInfo
418
419 - (void)dealloc {
420   [self->uri                release];
421   [self->icon               release];
422   [self->label              release];
423   [self->key                release];
424   [self->tabStyle           release];
425   [self->selectedTabStyle   release];
426   [self->tabIcon            release];
427   [self->selIcon            release];
428   [self->leftIcon           release];
429   [self->width              release];
430   [self->height             release];
431   [self->activeBg           release];
432   [self->inactiveBg         release];
433
434   [super dealloc];
435 }
436
437 /* accessors */
438
439 - (NSString *)key {
440   return self->key;
441 }
442 - (NSString *)label {
443   return self->label;
444 }
445 - (NSString *)icon {
446   return self->icon;
447 }
448 - (NSString *)uri {
449   return self->uri;
450 }
451 - (BOOL)isScript {
452   return self->isScript;
453 }
454
455 - (int)asBackground {
456   return self->asBackground;
457 }
458
459 - (NSString *)width {
460   return self->width;
461 }
462
463 - (NSString *)height {
464   return self->height;
465 }
466
467 - (NSString *)activeBg {
468   return self->activeBg;
469 }
470
471 - (NSString *)inactiveBg {
472   return self->inactiveBg;
473 }
474
475 @end /* UIxTabItemInfo */