]> err.no Git - sope/blob - sope-appserver/WOExtensions/JSConfirmPanel.m
updated framework version
[sope] / sope-appserver / WOExtensions / JSConfirmPanel.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 #import <NGObjWeb/WODynamicElement.h>
23
24 @interface JSConfirmPanel : WODynamicElement
25 {
26   WOAssociation *action;
27   WOAssociation *javaScriptFunction;
28   WOAssociation *pageName;
29   WOAssociation *confirmMessage;
30   WOAssociation *altTag;
31   WOAssociation *filename;
32   WOAssociation *targetWindow;
33   WOAssociation *string;
34
35   /* non WO */
36   WOAssociation *showPanel;
37   WOElement     *template;
38   WOAssociation *escapeJS;
39   WOAssociation *framework;
40 }
41
42 @end
43
44 #include "common.h"
45
46 @implementation JSConfirmPanel
47
48 + (int)version {
49   return [super version] + 0 /* v2 */;
50 }
51 + (void)initialize {
52   NSAssert2([super version] == 2,
53             @"invalid superclass (%@) version %i !",
54             NSStringFromClass([self superclass]), [super version]);
55 }
56
57 - (id)initWithName:(NSString *)_name
58   associations:(NSDictionary *)_config
59   template:(WOElement *)_subs
60 {
61   if ((self = [super initWithName:_name associations:_config template:_subs]))
62   {
63     int funcCount;
64
65     self->action             = WOExtGetProperty(_config,@"action");
66     self->javaScriptFunction = WOExtGetProperty(_config,@"javaScriptFunction");
67     self->pageName           = WOExtGetProperty(_config,@"pageName");
68     self->confirmMessage     = WOExtGetProperty(_config,@"confirmMessage");
69     self->altTag             = WOExtGetProperty(_config,@"altTag");
70     self->filename           = WOExtGetProperty(_config,@"filename");
71     self->targetWindow       = WOExtGetProperty(_config,@"targetWindow");
72     self->string             = WOExtGetProperty(_config,@"string");
73     self->showPanel          = WOExtGetProperty(_config,@"showPanel");
74     self->escapeJS           = WOExtGetProperty(_config,@"escapeJS");
75     self->framework          = WOExtGetProperty(_config,@"framework");
76
77     funcCount = 0;
78     if (self->action) funcCount++;
79     if (self->pageName) funcCount++;
80     if (self->javaScriptFunction) funcCount++;
81
82     if (funcCount > 1) {
83       NSLog(@"WARNING: JSConfirmPanel: choose only one of "
84             @"action | pageName | javaScriptFunction");
85     }
86     if (funcCount < 1) {
87       NSLog(@"WARNING: JSConfirmPanel: no function declared - choose one of"
88             @"action | pageName | javaScriptFunction");
89     }
90     if (!self->confirmMessage) {
91       NSLog(@"WARNING: JSConfirmPanel: no value for 'confirmMessage'"
92             @" - using default");
93     }
94     
95     self->template = [_subs retain];
96   }
97   return self;
98 }
99
100 - (void)dealloc {
101   [self->action             release];
102   [self->javaScriptFunction release];
103   [self->pageName           release];
104   [self->confirmMessage     release];
105   [self->altTag             release];
106   [self->filename           release];
107   [self->targetWindow       release];
108   [self->string             release];
109   [self->template           release];
110   [self->showPanel          release];
111   [self->escapeJS           release];
112   [self->framework          release];
113   [super dealloc];
114 }
115
116 /* request processing */
117
118 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
119   [self->template takeValuesFromRequest:_rq inContext:_ctx];
120 }
121
122 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx{
123   id       result;
124   NSString *name;
125
126   if (self->showPanel &&
127       ![self->showPanel boolValueInComponent:[_ctx component]]) {
128     return nil;
129   }
130   
131   if (self->pageName != nil) {
132     name   = [self->pageName stringValueInComponent: [_ctx component]];
133     result = [[_ctx application] pageWithName:name inContext:_ctx];
134   }
135   else if (self->action != nil) {
136     result = [self->action valueInComponent:[_ctx component]];
137   }
138   else {
139     result = [self->template invokeActionForRequest:_request inContext:_ctx];
140   }
141   return result;
142 }
143
144 /* response generation */
145
146 - (void)_appendPanelToResponse:(WOResponse *)_response 
147   message:(NSString *)_msg
148   inContext:(WOContext *)_ctx 
149 {
150   if (![self->showPanel boolValueInComponent:[_ctx component]])
151     return;
152   
153   [_response appendContentString:
154                @"<script type=\"text/javascript\">\nvar res = confirm(\""];
155   [_response appendContentHTMLString:_msg];
156   [_response appendContentString:@"\");\n if (res) {\n"];
157
158   if (self->javaScriptFunction) {
159     NSString *js;
160     
161     js = [self->javaScriptFunction stringValueInComponent:[_ctx component]];
162     [_response appendContentString:js];
163   }
164   else if (self->action || self->pageName) {
165     [_response appendContentString:@"document.location.href=\""];
166     [_response appendContentString:[_ctx componentActionURL]];
167     [_response appendContentString:@"\";"];
168   }
169     
170   [_response appendContentString:@"}"];
171   [_response appendContentString:@"</script>"];
172 }
173
174 - (void)_appendLinkToResponse:(WOResponse *)_response 
175   message:(NSString *)_msg
176   inContext:(WOContext *)_ctx 
177 {
178   WOComponent *comp;
179   NSString    *tmp;
180   NSArray     *languages;
181   
182   comp = [_ctx component];
183   
184   [_response appendContentString:@"<a onclick=\"javascript:return confirm('"];
185   [_response appendContentHTMLString:_msg];
186   [_response appendContentString:@"');\""];
187   [_response appendContentString:@" href=\""];
188   
189   if (self->javaScriptFunction) {
190       [_response appendContentString:@"javascript:"];
191       [_response appendContentHTMLAttributeValue:
192                  [self->javaScriptFunction stringValueInComponent:comp]];
193   }
194   else {
195       [_response appendContentString:[_ctx componentActionURL]];
196   }
197   [_response appendContentString:@"\" "];
198   if (self->targetWindow) {
199       [_response appendContentString:@" target=\""];
200       [_response appendContentHTMLAttributeValue:
201                  [self->targetWindow stringValueInComponent: comp]];
202       [_response appendContentString:@"\" "];
203   }
204   [self appendExtraAttributesToResponse:_response inContext:_ctx];
205   [_response appendContentString:@" >"];
206
207   /* link content */
208   if (self->filename != nil) { /* image */
209     WOResourceManager *rm;
210     NSString          *frameworkName;
211
212     frameworkName = (self->framework != nil)
213       ? [self->framework stringValueInComponent:comp] 
214       : [comp frameworkName];
215
216     rm        = [[_ctx application] resourceManager];
217     languages = [_ctx resourceLookupLanguages];
218       
219     tmp = [rm urlForResourceNamed:[self->filename stringValueInComponent:comp]
220               inFramework:frameworkName
221               languages:languages
222               request:[_ctx request]];
223     
224     [_response appendContentString:@"<img border=\"0\" src=\""];
225     [_response appendContentString:tmp];
226     [_response appendContentString:@"\" "];
227     
228     if (self->altTag != nil) {
229       [_response appendContentString:@"alt=\""];
230       [_response appendContentString:
231                    [self->altTag stringValueInComponent:comp]];
232       [_response appendContentString:@"\" "];
233     }
234
235     [_response appendContentString:
236                  (_ctx->wcFlags.xmlStyleEmptyElements ? @" />" : @">")];
237   }
238   
239   [self->template appendToResponse:_response inContext:_ctx];
240   
241   if (self->string != nil) {
242     [_response appendContentString:
243                  [self->string stringValueInComponent:comp]];
244   }
245   
246   /* close link */
247   [_response appendContentString:@"</a>"];
248 }
249
250 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
251   NSString    *msg;
252   WOComponent *comp;
253   
254   comp = [_ctx component];
255   msg  = (self->confirmMessage)
256     ? [self->confirmMessage stringValueInComponent:comp]
257     : @"Really?";
258   if (self->escapeJS != nil && [self->escapeJS boolValueInComponent:comp]) {
259     msg = [msg stringByApplyingJavaScriptEscaping];
260   }
261
262   if (self->showPanel)
263     [self _appendPanelToResponse:_response message:msg inContext:_ctx];
264   else 
265     [self _appendLinkToResponse:_response message:msg inContext:_ctx];
266 }
267
268 @end /* JSConfirmPanel */