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