]> err.no Git - sope/blob - sope-appserver/NGObjDOM/Dynamic.subproj/ODR_bind_tablecell.m
64f686b9a964138f3f5038344318567fd127e6f6
[sope] / sope-appserver / NGObjDOM / Dynamic.subproj / ODR_bind_tablecell.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 "ODR_bind_tablecell.h"
24 #include "ODR_bind_tableview.h"
25
26 #include <DOM/EDOM.h>
27 #include "common.h"
28
29 @implementation ODR_bind_tablecell
30
31 static NSNumber *YesNumber = nil;
32 static NSNumber *NoNumber  = nil;
33
34 + (void)initialize {
35   if (YesNumber == nil) YesNumber = [[NSNumber numberWithBool:YES] retain];
36   if (NoNumber  == nil) NoNumber  = [[NSNumber numberWithBool:NO]  retain];
37 }
38
39 - (void)appendSortIcon:(id)_node
40   toResponse:(WOResponse *)_response
41   inContext:(WOContext *)_ctx
42 {
43   NSString    *icon = nil;
44   NSString    *nav  = nil;
45   BOOL        doForm = [_ctx isInForm];
46   NSString    *sortedKey;
47   NSString    *label;
48   NSString    *sk;
49   int         sortDir;
50
51   sortedKey = [_ctx objectForKey:ODRTableView_SORTEDKEY];
52   sk        = [self stringFor:@"sortkey" node:_node ctx:_ctx];
53   
54   label     = [self stringFor:@"sortlabel" node:_node ctx:_ctx];
55   label     = (label) ? label : ODRTableLabelForKey(@"sort", _ctx);
56
57   if (sk == nil)
58     return;
59
60   if (![sk isEqualToString:sortedKey])
61     sortDir = 0;
62   else if ( [_ctx objectForKey:ODRTableView_ISDESCENDING] == nil ||
63            [[_ctx objectForKey:ODRTableView_ISDESCENDING] boolValue])
64     sortDir = -1;
65   else
66     sortDir = 1;
67   
68   switch (sortDir) {
69     case  1: nav = @"down"; break;
70     case  0: nav = @"non";  break;
71     case -1: nav = @"up";   break;
72   }
73   switch (sortDir) {
74     case  1:
75       icon = [self stringFor:@"downwardsorticon" node:_node ctx:_ctx];
76       if ([icon length] == 0)
77         icon = [_ctx objectForKey:ODRTableView_downwardIcon];
78       break;
79     case  0:
80       icon = [self stringFor:@"nonsorticon"      node:_node ctx:_ctx];
81       if ([icon length] == 0)
82         icon = [_ctx objectForKey:ODRTableView_nonSortIcon];
83       break;
84     case -1:
85       icon = [self stringFor:@"upwardsorticon"   node:_node ctx:_ctx];
86       if ([icon length] == 0)
87         icon = [_ctx objectForKey:ODRTableView_upwardIcon];
88       break;
89   }
90
91 #if DEBUG && 0
92   if (icon == nil) {
93     NSLog(@"%s: DID NOT FIND SORTICON (%i), ctx is %@\n  vars: %@",
94           __PRETTY_FUNCTION__,
95           sortDir, _ctx, [_ctx variableDictionary]);
96   }
97 #endif
98
99 #if 0
100   if (!ODRUriOfResource(icon,_ctx)) {
101     switch (sortDir) {
102       case  1: icon = [_ctx objectForKey:ODRTableView_downwardIcon]; break;
103       case  0: icon = [_ctx objectForKey:ODRTableView_nonSortIcon];  break;
104       case -1: icon = [_ctx objectForKey:ODRTableView_upwardIcon];   break;
105     }
106   }
107 #endif
108   
109   icon   = ODRUriOfResource(icon, _ctx);
110   doForm = doForm && (icon != nil);
111
112   // append something like that: sort.name.down
113   [_ctx appendElementIDComponent:@"sort"];
114   [_ctx appendElementIDComponent:sk];     // remember sortKey
115   [_ctx appendElementIDComponent:nav];    // remember sortDirection
116
117   // append as submit button
118   if (doForm)
119     ODRAppendButton(_response, [_ctx elementID], icon, label);
120
121   /* append as hyperlink */
122   else {
123     [_response appendContentString:@"<a href=\""];
124     [_response appendContentString:[_ctx componentActionURL]];
125     [_response appendContentString:@"\">"];
126     
127     if (icon) {
128       ODRAppendImage(_response, nil, icon, label);
129     }
130     else {
131       switch (sortDir) {
132         case  1: [_response appendContentString:@"&uarr;"]; break;
133         case  0: [_response appendContentString:@"-"]; break;
134         case -1: [_response appendContentString:@"&darr;"]; break;
135       }
136     }
137     [_response appendContentString:@"</a>"];
138   }
139   [_ctx deleteLastElementIDComponent]; // delete sortDirection
140   [_ctx deleteLastElementIDComponent]; // delete sortKey
141   [_ctx deleteLastElementIDComponent]; // delete @"sort"
142
143   return;
144 }
145
146 - (void)appendTitle:(id)_node
147   toResponse:(WOResponse *)_response
148   inContext:(WOContext *)_ctx
149 {
150   NSString *title;
151   NSString *tC, *tF, *tS; // text font attrtibutes
152   BOOL     hasFont;
153
154   tC  = [_ctx objectForKey:ODRTableView_fontColor];
155   tF  = [_ctx objectForKey:ODRTableView_fontFace];
156   tS  = [_ctx objectForKey:ODRTableView_fontSize];
157
158   hasFont = (tC || tF || tS) ? YES : NO;
159   
160   title   = [self stringFor:@"title" node:_node ctx:_ctx];
161
162   if (title) {
163     if (hasFont)
164       ODRAppendFont(_response, tC, tF, tS);                      //   <FONT...>
165    
166     [_response appendContentString:@" <b>"];
167     [_response appendContentString:title];
168     [_response appendContentString:@"</b>"];
169
170     if (hasFont)
171       [_response appendContentString:@"</font>"];               //   </FONT>
172   }
173 }
174
175 /* --- responder --- */
176
177 - (void)takeValuesForNode:(id)_node
178   fromRequest:(WORequest *)_req
179   inContext:(WOContext *)_ctx
180 {
181   NSString *k;
182   
183   k = [self stringFor:@"sortkey" node:_node ctx:_ctx];
184
185   if (k && [[_ctx objectForKey:ODRTableView_HeaderMode] boolValue]) {
186     NSString *tmp;
187     tmp = [[_ctx elementID] stringByAppendingFormat:@".sort.%@.", k];
188
189     if ([_req formValueForKey:[tmp stringByAppendingString:@"down.x"]]) {
190       [_ctx addActiveFormElement:self];
191       [_ctx setRequestSenderID:[tmp stringByAppendingString:@"down"]];
192     }
193     else if ([_req formValueForKey:[tmp stringByAppendingString:@"up.x"]]) {
194       [_ctx addActiveFormElement:self];
195       [_ctx setRequestSenderID:[tmp stringByAppendingString:@"up"]];
196     }
197     else if ([_req formValueForKey:[tmp stringByAppendingString:@"non.x"]]) {
198       [_ctx addActiveFormElement:self];
199       [_ctx setRequestSenderID:[tmp stringByAppendingString:@"non"]];
200     }
201     else
202       [super takeValuesForNode:_node  fromRequest:_req inContext:_ctx];
203   }
204   else
205     [super takeValuesForNode:_node  fromRequest:_req inContext:_ctx];
206 }
207
208 - (id)invokeActionForNode:(id)_node
209   fromRequest:(WORequest *)_req
210   inContext:(WOContext *)_ctx
211 {
212   id          result = nil;
213   NSString    *k;
214
215 #if DEBUG && 0
216   NSLog(@"%s:    invoke on tablecell %@ (eid=%@, sid=%@)", __PRETTY_FUNCTION__,
217         _node, [_ctx elementID], [_ctx senderID]);
218 #endif
219
220   k   = [self stringFor:@"sortkey" node:_node ctx:_ctx];
221   if ([[_ctx currentElementID] isEqual:@"sort"] && k != nil) {
222     NSString *tmp;
223     
224     [_ctx consumeElementID];                 // consume "sort"
225     [_ctx appendElementIDComponent:@"sort"]; // append  "sort"
226     
227     tmp = [_ctx currentElementID];
228     if (tmp != nil && [tmp isEqualToString:k]) {
229       BOOL doNegate = [self boolFor:@"negatesort" node:_node ctx:_ctx];
230
231       [_ctx consumeElementID];               // consume sortKey
232       [_ctx appendElementIDComponent:k];     // append  sortKey
233       [_ctx setObject:k forKey:ODRTableView_SORTEDKEY];
234
235       tmp = [_ctx currentElementID];
236       
237       if ([tmp isEqualToString:@"down"])
238         [_ctx setObject:YesNumber forKey:ODRTableView_ISDESCENDING];
239       else if ([tmp isEqualToString:@"up"])
240         [_ctx setObject:NoNumber forKey:ODRTableView_ISDESCENDING];
241       else if ([tmp isEqualToString:@"non"])
242         [_ctx setObject:[NSNumber numberWithBool:doNegate]
243                  forKey:ODRTableView_ISDESCENDING];
244       else {
245         [_ctx removeObjectForKey:ODRTableView_ISDESCENDING];
246         [_ctx removeObjectForKey:ODRTableView_SORTEDKEY];
247       }
248       [_ctx deleteLastElementIDComponent];
249     }
250     [_ctx deleteLastElementIDComponent];
251   }
252   else
253     result = [self invokeActionForChildNodes:[_node childNodes]
254                    fromRequest:_req
255                    inContext:_ctx];
256   
257   return result;
258 }
259
260 @end /* ODR_bind_tablecell */
261
262