]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WONestedList.m
minor improvement to WOHttpAdaptor, bumped framework revisions
[sope] / sope-appserver / NGObjWeb / DynamicElements / WONestedList.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 "WOHTMLDynamicElement.h"
23 #include "WOElement+private.h"
24 #include "decommon.h"
25
26 @interface WONestedList : WOHTMLDynamicElement
27 {
28   // WODynamicElement: extraAttributes
29   // WODynamicElement: otherTagString
30 @protected
31   WOAssociation *list;
32   WOAssociation *item;
33   WOAssociation *value;
34   WOAssociation *sublist;
35   WOAssociation *action;
36   WOAssociation *selection;
37   WOAssociation *index;
38   WOAssociation *level;
39   WOAssociation *isOrdered;
40   WOAssociation *prefix;
41   WOAssociation *suffix;
42 }
43
44 @end /* WONestedList */
45
46 @implementation WONestedList
47
48 - (id)initWithName:(NSString *)_name
49   associations:(NSDictionary *)_config
50   template:(WOElement *)_root
51 {
52   if ((self = [super initWithName:_name associations:_config template:_root])) {
53     self->action          = OWGetProperty(_config, @"action");
54     self->list            = OWGetProperty(_config, @"list");
55     self->item            = OWGetProperty(_config, @"item");
56     self->index           = OWGetProperty(_config, @"index");
57     self->selection       = OWGetProperty(_config, @"selection");
58     self->prefix          = OWGetProperty(_config, @"prefix");
59     self->suffix          = OWGetProperty(_config, @"suffix");
60     self->sublist         = OWGetProperty(_config, @"sublist");
61     self->value           = OWGetProperty(_config, @"value");
62     self->isOrdered       = OWGetProperty(_config, @"isOrdered");
63     self->level           = OWGetProperty(_config, @"level");
64   }
65   return self;
66 }
67
68 - (void)dealloc {
69   RELEASE(self->level);
70   RELEASE(self->isOrdered);
71   RELEASE(self->value);
72   RELEASE(self->sublist);
73   RELEASE(self->list);
74   RELEASE(self->item);
75   RELEASE(self->index);
76   RELEASE(self->selection);
77   RELEASE(self->prefix);
78   RELEASE(self->suffix);
79   RELEASE(self->action);
80   [super dealloc];
81 }
82
83 /* processing requests */
84
85 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
86   // not a container ..
87 }
88
89 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
90   WOComponent *sComponent = [_ctx component];
91   id       idxId   = nil;
92   id       object  = nil;
93   unsigned nesting = 0;
94   NSArray  *array;
95
96   array  = [self->list valueInComponent:sComponent];
97   if ([array count] < 1) return nil;
98   
99   idxId = [_ctx currentElementID]; // top level index
100   while ((idxId != nil) && (array != nil)) {
101     unsigned idx = [idxId unsignedIntValue];
102     
103     object = [array objectAtIndex:idx];
104
105     if ([self->level isValueSettable])
106       [self->level setUnsignedIntValue:nesting inComponent:sComponent];
107     if ([self->index isValueSettable])
108       [self->index setUnsignedIntValue:idx inComponent:sComponent];
109     if ([self->item isValueSettable])
110       [self->item setValue:object inComponent:sComponent];
111
112     array = [self->sublist valueInComponent:sComponent];
113     idxId = [_ctx consumeElementID]; // sub level index
114     nesting++;
115   }
116
117   if ([self->selection isValueSettable])
118     [self->selection setValue:object inComponent:sComponent];
119
120   return [self executeAction:self->action inContext:_ctx];
121 }
122
123 /* generating response */
124
125 - (void)appendList:(NSArray *)_list atLevel:(unsigned int)_level
126   toResponse:(WOResponse *)_response inContext:(WOContext *)_ctx
127 {
128   WOComponent *sComponent = [_ctx component];
129   unsigned    count       = [_list count];
130   unsigned    cnt;
131
132   if (count > 0) {
133     BOOL ordered;
134     
135     if ([self->level isValueSettable])
136       [self->level setUnsignedIntValue:_level inComponent:sComponent];
137
138     ordered = [self->isOrdered boolValueInComponent:sComponent];
139     
140     WOResponse_AddString(_response, ordered ? @"<ol>" : @"<ul>");
141
142     [_ctx appendZeroElementIDComponent];
143     for (cnt = 0; cnt < count; cnt++) {
144       id object = [_list objectAtIndex:cnt];
145
146       if ([self->index isValueSettable])
147         [self->index setUnsignedIntValue:cnt inComponent:sComponent];
148
149       if ([self->item isValueSettable])
150         [self->item setValue:object inComponent:sComponent];
151
152       // add item
153       WOResponse_AddCString(_response, "<li>");
154       {
155         NSArray *sl = [self->sublist valueInComponent:sComponent];
156         
157         if (self->prefix) {
158           NSString *ps;
159
160           ps = [self->prefix stringValueInComponent:sComponent];
161           WOResponse_AddString(_response, ps);
162         }
163         
164         if (self->value) {
165           if (self->action) {
166             WOResponse_AddCString(_response, "<a href=\"");
167             WOResponse_AddString(_response, [_ctx componentActionURL]);
168             [_response appendContentCharacter:'"'];
169             [self appendExtraAttributesToResponse:_response inContext:_ctx];
170             [_response appendContentCharacter:'>'];
171           }
172
173           WOResponse_AddHtmlString(_response,
174             [self->value stringValueInComponent:sComponent]);
175           if (self->action)
176             WOResponse_AddCString(_response, "</a>");
177         }
178
179         if (self->suffix) {
180           NSString *ss;
181
182           ss = [self->suffix stringValueInComponent:sComponent];
183           WOResponse_AddString(_response, ss);
184         }
185
186         if ([sl count] > 0) { // not a leaf
187           [self appendList:sl
188                 atLevel:(_level + 1)
189                 toResponse:_response
190                 inContext:_ctx];
191           if ([self->level isValueSettable])
192             [self->level setUnsignedIntValue:_level inComponent:sComponent];
193         }
194       }
195       WOResponse_AddCString(_response, "</li>\n");
196       
197       [_ctx incrementLastElementIDComponent];
198     }
199     [_ctx deleteLastElementIDComponent]; // list index
200
201     WOResponse_AddString(_response, ordered ? @"</ol>" : @"</ul>");
202
203     if ([self->level isValueSettable])
204       [self->level setUnsignedIntValue:_level inComponent:sComponent];
205   }
206 }
207
208 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
209   NSArray *top;
210   
211   top = [self->list valueInComponent:[_ctx component]];
212   if ([top count] > 0) {
213     [self appendList:top
214           atLevel:0
215           toResponse:_response
216           inContext:_ctx];
217   }
218 }
219
220 /* description */
221
222 - (NSString *)associationDescription {
223   NSMutableString *str;
224
225   str = [NSMutableString stringWithCapacity:64];
226   if (self->action)    [str appendFormat:@" action=%@",    self->action];
227   if (self->list)      [str appendFormat:@" list=%@",      self->list];
228   if (self->sublist)   [str appendFormat:@" sublist=%@",   self->sublist];
229   if (self->item)      [str appendFormat:@" item=%@",      self->item];
230   if (self->index)     [str appendFormat:@" index=%@",     self->index];
231   if (self->prefix)    [str appendFormat:@" prefix=%@",    self->prefix];
232   if (self->suffix)    [str appendFormat:@" suffix=%@",    self->suffix];
233   if (self->selection) [str appendFormat:@" selection=%@", self->selection];
234   if (self->value)     [str appendFormat:@" value=%@",     self->value];
235   if (self->isOrdered) [str appendFormat:@" isOrdered=%@", self->isOrdered];
236   if (self->level)     [str appendFormat:@" level=%@",     self->level];
237   return str;
238 }
239
240 @end /* WONestedList */