]> err.no Git - sope/blob - sope-appserver/WOExtensions/JSAlertPanel.m
Ported "fragmentID" stuff from JOPE to SOPE.
[sope] / sope-appserver / WOExtensions / JSAlertPanel.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 JSAlertPanel : WODynamicElement
25 {
26   WOAssociation *action;
27   WOAssociation *javaScriptFunction;
28   WOAssociation *pageName;
29   WOAssociation *alertMessage;
30   WOAssociation *altTag;
31   WOAssociation *filename;
32   WOAssociation *targetWindow;
33   WOAssociation *string;
34   
35   /* non WO */
36   WOElement     *template;
37   WOAssociation *escapeJS;
38   WOAssociation *framework;
39 }
40
41 @end
42
43 #include "common.h"
44
45 @implementation JSAlertPanel
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->alertMessage       = WOExtGetProperty(_config,@"alertMessage");
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->escapeJS           = WOExtGetProperty(_config,@"escapeJS");
73     self->framework          = WOExtGetProperty(_config,@"framework");
74     
75     funcCount = 0;
76     if (self->action) funcCount++;
77     if (self->pageName) funcCount++;
78     if (self->javaScriptFunction) funcCount++;
79
80     if (funcCount > 1) {
81       NSLog(@"WARNING: JSAlertPanel: choose only one of "
82             @"action | pageName | javaScriptFunction");
83     }
84     if (funcCount < 1) {
85       NSLog(@"WARNING: JSAlertPanel: no function declared - choose one of"
86             @"action | pageName | javaScriptFunction");
87     }
88     if (!self->alertMessage) {
89       NSLog(@"WARNING: JSAlertPanel: no value for 'alertMessage'"
90             @" - using default");
91     }
92     
93     self->template = RETAIN(_subs);
94   }
95   return self;
96 }
97
98 - (void)dealloc {
99   RELEASE(self->action);
100   RELEASE(self->javaScriptFunction);
101   RELEASE(self->pageName);
102   RELEASE(self->alertMessage);
103   RELEASE(self->altTag);
104   RELEASE(self->filename);
105   RELEASE(self->targetWindow);
106   RELEASE(self->string);
107   RELEASE(self->template);
108   RELEASE(self->escapeJS);
109   RELEASE(self->framework);
110   [super dealloc];
111 }
112
113 /* processing requests */
114
115 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
116   [self->template takeValuesFromRequest:_rq inContext:_ctx];
117 }
118
119 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
120   if (self->pageName) {
121     NSString *name;
122     
123     name = [self->pageName stringValueInComponent: [_ctx component]];
124     return [[_ctx application] pageWithName:name inContext:_ctx];
125   }
126   if (self->action)
127     return [self->action valueInComponent:[_ctx component]];
128   
129   return [self->template invokeActionForRequest:_rq inContext:_ctx];
130 }
131
132
133 - (void)appendToResponse:(WOResponse *)_response
134   inContext:(WOContext *)_ctx
135 {
136   WOComponent *comp;
137   NSString    *tmp;
138   NSArray     *languages;
139   
140   if ([_ctx isRenderingDisabled]) {
141     [self->template appendToResponse:_response inContext:_ctx];
142     return;
143   }
144
145   comp = [_ctx component];
146   
147   // link
148   [_response appendContentString:@"<a onclick=\"javascript:alert('"];
149   tmp = (self->alertMessage)
150     ? [self->alertMessage stringValueInComponent: comp]
151     : (NSString *)@"Press OK.";
152   if (self->escapeJS != nil && [self->escapeJS boolValueInComponent: comp]) {
153     tmp = [tmp stringByApplyingJavaScriptEscaping];
154   }
155   [_response appendContentHTMLString:tmp];
156   [_response appendContentString:@"');\""];
157   [_response appendContentString:@" href=\""];
158   
159   if (self->javaScriptFunction) {
160     [_response appendContentString:@"javascript:"];
161     [_response appendContentHTMLAttributeValue:
162                  [self->javaScriptFunction stringValueInComponent:comp]];
163   }
164   else {
165     [_response appendContentString:[_ctx componentActionURL]];
166   }
167   [_response appendContentString:@"\" "];
168   if (self->targetWindow) {
169     [_response appendContentString:@" target=\""];
170     [_response appendContentHTMLAttributeValue:
171                  [self->targetWindow stringValueInComponent: comp]];
172     [_response appendContentString:@"\" "];
173   }
174   [self appendExtraAttributesToResponse:_response inContext:_ctx];
175   [_response appendContentString:@" >"];
176
177   // link content  
178   if (self->filename != nil) {
179     WOResourceManager *rm;
180     NSString          *frameworkName;
181
182     frameworkName = (self->framework != nil)
183       ? [self->framework stringValueInComponent:comp] 
184       : [comp frameworkName];
185     
186     rm        = [[_ctx application] resourceManager];
187     languages = [_ctx resourceLookupLanguages];
188     
189     tmp = [rm urlForResourceNamed:[self->filename stringValueInComponent:comp]
190               inFramework:frameworkName
191               languages:languages
192               request:[_ctx request]];
193     
194     [_response appendContentString:@"<img border=\"0\" src=\""];
195     [_response appendContentString:tmp];
196     [_response appendContentString:@"\""];
197     
198     if (self->altTag != nil) {
199       [_response appendContentString:@" alt=\""];
200       [_response appendContentString:
201         [self->altTag stringValueInComponent:comp]];
202       [_response appendContentString:@"\" "];
203     }
204     
205     [_response appendContentString:
206                  (_ctx->wcFlags.xmlStyleEmptyElements ? @" />" : @">")];
207   }
208
209   [self->template appendToResponse:_response inContext:_ctx];
210   
211   if (self->string != nil) 
212     [_response appendContentString:[self->string stringValueInComponent:comp]];
213
214   /* close link */
215   [_response appendContentString:@"</a>"];
216 }
217
218 @end /* JSAlertPanel */