]> err.no Git - sope/blob - sope-appserver/WOExtensions/JSAlertPanel.m
fixed copyrights for 2005
[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   WOElement     *template;
36 }
37
38 @end
39
40 #include "common.h"
41
42 @implementation JSAlertPanel
43
44 + (int)version {
45   return [super version] + 0 /* v2 */;
46 }
47 + (void)initialize {
48   NSAssert2([super version] == 2,
49             @"invalid superclass (%@) version %i !",
50             NSStringFromClass([self superclass]), [super version]);
51 }
52
53 - (id)initWithName:(NSString *)_name
54   associations:(NSDictionary *)_config
55   template:(WOElement *)_subs
56 {
57   if ((self = [super initWithName:_name associations:_config template:_subs]))
58   {
59     int funcCount;
60
61     self->action             = WOExtGetProperty(_config,@"action");
62     self->javaScriptFunction = WOExtGetProperty(_config,@"javaScriptFunction");
63     self->pageName           = WOExtGetProperty(_config,@"pageName");
64     self->alertMessage       = WOExtGetProperty(_config,@"alertMessage");
65     self->altTag             = WOExtGetProperty(_config,@"altTag");
66     self->filename           = WOExtGetProperty(_config,@"filename");
67     self->targetWindow       = WOExtGetProperty(_config,@"targetWindow");
68     self->string             = WOExtGetProperty(_config,@"string");
69     
70     funcCount = 0;
71     if (self->action) funcCount++;
72     if (self->pageName) funcCount++;
73     if (self->javaScriptFunction) funcCount++;
74
75     if (funcCount > 1) {
76       NSLog(@"WARNING: JSAlertPanel: choose only one of "
77             @"action | pageName | javaScriptFunction");
78     }
79     if (funcCount < 1) {
80       NSLog(@"WARNING: JSAlertPanel: no function declared - choose one of"
81             @"action | pageName | javaScriptFunction");
82     }
83     if (!self->alertMessage) {
84       NSLog(@"WARNING: JSAlertPanel: no value for 'alertMessage'"
85             @" - using default");
86     }
87     
88     self->template = RETAIN(_subs);
89   }
90   return self;
91 }
92
93 - (void)dealloc {
94   RELEASE(self->action);
95   RELEASE(self->javaScriptFunction);
96   RELEASE(self->pageName);
97   RELEASE(self->alertMessage);
98   RELEASE(self->altTag);
99   RELEASE(self->filename);
100   RELEASE(self->targetWindow);
101   RELEASE(self->string);
102   RELEASE(self->template);
103   [super dealloc];
104 }
105
106 /* processing requests */
107
108 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
109   [self->template takeValuesFromRequest:_rq inContext:_ctx];
110 }
111
112 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
113   if (self->pageName) {
114     NSString *name;
115     
116     name = [self->pageName stringValueInComponent: [_ctx component]];
117     return [[_ctx application] pageWithName:name inContext:_ctx];
118   }
119   if (self->action)
120     return [self->action valueInComponent:[_ctx component]];
121   
122   return [self->template invokeActionForRequest:_rq inContext:_ctx];
123 }
124
125
126 - (void)appendToResponse:(WOResponse *)_response
127   inContext:(WOContext *)_ctx
128 {
129   WOComponent     *comp;
130   NSString        *tmp;
131   NSArray         *languages;
132   
133   comp = [_ctx component];
134   
135   // link
136   [_response appendContentString:@"<a onClick=\"javascript:alert('"];
137   tmp = (self->alertMessage)
138     ? [self->alertMessage stringValueInComponent: comp]
139     : @"Press OK.";
140   [_response appendContentHTMLString:tmp];
141   [_response appendContentString:@"');\""];
142   [_response appendContentString:@" href=\""];
143   
144   if (self->javaScriptFunction) {
145     [_response appendContentString:@"javascript:"];
146     [_response appendContentHTMLAttributeValue:
147                  [self->javaScriptFunction stringValueInComponent:comp]];
148   }
149   else {
150     [_response appendContentString:[_ctx componentActionURL]];
151   }
152   [_response appendContentString:@"\" "];
153   if (self->targetWindow) {
154     [_response appendContentString:@" target=\""];
155     [_response appendContentHTMLAttributeValue:
156                  [self->targetWindow stringValueInComponent: comp]];
157     [_response appendContentString:@"\" "];
158   }
159   [self appendExtraAttributesToResponse:_response inContext:_ctx];
160   [_response appendContentString:@" >"];
161
162   // link content  
163   if (self->filename) {
164     WOResourceManager *rm;
165
166     rm = [[_ctx application] resourceManager];
167     
168     languages = [_ctx hasSession]
169       ? [[_ctx session] languages]
170       : [[_ctx request] browserLanguages];
171     
172     tmp = [rm urlForResourceNamed:[self->filename stringValueInComponent:comp]
173               inFramework:[comp frameworkName]
174               languages:languages
175               request:[_ctx request]];
176     
177     [_response appendContentString:@"<img border=\"0\" src=\""];
178     [_response appendContentString:tmp];
179     [_response appendContentString:@"\""];
180     
181     if (self->altTag) {
182       [_response appendContentString:@" alt=\""];
183       [_response appendContentString:
184         [self->altTag stringValueInComponent:comp]];
185       [_response appendContentString:@"\" "];
186     }
187     [_response appendContentString:@" />"];
188   }
189   [self->template appendToResponse:_response inContext:_ctx];
190   if (self->string) 
191     [_response appendContentString:[self->string stringValueInComponent:comp]];
192
193   /* close link */
194   [_response appendContentString:@"</a>"];
195 }
196
197 @end /* JSAlertPanel */