]> err.no Git - sope/blob - skyrix-sope/NGObjDOM/Dynamic.subproj/ODR_bind_popupbutton.m
added svn:keywords and svn:ignore where appropriate. removed CVS artifacts.
[sope] / skyrix-sope / NGObjDOM / Dynamic.subproj / ODR_bind_popupbutton.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 <NGObjDOM/ODNodeRenderer.h>
24
25 /*
26   attributes:
27   
28     list
29     item
30     selection
31     string
32     noselectionstring
33     name
34
35   example:
36     <script>
37       var list = [ "1", "2", "3", "4" ];
38     </script>
39   
40     <wo:popupbutton list="list" item="item" selection="selection"/>
41 >
42 */
43
44 @interface ODR_bind_popupbutton : ODNodeRenderer
45 @end
46
47 #include <DOM/DOM.h>
48 #include "common.h"
49
50 @implementation ODR_bind_popupbutton
51
52 - (BOOL)requiresFormForNode:(id)_domNode inContext:(WOContext *)_ctx {
53   return YES;
54 }
55
56 - (void)takeValuesForNode:(id)_node
57   fromRequest:(WORequest *)_req
58   inContext:(WOContext *)_ctx
59 {
60   NSString *formValue;
61   NSString *name;
62
63   name = [self stringFor:@"name" node:_node ctx:_ctx];
64   name = (name) ? name : [_ctx elementID];
65   
66   formValue = [_req formValueForKey:name];
67     
68   if ([self isSettable:@"value" node:_node ctx:_ctx])
69     [self setValue:formValue for:@"value" node:_node ctx:_ctx];
70     
71   if (formValue) {
72     NSArray *objects;
73     id      object;
74       
75     objects = [self valueFor:@"list" node:_node ctx:_ctx];
76       
77     object = nil;
78     if ([self hasAttribute:@"value" node:_node ctx:_ctx]) {
79       /* has a value binding, walk list to find object */
80       unsigned i, toGo;
81
82       for (i = 0, toGo = [objects count]; i < toGo; i++) {
83         NSString *cv;
84           
85         object = [objects objectAtIndex:i];
86           
87         if ([self isSettable:@"item" node:_node ctx:_ctx])
88           [self setValue:object for:@"item" node:_node ctx:_ctx];
89
90         cv = [self stringFor:@"value" node:_node ctx:_ctx];
91           
92         if ([cv isEqualToString:formValue])
93           break;
94       }
95     }
96     else if (![formValue isEqualToString:@"$"]) {
97       /* an index binding */
98       int idx;
99         
100       idx = [formValue intValue];
101       if (idx >= (int)[objects count]) {
102         [[_ctx page] logWithFormat:@"popup-index %i out of range 0-%i",
103                      idx, [objects count] - 1];
104         object = nil;
105       }
106       else 
107         object = [objects objectAtIndex:idx];
108     }
109       
110     if ([self isSettable:@"selection" node:_node ctx:_ctx]) {
111       if ([self isSettable:@"item" node:_node ctx:_ctx])
112         [self setValue:object for:@"item" node:_node ctx:_ctx];
113
114       if ([self isSettable:@"selection" node:_node ctx:_ctx])
115         [self setValue:object for:@"selection" node:_node ctx:_ctx];
116     }
117   }
118   else {
119     // nothing selected
120     if ([self isSettable:@"item" node:_node ctx:_ctx])
121       [self setValue:nil for:@"item" node:_node ctx:_ctx];
122     if ([self isSettable:@"selection" node:_node ctx:_ctx])
123       [self setValue:nil for:@"selection" node:_node ctx:_ctx];
124   }
125 }
126
127 - (void)appendOptions:(id)_node
128   toResponse:(WOResponse *)_response
129   inContext:(WOContext *)_ctx
130 {
131   NSString *nilStr   = nil;
132   NSArray  *array    = nil;
133   id       selection = nil;
134   int      i, cnt;
135   
136   nilStr    = [self stringFor:@"noselectionstring" node:_node ctx:_ctx];
137   if (nilStr == nil)
138     nilStr = [self stringFor:@"noSelectionString" node:_node ctx:_ctx];
139   array     = [self  valueFor:@"list"              node:_node ctx:_ctx];
140   selection = [self  valueFor:@"selection"         node:_node ctx:_ctx];
141   cnt       = [array count];
142   
143   if (nilStr) {
144     [_response appendContentString:@"  <option value=\"$\">"];
145     [_response appendContentHTMLString:nilStr];
146     [_response appendContentString:@"\n"];
147   }
148     
149   for (i = 0; i < cnt; i++) {
150     NSString *v         = nil;
151     NSString *displayV  = nil;
152     id       object     = [array objectAtIndex:i];
153     BOOL     isSelected;
154
155     if ([self isSettable:@"item" node:_node ctx:_ctx])
156       [self setValue:object for:@"item" node:_node ctx:_ctx];
157
158     isSelected = (selection) ? [selection isEqual:object] : NO;
159
160     v = ([self hasAttribute:@"value" node:_node ctx:_ctx])
161       ? [self stringFor:@"value" node:_node ctx:_ctx]
162       : [NSString stringWithFormat:@"%i", i];
163
164     displayV = ([self hasAttribute:@"string" node:_node ctx:_ctx])
165       ? [self stringFor:@"string" node:_node ctx:_ctx]
166       : [object stringValue];
167
168     if (displayV == nil) displayV = @"<nil>";
169
170     [_response appendContentString:@"  <option value=\""];
171     [_response appendContentString:v];
172     [_response appendContentString:(isSelected) ? @"\" selected>" : @"\">"];
173     [_response appendContentHTMLString:displayV];
174     [_response appendContentString:@"\n"];
175   }
176 }
177
178 - (void)appendNode:(id)_node
179   toResponse:(WOResponse *)_response
180   inContext:(WOContext *)_ctx
181 {
182   NSString *name;
183
184   name = [self stringFor:@"name" node:_node ctx:_ctx];
185   name = (name) ? name : [_ctx elementID];
186
187   [_response appendContentString:@"<select name=\""];
188   [_response appendContentHTMLAttributeValue:name];
189   [_response appendContentString:@"\">\n"];
190
191   [self appendOptions:_node toResponse:_response inContext:_ctx];
192
193   [_response appendContentString:@"</select>"];
194 }
195
196 @end /* ODR_bind_popupbutton */
197
198
199 @interface ODR_bind_popup : ODR_bind_popupbutton
200 @end
201
202 @implementation ODR_bind_popup
203 @end